diff --git a/terraform/context_apply_test.go b/terraform/context_apply_test.go index d01174ed8..2bd97fd05 100644 --- a/terraform/context_apply_test.go +++ b/terraform/context_apply_test.go @@ -2677,6 +2677,63 @@ func TestContext2Apply_destroy(t *testing.T) { } } +// https://github.com/hashicorp/terraform/issues/2767 +func TestContext2Apply_destroyModulePrefix(t *testing.T) { + m := testModule(t, "apply-destroy-module-resource-prefix") + h := new(MockHook) + p := testProvider("aws") + p.ApplyFn = testApplyFn + p.DiffFn = testDiffFn + ctx := testContext2(t, &ContextOpts{ + Module: m, + Hooks: []Hook{h}, + Providers: map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + }, + }) + + // First plan and apply a create operation + if _, err := ctx.Plan(); err != nil { + t.Fatalf("err: %s", err) + } + + state, err := ctx.Apply() + if err != nil { + t.Fatalf("err: %s", err) + } + + // Verify that we got the apply info correct + if v := h.PreApplyInfo.HumanId(); v != "module.child.aws_instance.foo" { + t.Fatalf("bad: %s", v) + } + + // Next, plan and apply a destroy operation and reset the hook + h = new(MockHook) + ctx = testContext2(t, &ContextOpts{ + Destroy: true, + State: state, + Module: m, + Hooks: []Hook{h}, + Providers: 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) + } + + // Test that things were destroyed + if v := h.PreApplyInfo.HumanId(); v != "module.child.aws_instance.foo" { + t.Fatalf("bad: %s", v) + } +} + func TestContext2Apply_destroyNestedModule(t *testing.T) { m := testModule(t, "apply-destroy-nested-module") p := testProvider("aws") diff --git a/terraform/test-fixtures/apply-destroy-module-resource-prefix/child/main.tf b/terraform/test-fixtures/apply-destroy-module-resource-prefix/child/main.tf new file mode 100644 index 000000000..919f140bb --- /dev/null +++ b/terraform/test-fixtures/apply-destroy-module-resource-prefix/child/main.tf @@ -0,0 +1 @@ +resource "aws_instance" "foo" {} diff --git a/terraform/test-fixtures/apply-destroy-module-resource-prefix/main.tf b/terraform/test-fixtures/apply-destroy-module-resource-prefix/main.tf new file mode 100644 index 000000000..0f6991c53 --- /dev/null +++ b/terraform/test-fixtures/apply-destroy-module-resource-prefix/main.tf @@ -0,0 +1,3 @@ +module "child" { + source = "./child" +} diff --git a/terraform/transform_resource.go b/terraform/transform_resource.go index b877a6051..23a23f3c4 100644 --- a/terraform/transform_resource.go +++ b/terraform/transform_resource.go @@ -896,6 +896,9 @@ func (n *graphNodeExpandedResourceDestroy) EvalTree() EvalNode { Then: EvalNoop{}, }, + // Load the instance info so we have the module path set + &EvalInstanceInfo{Info: info}, + &EvalGetProvider{ Name: n.ProvidedBy()[0], Output: &provider,