Skip to content

Commit efe4d67

Browse files
feat: add hook support (#817)
- added support to add anonyms functions to hook into various parts of the test - `PreApplyHook` In upgrade tests, this hook will be called before the base apply - `PostApplyHook` In upgrade tests, this hook will be called after the base apply - `PreDestroyHook` If this fails, the destroy will continue - `PostDestroyHook`
1 parent 912f643 commit efe4d67

File tree

3 files changed

+71
-3
lines changed

3 files changed

+71
-3
lines changed

.secrets.baseline

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,15 +198,15 @@
198198
"hashed_secret": "b4e929aa58c928e3e44d12e6f873f39cd8207a25",
199199
"is_secret": false,
200200
"is_verified": false,
201-
"line_number": 643,
201+
"line_number": 664,
202202
"type": "Secret Keyword",
203203
"verified_result": null
204204
},
205205
{
206206
"hashed_secret": "16282376ddaaaf2bf60be9041a7504280f3f338b",
207207
"is_secret": false,
208208
"is_verified": false,
209-
"line_number": 656,
209+
"line_number": 677,
210210
"type": "Secret Keyword",
211211
"verified_result": null
212212
}

testhelper/test_options.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,16 @@ type TestOptions struct {
160160
IsUpgradeTest bool // Identifies if current test is an UPGRADE test, used for special processing
161161
UpgradeTestSkipped bool // Informs the calling test that conditions were met to skip the upgrade test
162162

163+
// Hooks These allow us to inject custom code into the test process
164+
// example to set a hook:
165+
// options.PreApplyHook = func(options *TestOptions) error {
166+
// // do something
167+
// return nil
168+
// }
169+
PreApplyHook func(options *TestOptions) error // In upgrade tests, this hook will be called before the base apply
170+
PostApplyHook func(options *TestOptions) error // In upgrade tests, this hook will be called after the base apply
171+
PreDestroyHook func(options *TestOptions) error // If this fails, the destroy will continue
172+
PostDestroyHook func(options *TestOptions) error
163173
}
164174

165175
// Default constructor for TestOptions struct. This constructor takes in an existing TestOptions object with minimal values set, and returns

testhelper/tests.go

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,17 @@ func (options *TestOptions) testTearDown() {
361361
logger.Log(options.Testing, fmt.Sprintf("Error output containg CBRRuleList %s not found in Statefile, skipping CBR Rule disable", options.CBRRuleListOutputVariable))
362362
}
363363
}
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+
}
364375
logger.Log(options.Testing, "START: Destroy")
365376
destroyOutput, destroyError := terraform.DestroyE(options.Testing, options.TerraformOptions)
366377
if assert.NoError(options.Testing, destroyError) == false {
@@ -432,7 +443,17 @@ func (options *TestOptions) testTearDown() {
432443
terraform.WorkspaceDelete(options.Testing, options.TerraformOptions, options.Prefix)
433444
}
434445
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+
}
436457
//Clean up terraform files
437458
CleanTerraformDir(options.TerraformDir)
438459
}
@@ -673,6 +694,16 @@ func (options *TestOptions) RunTestUpgrade() (*terraform.PlanStruct, error) {
673694
// Set TerraformDir to the appropriate directory within baseTempDir
674695
options.setTerraformDir(path.Join(baseTempDir, relativeTestSampleDir))
675696

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+
}
676707
logger.Log(options.Testing, "Init / Apply on Base repo:", baseRepo)
677708
logger.Log(options.Testing, "Init / Apply on Base branch:", baseBranch)
678709
logger.Log(options.Testing, "Init / Apply on Base branch dir:", options.TerraformOptions.TerraformDir)
@@ -683,6 +714,17 @@ func (options *TestOptions) RunTestUpgrade() (*terraform.PlanStruct, error) {
683714
options.testTearDown()
684715
return nil, resultErr
685716
}
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+
}
686728
// Get the path to the state file in baseTempDir
687729
baseStatePath := path.Join(options.TerraformOptions.TerraformDir, "terraform.tfstate")
688730

@@ -822,11 +864,27 @@ func (options *TestOptions) RunTest() (string, error) {
822864
// runTest Runs Test and returns the output as a string for assertions for internal use no setup or teardown
823865
func (options *TestOptions) runTest() (string, error) {
824866

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+
}
825875
logger.Log(options.Testing, "START: Init / Apply")
826876
output, err := terraform.InitAndApplyE(options.Testing, options.TerraformOptions)
827877
assert.Nil(options.Testing, err, "Failed", err)
828878
logger.Log(options.Testing, "FINISHED: Init / Apply")
829879

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+
}
830888
return output, err
831889
}
832890

0 commit comments

Comments
 (0)