|
8 | 8 |
|
9 | 9 | "github.com/hashicorp/hcl/v2"
|
10 | 10 | "github.com/hashicorp/hcl/v2/hcltest"
|
| 11 | + "github.com/zclconf/go-cty/cty" |
11 | 12 | )
|
12 | 13 |
|
13 | 14 | func TestDecodeActionBlock(t *testing.T) {
|
@@ -125,6 +126,129 @@ func TestDecodeActionBlock(t *testing.T) {
|
125 | 126 | }
|
126 | 127 | }
|
127 | 128 |
|
| 129 | +func TestDecodeActionTriggerBlock(t *testing.T) { |
| 130 | + conditionExpr := hcltest.MockExprLiteral(cty.True) |
| 131 | + eventsListExpr := hcltest.MockExprList([]hcl.Expression{hcltest.MockExprTraversalSrc("after_create"), hcltest.MockExprTraversalSrc("after_update")}) |
| 132 | + |
| 133 | + fooActionExpr := hcltest.MockExprTraversalSrc("action.action_type.foo") |
| 134 | + barActionExpr := hcltest.MockExprTraversalSrc("action.action_type.bar") |
| 135 | + fooAndBarExpr := hcltest.MockExprList([]hcl.Expression{fooActionExpr, barActionExpr}) |
| 136 | + |
| 137 | + // bad inputs! |
| 138 | + moduleActionExpr := hcltest.MockExprTraversalSrc("module.foo.action.action_type.bar") |
| 139 | + fooDataSourceExpr := hcltest.MockExprTraversalSrc("data.example.foo") |
| 140 | + |
| 141 | + tests := map[string]struct { |
| 142 | + input *hcl.Block |
| 143 | + want *ActionTrigger |
| 144 | + expectDiags []string |
| 145 | + }{ |
| 146 | + "simple example": { |
| 147 | + &hcl.Block{ |
| 148 | + Type: "action_trigger", |
| 149 | + Body: hcltest.MockBody(&hcl.BodyContent{ |
| 150 | + Attributes: hcltest.MockAttrs(map[string]hcl.Expression{ |
| 151 | + "condition": conditionExpr, |
| 152 | + "events": eventsListExpr, |
| 153 | + "actions": fooAndBarExpr, |
| 154 | + }), |
| 155 | + }), |
| 156 | + }, |
| 157 | + &ActionTrigger{ |
| 158 | + Condition: conditionExpr, |
| 159 | + Events: []ActionTriggerEvent{AfterCreate, AfterUpdate}, |
| 160 | + Actions: []ActionRef{ |
| 161 | + { |
| 162 | + mustAbsTraversalForExpr(fooActionExpr), |
| 163 | + fooActionExpr.Range(), |
| 164 | + }, |
| 165 | + { |
| 166 | + mustAbsTraversalForExpr(barActionExpr), |
| 167 | + barActionExpr.Range(), |
| 168 | + }, |
| 169 | + }, |
| 170 | + }, |
| 171 | + nil, |
| 172 | + }, |
| 173 | + "error - referencing actions in other modules": { |
| 174 | + &hcl.Block{ |
| 175 | + Type: "action_trigger", |
| 176 | + Body: hcltest.MockBody(&hcl.BodyContent{ |
| 177 | + Attributes: hcltest.MockAttrs(map[string]hcl.Expression{ |
| 178 | + "condition": conditionExpr, |
| 179 | + "events": eventsListExpr, |
| 180 | + "actions": hcltest.MockExprList([]hcl.Expression{moduleActionExpr}), |
| 181 | + }), |
| 182 | + }), |
| 183 | + }, |
| 184 | + &ActionTrigger{ |
| 185 | + Condition: conditionExpr, |
| 186 | + Events: []ActionTriggerEvent{AfterCreate, AfterUpdate}, |
| 187 | + Actions: []ActionRef{}, |
| 188 | + }, |
| 189 | + []string{ |
| 190 | + "MockExprTraversal:0,0-33: Invalid actions argument inside action_triggers; action_triggers.actions accepts a list of one or more actions, which must be in the current module.", |
| 191 | + ":0,0-0: No actions specified; At least one action must be specified for an action_trigger.", |
| 192 | + }, |
| 193 | + }, |
| 194 | + "error - action is not an action": { |
| 195 | + &hcl.Block{ |
| 196 | + Type: "action_trigger", |
| 197 | + Body: hcltest.MockBody(&hcl.BodyContent{ |
| 198 | + Attributes: hcltest.MockAttrs(map[string]hcl.Expression{ |
| 199 | + "condition": conditionExpr, |
| 200 | + "events": eventsListExpr, |
| 201 | + "actions": hcltest.MockExprList([]hcl.Expression{fooDataSourceExpr}), |
| 202 | + }), |
| 203 | + }), |
| 204 | + }, |
| 205 | + &ActionTrigger{ |
| 206 | + Condition: conditionExpr, |
| 207 | + Events: []ActionTriggerEvent{AfterCreate, AfterUpdate}, |
| 208 | + Actions: []ActionRef{}, |
| 209 | + }, |
| 210 | + []string{ |
| 211 | + "MockExprTraversal:0,0-16: Invalid actions argument inside action_triggers; action_triggers.actions accepts a list of one or more actions, which must be in the current module.", |
| 212 | + ":0,0-0: No actions specified; At least one action must be specified for an action_trigger.", |
| 213 | + }, |
| 214 | + }, |
| 215 | + "error - invalid event": { |
| 216 | + &hcl.Block{ |
| 217 | + Type: "action_trigger", |
| 218 | + Body: hcltest.MockBody(&hcl.BodyContent{ |
| 219 | + Attributes: hcltest.MockAttrs(map[string]hcl.Expression{ |
| 220 | + "condition": conditionExpr, |
| 221 | + "events": hcltest.MockExprList([]hcl.Expression{hcltest.MockExprTraversalSrc("not_an_event")}), |
| 222 | + "actions": hcltest.MockExprList([]hcl.Expression{fooActionExpr}), |
| 223 | + }), |
| 224 | + }), |
| 225 | + }, |
| 226 | + &ActionTrigger{ |
| 227 | + Condition: conditionExpr, |
| 228 | + Events: []ActionTriggerEvent{}, |
| 229 | + Actions: []ActionRef{ |
| 230 | + { |
| 231 | + mustAbsTraversalForExpr(fooActionExpr), |
| 232 | + fooActionExpr.Range(), |
| 233 | + }, |
| 234 | + }, |
| 235 | + }, |
| 236 | + []string{ |
| 237 | + "MockExprTraversal:0,0-12: Invalid \"event\" value not_an_event; The \"event\" argument supports the following values: before_create, after_create, before_update, after_update, before_destroy, after_destroy.", |
| 238 | + ":0,0-0: No events specified; At least one event must be specified for an action_trigger.", |
| 239 | + }, |
| 240 | + }, |
| 241 | + } |
| 242 | + |
| 243 | + for name, test := range tests { |
| 244 | + t.Run(name, func(t *testing.T) { |
| 245 | + got, diags := decodeActionTriggerBlock(test.input) |
| 246 | + assertExactDiagnostics(t, diags, test.expectDiags) |
| 247 | + assertResultDeepEqual(t, got, test.want) |
| 248 | + }) |
| 249 | + } |
| 250 | +} |
| 251 | + |
128 | 252 | func mustAbsTraversalForExpr(expr hcl.Expression) hcl.Traversal {
|
129 | 253 | trav, diags := hcl.AbsTraversalForExpr(expr)
|
130 | 254 | if diags.HasErrors() {
|
|
0 commit comments