From e02646e2d37729b1e1ee0b52e08b9f625213757d Mon Sep 17 00:00:00 2001 From: Aviad Levy Date: Tue, 5 Jul 2022 13:19:15 +0300 Subject: [PATCH] e2e test namespace deletion verify namespace actually deleted (#3329) --- tests/helper/helper.go | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/tests/helper/helper.go b/tests/helper/helper.go index a3e86dd51fd..9da51a499e8 100644 --- a/tests/helper/helper.go +++ b/tests/helper/helper.go @@ -19,6 +19,7 @@ import ( "github.com/stretchr/testify/assert" autoscalingv2beta2 "k8s.io/api/autoscaling/v2beta2" corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" "k8s.io/client-go/kubernetes/scheme" @@ -165,10 +166,26 @@ func CreateNamespace(t *testing.T, kc *kubernetes.Clientset, nsName string) { } func DeleteNamespace(t *testing.T, kc *kubernetes.Clientset, nsName string) { - err := KubeClient.CoreV1().Namespaces().Delete(context.Background(), nsName, metav1.DeleteOptions{}) + t.Logf("deleting namespace %s", nsName) + period := int64(0) + err := KubeClient.CoreV1().Namespaces().Delete(context.Background(), nsName, metav1.DeleteOptions{ + GracePeriodSeconds: &period, + }) assert.NoErrorf(t, err, "cannot delete kubernetes namespace - %s", err) } +func WaitForNamespaceDeletion(t *testing.T, kc *kubernetes.Clientset, nsName string) bool { + for i := 0; i < 30; i++ { + t.Logf("waiting for namespace %s deletion", nsName) + _, err := KubeClient.CoreV1().Namespaces().Get(context.Background(), nsName, metav1.GetOptions{}) + if err != nil && errors.IsNotFound(err) { + return true + } + time.Sleep(time.Second * 5) + } + return false +} + // Waits until deployment count hits target or number of iterations are done. func WaitForDeploymentReplicaCount(t *testing.T, kc *kubernetes.Clientset, name, namespace string, target, iterations, intervalSeconds int) bool { @@ -304,6 +321,8 @@ func CreateKubernetesResources(t *testing.T, kc *kubernetes.Clientset, nsName st } func DeleteKubernetesResources(t *testing.T, kc *kubernetes.Clientset, nsName string, data interface{}, configs map[string]string) { - DeleteNamespace(t, kc, nsName) KubectlDeleteMultipleWithTemplate(t, data, configs) + DeleteNamespace(t, kc, nsName) + deleted := WaitForNamespaceDeletion(t, kc, nsName) + assert.Truef(t, deleted, "%s namespace not deleted", nsName) }