Skip to content

Commit

Permalink
Added AfterSuite hook (#73)
Browse files Browse the repository at this point in the history
Signed-off-by: Marcus Noble <[email protected]>
  • Loading branch information
AverageMarcus authored May 28, 2024
1 parent 5c4494e commit 45904c0
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Added example E2E test suite that uses the hello-world app to self-test the framework
- Added `AfterSuite` hook to allow performing cleanup tasks after tests have finished.

## [1.1.4] - 2024-05-27

Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,12 @@ API documentation can be found at: [pkg.go.dev/github.com/giantswarm/apptest-fra

Once bootstrapped your repo will have a test suite called `basic` that you can start adding tests to.

There are 3 phases in which you can add tests:
There are 4 phases in which you can add tests:

- `BeforeInstall` - These are run first, before anything else is done, and should be used to check for any needed pre-requisites in the cluster.
- `AfterClusterReady` - These are run first, as soon as the workload cluster is deemed to be ready, and should be used to check for any needed pre-requisites in the cluster.
- `BeforeUpgrade` - These are only run if performing an upgrade tests and are run between installing the latest released version of your App and the version being tested. These are used to test that the App is in an expected state before performing the upgrade.
- `Tests` - This is where most of your tests will go and will be run after your App has been installed and marked as "Deployed" in the cluster.
- `AfterSuite` - This is performed during the cleanup after the tests have completed. This function will be triggered before the test App is uninstalled and before the workload cluster is deleted.

To add new test cases you can either add them inline within the above functions or call out to other functions and modules without your codebase so you can better structure different tests together. Be sure to follow the Ginkgo docs on writing [Spec Subjects](https://onsi.github.io/ginkgo/#spec-subjects-it).

Expand Down
5 changes: 4 additions & 1 deletion pkg/suite/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
// WithInstallNamespace("kube-system").
// WithIsUpgrade(isUpgrade).
// WithValuesFile("./values.yaml").
// BeforeInstall(func() {
// AfterClusterReady(func() {
// // Do any pre-install checks here (ensure the cluster has needed pre-reqs)
// }).
// BeforeUpgrade(func() {
Expand All @@ -21,6 +21,9 @@
// Tests(func() {
// // Include calls to app tests here
// }).
// AfterSuite(func() {
// // Include any post-test cleanup
// })
// Run(t, "Basic Test")
// }
package suite
33 changes: 25 additions & 8 deletions pkg/suite/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type suite struct {
afterClusterReady func()
beforeUpgrade func()
tests func()
afterSuite func()
}

// New create a new suite instance that allows configuring an App test suite
Expand Down Expand Up @@ -102,6 +103,14 @@ func (s *suite) AfterClusterReady(fn func()) *suite {
return s
}

// AfterSuite allows configuring tests that will run during the cleanup / teardown stage after
// all tests have completed. This is performed before the App is uninstalled and before the
// workload cluster is deleted.
func (s *suite) AfterSuite(fn func()) *suite {
s.afterSuite = fn
return s
}

// BeforeYpgrade allows configuring tests that will run after the App is installed
// but before it is upgraded to the test version.
// This only runs if `WithIsUpgrade` has been called with `true`.
Expand Down Expand Up @@ -262,16 +271,24 @@ func (s *suite) Run(t *testing.T, suiteName string) {

AfterSuite(func() {
defer func() {
// We defer this to ensure it happens even if uninstalling the app fails
logger.Log("Deleting workload cluster")
err := teardown.New(state.GetFramework()).Teardown(state.GetCluster())
Expect(err).NotTo(HaveOccurred())
By("Deleting workload cluster", func() {
// We defer this to ensure it happens even if uninstalling the app fails
logger.Log("Deleting workload cluster")
err := teardown.New(state.GetFramework()).Teardown(state.GetCluster())
Expect(err).NotTo(HaveOccurred())
})
}()

app := getInstallApp()
logger.Log("Uninstalling App %s", app.AppName)
err := state.GetFramework().MC().DeleteApp(state.GetContext(), *app)
Expect(err).NotTo(HaveOccurred())
if s.afterSuite != nil {
By("User-provided After Suite", s.afterSuite)
}

By("Uninstalling App", func() {
app := getInstallApp()
logger.Log("Uninstalling App %s", app.AppName)
err := state.GetFramework().MC().DeleteApp(state.GetContext(), *app)
Expect(err).NotTo(HaveOccurred())
})
})

Describe("", func() {
Expand Down
5 changes: 5 additions & 0 deletions tests/e2e/suites/basic/basic_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ func TestBasic(t *testing.T) {
ShouldNot(HaveOccurred())
})

}).
AfterSuite(func() {

logger.Log("Cleaning up after tests have completed")

}).
Run(t, "Basic Test")
}

0 comments on commit 45904c0

Please sign in to comment.