Skip to content

Commit 1caf99a

Browse files
add test cases for invoking an action in a module
1 parent 595aa3d commit 1caf99a

File tree

2 files changed

+224
-0
lines changed

2 files changed

+224
-0
lines changed

internal/terraform/context_apply_action_test.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1451,6 +1451,101 @@ action "action_example" "two" {
14511451
},
14521452
},
14531453

1454+
"action invoke in module": {
1455+
module: map[string]string{
1456+
"mod/main.tf": `
1457+
action "action_example" "one" {
1458+
config {
1459+
attr = "one"
1460+
}
1461+
}
1462+
action "action_example" "two" {
1463+
config {
1464+
attr = "two"
1465+
}
1466+
}
1467+
`,
1468+
1469+
"main.tf": `
1470+
module "mod" {
1471+
source = "./mod"
1472+
}
1473+
`,
1474+
},
1475+
expectInvokeActionCalled: true,
1476+
expectInvokeActionCalls: []providers.InvokeActionRequest{
1477+
{
1478+
ActionType: "action_example",
1479+
PlannedActionData: cty.ObjectVal(map[string]cty.Value{
1480+
"attr": cty.StringVal("one"),
1481+
}),
1482+
},
1483+
},
1484+
planOpts: &PlanOpts{
1485+
Mode: plans.RefreshOnlyMode,
1486+
ActionTargets: []addrs.Targetable{
1487+
addrs.AbsActionInstance{
1488+
Module: addrs.RootModuleInstance.Child("mod", addrs.NoKey),
1489+
Action: addrs.ActionInstance{
1490+
Action: addrs.Action{
1491+
Type: "action_example",
1492+
Name: "one",
1493+
},
1494+
Key: addrs.NoKey,
1495+
},
1496+
},
1497+
},
1498+
},
1499+
},
1500+
1501+
"action invoke in expanded module": {
1502+
module: map[string]string{
1503+
"mod/main.tf": `
1504+
action "action_example" "one" {
1505+
config {
1506+
attr = "one"
1507+
}
1508+
}
1509+
action "action_example" "two" {
1510+
config {
1511+
attr = "two"
1512+
}
1513+
}
1514+
`,
1515+
1516+
"main.tf": `
1517+
module "mod" {
1518+
count = 2
1519+
source = "./mod"
1520+
}
1521+
`,
1522+
},
1523+
expectInvokeActionCalled: true,
1524+
expectInvokeActionCalls: []providers.InvokeActionRequest{
1525+
{
1526+
ActionType: "action_example",
1527+
PlannedActionData: cty.ObjectVal(map[string]cty.Value{
1528+
"attr": cty.StringVal("one"),
1529+
}),
1530+
},
1531+
},
1532+
planOpts: &PlanOpts{
1533+
Mode: plans.RefreshOnlyMode,
1534+
ActionTargets: []addrs.Targetable{
1535+
addrs.AbsActionInstance{
1536+
Module: addrs.RootModuleInstance.Child("mod", addrs.IntKey(1)),
1537+
Action: addrs.ActionInstance{
1538+
Action: addrs.Action{
1539+
Type: "action_example",
1540+
Name: "one",
1541+
},
1542+
Key: addrs.NoKey,
1543+
},
1544+
},
1545+
},
1546+
},
1547+
},
1548+
14541549
"action invoke with count (all)": {
14551550
module: map[string]string{
14561551
"main.tf": `

internal/terraform/context_plan_actions_test.go

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2385,6 +2385,135 @@ action "test_action" "two" {
23852385
},
23862386
},
23872387

2388+
"action invoke in module": {
2389+
module: map[string]string{
2390+
"mod/main.tf": `
2391+
action "test_action" "one" {
2392+
config {
2393+
attr = "one"
2394+
}
2395+
}
2396+
action "test_action" "two" {
2397+
config {
2398+
attr = "two"
2399+
}
2400+
}
2401+
`,
2402+
"main.tf": `
2403+
module "mod" {
2404+
source = "./mod"
2405+
}
2406+
`,
2407+
},
2408+
planOpts: &PlanOpts{
2409+
Mode: plans.RefreshOnlyMode,
2410+
ActionTargets: []addrs.Targetable{
2411+
addrs.AbsActionInstance{
2412+
Module: addrs.RootModuleInstance.Child("mod", addrs.NoKey),
2413+
Action: addrs.ActionInstance{
2414+
Action: addrs.Action{
2415+
Type: "test_action",
2416+
Name: "one",
2417+
},
2418+
Key: addrs.NoKey,
2419+
},
2420+
},
2421+
},
2422+
},
2423+
expectPlanActionCalled: true,
2424+
assertPlan: func(t *testing.T, plan *plans.Plan) {
2425+
if len(plan.Changes.ActionInvocations) != 1 {
2426+
t.Fatalf("expected exactly one invocation, and found %d", len(plan.Changes.ActionInvocations))
2427+
}
2428+
2429+
ais := plan.Changes.ActionInvocations[0]
2430+
ai, err := ais.Decode(&testActionSchema)
2431+
if err != nil {
2432+
t.Fatal(err)
2433+
}
2434+
2435+
if _, ok := ai.ActionTrigger.(*plans.InvokeActionTrigger); !ok {
2436+
t.Fatalf("expected invoke action trigger type but was %T", ai.ActionTrigger)
2437+
}
2438+
2439+
expected := cty.ObjectVal(map[string]cty.Value{
2440+
"attr": cty.StringVal("one"),
2441+
})
2442+
if diff := cmp.Diff(ai.ConfigValue, expected, ctydebug.CmpOptions); len(diff) > 0 {
2443+
t.Fatalf("wrong value in plan: %s", diff)
2444+
}
2445+
2446+
if !ai.Addr.Equal(mustActionInstanceAddr(t, "module.mod.action.test_action.one")) {
2447+
t.Fatalf("wrong address in plan: %s", ai.Addr)
2448+
}
2449+
},
2450+
},
2451+
2452+
"action invoke in expanded module": {
2453+
module: map[string]string{
2454+
"mod/main.tf": `
2455+
action "test_action" "one" {
2456+
config {
2457+
attr = "one"
2458+
}
2459+
}
2460+
action "test_action" "two" {
2461+
config {
2462+
attr = "two"
2463+
}
2464+
}
2465+
`,
2466+
"main.tf": `
2467+
module "mod" {
2468+
count = 2
2469+
source = "./mod"
2470+
}
2471+
`,
2472+
},
2473+
planOpts: &PlanOpts{
2474+
Mode: plans.RefreshOnlyMode,
2475+
ActionTargets: []addrs.Targetable{
2476+
addrs.AbsActionInstance{
2477+
Module: addrs.RootModuleInstance.Child("mod", addrs.IntKey(1)),
2478+
Action: addrs.ActionInstance{
2479+
Action: addrs.Action{
2480+
Type: "test_action",
2481+
Name: "one",
2482+
},
2483+
Key: addrs.NoKey,
2484+
},
2485+
},
2486+
},
2487+
},
2488+
expectPlanActionCalled: true,
2489+
assertPlan: func(t *testing.T, plan *plans.Plan) {
2490+
if len(plan.Changes.ActionInvocations) != 1 {
2491+
t.Fatalf("expected exactly one invocation, and found %d", len(plan.Changes.ActionInvocations))
2492+
}
2493+
2494+
ais := plan.Changes.ActionInvocations[0]
2495+
ai, err := ais.Decode(&testActionSchema)
2496+
if err != nil {
2497+
t.Fatal(err)
2498+
}
2499+
2500+
if _, ok := ai.ActionTrigger.(*plans.InvokeActionTrigger); !ok {
2501+
t.Fatalf("expected invoke action trigger type but was %T", ai.ActionTrigger)
2502+
}
2503+
2504+
expected := cty.ObjectVal(map[string]cty.Value{
2505+
"attr": cty.StringVal("one"),
2506+
})
2507+
if diff := cmp.Diff(ai.ConfigValue, expected, ctydebug.CmpOptions); len(diff) > 0 {
2508+
t.Fatalf("wrong value in plan: %s", diff)
2509+
}
2510+
2511+
if !ai.Addr.Equal(mustActionInstanceAddr(t, "module.mod[1].action.test_action.one")) {
2512+
t.Fatalf("wrong address in plan: %s", ai.Addr)
2513+
}
2514+
},
2515+
},
2516+
23882517
"action invoke with count (all)": {
23892518
module: map[string]string{
23902519
"main.tf": `

0 commit comments

Comments
 (0)