mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-14 09:34:03 -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())
|
||||
expected := strings.TrimSpace(testImportCollisionStr)
|
||||
expected := `aws_instance.foo:
|
||||
ID = bar
|
||||
provider = provider.aws`
|
||||
|
||||
if actual != expected {
|
||||
t.Fatalf("bad: \n%s", actual)
|
||||
}
|
||||
@ -154,6 +157,13 @@ func TestContextImport_collision(t *testing.T) {
|
||||
func TestContextImport_missingType(t *testing.T) {
|
||||
p := testProvider("aws")
|
||||
m := testModule(t, "import-provider")
|
||||
|
||||
p.ImportStateReturn = []*InstanceState{
|
||||
&InstanceState{
|
||||
ID: "foo",
|
||||
},
|
||||
}
|
||||
|
||||
ctx := testContext2(t, &ContextOpts{
|
||||
Config: m,
|
||||
ProviderResolver: providers.ResolverFixed(
|
||||
@ -163,12 +173,6 @@ func TestContextImport_missingType(t *testing.T) {
|
||||
),
|
||||
})
|
||||
|
||||
p.ImportStateReturn = []*InstanceState{
|
||||
&InstanceState{
|
||||
ID: "foo",
|
||||
},
|
||||
}
|
||||
|
||||
state, diags := ctx.Import(&ImportOpts{
|
||||
Targets: []*ImportTarget{
|
||||
&ImportTarget{
|
||||
@ -194,17 +198,6 @@ func TestContextImport_missingType(t *testing.T) {
|
||||
func TestContextImport_moduleProvider(t *testing.T) {
|
||||
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{
|
||||
&InstanceState{
|
||||
ID: "foo",
|
||||
@ -256,7 +249,7 @@ func TestContextImport_moduleProvider(t *testing.T) {
|
||||
actual := strings.TrimSpace(state.String())
|
||||
expected := strings.TrimSpace(testImportStr)
|
||||
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.ReadResourceResponse = providers.ReadResourceResponse{
|
||||
NewState: cty.ObjectVal(map[string]cty.Value{
|
||||
"id": cty.StringVal("foo"),
|
||||
@ -644,7 +638,7 @@ func TestContextImport_moduleDiff(t *testing.T) {
|
||||
actual := strings.TrimSpace(state.String())
|
||||
expected := strings.TrimSpace(testImportModuleDiffStr)
|
||||
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())
|
||||
expected := strings.TrimSpace(testImportModuleExistingStr)
|
||||
if actual != expected {
|
||||
t.Fatalf("bad: \n%s", actual)
|
||||
t.Fatalf("\nexpected: %q\ngot: %q\n", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestContextImport_multiState(t *testing.T) {
|
||||
p := testProvider("aws")
|
||||
m := testModule(t, "import-provider")
|
||||
ctx := testContext2(t, &ContextOpts{
|
||||
Config: m,
|
||||
ProviderResolver: providers.ResolverFixed(
|
||||
map[string]providers.Factory{
|
||||
"aws": testProviderFuncFixed(p),
|
||||
|
||||
p.GetSchemaReturn = &ProviderSchema{
|
||||
Provider: &configschema.Block{
|
||||
Attributes: map[string]*configschema.Attribute{
|
||||
"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{
|
||||
&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{
|
||||
Targets: []*ImportTarget{
|
||||
&ImportTarget{
|
||||
@ -754,14 +769,26 @@ func TestContextImport_multiState(t *testing.T) {
|
||||
func TestContextImport_multiStateSame(t *testing.T) {
|
||||
p := testProvider("aws")
|
||||
m := testModule(t, "import-provider")
|
||||
ctx := testContext2(t, &ContextOpts{
|
||||
Config: m,
|
||||
ProviderResolver: providers.ResolverFixed(
|
||||
map[string]providers.Factory{
|
||||
"aws": testProviderFuncFixed(p),
|
||||
|
||||
p.GetSchemaReturn = &ProviderSchema{
|
||||
Provider: &configschema.Block{
|
||||
Attributes: map[string]*configschema.Attribute{
|
||||
"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{
|
||||
&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{
|
||||
Targets: []*ImportTarget{
|
||||
&ImportTarget{
|
||||
@ -889,11 +925,6 @@ aws_instance.foo.0:
|
||||
provider = provider.aws
|
||||
`
|
||||
|
||||
const testImportCollisionStr = `
|
||||
aws_instance.foo:
|
||||
ID = bar
|
||||
`
|
||||
|
||||
const testImportModuleStr = `
|
||||
<no state>
|
||||
module.foo:
|
||||
@ -911,9 +942,11 @@ module.a.b:
|
||||
`
|
||||
|
||||
const testImportModuleDiffStr = `
|
||||
<no state>
|
||||
module.bar:
|
||||
aws_instance.bar:
|
||||
ID = bar
|
||||
provider = provider.aws
|
||||
module.foo:
|
||||
aws_instance.foo:
|
||||
ID = foo
|
||||
@ -921,9 +954,11 @@ module.foo:
|
||||
`
|
||||
|
||||
const testImportModuleExistingStr = `
|
||||
<no state>
|
||||
module.foo:
|
||||
aws_instance.bar:
|
||||
ID = bar
|
||||
provider = provider.aws
|
||||
aws_instance.foo:
|
||||
ID = foo
|
||||
provider = provider.aws
|
||||
|
@ -128,6 +128,7 @@ func (p *MockProvider) getSchema() providers.GetSchemaResponse {
|
||||
Block: s,
|
||||
}
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
@ -247,7 +248,15 @@ func (p *MockProvider) ReadResource(r providers.ReadResourceRequest) providers.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 {
|
||||
@ -413,7 +422,19 @@ func (p *MockProvider) ImportResourceState(r providers.ImportResourceStateReques
|
||||
is.Attributes = make(map[string]string)
|
||||
}
|
||||
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)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
@ -423,10 +444,16 @@ func (p *MockProvider) ImportResourceState(r providers.ImportResourceStateReques
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
state, err = schema.CoerceValue(state)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
p.ImportResourceStateResponse.ImportedResources = append(
|
||||
p.ImportResourceStateResponse.ImportedResources,
|
||||
providers.ImportedResource{
|
||||
TypeName: r.TypeName,
|
||||
TypeName: is.Ephemeral.Type,
|
||||
State: state,
|
||||
Private: private,
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user