From 061597304cdcf95c200e9fb53f9392676134da6e Mon Sep 17 00:00:00 2001 From: James Bardin Date: Thu, 28 Sep 2017 15:09:42 -0400 Subject: [PATCH] add test for destroying with locals in a provider Verify that locals aren't removed from the state before providers are evaluated during destroy. --- terraform/context_apply_test.go | 48 +++++++++++++++++++ .../provider-with-locals/main.tf | 11 +++++ 2 files changed, 59 insertions(+) create mode 100644 terraform/test-fixtures/provider-with-locals/main.tf diff --git a/terraform/context_apply_test.go b/terraform/context_apply_test.go index c5bc754b6..c22e87a1c 100644 --- a/terraform/context_apply_test.go +++ b/terraform/context_apply_test.go @@ -8868,3 +8868,51 @@ func TestContext2Apply_destroyWithLocals(t *testing.T) { t.Fatalf("wrong final state\ngot:\n%s\nwant:\n%s", got, want) } } + +func TestContext2Apply_providerWithLocals(t *testing.T) { + m := testModule(t, "provider-with-locals") + p := testProvider("aws") + p.DiffFn = testDiffFn + p.ApplyFn = testApplyFn + ctx := testContext2(t, &ContextOpts{ + Module: m, + ProviderResolver: ResourceProviderResolverFixed( + map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + }, + ), + }) + + if _, err := ctx.Plan(); err != nil { + t.Fatalf("err: %s", err) + } + + state, err := ctx.Apply() + if err != nil { + t.Fatalf("err: %s", err) + } + + ctx = testContext2(t, &ContextOpts{ + Module: m, + ProviderResolver: ResourceProviderResolverFixed( + map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + }, + ), + State: state, + Destroy: true, + }) + + if _, err = ctx.Plan(); err != nil { + t.Fatalf("err: %s", err) + } + + state, err = ctx.Apply() + if err != nil { + t.Fatalf("err: %s", err) + } + + if state.HasResources() { + t.Fatal("expected no state, got:", state) + } +} diff --git a/terraform/test-fixtures/provider-with-locals/main.tf b/terraform/test-fixtures/provider-with-locals/main.tf new file mode 100644 index 000000000..44b6d46b3 --- /dev/null +++ b/terraform/test-fixtures/provider-with-locals/main.tf @@ -0,0 +1,11 @@ +provider "aws" { + alias = "${local.foo}" +} + +locals { + foo = "bar" +} + +resource "aws_instance" "foo" { + value = "${local.foo}" +}