Merge pull request #13226 from hashicorp/f-add-import-id-prefix

core: Add ImportStateIdPrefix field for testing
This commit is contained in:
Jake Champlin 2017-04-03 14:29:16 -04:00 committed by GitHub
commit 5bf77a6f1d
3 changed files with 85 additions and 0 deletions

View File

@ -174,6 +174,13 @@ type TestStep struct {
// determined by inspecting the state for ResourceName's ID. // determined by inspecting the state for ResourceName's ID.
ImportStateId string ImportStateId string
// ImportStateIdPrefix is the prefix added in front of ImportStateId.
// This can be useful in complex import cases, where more than one
// attribute needs to be passed on as the Import ID. Mainly in cases
// where the ID is not known, and a known prefix needs to be added to
// the unset ImportStateId field.
ImportStateIdPrefix string
// ImportStateCheck checks the results of ImportState. It should be // ImportStateCheck checks the results of ImportState. It should be
// used to verify that the resulting value of ImportState has the // used to verify that the resulting value of ImportState has the
// proper resources, IDs, and attributes. // proper resources, IDs, and attributes.

View File

@ -25,6 +25,10 @@ func testStepImportState(
importId = resource.Primary.ID importId = resource.Primary.ID
} }
importPrefix := step.ImportStateIdPrefix
if importPrefix != "" {
importId = fmt.Sprintf("%s%s", importPrefix, importId)
}
// Setup the context. We initialize with an empty state. We use the // Setup the context. We initialize with an empty state. We use the
// full config for provider configurations. // full config for provider configurations.

View File

@ -178,6 +178,80 @@ func TestTest_importStateDetectId(t *testing.T) {
} }
} }
func TestTest_importStateIdPrefix(t *testing.T) {
mp := testProvider()
mp.DiffReturn = nil
mp.ApplyFn = func(
info *terraform.InstanceInfo,
state *terraform.InstanceState,
diff *terraform.InstanceDiff) (*terraform.InstanceState, error) {
if !diff.Destroy {
return &terraform.InstanceState{
ID: "foo",
}, nil
}
return nil, nil
}
mp.RefreshFn = func(
i *terraform.InstanceInfo,
s *terraform.InstanceState) (*terraform.InstanceState, error) {
return s, nil
}
mp.ImportStateFn = func(
info *terraform.InstanceInfo, id string) ([]*terraform.InstanceState, error) {
if id != "bazfoo" {
return nil, fmt.Errorf("bad import ID: %s", id)
}
return []*terraform.InstanceState{
{
ID: "bar",
Ephemeral: terraform.EphemeralState{Type: "test_instance"},
},
}, nil
}
checked := false
checkFn := func(s []*terraform.InstanceState) error {
checked = true
if s[0].ID != "bar" {
return fmt.Errorf("bad: %#v", s)
}
return nil
}
mt := new(mockT)
Test(mt, TestCase{
Providers: map[string]terraform.ResourceProvider{
"test": mp,
},
Steps: []TestStep{
{
Config: testConfigStr,
},
{
ResourceName: "test_instance.foo",
ImportState: true,
ImportStateCheck: checkFn,
ImportStateIdPrefix: "baz",
},
},
})
if mt.failed() {
t.Fatalf("test failed: %s", mt.failMessage())
}
if !checked {
t.Fatal("didn't call check")
}
}
func TestTest_importStateVerify(t *testing.T) { func TestTest_importStateVerify(t *testing.T) {
mp := testProvider() mp := testProvider()
mp.DiffReturn = nil mp.DiffReturn = nil