diff --git a/test/integration/suite_test.go b/test/integration/suite_test.go index 1a36ab0929..4d860cf953 100644 --- a/test/integration/suite_test.go +++ b/test/integration/suite_test.go @@ -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) } diff --git a/test/integration/utils_test.go b/test/integration/utils_test.go index 2a1134743b..cda7b6d443 100644 --- a/test/integration/utils_test.go +++ b/test/integration/utils_test.go @@ -108,6 +108,11 @@ var ( // These need to come in the format : (e.g. "kind:", "gke:", 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() ) @@ -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) }