From 51a7e05f8a29b74cadc9d279675a182615315562 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 26 Jun 2015 12:00:02 -0700 Subject: [PATCH] terraform: all providers for ProvidedBy() should be added --- terraform/context_test.go | 37 +++++++++++++++++++ terraform/terraform_test.go | 9 +++++ .../apply-module-only-provider/child/main.tf | 2 + .../apply-module-only-provider/main.tf | 5 +++ terraform/transform_provider.go | 2 +- 5 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 terraform/test-fixtures/apply-module-only-provider/child/main.tf create mode 100644 terraform/test-fixtures/apply-module-only-provider/main.tf diff --git a/terraform/context_test.go b/terraform/context_test.go index 262775d8d..0cb256168 100644 --- a/terraform/context_test.go +++ b/terraform/context_test.go @@ -4358,6 +4358,43 @@ func TestContext2Apply_moduleOrphanProvider(t *testing.T) { } } +// This tests an issue where all the providers in a module but not +// in the root weren't being added to the root properly. In this test +// case: aws is explicitly added to root, but "test" should be added to. +// With the bug, it wasn't. +func TestContext2Apply_moduleOnlyProvider(t *testing.T) { + m := testModule(t, "apply-module-only-provider") + p := testProvider("aws") + p.ApplyFn = testApplyFn + p.DiffFn = testDiffFn + pTest := testProvider("test") + pTest.ApplyFn = testApplyFn + pTest.DiffFn = testDiffFn + + ctx := testContext2(t, &ContextOpts{ + Module: m, + Providers: map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + "test": testProviderFuncFixed(pTest), + }, + }) + + if _, err := ctx.Plan(); err != nil { + t.Fatalf("err: %s", err) + } + + state, err := ctx.Apply() + if err != nil { + t.Fatalf("err: %s", err) + } + + actual := strings.TrimSpace(state.String()) + expected := strings.TrimSpace(testTerraformApplyModuleOnlyProviderStr) + if actual != expected { + t.Fatalf("bad: \n%s", actual) + } +} + func TestContext2Apply_moduleProviderAlias(t *testing.T) { m := testModule(t, "apply-module-provider-alias") p := testProvider("aws") diff --git a/terraform/terraform_test.go b/terraform/terraform_test.go index 4ec622583..59dad38d4 100644 --- a/terraform/terraform_test.go +++ b/terraform/terraform_test.go @@ -379,6 +379,15 @@ do_instance.foo: type = do_instance ` +const testTerraformApplyModuleOnlyProviderStr = ` + +module.child: + aws_instance.foo: + ID = foo + test_instance.foo: + ID = foo +` + const testTerraformApplyModuleProviderAliasStr = ` module.child: diff --git a/terraform/test-fixtures/apply-module-only-provider/child/main.tf b/terraform/test-fixtures/apply-module-only-provider/child/main.tf new file mode 100644 index 000000000..e15099c17 --- /dev/null +++ b/terraform/test-fixtures/apply-module-only-provider/child/main.tf @@ -0,0 +1,2 @@ +resource "aws_instance" "foo" {} +resource "test_instance" "foo" {} diff --git a/terraform/test-fixtures/apply-module-only-provider/main.tf b/terraform/test-fixtures/apply-module-only-provider/main.tf new file mode 100644 index 000000000..2276b5f36 --- /dev/null +++ b/terraform/test-fixtures/apply-module-only-provider/main.tf @@ -0,0 +1,5 @@ +provider "aws" {} + +module "child" { + source = "./child" +} diff --git a/terraform/transform_provider.go b/terraform/transform_provider.go index 1031a5fa7..4c549ab45 100644 --- a/terraform/transform_provider.go +++ b/terraform/transform_provider.go @@ -167,7 +167,7 @@ func (t *MissingProviderTransformer) Transform(g *Graph) error { for _, p := range pv.ProvidedBy() { if _, ok := m[p]; ok { // This provider already exists as a configure node - break + continue } // If the provider has an alias in it, we just want the type