mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
convert import tests
This commit is contained in:
parent
3aebaf7a32
commit
4824eba07d
@ -145,7 +145,10 @@ func TestContextImport_collision(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
actual := strings.TrimSpace(state.String())
|
actual := strings.TrimSpace(state.String())
|
||||||
expected := strings.TrimSpace(testImportCollisionStr)
|
expected := `aws_instance.foo:
|
||||||
|
ID = bar
|
||||||
|
provider = provider.aws`
|
||||||
|
|
||||||
if actual != expected {
|
if actual != expected {
|
||||||
t.Fatalf("bad: \n%s", actual)
|
t.Fatalf("bad: \n%s", actual)
|
||||||
}
|
}
|
||||||
@ -154,6 +157,13 @@ func TestContextImport_collision(t *testing.T) {
|
|||||||
func TestContextImport_missingType(t *testing.T) {
|
func TestContextImport_missingType(t *testing.T) {
|
||||||
p := testProvider("aws")
|
p := testProvider("aws")
|
||||||
m := testModule(t, "import-provider")
|
m := testModule(t, "import-provider")
|
||||||
|
|
||||||
|
p.ImportStateReturn = []*InstanceState{
|
||||||
|
&InstanceState{
|
||||||
|
ID: "foo",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
ctx := testContext2(t, &ContextOpts{
|
ctx := testContext2(t, &ContextOpts{
|
||||||
Config: m,
|
Config: m,
|
||||||
ProviderResolver: providers.ResolverFixed(
|
ProviderResolver: providers.ResolverFixed(
|
||||||
@ -163,12 +173,6 @@ func TestContextImport_missingType(t *testing.T) {
|
|||||||
),
|
),
|
||||||
})
|
})
|
||||||
|
|
||||||
p.ImportStateReturn = []*InstanceState{
|
|
||||||
&InstanceState{
|
|
||||||
ID: "foo",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
state, diags := ctx.Import(&ImportOpts{
|
state, diags := ctx.Import(&ImportOpts{
|
||||||
Targets: []*ImportTarget{
|
Targets: []*ImportTarget{
|
||||||
&ImportTarget{
|
&ImportTarget{
|
||||||
@ -194,17 +198,6 @@ func TestContextImport_missingType(t *testing.T) {
|
|||||||
func TestContextImport_moduleProvider(t *testing.T) {
|
func TestContextImport_moduleProvider(t *testing.T) {
|
||||||
p := testProvider("aws")
|
p := testProvider("aws")
|
||||||
|
|
||||||
p.GetSchemaReturn = &ProviderSchema{
|
|
||||||
Provider: &configschema.Block{
|
|
||||||
Attributes: map[string]*configschema.Attribute{
|
|
||||||
"foo": {Type: cty.String, Optional: true},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
ResourceTypes: map[string]*configschema.Block{
|
|
||||||
"aws_instance": {},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
p.ImportStateReturn = []*InstanceState{
|
p.ImportStateReturn = []*InstanceState{
|
||||||
&InstanceState{
|
&InstanceState{
|
||||||
ID: "foo",
|
ID: "foo",
|
||||||
@ -256,7 +249,7 @@ func TestContextImport_moduleProvider(t *testing.T) {
|
|||||||
actual := strings.TrimSpace(state.String())
|
actual := strings.TrimSpace(state.String())
|
||||||
expected := strings.TrimSpace(testImportStr)
|
expected := strings.TrimSpace(testImportStr)
|
||||||
if actual != expected {
|
if actual != expected {
|
||||||
t.Fatalf("bad: \n%s", actual)
|
t.Fatalf("expected:\n%s\n\ngot:\n%s", expected, actual)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -432,6 +425,7 @@ func TestContextImport_refresh(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
p.ReadResourceFn = nil
|
p.ReadResourceFn = nil
|
||||||
|
|
||||||
p.ReadResourceResponse = providers.ReadResourceResponse{
|
p.ReadResourceResponse = providers.ReadResourceResponse{
|
||||||
NewState: cty.ObjectVal(map[string]cty.Value{
|
NewState: cty.ObjectVal(map[string]cty.Value{
|
||||||
"id": cty.StringVal("foo"),
|
"id": cty.StringVal("foo"),
|
||||||
@ -644,7 +638,7 @@ func TestContextImport_moduleDiff(t *testing.T) {
|
|||||||
actual := strings.TrimSpace(state.String())
|
actual := strings.TrimSpace(state.String())
|
||||||
expected := strings.TrimSpace(testImportModuleDiffStr)
|
expected := strings.TrimSpace(testImportModuleDiffStr)
|
||||||
if actual != expected {
|
if actual != expected {
|
||||||
t.Fatalf("bad: \n%s", actual)
|
t.Fatalf("\nexpected: %q\ngot: %q\n", expected, actual)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -702,21 +696,33 @@ func TestContextImport_moduleExisting(t *testing.T) {
|
|||||||
actual := strings.TrimSpace(state.String())
|
actual := strings.TrimSpace(state.String())
|
||||||
expected := strings.TrimSpace(testImportModuleExistingStr)
|
expected := strings.TrimSpace(testImportModuleExistingStr)
|
||||||
if actual != expected {
|
if actual != expected {
|
||||||
t.Fatalf("bad: \n%s", actual)
|
t.Fatalf("\nexpected: %q\ngot: %q\n", expected, actual)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestContextImport_multiState(t *testing.T) {
|
func TestContextImport_multiState(t *testing.T) {
|
||||||
p := testProvider("aws")
|
p := testProvider("aws")
|
||||||
m := testModule(t, "import-provider")
|
m := testModule(t, "import-provider")
|
||||||
ctx := testContext2(t, &ContextOpts{
|
|
||||||
Config: m,
|
p.GetSchemaReturn = &ProviderSchema{
|
||||||
ProviderResolver: providers.ResolverFixed(
|
Provider: &configschema.Block{
|
||||||
map[string]providers.Factory{
|
Attributes: map[string]*configschema.Attribute{
|
||||||
"aws": testProviderFuncFixed(p),
|
"foo": {Type: cty.String, Optional: true},
|
||||||
},
|
},
|
||||||
),
|
},
|
||||||
})
|
ResourceTypes: map[string]*configschema.Block{
|
||||||
|
"aws_instance": {
|
||||||
|
Attributes: map[string]*configschema.Attribute{
|
||||||
|
"id": {Type: cty.String, Computed: true},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"aws_instance_thing": {
|
||||||
|
Attributes: map[string]*configschema.Attribute{
|
||||||
|
"id": {Type: cty.String, Computed: true},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
p.ImportStateReturn = []*InstanceState{
|
p.ImportStateReturn = []*InstanceState{
|
||||||
&InstanceState{
|
&InstanceState{
|
||||||
@ -729,6 +735,15 @@ func TestContextImport_multiState(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ctx := testContext2(t, &ContextOpts{
|
||||||
|
Config: m,
|
||||||
|
ProviderResolver: providers.ResolverFixed(
|
||||||
|
map[string]providers.Factory{
|
||||||
|
"aws": testProviderFuncFixed(p),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
})
|
||||||
|
|
||||||
state, diags := ctx.Import(&ImportOpts{
|
state, diags := ctx.Import(&ImportOpts{
|
||||||
Targets: []*ImportTarget{
|
Targets: []*ImportTarget{
|
||||||
&ImportTarget{
|
&ImportTarget{
|
||||||
@ -754,14 +769,26 @@ func TestContextImport_multiState(t *testing.T) {
|
|||||||
func TestContextImport_multiStateSame(t *testing.T) {
|
func TestContextImport_multiStateSame(t *testing.T) {
|
||||||
p := testProvider("aws")
|
p := testProvider("aws")
|
||||||
m := testModule(t, "import-provider")
|
m := testModule(t, "import-provider")
|
||||||
ctx := testContext2(t, &ContextOpts{
|
|
||||||
Config: m,
|
p.GetSchemaReturn = &ProviderSchema{
|
||||||
ProviderResolver: providers.ResolverFixed(
|
Provider: &configschema.Block{
|
||||||
map[string]providers.Factory{
|
Attributes: map[string]*configschema.Attribute{
|
||||||
"aws": testProviderFuncFixed(p),
|
"foo": {Type: cty.String, Optional: true},
|
||||||
},
|
},
|
||||||
),
|
},
|
||||||
})
|
ResourceTypes: map[string]*configschema.Block{
|
||||||
|
"aws_instance": {
|
||||||
|
Attributes: map[string]*configschema.Attribute{
|
||||||
|
"id": {Type: cty.String, Computed: true},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"aws_instance_thing": {
|
||||||
|
Attributes: map[string]*configschema.Attribute{
|
||||||
|
"id": {Type: cty.String, Computed: true},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
p.ImportStateReturn = []*InstanceState{
|
p.ImportStateReturn = []*InstanceState{
|
||||||
&InstanceState{
|
&InstanceState{
|
||||||
@ -778,6 +805,15 @@ func TestContextImport_multiStateSame(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ctx := testContext2(t, &ContextOpts{
|
||||||
|
Config: m,
|
||||||
|
ProviderResolver: providers.ResolverFixed(
|
||||||
|
map[string]providers.Factory{
|
||||||
|
"aws": testProviderFuncFixed(p),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
})
|
||||||
|
|
||||||
state, diags := ctx.Import(&ImportOpts{
|
state, diags := ctx.Import(&ImportOpts{
|
||||||
Targets: []*ImportTarget{
|
Targets: []*ImportTarget{
|
||||||
&ImportTarget{
|
&ImportTarget{
|
||||||
@ -889,11 +925,6 @@ aws_instance.foo.0:
|
|||||||
provider = provider.aws
|
provider = provider.aws
|
||||||
`
|
`
|
||||||
|
|
||||||
const testImportCollisionStr = `
|
|
||||||
aws_instance.foo:
|
|
||||||
ID = bar
|
|
||||||
`
|
|
||||||
|
|
||||||
const testImportModuleStr = `
|
const testImportModuleStr = `
|
||||||
<no state>
|
<no state>
|
||||||
module.foo:
|
module.foo:
|
||||||
@ -911,9 +942,11 @@ module.a.b:
|
|||||||
`
|
`
|
||||||
|
|
||||||
const testImportModuleDiffStr = `
|
const testImportModuleDiffStr = `
|
||||||
|
<no state>
|
||||||
module.bar:
|
module.bar:
|
||||||
aws_instance.bar:
|
aws_instance.bar:
|
||||||
ID = bar
|
ID = bar
|
||||||
|
provider = provider.aws
|
||||||
module.foo:
|
module.foo:
|
||||||
aws_instance.foo:
|
aws_instance.foo:
|
||||||
ID = foo
|
ID = foo
|
||||||
@ -921,9 +954,11 @@ module.foo:
|
|||||||
`
|
`
|
||||||
|
|
||||||
const testImportModuleExistingStr = `
|
const testImportModuleExistingStr = `
|
||||||
|
<no state>
|
||||||
module.foo:
|
module.foo:
|
||||||
aws_instance.bar:
|
aws_instance.bar:
|
||||||
ID = bar
|
ID = bar
|
||||||
|
provider = provider.aws
|
||||||
aws_instance.foo:
|
aws_instance.foo:
|
||||||
ID = foo
|
ID = foo
|
||||||
provider = provider.aws
|
provider = provider.aws
|
||||||
|
@ -128,6 +128,7 @@ func (p *MockProvider) getSchema() providers.GetSchemaResponse {
|
|||||||
Block: s,
|
Block: s,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -247,7 +248,15 @@ func (p *MockProvider) ReadResource(r providers.ReadResourceRequest) providers.R
|
|||||||
return p.ReadResourceFn(r)
|
return p.ReadResourceFn(r)
|
||||||
}
|
}
|
||||||
|
|
||||||
return p.ReadResourceResponse
|
// make sure the NewState fits the schema
|
||||||
|
newState, err := p.GetSchemaReturn.ResourceTypes[r.TypeName].CoerceValue(p.ReadResourceResponse.NewState)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
resp := p.ReadResourceResponse
|
||||||
|
resp.NewState = newState
|
||||||
|
|
||||||
|
return resp
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *MockProvider) PlanResourceChange(r providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
|
func (p *MockProvider) PlanResourceChange(r providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
|
||||||
@ -413,7 +422,19 @@ func (p *MockProvider) ImportResourceState(r providers.ImportResourceStateReques
|
|||||||
is.Attributes = make(map[string]string)
|
is.Attributes = make(map[string]string)
|
||||||
}
|
}
|
||||||
is.Attributes["id"] = is.ID
|
is.Attributes["id"] = is.ID
|
||||||
schema := p.GetSchemaReturn.ResourceTypes[r.TypeName]
|
|
||||||
|
typeName := is.Ephemeral.Type
|
||||||
|
// Use the requested type if the resource has no type of it's own.
|
||||||
|
// We still return the empty type, which will error, but this prevents a panic.
|
||||||
|
if typeName == "" {
|
||||||
|
typeName = r.TypeName
|
||||||
|
}
|
||||||
|
|
||||||
|
schema := p.GetSchemaReturn.ResourceTypes[typeName]
|
||||||
|
if schema == nil {
|
||||||
|
panic("no schema found for " + typeName)
|
||||||
|
}
|
||||||
|
|
||||||
private, err := json.Marshal(is.Meta)
|
private, err := json.Marshal(is.Meta)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
@ -423,10 +444,16 @@ func (p *MockProvider) ImportResourceState(r providers.ImportResourceStateReques
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
state, err = schema.CoerceValue(state)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
p.ImportResourceStateResponse.ImportedResources = append(
|
p.ImportResourceStateResponse.ImportedResources = append(
|
||||||
p.ImportResourceStateResponse.ImportedResources,
|
p.ImportResourceStateResponse.ImportedResources,
|
||||||
providers.ImportedResource{
|
providers.ImportedResource{
|
||||||
TypeName: r.TypeName,
|
TypeName: is.Ephemeral.Type,
|
||||||
State: state,
|
State: state,
|
||||||
Private: private,
|
Private: private,
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user