Skip to content

Commit

Permalink
test(integration): clean up clusters when exiting
Browse files Browse the repository at this point in the history
  • Loading branch information
swiatekm committed Jun 4, 2024
1 parent 9fe62a5 commit 6799d58
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion tests/integration/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log"
"os"
"os/signal"
"testing"

"sigs.k8s.io/e2e-framework/pkg/env"
Expand Down Expand Up @@ -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)
}()
}
}

Expand All @@ -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(
Expand Down Expand Up @@ -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
}

0 comments on commit 6799d58

Please sign in to comment.