@@ -272,3 +272,176 @@ EOF
272
272
}
273
273
`
274
274
}
275
+
276
+ func TestAccShellShellScript_failedUpdate (t * testing.T ) {
277
+ resource .Test (t , resource.TestCase {
278
+ Providers : testAccProviders ,
279
+ Steps : []resource.TestStep {
280
+ {
281
+ Config : testAccShellScriptConfig_failedUpdate ("value1" ),
282
+ Check : resource .TestCheckResourceAttr ("shell_script.shell_script" , "environment.VALUE" , "value1" ),
283
+ },
284
+ {
285
+ Config : testAccShellScriptConfig_failedUpdate ("value2" ),
286
+ ExpectNonEmptyPlan : true ,
287
+ ExpectError : regexp .MustCompile ("Error occured during shell execution" ),
288
+ Check : resource .TestCheckResourceAttr ("shell_script.shell_script" , "environment.VALUE" , "value1" ),
289
+ },
290
+ },
291
+ })
292
+ }
293
+
294
+ func testAccShellScriptConfig_failedUpdate (value string ) string {
295
+ return fmt .Sprintf (`
296
+ resource "shell_script" "shell_script" {
297
+ lifecycle_commands {
298
+ create = "echo"
299
+ read = <<-EOF
300
+ echo -n '{"test": true}'
301
+ EOF
302
+ update = "exit 1"
303
+ delete = "echo"
304
+ }
305
+ environment = {
306
+ VALUE = "%s"
307
+ }
308
+ }
309
+ ` , value )
310
+ }
311
+
312
+ func testAccCheckNoFiles (files ... string ) func (t * terraform.State ) error {
313
+ return func (t * terraform.State ) error {
314
+ for _ , f := range files {
315
+ if _ , err := os .Stat (f ); err == nil {
316
+ return fmt .Errorf ("'%s' should no longer exist" , f )
317
+ }
318
+ }
319
+ return nil
320
+ }
321
+ }
322
+
323
+ func TestAccShellShellScript_recreate (t * testing.T ) {
324
+ file1 , file2 := "/tmp/some-file-" + acctest .RandString (16 ), "/tmp/some-file-" + acctest .RandString (16 )
325
+ resource .Test (t , resource.TestCase {
326
+ Providers : testAccProviders ,
327
+ CheckDestroy : testAccCheckNoFiles (file1 , file2 ),
328
+ Steps : []resource.TestStep {
329
+ {
330
+ Config : testAccShellShellScriptConfig_recreate (file1 ),
331
+ },
332
+ {
333
+ Config : testAccShellShellScriptConfig_recreate (file2 ),
334
+ },
335
+ },
336
+ })
337
+ }
338
+ func testAccShellShellScriptConfig_recreate (filename string ) string {
339
+ return fmt .Sprintf (`
340
+ resource "shell_script" "shell_script" {
341
+ lifecycle_commands {
342
+ create = <<-EOF
343
+ echo -n '{"test": true}' > "$FILE"
344
+ EOF
345
+ read = <<-EOF
346
+ cat "$FILE"
347
+ EOF
348
+ delete = <<-EOF
349
+ rm "$FILE"
350
+ EOF
351
+ }
352
+ environment = {
353
+ FILE = "%s"
354
+ }
355
+ }
356
+ ` , filename )
357
+ }
358
+
359
+ func TestAccShellShellScript_readFailed (t * testing.T ) {
360
+ file := "/tmp/test-file-" + acctest .RandString (16 )
361
+ resource .Test (t , resource.TestCase {
362
+ Providers : testAccProviders ,
363
+ CheckDestroy : testAccCheckNoFiles (file ),
364
+ Steps : []resource.TestStep {
365
+ {
366
+ Config : testAccShellShellScriptConfig_readFailed (file , true ),
367
+ ExpectNonEmptyPlan : true ,
368
+ Check : resource .TestCheckResourceAttr ("shell_script.shell_script" , "output.test" , "true" ),
369
+ },
370
+ {
371
+ Config : testAccShellShellScriptConfig_readFailed (file , false ),
372
+ Check : resource .TestCheckResourceAttr ("shell_script.shell_script" , "output.test" , "true" ),
373
+ },
374
+ },
375
+ })
376
+ }
377
+ func testAccShellShellScriptConfig_readFailed (filename string , bug bool ) string {
378
+ return fmt .Sprintf (`
379
+ resource "shell_script" "shell_script" {
380
+ lifecycle_commands {
381
+ create = <<-EOF
382
+ echo -n '{"test": true}' > "$FILE"
383
+ EOF
384
+ read = <<-EOF
385
+ { cat "$FILE"; [ "$BUG" == "true" ] && rm "$FILE" || true ;}
386
+ EOF
387
+ delete = <<-EOF
388
+ rm "$FILE"
389
+ EOF
390
+ }
391
+ environment = {
392
+ FILE = "%s"
393
+ BUG = "%t"
394
+ }
395
+ }
396
+ ` , filename , bug )
397
+ }
398
+
399
+ func TestAccShellShellScript_updateCommands (t * testing.T ) {
400
+ file := "/tmp/test-file-" + acctest .RandString (16 )
401
+ resource .Test (t , resource.TestCase {
402
+ Providers : testAccProviders ,
403
+ Steps : []resource.TestStep {
404
+ {
405
+ Config : testAccShellShellScriptConfig_updateCommands (file , true ),
406
+ Check : resource .TestCheckResourceAttr ("shell_script.shell_script" , "output.bug" , "false" ),
407
+ ExpectNonEmptyPlan : true ,
408
+ },
409
+ {
410
+ Config : testAccShellShellScriptConfig_updateCommands (file , false ),
411
+ Check : resource .TestCheckResourceAttr ("shell_script.shell_script" , "output.bug" , "false" ),
412
+ },
413
+ {
414
+ Config : testAccShellShellScriptConfig_updateCommands (file , false ),
415
+ Check : resource .TestCheckResourceAttr ("shell_script.shell_script" , "output.bug" , "false" ),
416
+ },
417
+ },
418
+ })
419
+ }
420
+ func testAccShellShellScriptConfig_updateCommands (filename string , bug bool ) string {
421
+ var read = `cat "$FILE"`
422
+ if bug {
423
+ read = `[ -f "$FILE.bug" ] && cat "$FILE.bug" || { cat "$FILE" ; echo -n '{}' > "$FILE.bug" ;}`
424
+ }
425
+
426
+ return fmt .Sprintf (`
427
+ resource "shell_script" "shell_script" {
428
+ lifecycle_commands {
429
+ create = <<-EOF
430
+ echo -n '{"bug": false}' > "$FILE"
431
+ EOF
432
+ read = <<-EOF
433
+ %s
434
+ EOF
435
+ update = <<-EOF
436
+ echo -n '{"bug": true}' > "$FILE"
437
+ EOF
438
+ delete = <<-EOF
439
+ rm "$FILE"
440
+ EOF
441
+ }
442
+ environment = {
443
+ FILE = "%s"
444
+ }
445
+ }
446
+ ` , read , filename )
447
+ }
0 commit comments