From 8a4883fd135e75c1676c04220ced6f02939dd299 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Wed, 19 Oct 2022 17:28:46 -0400 Subject: [PATCH] don't eval checks on destroy --- internal/terraform/context_apply2_test.go | 54 +++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/internal/terraform/context_apply2_test.go b/internal/terraform/context_apply2_test.go index 44da0cc8d8..100e75def8 100644 --- a/internal/terraform/context_apply2_test.go +++ b/internal/terraform/context_apply2_test.go @@ -1587,3 +1587,57 @@ output "data" { _, diags = ctx.Apply(plan, m) assertNoErrors(t, diags) } + +// don't evaluate conditions on outputs when destroying +func TestContext2Apply_noOutputChecksOnDestroy(t *testing.T) { + m := testModuleInline(t, map[string]string{ + "main.tf": ` +module "mod" { + source = "./mod" +} + +output "from_resource" { + value = module.mod.from_resource +} +`, + + "./mod/main.tf": ` +resource "test_object" "x" { + test_string = "wrong val" +} + +output "from_resource" { + value = test_object.x.test_string + precondition { + condition = test_object.x.test_string == "ok" + error_message = "resource error" + } +} +`}) + + p := simpleMockProvider() + + state := states.NewState() + mod := state.EnsureModule(addrs.RootModuleInstance.Child("mod", addrs.NoKey)) + mod.SetResourceInstanceCurrent( + mustResourceInstanceAddr("test_object.x").Resource, + &states.ResourceInstanceObjectSrc{ + Status: states.ObjectReady, + AttrsJSON: []byte(`{"test_string":"wrong_val"}`), + }, + mustProviderConfig(`provider["registry.terraform.io/hashicorp/test"]`), + ) + + ctx := testContext2(t, &ContextOpts{ + Providers: map[addrs.Provider]providers.Factory{ + addrs.NewDefaultProvider("test"): testProviderFuncFixed(p), + }, + }) + + opts := SimplePlanOpts(plans.DestroyMode, nil) + plan, diags := ctx.Plan(m, state, opts) + assertNoErrors(t, diags) + + _, diags = ctx.Apply(plan, m) + assertNoErrors(t, diags) +}