From f9ff7d1ee89a44f40635c655f0149c3ff754933f Mon Sep 17 00:00:00 2001 From: James Bardin Date: Wed, 24 Jun 2020 12:32:10 -0400 Subject: [PATCH] test for targeting with modules and output --- terraform/context_apply_test.go | 41 ++++++++++++------- .../testdata/destroy-targeted/child/main.tf | 10 +++++ terraform/testdata/destroy-targeted/main.tf | 12 ++++++ 3 files changed, 49 insertions(+), 14 deletions(-) create mode 100644 terraform/testdata/destroy-targeted/child/main.tf create mode 100644 terraform/testdata/destroy-targeted/main.tf diff --git a/terraform/context_apply_test.go b/terraform/context_apply_test.go index a99aae428..e06773bb1 100644 --- a/terraform/context_apply_test.go +++ b/terraform/context_apply_test.go @@ -7389,7 +7389,7 @@ aws_instance.foo.1: } func TestContext2Apply_targetedDestroy(t *testing.T) { - m := testModule(t, "apply-targeted") + m := testModule(t, "destroy-targeted") p := testProvider("aws") p.ApplyFn = testApplyFn p.DiffFn = testDiffFn @@ -7397,18 +7397,21 @@ func TestContext2Apply_targetedDestroy(t *testing.T) { state := states.NewState() root := state.EnsureModule(addrs.RootModuleInstance) root.SetResourceInstanceCurrent( - mustResourceInstanceAddr("aws_instance.foo").Resource, + mustResourceInstanceAddr("aws_instance.a").Resource, &states.ResourceInstanceObjectSrc{ Status: states.ObjectReady, - AttrsJSON: []byte(`{"id":"i-bcd345"}`), + AttrsJSON: []byte(`{"id":"bar"}`), }, mustProviderConfig(`provider["registry.terraform.io/hashicorp/aws"]`), ) - root.SetResourceInstanceCurrent( - mustResourceInstanceAddr("aws_instance.bar").Resource, + root.SetOutputValue("out", cty.StringVal("bar"), false) + + child := state.EnsureModule(addrs.RootModuleInstance.Child("child", addrs.NoKey)) + child.SetResourceInstanceCurrent( + mustResourceInstanceAddr("aws_instance.b").Resource, &states.ResourceInstanceObjectSrc{ Status: states.ObjectReady, - AttrsJSON: []byte(`{"id":"i-abc123"}`), + AttrsJSON: []byte(`{"id":"i-bcd345"}`), }, mustProviderConfig(`provider["registry.terraform.io/hashicorp/aws"]`), ) @@ -7421,12 +7424,16 @@ func TestContext2Apply_targetedDestroy(t *testing.T) { State: state, Targets: []addrs.Targetable{ addrs.RootModuleInstance.Resource( - addrs.ManagedResourceMode, "aws_instance", "foo", + addrs.ManagedResourceMode, "aws_instance", "a", ), }, Destroy: true, }) + if diags := ctx.Validate(); diags.HasErrors() { + t.Fatalf("validate errors: %s", diags.Err()) + } + if _, diags := ctx.Plan(); diags.HasErrors() { t.Fatalf("plan errors: %s", diags.Err()) } @@ -7437,15 +7444,21 @@ func TestContext2Apply_targetedDestroy(t *testing.T) { } mod := state.RootModule() - if len(mod.Resources) != 1 { - t.Fatalf("expected 1 resource, got: %#v", mod.Resources) + if len(mod.Resources) != 0 { + t.Fatalf("expected 0 resources, got: %#v", mod.Resources) } - checkStateString(t, state, ` -aws_instance.bar: - ID = i-abc123 - provider = provider["registry.terraform.io/hashicorp/aws"] - `) + // the root output should have been removed too, since it is derived solely + // from the targeted resource + if len(mod.OutputValues) != 0 { + t.Fatalf("expected 0 outputs, got: %#v", mod.OutputValues) + } + + // the module instance should remain + mod = state.Module(addrs.RootModuleInstance.Child("child", addrs.NoKey)) + if len(mod.Resources) != 1 { + t.Fatalf("expected 1 resources, got: %#v", mod.Resources) + } } func TestContext2Apply_targetedDestroyCountDeps(t *testing.T) { diff --git a/terraform/testdata/destroy-targeted/child/main.tf b/terraform/testdata/destroy-targeted/child/main.tf new file mode 100644 index 000000000..47ef076b1 --- /dev/null +++ b/terraform/testdata/destroy-targeted/child/main.tf @@ -0,0 +1,10 @@ +variable "in" { +} + +resource "aws_instance" "b" { + foo = var.in +} + +output "out" { + value = var.in +} diff --git a/terraform/testdata/destroy-targeted/main.tf b/terraform/testdata/destroy-targeted/main.tf new file mode 100644 index 000000000..70048b50c --- /dev/null +++ b/terraform/testdata/destroy-targeted/main.tf @@ -0,0 +1,12 @@ +resource "aws_instance" "a" { + foo = "bar" +} + +module "child" { + source = "./child" + in = aws_instance.a.id +} + +output "out" { + value = aws_instance.a.id +}