Merge pull request #19544 from hashicorp/jbardin/import-tests
Fix provider import tests
This commit is contained in:
commit
98870fadb1
|
@ -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())
|
||||||
|
|
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,7 +38,6 @@ func TestContextImport_basic(t *testing.T) {
|
||||||
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
|
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
|
||||||
),
|
),
|
||||||
ID: "bar",
|
ID: "bar",
|
||||||
ProviderAddr: addrs.RootModuleInstance.ProviderConfigDefault("aws"),
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
@ -78,7 +77,6 @@ func TestContextImport_countIndex(t *testing.T) {
|
||||||
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"),
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
@ -136,7 +134,6 @@ func TestContextImport_collision(t *testing.T) {
|
||||||
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
|
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
|
||||||
),
|
),
|
||||||
ID: "bar",
|
ID: "bar",
|
||||||
ProviderAddr: addrs.RootModuleInstance.ProviderConfigDefault("aws"),
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
@ -180,7 +177,6 @@ func TestContextImport_missingType(t *testing.T) {
|
||||||
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
|
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
|
||||||
),
|
),
|
||||||
ID: "bar",
|
ID: "bar",
|
||||||
ProviderAddr: addrs.RootModuleInstance.ProviderConfigDefault("aws"),
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
@ -234,7 +230,6 @@ func TestContextImport_moduleProvider(t *testing.T) {
|
||||||
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
|
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
|
||||||
),
|
),
|
||||||
ID: "bar",
|
ID: "bar",
|
||||||
ProviderAddr: addrs.RootModuleInstance.ProviderConfigDefault("aws"),
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
@ -292,7 +287,6 @@ func TestContextImport_providerModule(t *testing.T) {
|
||||||
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
|
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
|
||||||
),
|
),
|
||||||
ID: "bar",
|
ID: "bar",
|
||||||
ProviderAddr: addrs.RootModuleInstance.ProviderConfigDefault("aws"),
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
@ -350,7 +344,6 @@ func TestContextImport_providerVarConfig(t *testing.T) {
|
||||||
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
|
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
|
||||||
),
|
),
|
||||||
ID: "bar",
|
ID: "bar",
|
||||||
ProviderAddr: addrs.RootModuleInstance.ProviderConfigDefault("aws"),
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
provider "aws" {
|
||||||
|
foo = data.template_data_source.d.foo
|
||||||
|
}
|
||||||
|
|
||||||
|
data "template_data_source" "d" {
|
||||||
|
foo = "bar"
|
||||||
|
}
|
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue