From c203a81eedfc3721371c4381dceb5c74f74fa107 Mon Sep 17 00:00:00 2001 From: Mark Mandel Date: Fri, 26 Jun 2020 15:11:14 -0700 Subject: [PATCH] Cleanup e2e namespaces before test start (#1655) When an e2e test panics, it can leave behind populated GameServer namespaces, that are never cleaned up! This implements a fix in which the e2e test will search for e2e namespaces in the cluster, and delete them all before tests begin. Closes #1653 --- test/e2e/framework/framework.go | 6 +++++- test/e2e/main_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/test/e2e/framework/framework.go b/test/e2e/framework/framework.go index 13cb24aa5a..cbb79593a7 100644 --- a/test/e2e/framework/framework.go +++ b/test/e2e/framework/framework.go @@ -58,6 +58,10 @@ const ( AutoCleanupLabelValue = "true" ) +// NamespaceLabel is the label that is put on all namespaces that are created +// for e2e tests. +var NamespaceLabel = map[string]string{"owner": "e2e-test"} + // Framework is a testing framework type Framework struct { KubeClient kubernetes.Interface @@ -500,7 +504,7 @@ func (f *Framework) CreateNamespace(namespace string) error { ns := &corev1.Namespace{ ObjectMeta: metav1.ObjectMeta{ Name: namespace, - Labels: map[string]string{"owner": "e2e-test"}, + Labels: NamespaceLabel, }, } diff --git a/test/e2e/main_test.go b/test/e2e/main_test.go index 2a2e0ffee3..ac3796d22e 100644 --- a/test/e2e/main_test.go +++ b/test/e2e/main_test.go @@ -23,6 +23,8 @@ import ( e2eframework "agones.dev/agones/test/e2e/framework" "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/labels" ) var framework *e2eframework.Framework @@ -49,6 +51,11 @@ func TestMain(m *testing.M) { os.Exit(1) } + if err = cleanupNamespaces(framework); err != nil { + log.WithError(err).Error("failed to cleanup e2e namespaces") + os.Exit(1) + } + if framework.Namespace == "" { // use a custom namespace - Unix timestamp framework.Namespace = strconv.Itoa(int(time.Now().Unix())) @@ -84,3 +91,21 @@ func TestMain(m *testing.M) { exitCode = m.Run() } + +func cleanupNamespaces(framework *e2eframework.Framework) error { + // list all e2e namespaces + opts := metav1.ListOptions{LabelSelector: labels.Set(e2eframework.NamespaceLabel).String()} + list, err := framework.KubeClient.CoreV1().Namespaces().List(opts) + if err != nil { + return err + } + + // loop through them, and delete them + for _, ns := range list.Items { + if err := framework.DeleteNamespace(ns.ObjectMeta.Name); err != nil { + return err + } + } + + return nil +}