@@ -8,18 +8,20 @@ import (
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 ) {
14
15
fooResourceExpr := hcltest .MockExprTraversalSrc ("resource_type.foo" )
15
16
barResourceExpr := hcltest .MockExprTraversalSrc ("resource_type.bar" )
16
-
17
17
fooAndBarExpr := hcltest .MockExprList ([]hcl.Expression {fooResourceExpr , barResourceExpr })
18
+ moduleResourceExpr := hcltest .MockExprTraversalSrc ("module.foo.resource_type.bar" )
19
+ fooDataSourceExpr := hcltest .MockExprTraversalSrc ("data.example.foo" )
18
20
19
21
tests := map [string ]struct {
20
22
input * hcl.Block
21
23
want * Action
22
- expectDiags []hcl. Diagnostic
24
+ expectDiags []string
23
25
}{
24
26
"one linked resource" : {
25
27
& hcl.Block {
@@ -67,16 +69,181 @@ func TestDecodeActionBlock(t *testing.T) {
67
69
},
68
70
nil ,
69
71
},
72
+ "invalid linked resources (module ref)" : { // for now! This test will change when we support cross-module actions
73
+ & hcl.Block {
74
+ Type : "action" ,
75
+ Labels : []string {"an_action" , "foo" },
76
+ Body : hcltest .MockBody (& hcl.BodyContent {
77
+ Attributes : hcl.Attributes {
78
+ "linked_resources" : {
79
+ Name : "linked_resources" ,
80
+ Expr : hcltest .MockExprList ([]hcl.Expression {moduleResourceExpr }),
81
+ },
82
+ },
83
+ }),
84
+ DefRange : blockRange ,
85
+ LabelRanges : []hcl.Range {{}},
86
+ },
87
+ & Action {
88
+ Type : "an_action" ,
89
+ Name : "foo" ,
90
+ LinkedResources : []hcl.Traversal {},
91
+ DeclRange : blockRange ,
92
+ },
93
+ []string {`:0,0-0: Invalid "linked_resources"; "linked_resources" must only refer to managed resources in the current module.` },
94
+ },
95
+ "invalid linked resource (datasource ref)" : {
96
+ & hcl.Block {
97
+ Type : "action" ,
98
+ Labels : []string {"an_action" , "foo" },
99
+ Body : hcltest .MockBody (& hcl.BodyContent {
100
+ Attributes : hcl.Attributes {
101
+ "linked_resource" : {
102
+ Name : "linked_resource" ,
103
+ Expr : fooDataSourceExpr ,
104
+ },
105
+ },
106
+ }),
107
+ DefRange : blockRange ,
108
+ LabelRanges : []hcl.Range {{}},
109
+ },
110
+ & Action {
111
+ Type : "an_action" ,
112
+ Name : "foo" ,
113
+ LinkedResources : nil ,
114
+ DeclRange : blockRange ,
115
+ },
116
+ []string {`:0,0-0: Invalid "linked_resource"; "linked_resource" must only refer to a managed resource in the current module.` },
117
+ },
70
118
}
71
119
72
120
for name , test := range tests {
73
121
t .Run (name , func (t * testing.T ) {
74
122
got , diags := decodeActionBlock (test .input )
75
- if len (diags ) != len (test .expectDiags ) {
76
- t .Error (diags .Error ())
77
- t .Fatalf ("Wrong result! Expected %d diagnostics, got %d" , len (test .expectDiags ), len (diags ))
78
- }
123
+ assertExactDiagnostics (t , diags , test .expectDiags )
124
+ assertResultDeepEqual (t , got , test .want )
125
+ })
126
+ }
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
+ }
79
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 )
80
247
assertResultDeepEqual (t , got , test .want )
81
248
})
82
249
}
0 commit comments