Skip to content

Commit a4cc769

Browse files
actions: add extra test cases around conditions referencing the triggering resource
1 parent ab41592 commit a4cc769

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
@@ -1852,6 +1852,46 @@ resource "test_object" "a" {
18521852
},
18531853
expectInvokeActionCalled: true,
18541854
},
1855+
"referencing triggering resource in after_* condition": {
1856+
module: map[string]string{
1857+
"main.tf": `
1858+
action "action_example" "hello" {
1859+
config {
1860+
attr = "hello"
1861+
}
1862+
}
1863+
action "action_example" "world" {
1864+
config {
1865+
attr = "world"
1866+
}
1867+
}
1868+
resource "test_object" "a" {
1869+
name = "foo"
1870+
lifecycle {
1871+
action_trigger {
1872+
events = [after_create]
1873+
condition = test_object.a.name == "foo"
1874+
actions = [action.action_example.hello]
1875+
}
1876+
action_trigger {
1877+
events = [after_update]
1878+
condition = test_object.a.name == "bar"
1879+
actions = [action.action_example.world]
1880+
}
1881+
}
1882+
}
1883+
`,
1884+
},
1885+
expectInvokeActionCalled: true,
1886+
expectInvokeActionCalls: []providers.InvokeActionRequest{
1887+
{
1888+
ActionType: "action_example",
1889+
PlannedActionData: cty.ObjectVal(map[string]cty.Value{
1890+
"attr": cty.StringVal("hello"),
1891+
}),
1892+
},
1893+
},
1894+
},
18551895
"multiple events triggering in same action trigger": {
18561896
module: map[string]string{
18571897
"main.tf": `

internal/terraform/context_plan_actions_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3053,6 +3053,73 @@ resource "test_object" "a" {
30533053
},
30543054
},
30553055

3056+
"referencing triggering resource in before_* condition": {
3057+
module: map[string]string{
3058+
"main.tf": `
3059+
action "test_action" "hello" {}
3060+
action "test_action" "world" {}
3061+
resource "test_object" "a" {
3062+
name = "foo"
3063+
lifecycle {
3064+
action_trigger {
3065+
events = [before_create]
3066+
condition = test_object.a.name == "foo"
3067+
actions = [action.test_action.hello]
3068+
}
3069+
action_trigger {
3070+
events = [before_update]
3071+
condition = test_object.a.name == "bar"
3072+
actions = [action.test_action.world]
3073+
}
3074+
}
3075+
}
3076+
`,
3077+
},
3078+
expectPlanActionCalled: true,
3079+
3080+
assertPlanDiagnostics: func(t *testing.T, diags tfdiags.Diagnostics) {
3081+
if !diags.HasErrors() {
3082+
t.Errorf("expected errors, got none")
3083+
}
3084+
3085+
err := diags.Err().Error()
3086+
if !strings.Contains(err, "Cycle:") || !strings.Contains(err, "action.test_action.hello") || !strings.Contains(err, "test_object.a") {
3087+
t.Fatalf("Expected '[Error] Cycle: action.test_action.hello (instance), test_object.a', got '%s'", err)
3088+
}
3089+
},
3090+
},
3091+
3092+
"referencing triggering resource in after_* condition": {
3093+
module: map[string]string{
3094+
"main.tf": `
3095+
action "test_action" "hello" {}
3096+
action "test_action" "world" {}
3097+
resource "test_object" "a" {
3098+
name = "foo"
3099+
lifecycle {
3100+
action_trigger {
3101+
events = [after_create]
3102+
condition = test_object.a.name == "foo"
3103+
actions = [action.test_action.hello]
3104+
}
3105+
action_trigger {
3106+
events = [after_update]
3107+
condition = test_object.a.name == "bar"
3108+
actions = [action.test_action.world]
3109+
}
3110+
}
3111+
}
3112+
`,
3113+
},
3114+
expectPlanActionCalled: true,
3115+
3116+
assertPlan: func(t *testing.T, p *plans.Plan) {
3117+
if len(p.Changes.ActionInvocations) != 1 {
3118+
t.Errorf("expected 1 action invocation, got %d", len(p.Changes.ActionInvocations))
3119+
}
3120+
},
3121+
},
3122+
30563123
"using each in before_* condition": {
30573124
module: map[string]string{
30583125
"main.tf": `

0 commit comments

Comments
 (0)