Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensures cluster-wide resources are removed during test cleanup #674

Merged
merged 2 commits into from
Oct 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading