From 0d6b5f15598cd418b9bcd41f33074015cd1c63e7 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Wed, 19 Feb 2020 10:36:26 -0500 Subject: [PATCH] update some destroy provisioner tests to use for_each --- terraform/context_apply_test.go | 109 ++++++++---------- .../apply-provisioner-destroy/main.tf | 10 +- 2 files changed, 56 insertions(+), 63 deletions(-) diff --git a/terraform/context_apply_test.go b/terraform/context_apply_test.go index af5916fbb5..2bf81d0508 100644 --- a/terraform/context_apply_test.go +++ b/terraform/context_apply_test.go @@ -5266,28 +5266,23 @@ func TestContext2Apply_provisionerDestroy(t *testing.T) { p.DiffFn = testDiffFn pr.ApplyFn = func(rs *InstanceState, c *ResourceConfig) error { val, ok := c.Config["command"] - if !ok || val != "destroy" { + if !ok || val != "destroy a" { t.Fatalf("bad value for foo: %v %#v", val, c) } return nil } - state := MustShimLegacyState(&State{ - Modules: []*ModuleState{ - &ModuleState{ - Path: rootModulePath, - Resources: map[string]*ResourceState{ - "aws_instance.foo": &ResourceState{ - Type: "aws_instance", - Primary: &InstanceState{ - ID: "bar", - }, - }, - }, - }, + state := states.NewState() + root := state.RootModule() + root.SetResourceInstanceCurrent( + mustResourceInstanceAddr(`aws_instance.foo["a"]`).Resource, + &states.ResourceInstanceObjectSrc{ + Status: states.ObjectReady, + AttrsJSON: []byte(`{"id":"bar"}`), }, - }) + mustProviderConfig(`provider["registry.terraform.io/-/aws"]`), + ) ctx := testContext2(t, &ContextOpts{ Config: m, @@ -5331,21 +5326,16 @@ func TestContext2Apply_provisionerDestroyFail(t *testing.T) { return fmt.Errorf("provisioner error") } - state := MustShimLegacyState(&State{ - Modules: []*ModuleState{ - &ModuleState{ - Path: rootModulePath, - Resources: map[string]*ResourceState{ - "aws_instance.foo": &ResourceState{ - Type: "aws_instance", - Primary: &InstanceState{ - ID: "bar", - }, - }, - }, - }, + state := states.NewState() + root := state.RootModule() + root.SetResourceInstanceCurrent( + mustResourceInstanceAddr(`aws_instance.foo["a"]`).Resource, + &states.ResourceInstanceObjectSrc{ + Status: states.ObjectReady, + AttrsJSON: []byte(`{"id":"bar"}`), }, - }) + mustProviderConfig(`provider["registry.terraform.io/-/aws"]`), + ) ctx := testContext2(t, &ContextOpts{ Config: m, @@ -5371,7 +5361,7 @@ func TestContext2Apply_provisionerDestroyFail(t *testing.T) { } checkStateString(t, state, ` -aws_instance.foo: +aws_instance.foo["a"]: ID = bar provider = provider["registry.terraform.io/-/aws"] `) @@ -5405,21 +5395,16 @@ func TestContext2Apply_provisionerDestroyFailContinue(t *testing.T) { return fmt.Errorf("provisioner error") } - state := MustShimLegacyState(&State{ - Modules: []*ModuleState{ - &ModuleState{ - Path: rootModulePath, - Resources: map[string]*ResourceState{ - "aws_instance.foo": &ResourceState{ - Type: "aws_instance", - Primary: &InstanceState{ - ID: "bar", - }, - }, - }, - }, + state := states.NewState() + root := state.RootModule() + root.SetResourceInstanceCurrent( + mustResourceInstanceAddr(`aws_instance.foo["a"]`).Resource, + &states.ResourceInstanceObjectSrc{ + Status: states.ObjectReady, + AttrsJSON: []byte(`{"id":"bar"}`), }, - }) + mustProviderConfig(`provider["registry.terraform.io/-/aws"]`), + ) ctx := testContext2(t, &ContextOpts{ Config: m, @@ -5547,7 +5532,7 @@ func TestContext2Apply_provisionerDestroyTainted(t *testing.T) { destroyCalled := false pr.ApplyFn = func(rs *InstanceState, c *ResourceConfig) error { - expected := "create" + expected := "create a b" if rs.ID == "bar" { destroyCalled = true return nil @@ -5561,22 +5546,16 @@ func TestContext2Apply_provisionerDestroyTainted(t *testing.T) { return nil } - state := MustShimLegacyState(&State{ - Modules: []*ModuleState{ - &ModuleState{ - Path: rootModulePath, - Resources: map[string]*ResourceState{ - "aws_instance.foo": &ResourceState{ - Type: "aws_instance", - Primary: &InstanceState{ - ID: "bar", - Tainted: true, - }, - }, - }, - }, + state := states.NewState() + root := state.RootModule() + root.SetResourceInstanceCurrent( + mustResourceInstanceAddr(`aws_instance.foo["a"]`).Resource, + &states.ResourceInstanceObjectSrc{ + Status: states.ObjectTainted, + AttrsJSON: []byte(`{"id":"bar"}`), }, - }) + mustProviderConfig(`provider["registry.terraform.io/-/aws"]`), + ) ctx := testContext2(t, &ContextOpts{ Config: m, @@ -5589,6 +5568,14 @@ func TestContext2Apply_provisionerDestroyTainted(t *testing.T) { Provisioners: map[string]ProvisionerFactory{ "shell": testProvisionerFuncFixed(pr), }, + Variables: InputValues{ + "input": &InputValue{ + Value: cty.MapVal(map[string]cty.Value{ + "a": cty.StringVal("b"), + }), + SourceType: ValueFromInput, + }, + }, }) if _, diags := ctx.Plan(); diags.HasErrors() { @@ -5601,7 +5588,7 @@ func TestContext2Apply_provisionerDestroyTainted(t *testing.T) { } checkStateString(t, state, ` -aws_instance.foo: +aws_instance.foo["a"]: ID = foo provider = provider["registry.terraform.io/-/aws"] foo = bar diff --git a/terraform/testdata/apply-provisioner-destroy/main.tf b/terraform/testdata/apply-provisioner-destroy/main.tf index 38410ccc03..d5fc54e127 100644 --- a/terraform/testdata/apply-provisioner-destroy/main.tf +++ b/terraform/testdata/apply-provisioner-destroy/main.tf @@ -1,12 +1,18 @@ resource "aws_instance" "foo" { + for_each = var.input foo = "bar" provisioner "shell" { - command = "create" + command = "create ${each.key} ${each.value}" } provisioner "shell" { - command = "destroy" when = "destroy" + command = "destroy ${each.key}" } } + +variable "input" { + type = map(string) + default = {} +}