From b832fb305b3cb55a99e6e4f761f9ffad12fdf336 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Sat, 21 May 2016 07:59:52 -0700 Subject: [PATCH] core: context test for destroying data resources Earlier we had a bug where data resources would not yet removed from the state during a destroy. This was fixed in cd0c452, and this test will hopefully make sure it stays fixed. --- terraform/context_apply_test.go | 53 +++++++++++++++++++ .../apply-destroy-data-resource/main.tf | 5 ++ 2 files changed, 58 insertions(+) create mode 100644 terraform/test-fixtures/apply-destroy-data-resource/main.tf diff --git a/terraform/context_apply_test.go b/terraform/context_apply_test.go index 0939ec97a..150ac13fe 100644 --- a/terraform/context_apply_test.go +++ b/terraform/context_apply_test.go @@ -340,6 +340,59 @@ func TestContext2Apply_destroyComputed(t *testing.T) { } } +func TestContext2Apply_destroyData(t *testing.T) { + m := testModule(t, "apply-destroy-data-resource") + p := testProvider("null") + p.ApplyFn = testApplyFn + p.DiffFn = testDiffFn + state := &State{ + Modules: []*ModuleState{ + &ModuleState{ + Path: rootModulePath, + Resources: map[string]*ResourceState{ + "data.null_data_source.testing": &ResourceState{ + Type: "aws_instance", + Primary: &InstanceState{ + ID: "-", + Attributes: map[string]string{ + "inputs.#": "1", + "inputs.test": "yes", + }, + }, + }, + }, + }, + }, + } + ctx := testContext2(t, &ContextOpts{ + Module: m, + Providers: map[string]ResourceProviderFactory{ + "null": testProviderFuncFixed(p), + }, + State: state, + Destroy: true, + }) + + if p, err := ctx.Plan(); err != nil { + t.Fatalf("err: %s", err) + } else { + t.Logf(p.String()) + } + + newState, err := ctx.Apply() + if err != nil { + t.Fatalf("err: %s", err) + } + + if got := len(newState.Modules); got != 1 { + t.Fatalf("state has %d modules after destroy; want 1", got) + } + + if got := len(newState.Modules[0].Resources); got != 0 { + t.Fatalf("state has %d resources after destroy; want 0", got) + } +} + // https://github.com/hashicorp/terraform/pull/5096 func TestContext2Apply_destroySkipsCBD(t *testing.T) { // Config contains CBD resource depending on non-CBD resource, which triggers diff --git a/terraform/test-fixtures/apply-destroy-data-resource/main.tf b/terraform/test-fixtures/apply-destroy-data-resource/main.tf new file mode 100644 index 000000000..cb16d9f34 --- /dev/null +++ b/terraform/test-fixtures/apply-destroy-data-resource/main.tf @@ -0,0 +1,5 @@ +data "null_data_source" "testing" { + inputs = { + test = "yes" + } +}