From 4ab8973520465c2bbc7f7b7d6232d48dadcce5f4 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Fri, 21 Apr 2017 17:19:37 -0700 Subject: [PATCH] core: provide config to all import context tests We're going to use config to determine provider dependencies, so we need to always provide a config when instantiating a context or we'll end up loading no providers at all. We previously had a test for running "terraform import -config=''" to disable the config entirely, but this test is now removed because it makes no sense. The actual functionality its testing still remains for now, but it will be removed in a subsequent commit when we start requiring that a resource to be imported must already exist in configuration. --- command/import_test.go | 61 ++----------------- .../import-provider-aliased/main.tf | 5 ++ .../import-provider-implicit/main.tf | 4 ++ terraform/context_import_test.go | 40 ++++++++++-- 4 files changed, 47 insertions(+), 63 deletions(-) create mode 100644 command/test-fixtures/import-provider-aliased/main.tf create mode 100644 command/test-fixtures/import-provider-implicit/main.tf diff --git a/command/import_test.go b/command/import_test.go index 5b8838ccb..1be2563bd 100644 --- a/command/import_test.go +++ b/command/import_test.go @@ -9,6 +9,8 @@ import ( ) func TestImport(t *testing.T) { + defer testChdir(t, testFixturePath("import-provider-implicit"))() + statePath := testTempFile(t) p := testProvider() @@ -102,63 +104,6 @@ func TestImport_providerConfig(t *testing.T) { testStateOutput(t, statePath, testImportStr) } -func TestImport_providerConfigDisable(t *testing.T) { - defer testChdir(t, testFixturePath("import-provider"))() - - statePath := testTempFile(t) - - p := testProvider() - ui := new(cli.MockUi) - c := &ImportCommand{ - Meta: Meta{ - testingOverrides: metaOverridesForProvider(p), - Ui: ui, - }, - } - - p.ImportStateFn = nil - p.ImportStateReturn = []*terraform.InstanceState{ - &terraform.InstanceState{ - ID: "yay", - Ephemeral: terraform.EphemeralState{ - Type: "test_instance", - }, - }, - } - - configured := false - p.ConfigureFn = func(c *terraform.ResourceConfig) error { - configured = true - - if v, ok := c.Get("foo"); ok { - return fmt.Errorf("bad value: %#v", v) - } - - return nil - } - - args := []string{ - "-state", statePath, - "-config", "", - "test_instance.foo", - "bar", - } - if code := c.Run(args); code != 0 { - t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) - } - - // Verify that we were called - if !configured { - t.Fatal("Configure should be called") - } - - if !p.ImportStateCalled { - t.Fatal("ImportState should be called") - } - - testStateOutput(t, statePath, testImportStr) -} - func TestImport_providerConfigWithVar(t *testing.T) { defer testChdir(t, testFixturePath("import-provider-var"))() @@ -1015,6 +960,8 @@ func TestRefresh_displaysOutputs(t *testing.T) { */ func TestImport_customProvider(t *testing.T) { + defer testChdir(t, testFixturePath("import-provider-aliased"))() + statePath := testTempFile(t) p := testProvider() diff --git a/command/test-fixtures/import-provider-aliased/main.tf b/command/test-fixtures/import-provider-aliased/main.tf new file mode 100644 index 000000000..92f563ae8 --- /dev/null +++ b/command/test-fixtures/import-provider-aliased/main.tf @@ -0,0 +1,5 @@ +provider "test" { + foo = "bar" + + alias = "alias" +} diff --git a/command/test-fixtures/import-provider-implicit/main.tf b/command/test-fixtures/import-provider-implicit/main.tf new file mode 100644 index 000000000..02ffc5bc7 --- /dev/null +++ b/command/test-fixtures/import-provider-implicit/main.tf @@ -0,0 +1,4 @@ +# Declaring this resource implies that we depend on the +# "test" provider, making it available for import. +resource "test_instance" "foo" { +} diff --git a/terraform/context_import_test.go b/terraform/context_import_test.go index ed05b947b..1316b5c0b 100644 --- a/terraform/context_import_test.go +++ b/terraform/context_import_test.go @@ -8,7 +8,9 @@ import ( func TestContextImport_basic(t *testing.T) { p := testProvider("aws") + m := testModule(t, "import-provider") ctx := testContext2(t, &ContextOpts{ + Module: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -43,7 +45,9 @@ func TestContextImport_basic(t *testing.T) { func TestContextImport_countIndex(t *testing.T) { p := testProvider("aws") + m := testModule(t, "import-provider") ctx := testContext2(t, &ContextOpts{ + Module: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -79,7 +83,9 @@ func TestContextImport_countIndex(t *testing.T) { func TestContextImport_collision(t *testing.T) { p := testProvider("aws") + m := testModule(t, "import-provider") ctx := testContext2(t, &ContextOpts{ + Module: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -131,7 +137,9 @@ func TestContextImport_collision(t *testing.T) { func TestContextImport_missingType(t *testing.T) { p := testProvider("aws") + m := testModule(t, "import-provider") ctx := testContext2(t, &ContextOpts{ + Module: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -166,7 +174,9 @@ func TestContextImport_missingType(t *testing.T) { func TestContextImport_moduleProvider(t *testing.T) { p := testProvider("aws") + m := testModule(t, "import-provider") ctx := testContext2(t, &ContextOpts{ + Module: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -192,8 +202,6 @@ func TestContextImport_moduleProvider(t *testing.T) { return nil } - m := testModule(t, "import-provider") - state, err := ctx.Import(&ImportOpts{ Module: m, Targets: []*ImportTarget{ @@ -221,7 +229,9 @@ func TestContextImport_moduleProvider(t *testing.T) { // Test that import sets up the graph properly for provider inheritance func TestContextImport_providerInherit(t *testing.T) { p := testProvider("aws") + m := testModule(t, "import-provider-inherit") ctx := testContext2(t, &ContextOpts{ + Module: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -247,8 +257,6 @@ func TestContextImport_providerInherit(t *testing.T) { return nil } - m := testModule(t, "import-provider-inherit") - _, err := ctx.Import(&ImportOpts{ Module: m, Targets: []*ImportTarget{ @@ -271,8 +279,9 @@ func TestContextImport_providerInherit(t *testing.T) { // that configuration for import. func TestContextImport_providerVarConfig(t *testing.T) { p := testProvider("aws") + m := testModule(t, "import-provider-vars") ctx := testContext2(t, &ContextOpts{ - Module: testModule(t, "import-provider-vars"), + Module: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -327,8 +336,9 @@ func TestContextImport_providerVarConfig(t *testing.T) { // Test that provider configs can't reference resources. func TestContextImport_providerNonVarConfig(t *testing.T) { p := testProvider("aws") + m := testModule(t, "import-provider-non-vars") ctx := testContext2(t, &ContextOpts{ - Module: testModule(t, "import-provider-non-vars"), + Module: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -358,7 +368,9 @@ func TestContextImport_providerNonVarConfig(t *testing.T) { func TestContextImport_refresh(t *testing.T) { p := testProvider("aws") + m := testModule(t, "import-provider") ctx := testContext2(t, &ContextOpts{ + Module: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -401,7 +413,9 @@ func TestContextImport_refresh(t *testing.T) { func TestContextImport_refreshNil(t *testing.T) { p := testProvider("aws") + m := testModule(t, "import-provider") ctx := testContext2(t, &ContextOpts{ + Module: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -441,7 +455,9 @@ func TestContextImport_refreshNil(t *testing.T) { func TestContextImport_module(t *testing.T) { p := testProvider("aws") + m := testModule(t, "import-provider") ctx := testContext2(t, &ContextOpts{ + Module: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -477,7 +493,9 @@ func TestContextImport_module(t *testing.T) { func TestContextImport_moduleDepth2(t *testing.T) { p := testProvider("aws") + m := testModule(t, "import-provider") ctx := testContext2(t, &ContextOpts{ + Module: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -513,7 +531,9 @@ func TestContextImport_moduleDepth2(t *testing.T) { func TestContextImport_moduleDiff(t *testing.T) { p := testProvider("aws") + m := testModule(t, "import-provider") ctx := testContext2(t, &ContextOpts{ + Module: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -565,7 +585,9 @@ func TestContextImport_moduleDiff(t *testing.T) { func TestContextImport_moduleExisting(t *testing.T) { p := testProvider("aws") + m := testModule(t, "import-provider") ctx := testContext2(t, &ContextOpts{ + Module: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -617,7 +639,9 @@ func TestContextImport_moduleExisting(t *testing.T) { func TestContextImport_multiState(t *testing.T) { p := testProvider("aws") + m := testModule(t, "import-provider") ctx := testContext2(t, &ContextOpts{ + Module: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -657,7 +681,9 @@ func TestContextImport_multiState(t *testing.T) { func TestContextImport_multiStateSame(t *testing.T) { p := testProvider("aws") + m := testModule(t, "import-provider") ctx := testContext2(t, &ContextOpts{ + Module: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -701,7 +727,9 @@ func TestContextImport_multiStateSame(t *testing.T) { func TestContextImport_customProvider(t *testing.T) { p := testProvider("aws") + m := testModule(t, "import-provider") ctx := testContext2(t, &ContextOpts{ + Module: m, ProviderResolver: ResourceProviderResolverFixed( map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p),