Skip to content

Commit

Permalink
fix: ensures cluster-wide resources are removed during test cleanup (#…
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
bartoszmajsak authored Oct 30, 2023
1 parent 1e3b083 commit e5bc958
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 @@ -32,6 +33,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 @@ -197,10 +199,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) *dsci.DSCInitialization {
Expand All @@ -223,20 +250,6 @@ func createDSCI(appName string) *dsci.DSCInitialization {
}
}

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 e5bc958

Please sign in to comment.