Skip to content

Commit 02fd462

Browse files
add tests for circular dependencies
1 parent 476cfeb commit 02fd462

File tree

1 file changed

+76
-3
lines changed

1 file changed

+76
-3
lines changed

internal/terraform/context_plan_actions_test.go

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,17 @@ func TestContextPlan_actions(t *testing.T) {
2929
planActionResponse *providers.PlanActionResponse
3030
planOpts *PlanOpts
3131

32-
expectPlanActionCalled bool
32+
expectPlanActionCalled bool
33+
34+
// Some tests can produce race-conditions in the error messages, so we
35+
// have two ways of checking the diagnostics. Use expectValidateDiagnostics
36+
// by default, if there is a race condition and you want to allow multiple
37+
// versions, please use assertValidateDiagnostics.
3338
expectValidateDiagnostics func(m *configs.Config) tfdiags.Diagnostics
34-
expectPlanDiagnostics func(m *configs.Config) tfdiags.Diagnostics
35-
assertPlan func(*testing.T, *plans.Plan)
39+
assertValidateDiagnostics func(*testing.T, tfdiags.Diagnostics)
40+
41+
expectPlanDiagnostics func(m *configs.Config) tfdiags.Diagnostics
42+
assertPlan func(*testing.T, *plans.Plan)
3643
}{
3744
"unreferenced": {
3845
module: map[string]string{
@@ -1116,6 +1123,70 @@ resource "other_object" "a" {
11161123
}
11171124
},
11181125
},
1126+
1127+
"action config refers to before triggering resource leads to circular dependency": {
1128+
module: map[string]string{
1129+
"main.tf": `
1130+
action "test_unlinked" "hello" {
1131+
config {
1132+
attr = test_object.a.name
1133+
}
1134+
}
1135+
resource "test_object" "a" {
1136+
lifecycle {
1137+
action_trigger {
1138+
events = [before_create]
1139+
actions = [action.test_unlinked.hello]
1140+
}
1141+
}
1142+
}
1143+
`,
1144+
},
1145+
expectPlanActionCalled: false,
1146+
assertValidateDiagnostics: func(t *testing.T, diags tfdiags.Diagnostics) {
1147+
if !diags.HasErrors() {
1148+
t.Fatalf("expected diagnostics to have errors, but it does not")
1149+
}
1150+
if len(diags) != 1 {
1151+
t.Fatalf("expected diagnostics to have 1 error, but it has %d", len(diags))
1152+
}
1153+
if diags[0].Description().Summary != "Cycle: test_object.a, action.test_unlinked.hello (expand)" && diags[0].Description().Summary != "Cycle: action.test_unlinked.hello (expand), test_object.a" {
1154+
t.Fatalf("expected diagnostic to have summary 'Cycle: test_object.a, action.test_unlinked.hello (expand)' or 'Cycle: action.test_unlinked.hello (expand), test_object.a', but got '%s'", diags[0].Description().Summary)
1155+
}
1156+
},
1157+
},
1158+
1159+
"action config refers to after triggering resource leads to circular dependency": {
1160+
module: map[string]string{
1161+
"main.tf": `
1162+
action "test_unlinked" "hello" {
1163+
config {
1164+
attr = test_object.a.name
1165+
}
1166+
}
1167+
resource "test_object" "a" {
1168+
lifecycle {
1169+
action_trigger {
1170+
events = [after_create]
1171+
actions = [action.test_unlinked.hello]
1172+
}
1173+
}
1174+
}
1175+
`,
1176+
},
1177+
expectPlanActionCalled: false,
1178+
assertValidateDiagnostics: func(t *testing.T, diags tfdiags.Diagnostics) {
1179+
if !diags.HasErrors() {
1180+
t.Fatalf("expected diagnostics to have errors, but it does not")
1181+
}
1182+
if len(diags) != 1 {
1183+
t.Fatalf("expected diagnostics to have 1 error, but it has %d", len(diags))
1184+
}
1185+
if diags[0].Description().Summary != "Cycle: test_object.a, action.test_unlinked.hello (expand)" && diags[0].Description().Summary != "Cycle: action.test_unlinked.hello (expand), test_object.a" {
1186+
t.Fatalf("expected diagnostic to have summary 'Cycle: test_object.a, action.test_unlinked.hello (expand)' or 'Cycle: action.test_unlinked.hello (expand), test_object.a', but got '%s'", diags[0].Description().Summary)
1187+
}
1188+
},
1189+
},
11191190
} {
11201191
t.Run(name, func(t *testing.T) {
11211192
if tc.toBeImplemented {
@@ -1248,6 +1319,8 @@ resource "other_object" "a" {
12481319
diags := ctx.Validate(m, &ValidateOpts{})
12491320
if tc.expectValidateDiagnostics != nil {
12501321
tfdiags.AssertDiagnosticsMatch(t, diags, tc.expectValidateDiagnostics(m))
1322+
} else if tc.assertValidateDiagnostics != nil {
1323+
tc.assertValidateDiagnostics(t, diags)
12511324
} else {
12521325
tfdiags.AssertNoDiagnostics(t, diags)
12531326
}

0 commit comments

Comments
 (0)