From 9e5d1f10b033ce345bcef4d0039123ce4837aa5d Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 1 Nov 2016 22:42:41 -0700 Subject: [PATCH] terraform: add test to verify orphan outputs in modules are removed For #7598 This doesn't work with the old graph, we guard it as such. --- terraform/context_apply_test.go | 53 +++++++++++++++++++ terraform/terraform_test.go | 8 +++ .../apply-output-orphan-module/child/main.tf | 1 + .../apply-output-orphan-module/main.tf | 3 ++ 4 files changed, 65 insertions(+) create mode 100644 terraform/test-fixtures/apply-output-orphan-module/child/main.tf create mode 100644 terraform/test-fixtures/apply-output-orphan-module/main.tf diff --git a/terraform/context_apply_test.go b/terraform/context_apply_test.go index bd7e8049d6..8ece50adb5 100644 --- a/terraform/context_apply_test.go +++ b/terraform/context_apply_test.go @@ -12,6 +12,7 @@ import ( "time" "github.com/hashicorp/terraform/config/module" + "github.com/hashicorp/terraform/helper/experiment" ) func TestContext2Apply_basic(t *testing.T) { @@ -2113,6 +2114,58 @@ func TestContext2Apply_outputOrphan(t *testing.T) { } } +func TestContext2Apply_outputOrphanModule(t *testing.T) { + if !experiment.Enabled(experiment.X_newApply) { + t.SkipNow() + } + + m := testModule(t, "apply-output-orphan-module") + p := testProvider("aws") + p.ApplyFn = testApplyFn + p.DiffFn = testDiffFn + + state := &State{ + Modules: []*ModuleState{ + &ModuleState{ + Path: []string{"root", "child"}, + Outputs: map[string]*OutputState{ + "foo": &OutputState{ + Type: "string", + Value: "bar", + }, + "bar": &OutputState{ + Type: "string", + Value: "baz", + }, + }, + }, + }, + } + + ctx := testContext2(t, &ContextOpts{ + Module: m, + Providers: map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + }, + State: state, + }) + + 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(testTerraformApplyOutputOrphanModuleStr) + if actual != expected { + t.Fatalf("bad: \n%s", actual) + } +} + func TestContext2Apply_providerComputedVar(t *testing.T) { m := testModule(t, "apply-provider-computed") p := testProvider("aws") diff --git a/terraform/terraform_test.go b/terraform/terraform_test.go index 52f17bdb40..56386641f9 100644 --- a/terraform/terraform_test.go +++ b/terraform/terraform_test.go @@ -501,6 +501,14 @@ Outputs: foo = bar ` +const testTerraformApplyOutputOrphanModuleStr = ` +module.child: + + Outputs: + + foo = bar +` + const testTerraformApplyProvisionerStr = ` aws_instance.bar: ID = foo diff --git a/terraform/test-fixtures/apply-output-orphan-module/child/main.tf b/terraform/test-fixtures/apply-output-orphan-module/child/main.tf new file mode 100644 index 0000000000..70619c4e36 --- /dev/null +++ b/terraform/test-fixtures/apply-output-orphan-module/child/main.tf @@ -0,0 +1 @@ +output "foo" { value = "bar" } diff --git a/terraform/test-fixtures/apply-output-orphan-module/main.tf b/terraform/test-fixtures/apply-output-orphan-module/main.tf new file mode 100644 index 0000000000..0f6991c536 --- /dev/null +++ b/terraform/test-fixtures/apply-output-orphan-module/main.tf @@ -0,0 +1,3 @@ +module "child" { + source = "./child" +}