diff --git a/terraform/context_apply_test.go b/terraform/context_apply_test.go index 32338ab92..ebd9ae4ae 100644 --- a/terraform/context_apply_test.go +++ b/terraform/context_apply_test.go @@ -7623,7 +7623,7 @@ func TestContext2Apply_destroyProvisionerWithLocals(t *testing.T) { State: &State{ Modules: []*ModuleState{ &ModuleState{ - Path: rootModulePath, + Path: []string{"root", "mod"}, Resources: map[string]*ResourceState{ "aws_instance.foo": resourceState("aws_instance", "1234"), }, @@ -7646,6 +7646,68 @@ func TestContext2Apply_destroyProvisionerWithLocals(t *testing.T) { } } +func TestContext2Apply_destroyProvisionerWithOutput(t *testing.T) { + m := testModule(t, "apply-provisioner-destroy-outputs") + p := testProvider("aws") + p.ApplyFn = testApplyFn + p.DiffFn = testDiffFn + + pr := testProvisioner() + pr.ApplyFn = func(is *InstanceState, rc *ResourceConfig) error { + cmd, ok := rc.Get("command") + if !ok || cmd != "3" { + fmt.Printf("%#v\n", rc.Config) + return fmt.Errorf("provisioner for %s got %v:%s", is.ID, ok, cmd) + } + return nil + } + + ctx := testContext2(t, &ContextOpts{ + Module: m, + ProviderResolver: ResourceProviderResolverFixed( + map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + }, + ), + Provisioners: map[string]ResourceProvisionerFactory{ + "shell": testProvisionerFuncFixed(pr), + }, + State: &State{ + Modules: []*ModuleState{ + &ModuleState{ + Path: []string{"root"}, + Resources: map[string]*ResourceState{ + "aws_instance.foo": resourceState("aws_instance", "1"), + }, + }, + &ModuleState{ + Path: []string{"root", "mod"}, + Resources: map[string]*ResourceState{ + "aws_instance.baz": resourceState("aws_instance", "3"), + }, + // state needs to be properly initialized + Outputs: map[string]*OutputState{}, + }, + &ModuleState{ + Path: []string{"root", "mod2"}, + Resources: map[string]*ResourceState{ + "aws_instance.bar": resourceState("aws_instance", "2"), + }, + }, + }, + }, + Destroy: true, + }) + + if _, err := ctx.Plan(); err != nil { + t.Fatal(err) + } + + if _, err := ctx.Apply(); err != nil { + t.Fatal(err) + } +} + func TestContext2Apply_targetedDestroyCountDeps(t *testing.T) { m := testModule(t, "apply-destroy-targeted-count") p := testProvider("aws") diff --git a/terraform/context_test.go b/terraform/context_test.go index 7ef9245d7..36ea61e89 100644 --- a/terraform/context_test.go +++ b/terraform/context_test.go @@ -375,6 +375,9 @@ func resourceState(resourceType, resourceID string) *ResourceState { Type: resourceType, Primary: &InstanceState{ ID: resourceID, + Attributes: map[string]string{ + "id": resourceID, + }, }, } } diff --git a/terraform/test-fixtures/apply-provisioner-destroy-outputs/main.tf b/terraform/test-fixtures/apply-provisioner-destroy-outputs/main.tf new file mode 100644 index 000000000..bb4e01db9 --- /dev/null +++ b/terraform/test-fixtures/apply-provisioner-destroy-outputs/main.tf @@ -0,0 +1,19 @@ +module "mod" { + source = "./mod" +} + +locals { + value = "${module.mod.value}" +} + +resource "aws_instance" "foo" { + provisioner "shell" { + command = "${local.value}" + when = "destroy" + } +} + +module "mod2" { + source = "./mod2" + value = "${module.mod.value}" +} diff --git a/terraform/test-fixtures/apply-provisioner-destroy-outputs/mod/main.tf b/terraform/test-fixtures/apply-provisioner-destroy-outputs/mod/main.tf new file mode 100644 index 000000000..1f4ad09c5 --- /dev/null +++ b/terraform/test-fixtures/apply-provisioner-destroy-outputs/mod/main.tf @@ -0,0 +1,5 @@ +output "value" { + value = "${aws_instance.baz.id}" +} + +resource "aws_instance" "baz" {} diff --git a/terraform/test-fixtures/apply-provisioner-destroy-outputs/mod2/main.tf b/terraform/test-fixtures/apply-provisioner-destroy-outputs/mod2/main.tf new file mode 100644 index 000000000..a476bb920 --- /dev/null +++ b/terraform/test-fixtures/apply-provisioner-destroy-outputs/mod2/main.tf @@ -0,0 +1,10 @@ +variable "value" { +} + +resource "aws_instance" "bar" { + provisioner "shell" { + command = "${var.value}" + when = "destroy" + } +} +