diff --git a/helper/resource/testing.go b/helper/resource/testing.go index 9557207c3..23af81096 100644 --- a/helper/resource/testing.go +++ b/helper/resource/testing.go @@ -174,6 +174,13 @@ type TestStep struct { // determined by inspecting the state for ResourceName's ID. 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 // used to verify that the resulting value of ImportState has the // proper resources, IDs, and attributes. diff --git a/helper/resource/testing_import_state.go b/helper/resource/testing_import_state.go index d5c579629..28ad10526 100644 --- a/helper/resource/testing_import_state.go +++ b/helper/resource/testing_import_state.go @@ -25,6 +25,10 @@ func testStepImportState( 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 // full config for provider configurations. diff --git a/helper/resource/testing_import_state_test.go b/helper/resource/testing_import_state_test.go index 56e6a4845..96b1edc3d 100644 --- a/helper/resource/testing_import_state_test.go +++ b/helper/resource/testing_import_state_test.go @@ -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) { mp := testProvider() mp.DiffReturn = nil