From 0573ff679355aefdc0088085c6ecaba9dc988a55 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Fri, 21 Apr 2017 16:43:14 -0700 Subject: [PATCH] helper/resource: pass config when testing import Previously having a config was mutually exclusive with running an import, but we need to provide a config so that the provider is declared, or else we can't actually complete the import in the future world where providers are installed dynamically based on their declarations. --- helper/resource/testing.go | 15 +++++++++------ helper/resource/testing_import_state_test.go | 6 ++++++ helper/resource/testing_test.go | 12 ++++++++---- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/helper/resource/testing.go b/helper/resource/testing.go index ebdbde2b5..a700a8c79 100644 --- a/helper/resource/testing.go +++ b/helper/resource/testing.go @@ -400,15 +400,18 @@ func Test(t TestT, c TestCase) { var err error log.Printf("[WARN] Test: Executing step %d", i) - // Determine the test mode to execute - if step.Config != "" { - state, err = testStepConfig(opts, state, step) - } else if step.ImportState { - state, err = testStepImportState(opts, state, step) - } else { + if step.Config == "" && !step.ImportState { err = fmt.Errorf( "unknown test mode for step. Please see TestStep docs\n\n%#v", step) + } else { + if step.ImportState { + // Can optionally set step.Config in addition to + // step.ImportState, to provide config for the import. + state, err = testStepImportState(opts, state, step) + } else { + state, err = testStepConfig(opts, state, step) + } } // If there was an error, exit diff --git a/helper/resource/testing_import_state_test.go b/helper/resource/testing_import_state_test.go index 96b1edc3d..8ef8323ba 100644 --- a/helper/resource/testing_import_state_test.go +++ b/helper/resource/testing_import_state_test.go @@ -40,6 +40,7 @@ func TestTest_importState(t *testing.T) { Steps: []TestStep{ TestStep{ + Config: testConfigStrProvider, ResourceName: "test_instance.foo", ImportState: true, ImportStateId: "foo", @@ -89,6 +90,7 @@ func TestTest_importStateFail(t *testing.T) { Steps: []TestStep{ TestStep{ + Config: testConfigStrProvider, ResourceName: "test_instance.foo", ImportState: true, ImportStateId: "foo", @@ -163,6 +165,7 @@ func TestTest_importStateDetectId(t *testing.T) { Config: testConfigStr, }, TestStep{ + Config: testConfigStr, ResourceName: "test_instance.foo", ImportState: true, ImportStateCheck: checkFn, @@ -236,6 +239,7 @@ func TestTest_importStateIdPrefix(t *testing.T) { Config: testConfigStr, }, { + Config: testConfigStr, ResourceName: "test_instance.foo", ImportState: true, ImportStateCheck: checkFn, @@ -309,6 +313,7 @@ func TestTest_importStateVerify(t *testing.T) { Config: testConfigStr, }, TestStep{ + Config: testConfigStr, ResourceName: "test_instance.foo", ImportState: true, ImportStateVerify: true, @@ -371,6 +376,7 @@ func TestTest_importStateVerifyFail(t *testing.T) { Config: testConfigStr, }, TestStep{ + Config: testConfigStr, ResourceName: "test_instance.foo", ImportState: true, ImportStateVerify: true, diff --git a/helper/resource/testing_test.go b/helper/resource/testing_test.go index cfadc2509..d1c967bbd 100644 --- a/helper/resource/testing_test.go +++ b/helper/resource/testing_test.go @@ -619,10 +619,6 @@ func testProvider() *terraform.MockResourceProvider { return mp } -const testConfigStr = ` -resource "test_instance" "foo" {} -` - func TestTest_Main(t *testing.T) { flag.Parse() if *flagSweep == "" { @@ -777,3 +773,11 @@ func TestTest_Main(t *testing.T) { func mockSweeperFunc(s string) error { return nil } + +const testConfigStr = ` +resource "test_instance" "foo" {} +` + +const testConfigStrProvider = ` +provider "test" {} +`