Merge pull request #19544 from hashicorp/jbardin/import-tests

Fix provider import tests
This commit is contained in:
James Bardin 2018-12-05 20:30:46 -05:00 committed by GitHub
commit 98870fadb1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 68 additions and 23 deletions

View File

@ -45,7 +45,11 @@ func testStep(opts terraform.ContextOpts, state *terraform.State, step TestStep,
// Build the context // Build the context
opts.Config = cfg opts.Config = cfg
opts.State = terraform.MustShimLegacyState(state) opts.State, err = terraform.ShimLegacyState(state)
if err != nil {
return nil, err
}
opts.Destroy = step.Destroy opts.Destroy = step.Destroy
ctx, stepDiags := terraform.NewContext(&opts) ctx, stepDiags := terraform.NewContext(&opts)
if stepDiags.HasErrors() { if stepDiags.HasErrors() {
@ -61,7 +65,11 @@ func testStep(opts terraform.ContextOpts, state *terraform.State, step TestStep,
// Refresh! // Refresh!
newState, stepDiags := ctx.Refresh() newState, stepDiags := ctx.Refresh()
state = mustShimNewState(newState, schemas) // shim the state first so the test can check the state on errors
state, err = shimNewState(newState, schemas)
if err != nil {
return nil, err
}
if stepDiags.HasErrors() { if stepDiags.HasErrors() {
return state, fmt.Errorf("Error refreshing: %s", stepDiags.Err()) return state, fmt.Errorf("Error refreshing: %s", stepDiags.Err())
} }
@ -83,7 +91,11 @@ func testStep(opts terraform.ContextOpts, state *terraform.State, step TestStep,
// Apply the diff, creating real resources. // Apply the diff, creating real resources.
newState, stepDiags = ctx.Apply() newState, stepDiags = ctx.Apply()
state = mustShimNewState(newState, schemas) // shim the state first so the test can check the state on errors
state, err = shimNewState(newState, schemas)
if err != nil {
return nil, err
}
if stepDiags.HasErrors() { if stepDiags.HasErrors() {
return state, fmt.Errorf("Error applying: %s", stepDiags.Err()) return state, fmt.Errorf("Error applying: %s", stepDiags.Err())
} }
@ -123,7 +135,11 @@ func testStep(opts terraform.ContextOpts, state *terraform.State, step TestStep,
if stepDiags.HasErrors() { if stepDiags.HasErrors() {
return state, fmt.Errorf("Error on follow-up refresh: %s", stepDiags.Err()) return state, fmt.Errorf("Error on follow-up refresh: %s", stepDiags.Err())
} }
state = mustShimNewState(newState, schemas)
state, err = shimNewState(newState, schemas)
if err != nil {
return nil, err
}
} }
if p, stepDiags = ctx.Plan(); stepDiags.HasErrors() { if p, stepDiags = ctx.Plan(); stepDiags.HasErrors() {
return state, fmt.Errorf("Error on second follow-up plan: %s", stepDiags.Err()) return state, fmt.Errorf("Error on second follow-up plan: %s", stepDiags.Err())

View File

@ -91,7 +91,10 @@ func testStepImportState(
return state, stepDiags.Err() return state, stepDiags.Err()
} }
newState := mustShimNewState(importedState, schemas) newState, err := shimNewState(importedState, schemas)
if err != nil {
return nil, err
}
// Go through the new state and verify // Go through the new state and verify
if step.ImportStateCheck != nil { if step.ImportStateCheck != nil {
@ -127,13 +130,31 @@ func testStepImportState(
r.Primary.ID) r.Primary.ID)
} }
// don't add empty flatmapped containers, so we can more easily
// compare the attributes
skipEmpty := func(k, v string) bool {
if strings.HasSuffix(k, ".#") || strings.HasSuffix(k, ".%") {
if v == "0" {
return true
}
}
return false
}
// Compare their attributes // Compare their attributes
actual := make(map[string]string) actual := make(map[string]string)
for k, v := range r.Primary.Attributes { for k, v := range r.Primary.Attributes {
if skipEmpty(k, v) {
continue
}
actual[k] = v actual[k] = v
} }
expected := make(map[string]string) expected := make(map[string]string)
for k, v := range oldR.Primary.Attributes { for k, v := range oldR.Primary.Attributes {
if skipEmpty(k, v) {
continue
}
expected[k] = v expected[k] = v
} }

View File

@ -901,14 +901,14 @@ func parseVariableAsHCL(name string, input string, targetType config.VariableTyp
} }
} }
// shimLegacyState is a helper that takes the legacy state type and // ShimLegacyState is a helper that takes the legacy state type and
// converts it to the new state type. // converts it to the new state type.
// //
// This is implemented as a state file upgrade, so it will not preserve // This is implemented as a state file upgrade, so it will not preserve
// parts of the state structure that are not included in a serialized state, // parts of the state structure that are not included in a serialized state,
// such as the resolved results of any local values, outputs in non-root // such as the resolved results of any local values, outputs in non-root
// modules, etc. // modules, etc.
func shimLegacyState(legacy *State) (*states.State, error) { func ShimLegacyState(legacy *State) (*states.State, error) {
if legacy == nil { if legacy == nil {
return nil, nil return nil, nil
} }
@ -928,7 +928,7 @@ func shimLegacyState(legacy *State) (*states.State, error) {
// the conversion does not succeed. This is primarily intended for tests where // the conversion does not succeed. This is primarily intended for tests where
// the given legacy state is an object constructed within the test. // the given legacy state is an object constructed within the test.
func MustShimLegacyState(legacy *State) *states.State { func MustShimLegacyState(legacy *State) *states.State {
ret, err := shimLegacyState(legacy) ret, err := ShimLegacyState(legacy)
if err != nil { if err != nil {
panic(err) panic(err)
} }

View File

@ -37,8 +37,7 @@ func TestContextImport_basic(t *testing.T) {
Addr: addrs.RootModuleInstance.ResourceInstance( Addr: addrs.RootModuleInstance.ResourceInstance(
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey, addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
), ),
ID: "bar", ID: "bar",
ProviderAddr: addrs.RootModuleInstance.ProviderConfigDefault("aws"),
}, },
}, },
}) })
@ -77,8 +76,7 @@ func TestContextImport_countIndex(t *testing.T) {
Addr: addrs.RootModuleInstance.ResourceInstance( Addr: addrs.RootModuleInstance.ResourceInstance(
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.IntKey(0), addrs.ManagedResourceMode, "aws_instance", "foo", addrs.IntKey(0),
), ),
ID: "bar", ID: "bar",
ProviderAddr: addrs.RootModuleInstance.ProviderConfigDefault("aws"),
}, },
}, },
}) })
@ -135,8 +133,7 @@ func TestContextImport_collision(t *testing.T) {
Addr: addrs.RootModuleInstance.ResourceInstance( Addr: addrs.RootModuleInstance.ResourceInstance(
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey, addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
), ),
ID: "bar", ID: "bar",
ProviderAddr: addrs.RootModuleInstance.ProviderConfigDefault("aws"),
}, },
}, },
}) })
@ -179,8 +176,7 @@ func TestContextImport_missingType(t *testing.T) {
Addr: addrs.RootModuleInstance.ResourceInstance( Addr: addrs.RootModuleInstance.ResourceInstance(
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey, addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
), ),
ID: "bar", ID: "bar",
ProviderAddr: addrs.RootModuleInstance.ProviderConfigDefault("aws"),
}, },
}, },
}) })
@ -233,8 +229,7 @@ func TestContextImport_moduleProvider(t *testing.T) {
Addr: addrs.RootModuleInstance.ResourceInstance( Addr: addrs.RootModuleInstance.ResourceInstance(
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey, addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
), ),
ID: "bar", ID: "bar",
ProviderAddr: addrs.RootModuleInstance.ProviderConfigDefault("aws"),
}, },
}, },
}) })
@ -291,8 +286,7 @@ func TestContextImport_providerModule(t *testing.T) {
Addr: addrs.RootModuleInstance.Child("child", addrs.NoKey).ResourceInstance( Addr: addrs.RootModuleInstance.Child("child", addrs.NoKey).ResourceInstance(
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey, addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
), ),
ID: "bar", ID: "bar",
ProviderAddr: addrs.RootModuleInstance.ProviderConfigDefault("aws"),
}, },
}, },
}) })
@ -349,8 +343,7 @@ func TestContextImport_providerVarConfig(t *testing.T) {
Addr: addrs.RootModuleInstance.ResourceInstance( Addr: addrs.RootModuleInstance.ResourceInstance(
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey, addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
), ),
ID: "bar", ID: "bar",
ProviderAddr: addrs.RootModuleInstance.ProviderConfigDefault("aws"),
}, },
}, },
}) })

View File

@ -0,0 +1,7 @@
provider "aws" {
foo = data.template_data_source.d.foo
}
data "template_data_source" "d" {
foo = "bar"
}

View File

@ -16,10 +16,18 @@ type ImportStateTransformer struct {
func (t *ImportStateTransformer) Transform(g *Graph) error { func (t *ImportStateTransformer) Transform(g *Graph) error {
for _, target := range t.Targets { for _, target := range t.Targets {
// The ProviderAddr may not be supplied for non-aliased providers.
// This will be populated if the targets come from the cli, but tests
// may not specify implied provider addresses.
providerAddr := target.ProviderAddr
if providerAddr.ProviderConfig.Type == "" {
providerAddr = target.Addr.Resource.Resource.DefaultProviderConfig().Absolute(target.Addr.Module)
}
node := &graphNodeImportState{ node := &graphNodeImportState{
Addr: target.Addr, Addr: target.Addr,
ID: target.ID, ID: target.ID,
ProviderAddr: target.ProviderAddr, ProviderAddr: providerAddr,
} }
g.Add(node) g.Add(node)
} }