Skip to content

Commit

Permalink
e2e test namespace deletion verify namespace actually deleted (#3329)
Browse files Browse the repository at this point in the history
  • Loading branch information
aviadlevy authored Jul 5, 2022
1 parent c0934de commit e02646e
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions tests/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}

0 comments on commit e02646e

Please sign in to comment.