From 5257dfcd155608f71c40039e49b67cef0a04b4ab Mon Sep 17 00:00:00 2001 From: "Da K. Ma" Date: Mon, 7 May 2018 11:20:59 +0800 Subject: [PATCH] Cleanup Pods in TestNominatedNodeCleanUp. Signed-off-by: Da K. Ma --- test/integration/scheduler/preemption_test.go | 4 +++ test/integration/scheduler/util.go | 26 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/test/integration/scheduler/preemption_test.go b/test/integration/scheduler/preemption_test.go index af88cd84d6c..8e2e4976b79 100644 --- a/test/integration/scheduler/preemption_test.go +++ b/test/integration/scheduler/preemption_test.go @@ -506,7 +506,11 @@ func TestNominatedNodeCleanUp(t *testing.T) { // Initialize scheduler. context := initTest(t, "preemption") defer cleanupTest(t, context) + cs := context.clientSet + + defer cleanupPodsInNamespace(cs, t, context.ns.Name) + // Create a node with some resources and a label. nodeRes := &v1.ResourceList{ v1.ResourcePods: *resource.NewQuantity(32, resource.DecimalSI), diff --git a/test/integration/scheduler/util.go b/test/integration/scheduler/util.go index 6074913556b..d42803c1786 100644 --- a/test/integration/scheduler/util.go +++ b/test/integration/scheduler/util.go @@ -534,3 +534,29 @@ func cleanupPods(cs clientset.Interface, t *testing.T, pods []*v1.Pod) { } } } + +// noPodsInNamespace returns true if no pods in the given namespace. +func noPodsInNamespace(c clientset.Interface, podNamespace string) wait.ConditionFunc { + return func() (bool, error) { + pods, err := c.CoreV1().Pods(podNamespace).List(metav1.ListOptions{}) + if err != nil { + return false, err + } + + return len(pods.Items) == 0, nil + } +} + +// cleanupPodsInNamespace deletes the pods in the given namespace and waits for them to +// be actually deleted. +func cleanupPodsInNamespace(cs clientset.Interface, t *testing.T, ns string) { + if err := cs.CoreV1().Pods(ns).DeleteCollection(nil, metav1.ListOptions{}); err != nil { + t.Errorf("error while listing pod in namespace %v: %v", ns, err) + return + } + + if err := wait.Poll(time.Second, wait.ForeverTestTimeout, + noPodsInNamespace(cs, ns)); err != nil { + t.Errorf("error while waiting for pods in namespace %v: %v", ns, err) + } +}