4
4
package terraform
5
5
6
6
import (
7
+ "path/filepath"
7
8
"testing"
8
9
10
+ "github.com/hashicorp/hcl/v2"
9
11
"github.com/hashicorp/terraform/internal/addrs"
12
+ "github.com/hashicorp/terraform/internal/configs"
10
13
"github.com/hashicorp/terraform/internal/configs/configschema"
11
14
"github.com/hashicorp/terraform/internal/lang/marks"
12
15
"github.com/hashicorp/terraform/internal/plans"
@@ -31,7 +34,7 @@ func TestContext2Apply_actions(t *testing.T) {
31
34
expectInvokeActionCalled bool
32
35
expectInvokeActionCalls []providers.InvokeActionRequest
33
36
34
- expectDiagnostics tfdiags.Diagnostics
37
+ expectDiagnostics func ( m * configs. Config ) tfdiags.Diagnostics
35
38
}{
36
39
"unreferenced" : {
37
40
module : map [string ]string {
@@ -147,7 +150,6 @@ resource "test_object" "a" {
147
150
},
148
151
149
152
"before_create failing" : {
150
- toBeImplemented : true , // We need to revisit the diagnostic enhancement
151
153
module : map [string ]string {
152
154
"main.tf" : `
153
155
action "act_unlinked" "hello" {}
@@ -176,17 +178,21 @@ resource "test_object" "a" {
176
178
}
177
179
},
178
180
179
- expectDiagnostics : tfdiags.Diagnostics {
180
- tfdiags .Sourceless (
181
- tfdiags .Error ,
182
- "Failed to apply actions before test_object.a" ,
183
- "An error occured while invoking action action.act_unlinked.hello: test case for failing: this simulates a provider failing\n " ,
184
- ),
181
+ expectDiagnostics : func (m * configs.Config ) tfdiags.Diagnostics {
182
+ return tfdiags.Diagnostics {}.Append (& hcl.Diagnostic {
183
+ Severity : hcl .DiagError ,
184
+ Summary : "Error when invoking action" ,
185
+ Detail : "test case for failing: this simulates a provider failing" ,
186
+ Subject : & hcl.Range {
187
+ Filename : filepath .Join (m .Module .SourceDir , "main.tf" ),
188
+ Start : hcl.Pos {Line : 7 , Column : 18 , Byte : 146 },
189
+ End : hcl.Pos {Line : 7 , Column : 43 , Byte : 171 },
190
+ },
191
+ })
185
192
},
186
193
},
187
194
188
195
"before_create failing with successfully completed actions" : {
189
- toBeImplemented : true , // We need to revisit the diagnostic enhancement
190
196
module : map [string ]string {
191
197
"main.tf" : `
192
198
action "act_unlinked" "hello" {}
@@ -227,21 +233,24 @@ resource "test_object" "a" {
227
233
}
228
234
},
229
235
230
- expectDiagnostics : tfdiags.Diagnostics {
231
- tfdiags .Sourceless (
232
- tfdiags .Error ,
233
- "Failed to apply actions before test_object.a" ,
234
- `An error occured while invoking action action.act_unlinked.failure: test case for failing: this simulates a provider failing
235
- The following actions were successfully invoked:
236
- - action.act_unlinked.hello
237
- - action.act_unlinked.world
238
- As the resource did not change, these actions will be re-invoked in the next apply.` ,
239
- ),
236
+ expectDiagnostics : func (m * configs.Config ) tfdiags.Diagnostics {
237
+ return tfdiags.Diagnostics {}.Append (
238
+ & hcl.Diagnostic {
239
+ Severity : hcl .DiagError ,
240
+ Summary : "Error when invoking action" ,
241
+ Detail : `test case for failing: this simulates a provider failing` ,
242
+ Subject : & hcl.Range {
243
+ Filename : filepath .Join (m .Module .SourceDir , "main.tf" ),
244
+ Start : hcl.Pos {Line : 13 , Column : 72 , Byte : 305 },
245
+ End : hcl.Pos {Line : 13 , Column : 99 , Byte : 332 },
246
+ },
247
+ },
248
+ )
249
+
240
250
},
241
251
},
242
252
243
253
"before_create failing when calling invoke" : {
244
- toBeImplemented : true , // We need to revisit the diagnostic enhancement
245
254
module : map [string ]string {
246
255
"main.tf" : `
247
256
action "act_unlinked" "hello" {}
@@ -265,115 +274,87 @@ resource "test_object" "a" {
265
274
),
266
275
}
267
276
},
268
- expectDiagnostics : tfdiags.Diagnostics {
269
- tfdiags .Sourceless (
270
- tfdiags .Error ,
271
- "Failed to apply actions before test_object.a" ,
272
- "An error occured while invoking action action.act_unlinked.hello: test case for failing: this simulates a provider failing before the action is invoked\n " ,
273
- ),
274
- },
275
- },
276
-
277
- "after_create failing" : {
278
- toBeImplemented : true , // We need to revisit the diagnostic enhancement
279
- module : map [string ]string {
280
- "main.tf" : `
281
- action "act_unlinked" "hello" {}
282
- resource "test_object" "a" {
283
- lifecycle {
284
- action_trigger {
285
- events = [after_create]
286
- actions = [action.act_unlinked.hello]
287
- }
288
- }
289
- }
290
- ` ,
291
- },
292
- expectInvokeActionCalled : true ,
293
- events : func (req providers.InvokeActionRequest ) []providers.InvokeActionEvent {
294
- return []providers.InvokeActionEvent {
295
- providers.InvokeActionEvent_Completed {
296
- Diagnostics : tfdiags.Diagnostics {
297
- tfdiags .Sourceless (
298
- tfdiags .Error ,
299
- "test case for failing" ,
300
- "this simulates a provider failing" ,
301
- ),
277
+ expectDiagnostics : func (m * configs.Config ) tfdiags.Diagnostics {
278
+ return tfdiags.Diagnostics {}.Append (
279
+ & hcl.Diagnostic {
280
+ Severity : hcl .DiagError ,
281
+ Summary : "Error when invoking action" ,
282
+ Detail : "test case for failing: this simulates a provider failing before the action is invoked" ,
283
+ Subject : & hcl.Range {
284
+ Filename : filepath .Join (m .Module .SourceDir , "main.tf" ),
285
+ Start : hcl.Pos {Line : 7 , Column : 18 , Byte : 146 },
286
+ End : hcl.Pos {Line : 7 , Column : 43 , Byte : 171 },
302
287
},
303
288
},
304
- }
305
- },
306
-
307
- expectDiagnostics : tfdiags.Diagnostics {
308
- tfdiags .Sourceless (
309
- tfdiags .Error ,
310
- "Failed to apply actions after test_object.a" ,
311
- `An error occured while invoking action action.act_unlinked.hello: test case for failing: this simulates a provider failing
312
-
313
- The following actions were not yet invoked:
314
- - action.act_unlinked.hello
315
- These actions will not be triggered in the next apply, please run "terraform invoke" to invoke them.` ,
316
- ),
289
+ )
317
290
},
318
291
},
319
292
320
- "after_create failing with successfully completed actions" : {
321
- toBeImplemented : true , // We need to revisit the diagnostic enhancement
293
+ "failing an action by action event stops next actions in list" : {
322
294
module : map [string ]string {
323
295
"main.tf" : `
324
296
action "act_unlinked" "hello" {}
325
- action "act_unlinked" "world" {}
326
297
action "act_unlinked" "failure" {
327
298
config {
328
299
attr = "failure"
329
300
}
330
301
}
302
+ action "act_unlinked" "goodbye" {}
331
303
resource "test_object" "a" {
332
304
lifecycle {
333
305
action_trigger {
334
- events = [after_create ]
335
- actions = [action.act_unlinked.hello, action.act_unlinked.world , action.act_unlinked.failure ]
306
+ events = [before_create ]
307
+ actions = [action.act_unlinked.hello, action.act_unlinked.failure , action.act_unlinked.goodbye ]
336
308
}
337
309
}
338
310
}
339
311
` ,
340
312
},
341
313
expectInvokeActionCalled : true ,
342
- events : func (req providers.InvokeActionRequest ) []providers.InvokeActionEvent {
343
- if ! req .PlannedActionData .IsNull () && req .PlannedActionData .GetAttr ("attr" ).AsString () == "failure" {
314
+ events : func (r providers.InvokeActionRequest ) []providers.InvokeActionEvent {
315
+ if ! r .PlannedActionData .IsNull () && r .PlannedActionData .GetAttr ("attr" ).AsString () == "failure" {
344
316
return []providers.InvokeActionEvent {
345
317
providers.InvokeActionEvent_Completed {
346
- Diagnostics : tfdiags.Diagnostics {
347
- tfdiags .Sourceless (
348
- tfdiags .Error ,
349
- "test case for failing" ,
350
- "this simulates a provider failing" ,
351
- ),
352
- },
318
+ Diagnostics : tfdiags.Diagnostics {}.Append (tfdiags .Sourceless (tfdiags .Error , "test case for failing" , "this simulates a provider failing" )),
353
319
},
354
320
}
355
- } else {
356
- return []providers.InvokeActionEvent {
357
- providers.InvokeActionEvent_Completed {},
358
- }
359
321
}
360
- },
361
322
362
- expectDiagnostics : tfdiags.Diagnostics {
363
- tfdiags .Sourceless (
364
- tfdiags .Error ,
365
- "Failed to apply actions after test_object.a" ,
366
- `An error occured while invoking action action.act_unlinked.failure: test case for failing: this simulates a provider failing
323
+ return []providers.InvokeActionEvent {
324
+ providers.InvokeActionEvent_Completed {},
325
+ }
367
326
368
- The following actions were not yet invoked:
369
- - action.act_unlinked.failure
370
- These actions will not be triggered in the next apply, please run "terraform invoke" to invoke them.` ,
371
- ),
372
327
},
328
+ expectDiagnostics : func (m * configs.Config ) tfdiags.Diagnostics {
329
+ return tfdiags.Diagnostics {}.Append (
330
+ & hcl.Diagnostic {
331
+ Severity : hcl .DiagError ,
332
+ Summary : "Error when invoking action" ,
333
+ Detail : "test case for failing: this simulates a provider failing" ,
334
+ Subject : & hcl.Range {
335
+ Filename : filepath .Join (m .Module .SourceDir , "main.tf" ),
336
+ Start : hcl.Pos {Line : 13 , Column : 45 , Byte : 280 },
337
+ End : hcl.Pos {Line : 13 , Column : 72 , Byte : 307 },
338
+ },
339
+ },
340
+ )
341
+ },
342
+
343
+ // We expect two calls but not the third one, because the second action fails
344
+ expectInvokeActionCalls : []providers.InvokeActionRequest {{
345
+ ActionType : "act_unlinked" ,
346
+ PlannedActionData : cty .NullVal (cty .Object (map [string ]cty.Type {
347
+ "attr" : cty .String ,
348
+ })),
349
+ }, {
350
+ ActionType : "act_unlinked" ,
351
+ PlannedActionData : cty .ObjectVal (map [string ]cty.Value {
352
+ "attr" : cty .StringVal ("failure" ),
353
+ }),
354
+ }},
373
355
},
374
356
375
- "failing an action stops next actions in list" : {
376
- toBeImplemented : true , // We need to revisit the diagnostic enhancement
357
+ "failing an action during invocation stops next actions in list" : {
377
358
module : map [string ]string {
378
359
"main.tf" : `
379
360
action "act_unlinked" "hello" {}
@@ -407,15 +388,19 @@ resource "test_object" "a" {
407
388
}
408
389
return tfdiags.Diagnostics {}
409
390
},
410
- expectDiagnostics : tfdiags.Diagnostics {
411
- tfdiags .Sourceless (
412
- tfdiags .Error ,
413
- "Failed to apply actions before test_object.a" ,
414
- `An error occured while invoking action action.act_unlinked.failure: test case for failing: this simulates a provider failing
415
- The following actions were successfully invoked:
416
- - action.act_unlinked.hello
417
- As the resource did not change, these actions will be re-invoked in the next apply.` ,
418
- ),
391
+ expectDiagnostics : func (m * configs.Config ) tfdiags.Diagnostics {
392
+ return tfdiags.Diagnostics {}.Append (
393
+ & hcl.Diagnostic {
394
+ Severity : hcl .DiagError ,
395
+ Summary : "Error when invoking action" ,
396
+ Detail : "test case for failing: this simulates a provider failing" ,
397
+ Subject : & hcl.Range {
398
+ Filename : filepath .Join (m .Module .SourceDir , "main.tf" ),
399
+ Start : hcl.Pos {Line : 13 , Column : 45 , Byte : 280 },
400
+ End : hcl.Pos {Line : 13 , Column : 72 , Byte : 307 },
401
+ },
402
+ },
403
+ )
419
404
},
420
405
421
406
// We expect two calls but not the third one, because the second action fails
@@ -433,7 +418,6 @@ As the resource did not change, these actions will be re-invoked in the next app
433
418
},
434
419
435
420
"failing an action stops next action triggers" : {
436
- toBeImplemented : true , // We need to revisit the diagnostic enhancement
437
421
module : map [string ]string {
438
422
"main.tf" : `
439
423
action "act_unlinked" "hello" {}
@@ -475,15 +459,19 @@ resource "test_object" "a" {
475
459
}
476
460
return tfdiags.Diagnostics {}
477
461
},
478
- expectDiagnostics : tfdiags.Diagnostics {
479
- tfdiags .Sourceless (
480
- tfdiags .Error ,
481
- "Failed to apply actions before test_object.a" ,
482
- `An error occured while invoking action action.act_unlinked.failure: test case for failing: this simulates a provider failing
483
- The following actions were successfully invoked:
484
- - action.act_unlinked.hello
485
- As the resource did not change, these actions will be re-invoked in the next apply.` ,
486
- ),
462
+ expectDiagnostics : func (m * configs.Config ) tfdiags.Diagnostics {
463
+ return tfdiags.Diagnostics {}.Append (
464
+ & hcl.Diagnostic {
465
+ Severity : hcl .DiagError ,
466
+ Summary : "Error when invoking action" ,
467
+ Detail : "test case for failing: this simulates a provider failing" ,
468
+ Subject : & hcl.Range {
469
+ Filename : filepath .Join (m .Module .SourceDir , "main.tf" ),
470
+ Start : hcl.Pos {Line : 17 , Column : 18 , Byte : 355 },
471
+ End : hcl.Pos {Line : 17 , Column : 45 , Byte : 382 },
472
+ },
473
+ },
474
+ )
487
475
},
488
476
// We expect two calls but not the third one, because the second action fails
489
477
expectInvokeActionCalls : []providers.InvokeActionRequest {{
@@ -962,8 +950,8 @@ resource "test_object" "a" {
962
950
tfdiags .AssertNoDiagnostics (t , diags )
963
951
964
952
_ , diags = ctx .Apply (plan , m , nil )
965
- if tc .expectDiagnostics . HasErrors () {
966
- tfdiags .AssertDiagnosticsMatch (t , diags , tc .expectDiagnostics )
953
+ if tc .expectDiagnostics != nil {
954
+ tfdiags .AssertDiagnosticsMatch (t , diags , tc .expectDiagnostics ( m ) )
967
955
} else {
968
956
tfdiags .AssertNoDiagnostics (t , diags )
969
957
}
0 commit comments