Skip to content

Commit

Permalink
fix: ensure test environment cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
shaneutt committed Aug 5, 2021
1 parent 9d76289 commit 15d7609
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
10 changes: 10 additions & 0 deletions test/integration/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,16 @@ func TestMain(m *testing.M) {

fmt.Printf("INFO: testing environment is ready KUBERNETES_VERSION=(%v): running tests\n", clusterVersion)
code := m.Run()

fmt.Println("INFO: performing test cleanup")
if keepTestCluster == "" && existingCluster == "" {
ctx, cancel := context.WithTimeout(context.Background(), environmentCleanupTimeout)
defer cancel()

fmt.Printf("INFO: cluster %s is being deleted\n", env.Cluster().Name())
exitOnErr(env.Cleanup(ctx))
}

os.Exit(code)
}

Expand Down
9 changes: 8 additions & 1 deletion test/integration/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ var (
// These need to come in the format <TYPE>:<NAME> (e.g. "kind:<NAME>", "gke:<NAME>", e.t.c.).
existingCluster = os.Getenv("KONG_TEST_CLUSTER")

// keepTestCluster indicates whether the caller wants the cluster created by the test suite
// to persist after the test for inspection. This has a nil effect when an existing cluster
// is provided, as cleanup is not performed for existing clusters.
keepTestCluster = os.Getenv("KONG_TEST_CLUSTER_PERSIST")

// maxBatchSize indicates the maximum number of objects that should be POSTed per second during testing
maxBatchSize = determineMaxBatchSize()
)
Expand Down Expand Up @@ -190,10 +195,12 @@ func exitOnErrWithCode(err error, exitCode int) {
return
}

if env != nil && existingCluster == "" {
fmt.Println("WARNING: failure occurred, performing test cleanup")
if env != nil && existingCluster == "" && keepTestCluster == "" {
ctx, cancel := context.WithTimeout(context.Background(), environmentCleanupTimeout)
defer cancel()

fmt.Printf("INFO: cluster %s is being deleted\n", env.Cluster().Name())
if cleanupErr := env.Cleanup(ctx); cleanupErr != nil {
err = fmt.Errorf("cleanup failed after test failure occurred CLEANUP_FAILURE=(%s) TEST_FAILURE=(%s)", cleanupErr, err)
}
Expand Down

0 comments on commit 15d7609

Please sign in to comment.