actions: add extra test cases around conditions referencing the triggering resource

pull/37687/head
Daniel Schmidt 8 months ago
parent ab41592fb7
commit a4cc769e5b

@ -1852,6 +1852,46 @@ resource "test_object" "a" {
},
expectInvokeActionCalled: true,
},
"referencing triggering resource in after_* condition": {
module: map[string]string{
"main.tf": `
action "action_example" "hello" {
config {
attr = "hello"
}
}
action "action_example" "world" {
config {
attr = "world"
}
}
resource "test_object" "a" {
name = "foo"
lifecycle {
action_trigger {
events = [after_create]
condition = test_object.a.name == "foo"
actions = [action.action_example.hello]
}
action_trigger {
events = [after_update]
condition = test_object.a.name == "bar"
actions = [action.action_example.world]
}
}
}
`,
},
expectInvokeActionCalled: true,
expectInvokeActionCalls: []providers.InvokeActionRequest{
{
ActionType: "action_example",
PlannedActionData: cty.ObjectVal(map[string]cty.Value{
"attr": cty.StringVal("hello"),
}),
},
},
},
"multiple events triggering in same action trigger": {
module: map[string]string{
"main.tf": `

@ -3053,6 +3053,73 @@ resource "test_object" "a" {
},
},
"referencing triggering resource in before_* condition": {
module: map[string]string{
"main.tf": `
action "test_action" "hello" {}
action "test_action" "world" {}
resource "test_object" "a" {
name = "foo"
lifecycle {
action_trigger {
events = [before_create]
condition = test_object.a.name == "foo"
actions = [action.test_action.hello]
}
action_trigger {
events = [before_update]
condition = test_object.a.name == "bar"
actions = [action.test_action.world]
}
}
}
`,
},
expectPlanActionCalled: true,
assertPlanDiagnostics: func(t *testing.T, diags tfdiags.Diagnostics) {
if !diags.HasErrors() {
t.Errorf("expected errors, got none")
}
err := diags.Err().Error()
if !strings.Contains(err, "Cycle:") || !strings.Contains(err, "action.test_action.hello") || !strings.Contains(err, "test_object.a") {
t.Fatalf("Expected '[Error] Cycle: action.test_action.hello (instance), test_object.a', got '%s'", err)
}
},
},
"referencing triggering resource in after_* condition": {
module: map[string]string{
"main.tf": `
action "test_action" "hello" {}
action "test_action" "world" {}
resource "test_object" "a" {
name = "foo"
lifecycle {
action_trigger {
events = [after_create]
condition = test_object.a.name == "foo"
actions = [action.test_action.hello]
}
action_trigger {
events = [after_update]
condition = test_object.a.name == "bar"
actions = [action.test_action.world]
}
}
}
`,
},
expectPlanActionCalled: true,
assertPlan: func(t *testing.T, p *plans.Plan) {
if len(p.Changes.ActionInvocations) != 1 {
t.Errorf("expected 1 action invocation, got %d", len(p.Changes.ActionInvocations))
}
},
},
"using each in before_* condition": {
module: map[string]string{
"main.tf": `

Loading…
Cancel
Save