Skip to content

Commit

Permalink
fix: ensures cluster-wide resources are removed during test cleanup (o…
Browse files Browse the repository at this point in the history
…pendatahub-io#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 e5bc958)
  • Loading branch information
bartoszmajsak authored and VaishnaviHire committed Nov 15, 2023
1 parent 83a9295 commit ab5342a
Showing 1 changed file with 31 additions and 18 deletions.
49 changes: 31 additions & 18 deletions controllers/dscinitialization/dscinitialization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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() {
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down

0 comments on commit ab5342a

Please sign in to comment.