@@ -361,6 +361,17 @@ func (options *TestOptions) testTearDown() {
361
361
logger .Log (options .Testing , fmt .Sprintf ("Error output containg CBRRuleList %s not found in Statefile, skipping CBR Rule disable" , options .CBRRuleListOutputVariable ))
362
362
}
363
363
}
364
+ if options .PreDestroyHook != nil {
365
+ logger .Log (options .Testing , "START: PreDestroyHook" )
366
+ hookErr := options .PreDestroyHook (options )
367
+ if hookErr != nil {
368
+ logger .Log (options .Testing , "Error running PreDestroyHook" )
369
+ logger .Log (options .Testing , hookErr )
370
+ logger .Log (options .Testing , "END: PreDestroyHook, continuing with destroy" )
371
+ } else {
372
+ logger .Log (options .Testing , "END: PreDestroyHook" )
373
+ }
374
+ }
364
375
logger .Log (options .Testing , "START: Destroy" )
365
376
destroyOutput , destroyError := terraform .DestroyE (options .Testing , options .TerraformOptions )
366
377
if assert .NoError (options .Testing , destroyError ) == false {
@@ -432,7 +443,17 @@ func (options *TestOptions) testTearDown() {
432
443
terraform .WorkspaceDelete (options .Testing , options .TerraformOptions , options .Prefix )
433
444
}
434
445
logger .Log (options .Testing , "END: Destroy" )
435
-
446
+ if options .PostDestroyHook != nil {
447
+ logger .Log (options .Testing , "START: PostDestroyHook" )
448
+ hookErr := options .PostDestroyHook (options )
449
+ if hookErr != nil {
450
+ logger .Log (options .Testing , "Error running PostDestroyHook" )
451
+ logger .Log (options .Testing , hookErr )
452
+ logger .Log (options .Testing , "END: PostDestroyHook" )
453
+ } else {
454
+ logger .Log (options .Testing , "END: PostDestroyHook" )
455
+ }
456
+ }
436
457
//Clean up terraform files
437
458
CleanTerraformDir (options .TerraformDir )
438
459
}
@@ -673,6 +694,16 @@ func (options *TestOptions) RunTestUpgrade() (*terraform.PlanStruct, error) {
673
694
// Set TerraformDir to the appropriate directory within baseTempDir
674
695
options .setTerraformDir (path .Join (baseTempDir , relativeTestSampleDir ))
675
696
697
+ if options .PreApplyHook != nil {
698
+ logger .Log (options .Testing , "Running PreApplyHook" )
699
+ hookErr := options .PreApplyHook (options )
700
+ if hookErr != nil {
701
+ assert .Nilf (options .Testing , hookErr , "PreApplyHook failed" )
702
+ options .testTearDown ()
703
+ return nil , hookErr
704
+ }
705
+ logger .Log (options .Testing , "PreApplyHook completed successfully" )
706
+ }
676
707
logger .Log (options .Testing , "Init / Apply on Base repo:" , baseRepo )
677
708
logger .Log (options .Testing , "Init / Apply on Base branch:" , baseBranch )
678
709
logger .Log (options .Testing , "Init / Apply on Base branch dir:" , options .TerraformOptions .TerraformDir )
@@ -683,6 +714,17 @@ func (options *TestOptions) RunTestUpgrade() (*terraform.PlanStruct, error) {
683
714
options .testTearDown ()
684
715
return nil , resultErr
685
716
}
717
+
718
+ if options .PostApplyHook != nil {
719
+ logger .Log (options .Testing , "Running PostApplyHook" )
720
+ hookErr := options .PostApplyHook (options )
721
+ if hookErr != nil {
722
+ assert .Nilf (options .Testing , hookErr , "PostApplyHook failed" )
723
+ options .testTearDown ()
724
+ return nil , hookErr
725
+ }
726
+ logger .Log (options .Testing , "PostApplyHook completed successfully" )
727
+ }
686
728
// Get the path to the state file in baseTempDir
687
729
baseStatePath := path .Join (options .TerraformOptions .TerraformDir , "terraform.tfstate" )
688
730
@@ -822,11 +864,27 @@ func (options *TestOptions) RunTest() (string, error) {
822
864
// runTest Runs Test and returns the output as a string for assertions for internal use no setup or teardown
823
865
func (options * TestOptions ) runTest () (string , error ) {
824
866
867
+ if options .PreApplyHook != nil {
868
+ logger .Log (options .Testing , "Running PreApplyHook" )
869
+ hook_err := options .PreApplyHook (options )
870
+ if hook_err != nil {
871
+ return "" , hook_err
872
+ }
873
+ logger .Log (options .Testing , "Finished PreApplyHook" )
874
+ }
825
875
logger .Log (options .Testing , "START: Init / Apply" )
826
876
output , err := terraform .InitAndApplyE (options .Testing , options .TerraformOptions )
827
877
assert .Nil (options .Testing , err , "Failed" , err )
828
878
logger .Log (options .Testing , "FINISHED: Init / Apply" )
829
879
880
+ if err == nil && options .PostApplyHook != nil {
881
+ logger .Log (options .Testing , "Running PostApplyHook" )
882
+ hook_err := options .PostApplyHook (options )
883
+ if hook_err != nil {
884
+ return "" , hook_err
885
+ }
886
+ logger .Log (options .Testing , "Finished PostApplyHook" )
887
+ }
830
888
return output , err
831
889
}
832
890
0 commit comments