From ab5342a40baca97dc6a182d430a77066f7b83540 Mon Sep 17 00:00:00 2001 From: Bartosz Majsak Date: Mon, 30 Oct 2023 19:38:57 +0100 Subject: [PATCH] fix: ensures cluster-wide resources are removed during test cleanup (#674) Implements wait with timeout for DSCI to be actually deleted from the cluster as part of the test cleanup. Otherwise we might end up with DSCI not deleted yet when another test starts. This leads to failing/flaky tests. (cherry picked from commit e5bc9582c5b1f094bf6f28e71b2de862bec16e7c) --- .../dscinitialization_test.go | 49 ++++++++++++------- 1 file changed, 31 insertions(+), 18 deletions(-) diff --git a/controllers/dscinitialization/dscinitialization_test.go b/controllers/dscinitialization/dscinitialization_test.go index 58ebe287465..d1252d8027f 100644 --- a/controllers/dscinitialization/dscinitialization_test.go +++ b/controllers/dscinitialization/dscinitialization_test.go @@ -10,6 +10,7 @@ import ( corev1 "k8s.io/api/core/v1" netv1 "k8s.io/api/networking/v1" authv1 "k8s.io/api/rbac/v1" + "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "sigs.k8s.io/controller-runtime/pkg/client" ) @@ -34,6 +35,7 @@ var _ = Describe("DataScienceCluster initialization", func() { foundDsci := &dsci.DSCInitialization{} Eventually(dscInitializationIsReady(applicationName, workingNamespace, foundDsci), timeout, interval).Should(BeTrue()) }) + AfterEach(cleanupResources) It("Should create default application namespace", func() { @@ -249,10 +251,35 @@ var _ = Describe("DataScienceCluster initialization", func() { func cleanupResources() { defaultNamespace := client.InNamespace(workingNamespace) appNamespace := client.InNamespace(applicationNamespace) - Expect(k8sClient.DeleteAllOf(context.TODO(), &dsci.DSCInitialization{}, defaultNamespace)).ToNot(HaveOccurred()) - Expect(k8sClient.DeleteAllOf(context.TODO(), &netv1.NetworkPolicy{}, appNamespace)).ToNot(HaveOccurred()) - Expect(k8sClient.DeleteAllOf(context.TODO(), &corev1.ConfigMap{}, appNamespace)).ToNot(HaveOccurred()) - Expect(k8sClient.DeleteAllOf(context.TODO(), &authv1.RoleBinding{}, appNamespace)).ToNot(HaveOccurred()) + Expect(k8sClient.DeleteAllOf(context.TODO(), &dsci.DSCInitialization{}, defaultNamespace)).To(Succeed()) + Expect(k8sClient.DeleteAllOf(context.TODO(), &netv1.NetworkPolicy{}, appNamespace)).To(Succeed()) + Expect(k8sClient.DeleteAllOf(context.TODO(), &corev1.ConfigMap{}, appNamespace)).To(Succeed()) + Expect(k8sClient.DeleteAllOf(context.TODO(), &authv1.RoleBinding{}, appNamespace)).To(Succeed()) + Eventually(noInstanceExistsIn(workingNamespace, &dsci.DSCInitializationList{}), timeout, interval).Should(BeTrue()) +} + +func noInstanceExistsIn(namespace string, list client.ObjectList) func() bool { + return func() bool { + if err := k8sClient.List(ctx, list, &client.ListOptions{Namespace: namespace}); err != nil { + return false + } + + return meta.LenList(list) == 0 + } +} + +func namespaceExists(ns string, obj client.Object) func() bool { + return func() bool { + err := k8sClient.Get(context.Background(), client.ObjectKey{Name: ns}, obj) + return err == nil + } +} + +func objectExists(ns string, name string, obj client.Object) func() bool { //nolint + return func() bool { + err := k8sClient.Get(context.Background(), client.ObjectKey{Name: ns, Namespace: name}, obj) + return err == nil + } } func createDSCI(appName string, enableMonitoring operatorv1.ManagementState, monitoringNS string) *dsci.DSCInitialization { @@ -275,20 +302,6 @@ func createDSCI(appName string, enableMonitoring operatorv1.ManagementState, mon } } -func namespaceExists(ns string, obj client.Object) func() bool { - return func() bool { - err := k8sClient.Get(context.Background(), client.ObjectKey{Name: ns}, obj) - return err == nil - } -} - -func objectExists(ns string, name string, obj client.Object) func() bool { //nolint - return func() bool { - err := k8sClient.Get(context.Background(), client.ObjectKey{Name: ns, Namespace: name}, obj) - return err == nil - } -} - func dscInitializationIsReady(ns string, name string, dsciObj *dsci.DSCInitialization) func() bool { //nolint return func() bool { _ = k8sClient.Get(context.Background(), client.ObjectKey{Name: ns, Namespace: name}, dsciObj)