Skip to content

Commit 1cd0835

Browse files
add test cases for invoking an action in a module
1 parent 053738f commit 1cd0835

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
@@ -1449,6 +1449,101 @@ action "action_example" "two" {
14491449
},
14501450
},
14511451

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