Skip to content

Commit e51c9bf

Browse files
actions: add extra test cases around conditions referencing the triggering resource
1 parent 4b713ba commit e51c9bf

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

internal/terraform/context_apply_action_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1906,6 +1906,46 @@ resource "test_object" "a" {
19061906
},
19071907
expectInvokeActionCalled: true,
19081908
},
1909+
"referencing triggering resource in after_* condition": {
1910+
module: map[string]string{
1911+
"main.tf": `
1912+
action "action_example" "hello" {
1913+
config {
1914+
attr = "hello"
1915+
}
1916+
}
1917+
action "action_example" "world" {
1918+
config {
1919+
attr = "world"
1920+
}
1921+
}
1922+
resource "test_object" "a" {
1923+
name = "foo"
1924+
lifecycle {
1925+
action_trigger {
1926+
events = [after_create]
1927+
condition = test_object.a.name == "foo"
1928+
actions = [action.action_example.hello]
1929+
}
1930+
action_trigger {
1931+
events = [after_update]
1932+
condition = test_object.a.name == "bar"
1933+
actions = [action.action_example.world]
1934+
}
1935+
}
1936+
}
1937+
`,
1938+
},
1939+
expectInvokeActionCalled: true,
1940+
expectInvokeActionCalls: []providers.InvokeActionRequest{
1941+
{
1942+
ActionType: "action_example",
1943+
PlannedActionData: cty.ObjectVal(map[string]cty.Value{
1944+
"attr": cty.StringVal("hello"),
1945+
}),
1946+
},
1947+
},
1948+
},
19091949
"multiple events triggering in same action trigger": {
19101950
module: map[string]string{
19111951
"main.tf": `

internal/terraform/context_plan_actions_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3000,6 +3000,73 @@ resource "test_object" "a" {
30003000
},
30013001
},
30023002

3003+
"referencing triggering resource in before_* condition": {
3004+
module: map[string]string{
3005+
"main.tf": `
3006+
action "test_action" "hello" {}
3007+
action "test_action" "world" {}
3008+
resource "test_object" "a" {
3009+
name = "foo"
3010+
lifecycle {
3011+
action_trigger {
3012+
events = [before_create]
3013+
condition = test_object.a.name == "foo"
3014+
actions = [action.test_action.hello]
3015+
}
3016+
action_trigger {
3017+
events = [before_update]
3018+
condition = test_object.a.name == "bar"
3019+
actions = [action.test_action.world]
3020+
}
3021+
}
3022+
}
3023+
`,
3024+
},
3025+
expectPlanActionCalled: true,
3026+
3027+
assertPlanDiagnostics: func(t *testing.T, diags tfdiags.Diagnostics) {
3028+
if !diags.HasErrors() {
3029+
t.Errorf("expected errors, got none")
3030+
}
3031+
3032+
err := diags.Err().Error()
3033+
if !strings.Contains(err, "Cycle:") || !strings.Contains(err, "action.test_action.hello") || !strings.Contains(err, "test_object.a") {
3034+
t.Fatalf("Expected '[Error] Cycle: action.test_action.hello (instance), test_object.a', got '%s'", err)
3035+
}
3036+
},
3037+
},
3038+
3039+
"referencing triggering resource in after_* condition": {
3040+
module: map[string]string{
3041+
"main.tf": `
3042+
action "test_action" "hello" {}
3043+
action "test_action" "world" {}
3044+
resource "test_object" "a" {
3045+
name = "foo"
3046+
lifecycle {
3047+
action_trigger {
3048+
events = [after_create]
3049+
condition = test_object.a.name == "foo"
3050+
actions = [action.test_action.hello]
3051+
}
3052+
action_trigger {
3053+
events = [after_update]
3054+
condition = test_object.a.name == "bar"
3055+
actions = [action.test_action.world]
3056+
}
3057+
}
3058+
}
3059+
`,
3060+
},
3061+
expectPlanActionCalled: true,
3062+
3063+
assertPlan: func(t *testing.T, p *plans.Plan) {
3064+
if len(p.Changes.ActionInvocations) != 1 {
3065+
t.Errorf("expected 1 action invocation, got %d", len(p.Changes.ActionInvocations))
3066+
}
3067+
},
3068+
},
3069+
30033070
"using each in before_* condition": {
30043071
module: map[string]string{
30053072
"main.tf": `

0 commit comments

Comments
 (0)