Skip to content

Commit

Permalink
e2e: run cleanup before tests in addition to after
Browse files Browse the repository at this point in the history
  • Loading branch information
jkowalski authored and markmandel committed Jan 28, 2019
1 parent cf37064 commit 7494dd1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
3 changes: 2 additions & 1 deletion test/e2e/fleetautoscaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,9 +489,10 @@ func defaultAutoscalerWebhook() (*corev1.Pod, *corev1.Service) {
l := make(map[string]string)
appName := fmt.Sprintf("autoscaler-webhook-%v", time.Now().UnixNano())
l["app"] = appName
l[e2e.AutoCleanupLabelKey] = e2e.AutoCleanupLabelValue
pod := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "auto-webhook",
GenerateName: "auto-webhook-",
Namespace: defaultNs,
Labels: l,
},
Expand Down
41 changes: 32 additions & 9 deletions test/e2e/framework/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ import (
"k8s.io/client-go/tools/clientcmd"
)

// special labels that can be put on pods to trigger automatic cleanup.
const (
AutoCleanupLabelKey = "stable.agones.dev/e2e-test-auto-cleanup"
AutoCleanupLabelValue = "true"
)

// Framework is a testing framework
type Framework struct {
KubeClient kubernetes.Interface
Expand Down Expand Up @@ -186,34 +192,51 @@ func (f *Framework) WaitForFleetGameServersCondition(flt *v1alpha1.Fleet, cond f
})
}

// CleanUp Delete all Agones resources in a given namespace
// CleanUp Delete all Agones resources in a given namespace.
func (f *Framework) CleanUp(ns string) error {
logrus.Info("Done. Cleaning up now.")
logrus.Info("Cleaning up now.")
defer logrus.Info("Finished cleanup.")
alpha1 := f.AgonesClient.StableV1alpha1()
do := &metav1.DeleteOptions{}
options := metav1.ListOptions{}
err := alpha1.Fleets(ns).DeleteCollection(do, options)
deleteOptions := &metav1.DeleteOptions{}
listOptions := metav1.ListOptions{}

// find and delete pods created by tests and labeled with our special label
pods := f.KubeClient.CoreV1().Pods(ns)
podList, err := pods.List(metav1.ListOptions{
LabelSelector: AutoCleanupLabelKey + "=" + AutoCleanupLabelValue,
})
if err != nil {
return err
}

for _, p := range podList.Items {
if err = pods.Delete(p.ObjectMeta.Name, deleteOptions); err != nil {
return err
}
}

err = alpha1.Fleets(ns).DeleteCollection(deleteOptions, listOptions)
if err != nil {
return err
}

err = alpha1.GameServerAllocations(ns).DeleteCollection(do, options)
err = alpha1.GameServerAllocations(ns).DeleteCollection(deleteOptions, listOptions)
if err != nil {
return err
}

err = alpha1.FleetAllocations(ns).DeleteCollection(do, options)
err = alpha1.FleetAllocations(ns).DeleteCollection(deleteOptions, listOptions)
if err != nil {
return err
}

err = alpha1.FleetAutoscalers(ns).DeleteCollection(do, options)
err = alpha1.FleetAutoscalers(ns).DeleteCollection(deleteOptions, listOptions)
if err != nil {
return err
}

return alpha1.GameServers(ns).
DeleteCollection(do, options)
DeleteCollection(deleteOptions, listOptions)
}

// PingGameServer pings a gameserver and returns its reply
Expand Down
7 changes: 7 additions & 0 deletions test/e2e/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ func TestMain(m *testing.M) {
log.Printf("failed to setup framework: %v\n", err)
os.Exit(1)
}

// run cleanup before tests, to ensure no resources from previous runs exist.
err = framework.CleanUp(defaultNs)
if err != nil {
log.Printf("failed to cleanup resources: %v\n", err)
}

defer func() {
err = framework.CleanUp(defaultNs)
if err != nil {
Expand Down

0 comments on commit 7494dd1

Please sign in to comment.