From 6799d58a2b9e3fd5d1ce6ed958cbaffd4526185d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20=C5=9Awi=C4=85tek?= Date: Tue, 4 Jun 2024 16:48:22 +0200 Subject: [PATCH] test(integration): clean up clusters when exiting --- tests/integration/main_test.go | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/tests/integration/main_test.go b/tests/integration/main_test.go index 1f1a774ad..a671e16e7 100644 --- a/tests/integration/main_test.go +++ b/tests/integration/main_test.go @@ -5,6 +5,7 @@ import ( "fmt" "log" "os" + "os/signal" "testing" "sigs.k8s.io/e2e-framework/pkg/env" @@ -57,6 +58,23 @@ func TestMain(m *testing.M) { testenv.Setup(envfuncs.CreateClusterWithConfig(kindProvider, kindClusterName, kindClusterConfigPath, clusterOpts...)) ConfigureTestEnv(testenv) testenv.Finish(envfuncs.DestroyCluster(kindClusterName)) + + // ensure the cluster is deleted on panic or SIGINT + defer func() { + err := kindProvider.WithName(kindClusterName).Destroy(context.Background()) + if err != nil { + log.Printf("Couldn't delete cluster %s: %v", kindClusterName, err) + } + }() + sigChan := SetupSignalHandler() + go func() { + <-sigChan + err := kindProvider.WithName(kindClusterName).Destroy(context.Background()) + if err != nil { + log.Printf("Couldn't delete cluster %s: %v", kindClusterName, err) + } + os.Exit(1) + }() } } @@ -66,7 +84,6 @@ func TestMain(m *testing.M) { func ConfigureTestEnv(testenv env.Environment) { // Before - testenv.Setup(envfuncs.CreateNamespace(internal.LogsGeneratorNamespace)) for _, f := range stepfuncs.IntoTestEnvFuncs( @@ -152,3 +169,9 @@ func GetImageArchiveFilename() (string, error) { return fileName, nil } + +func SetupSignalHandler() chan os.Signal { + sigChan := make(chan os.Signal, 1) + signal.Notify(sigChan, os.Interrupt) + return sigChan +}