Skip to content

Commit

Permalink
Cleanup e2e namespaces before test start (#1655)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
markmandel authored Jun 26, 2020
1 parent a02a6fe commit c203a81
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
6 changes: 5 additions & 1 deletion test/e2e/framework/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
},
}

Expand Down
25 changes: 25 additions & 0 deletions test/e2e/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()))
Expand Down Expand Up @@ -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
}

0 comments on commit c203a81

Please sign in to comment.