Skip to content

Commit 303dc90

Browse files
add more test cases to ensure destroying is no issue
1 parent c20bf67 commit 303dc90

File tree

1 file changed

+62
-1
lines changed

1 file changed

+62
-1
lines changed

internal/terraform/context_plan_actions_test.go

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ func TestContextPlan_actions(t *testing.T) {
2626
module map[string]string
2727
buildState func(*states.SyncState)
2828
planActionResponse *providers.PlanActionResponse
29+
planOpts *PlanOpts
2930

3031
expectPlanActionCalled bool
3132
expectValidateDiagnostics func(m *configs.Config) tfdiags.Diagnostics
@@ -624,6 +625,61 @@ output "my_output2" {
624625
)
625626
},
626627
},
628+
629+
"destroy run": {
630+
module: map[string]string{
631+
"main.tf": `
632+
action "test_unlinked" "hello" {}
633+
resource "test_object" "a" {
634+
lifecycle {
635+
action_trigger {
636+
events = [before_create, after_update]
637+
actions = [action.test_unlinked.hello]
638+
}
639+
}
640+
}
641+
`,
642+
},
643+
expectPlanActionCalled: false,
644+
planOpts: SimplePlanOpts(plans.DestroyMode, InputValues{}),
645+
},
646+
647+
// Since if we just destroy a node there is no reference to an action in config, we try
648+
// to provoke an error by just removing a resource instance.
649+
"destroying expanded node": {
650+
module: map[string]string{
651+
"main.tf": `
652+
action "test_unlinked" "hello" {}
653+
resource "test_object" "a" {
654+
count = 2
655+
lifecycle {
656+
action_trigger {
657+
events = [before_create, after_update]
658+
actions = [action.test_unlinked.hello]
659+
}
660+
}
661+
}
662+
`,
663+
},
664+
expectPlanActionCalled: false,
665+
666+
buildState: func(s *states.SyncState) {
667+
s.SetResourceInstanceCurrent(mustResourceInstanceAddr("test_object.a[0]"), &states.ResourceInstanceObjectSrc{
668+
AttrsJSON: []byte(`{}`),
669+
Status: states.ObjectReady,
670+
}, mustProviderConfig(`provider["registry.terraform.io/hashicorp/test"]`))
671+
672+
s.SetResourceInstanceCurrent(mustResourceInstanceAddr("test_object.a[1]"), &states.ResourceInstanceObjectSrc{
673+
AttrsJSON: []byte(`{}`),
674+
Status: states.ObjectReady,
675+
}, mustProviderConfig(`provider["registry.terraform.io/hashicorp/test"]`))
676+
677+
s.SetResourceInstanceCurrent(mustResourceInstanceAddr("test_object.a[2]"), &states.ResourceInstanceObjectSrc{
678+
AttrsJSON: []byte(`{}`),
679+
Status: states.ObjectReady,
680+
}, mustProviderConfig(`provider["registry.terraform.io/hashicorp/test"]`))
681+
},
682+
},
627683
} {
628684
t.Run(name, func(t *testing.T) {
629685
if tc.toBeImplemented {
@@ -691,7 +747,12 @@ output "my_output2" {
691747
prevRunState = states.BuildState(tc.buildState)
692748
}
693749

694-
_, diags = ctx.Plan(m, prevRunState, SimplePlanOpts(plans.NormalMode, InputValues{}))
750+
opts := SimplePlanOpts(plans.NormalMode, InputValues{})
751+
if tc.planOpts != nil {
752+
opts = tc.planOpts
753+
}
754+
755+
_, diags = ctx.Plan(m, prevRunState, opts)
695756

696757
if tc.expectPlanDiagnostics != nil {
697758
tfdiags.AssertDiagnosticsMatch(t, diags, tc.expectPlanDiagnostics(m))

0 commit comments

Comments
 (0)