diff --git a/tests/e2e/service_bindings_test.go b/tests/e2e/service_bindings_test.go index afd25c84f..ce76fe3e9 100644 --- a/tests/e2e/service_bindings_test.go +++ b/tests/e2e/service_bindings_test.go @@ -63,7 +63,7 @@ var _ = Describe("Service Bindings", func() { }) AfterEach(func() { - broker.NewCatalogDeleter(rootNamespace).ForBrokerGUID(brokerGUID).Delete() + broker.NewDeleter(rootNamespace).ForBrokerGUID(brokerGUID).Delete() }) It("succeeds with a job redirect", func() { @@ -125,7 +125,7 @@ var _ = Describe("Service Bindings", func() { }) AfterEach(func() { - broker.NewCatalogDeleter(rootNamespace).ForBrokerGUID(brokerGUID).Delete() + broker.NewDeleter(rootNamespace).ForBrokerGUID(brokerGUID).Delete() }) It("succeeds with a job redirect", func() { diff --git a/tests/e2e/service_brokers_test.go b/tests/e2e/service_brokers_test.go index e1a1a045e..427e3b8ef 100644 --- a/tests/e2e/service_brokers_test.go +++ b/tests/e2e/service_brokers_test.go @@ -51,7 +51,7 @@ var _ = Describe("Service Brokers", func() { jobURLSplit := strings.Split(jobURL, "~") Expect(jobURLSplit).To(HaveLen(2)) DeferCleanup(func() { - broker.NewCatalogDeleter(rootNamespace).ForBrokerGUID(jobURLSplit[1]).Delete() + broker.NewDeleter(rootNamespace).ForBrokerGUID(jobURLSplit[1]).Delete() }) }) }) @@ -67,7 +67,7 @@ var _ = Describe("Service Brokers", func() { }) AfterEach(func() { - broker.NewCatalogDeleter(rootNamespace).ForBrokerGUID(brokerGUID).Delete() + broker.NewDeleter(rootNamespace).ForBrokerGUID(brokerGUID).Delete() }) JustBeforeEach(func() { @@ -94,7 +94,7 @@ var _ = Describe("Service Brokers", func() { }) AfterEach(func() { - broker.NewCatalogDeleter(rootNamespace).ForBrokerGUID(brokerGUID).Delete() + broker.NewDeleter(rootNamespace).ForBrokerGUID(brokerGUID).Delete() }) JustBeforeEach(func() { @@ -123,7 +123,7 @@ var _ = Describe("Service Brokers", func() { }) AfterEach(func() { - broker.NewCatalogDeleter(rootNamespace).ForBrokerGUID(brokerGUID).Delete() + broker.NewDeleter(rootNamespace).ForBrokerGUID(brokerGUID).Delete() }) JustBeforeEach(func() { @@ -166,7 +166,7 @@ var _ = Describe("Service Brokers", func() { }) AfterEach(func() { - broker.NewCatalogDeleter(rootNamespace).ForBrokerGUID(brokerGUID).Delete() + broker.NewDeleter(rootNamespace).ForBrokerGUID(brokerGUID).Delete() }) JustBeforeEach(func() { diff --git a/tests/e2e/service_instances_test.go b/tests/e2e/service_instances_test.go index 1889a7992..9305100ee 100644 --- a/tests/e2e/service_instances_test.go +++ b/tests/e2e/service_instances_test.go @@ -148,7 +148,7 @@ var _ = Describe("Service Instances", func() { }) AfterEach(func() { - broker.NewCatalogDeleter(rootNamespace).ForBrokerGUID(brokerGUID).Delete() + broker.NewDeleter(rootNamespace).ForBrokerGUID(brokerGUID).Delete() }) It("succeeds with a job redirect", func() { @@ -231,7 +231,7 @@ var _ = Describe("Service Instances", func() { }) AfterEach(func() { - broker.NewCatalogDeleter(rootNamespace).ForBrokerGUID(brokerGUID).Delete() + broker.NewDeleter(rootNamespace).ForBrokerGUID(brokerGUID).Delete() }) It("succeeds with a job redirect", func() { diff --git a/tests/e2e/service_offerings_test.go b/tests/e2e/service_offerings_test.go index bceac25fc..1c5b99e22 100644 --- a/tests/e2e/service_offerings_test.go +++ b/tests/e2e/service_offerings_test.go @@ -21,7 +21,7 @@ var _ = Describe("Service Offerings", func() { }) AfterEach(func() { - broker.NewCatalogDeleter(rootNamespace).ForBrokerGUID(brokerGUID).Delete() + broker.NewDeleter(rootNamespace).ForBrokerGUID(brokerGUID).Delete() }) Describe("List", func() { diff --git a/tests/e2e/service_plans_test.go b/tests/e2e/service_plans_test.go index d514926f0..54a0dccf2 100644 --- a/tests/e2e/service_plans_test.go +++ b/tests/e2e/service_plans_test.go @@ -26,7 +26,7 @@ var _ = Describe("Service Plans", func() { }) AfterEach(func() { - broker.NewCatalogDeleter(rootNamespace).ForBrokerGUID(brokerGUID).Delete() + broker.NewDeleter(rootNamespace).ForBrokerGUID(brokerGUID).Delete() }) Describe("List", func() { diff --git a/tests/helpers/broker/deleter.go b/tests/helpers/broker/deleter.go index fc4e6e9d9..cdf3bd3f8 100644 --- a/tests/helpers/broker/deleter.go +++ b/tests/helpers/broker/deleter.go @@ -17,15 +17,16 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" ) -type CatalogDeleter struct { - ctx context.Context - k8sClient client.Client - rootNamespace string - catalogLabelSelector client.MatchingLabels - maxRetries int +type Deleter struct { + ctx context.Context + k8sClient client.Client + rootNamespace string + catalogLabelSelector client.MatchingLabels + deleteCFServiceBroker func() + maxRetries int } -func NewCatalogDeleter(rootNamespace string) *CatalogDeleter { +func NewDeleter(rootNamespace string) *Deleter { ctx := context.Background() config, err := controllerruntime.GetConfig() @@ -34,7 +35,7 @@ func NewCatalogDeleter(rootNamespace string) *CatalogDeleter { k8sClient, err := client.New(config, client.Options{Scheme: scheme.Scheme}) Expect(err).NotTo(HaveOccurred()) - return &CatalogDeleter{ + return &Deleter{ ctx: ctx, k8sClient: k8sClient, rootNamespace: rootNamespace, @@ -42,23 +43,45 @@ func NewCatalogDeleter(rootNamespace string) *CatalogDeleter { } } -func (d *CatalogDeleter) ForBrokerGUID(brokerGUID string) *CatalogDeleter { +func (d *Deleter) ForBrokerGUID(brokerGUID string) *Deleter { d.catalogLabelSelector = client.MatchingLabels{ korifiv1alpha1.RelServiceBrokerGUIDLabel: brokerGUID, } + d.deleteCFServiceBroker = func() { + Expect(client.IgnoreNotFound(d.k8sClient.Delete(d.ctx, &korifiv1alpha1.CFServiceBroker{ + ObjectMeta: metav1.ObjectMeta{ + Namespace: d.rootNamespace, + Name: brokerGUID, + }, + }))).To(Succeed()) + } + return d } -func (d *CatalogDeleter) ForBrokerName(brokerName string) *CatalogDeleter { +func (d *Deleter) ForBrokerName(brokerName string) *Deleter { d.catalogLabelSelector = client.MatchingLabels{ korifiv1alpha1.RelServiceBrokerNameLabel: brokerName, } + d.deleteCFServiceBroker = func() { + allBrokers := &korifiv1alpha1.CFServiceBrokerList{} + Expect(d.k8sClient.List(d.ctx, allBrokers, client.InNamespace(d.rootNamespace))).To(Succeed()) + + for _, b := range allBrokers.Items { + if b.Spec.Name != brokerName { + continue + } + + Expect(client.IgnoreNotFound(d.k8sClient.Delete(d.ctx, &b))).To(Succeed()) + } + } + return d } -func (d *CatalogDeleter) Delete() { +func (d *Deleter) Delete() { GinkgoHelper() servicePlans := &korifiv1alpha1.CFServicePlanList{} @@ -66,12 +89,6 @@ func (d *CatalogDeleter) Delete() { for _, plan := range servicePlans.Items { Expect(d.cleanupBindings(plan.Name)).To(Succeed()) Expect(d.cleanupInsances(plan.Name)).To(Succeed()) - Expect(client.IgnoreNotFound(d.k8sClient.Delete(d.ctx, &korifiv1alpha1.CFServiceBroker{ - ObjectMeta: metav1.ObjectMeta{ - Namespace: d.rootNamespace, - Name: plan.Labels[korifiv1alpha1.RelServiceBrokerGUIDLabel], - }, - }))).To(Succeed()) } Expect(d.k8sClient.DeleteAllOf( @@ -87,9 +104,11 @@ func (d *CatalogDeleter) Delete() { client.InNamespace(d.rootNamespace), d.catalogLabelSelector, )).To(Succeed()) + + d.deleteCFServiceBroker() } -func (d *CatalogDeleter) cleanupBindings(planName string) error { +func (d *Deleter) cleanupBindings(planName string) error { for retries := 0; retries < d.maxRetries; retries++ { serviceBindings := &korifiv1alpha1.CFServiceBindingList{} Expect(d.k8sClient.List(d.ctx, serviceBindings, client.MatchingLabels{ @@ -110,7 +129,7 @@ func (d *CatalogDeleter) cleanupBindings(planName string) error { return fmt.Errorf("failed to clean up service bindings for plan %q", planName) } -func (d *CatalogDeleter) cleanupInsances(planName string) error { +func (d *Deleter) cleanupInsances(planName string) error { for retries := 0; retries < d.maxRetries; retries++ { serviceInstances := &korifiv1alpha1.CFServiceInstanceList{} Expect(d.k8sClient.List(d.ctx, serviceInstances)).To(Succeed()) diff --git a/tests/smoke/bind_service_test.go b/tests/smoke/bind_service_test.go index 4d967592c..559507651 100644 --- a/tests/smoke/bind_service_test.go +++ b/tests/smoke/bind_service_test.go @@ -2,6 +2,7 @@ package smoke_test import ( "code.cloudfoundry.org/korifi/tests/helpers" + "code.cloudfoundry.org/korifi/tests/helpers/broker" "github.com/google/uuid" . "github.com/onsi/ginkgo/v2" @@ -49,6 +50,9 @@ var _ = Describe("cf bind-service", func() { "broker-password", sharedData.BrokerURL, )).To(Exit(0)) + DeferCleanup(func() { + broker.NewDeleter(sharedData.RootNamespace).ForBrokerName(brokerName).Delete() + }) Expect(helpers.Cf("enable-service-access", "sample-service", "-b", brokerName)).To(Exit(0)) session := helpers.Cf("create-service", "sample-service", "sample", serviceName, "-b", brokerName) diff --git a/tests/smoke/catalog_test.go b/tests/smoke/catalog_test.go index 57119636c..3aef71368 100644 --- a/tests/smoke/catalog_test.go +++ b/tests/smoke/catalog_test.go @@ -26,7 +26,7 @@ var _ = Describe("Service Catalog", func() { }) AfterEach(func() { - broker.NewCatalogDeleter(sharedData.RootNamespace).ForBrokerName(brokerName).Delete() + broker.NewDeleter(sharedData.RootNamespace).ForBrokerName(brokerName).Delete() }) Describe("cf service-brokers", func() { diff --git a/tests/smoke/services_test.go b/tests/smoke/services_test.go index 0a361de4c..1617555f0 100644 --- a/tests/smoke/services_test.go +++ b/tests/smoke/services_test.go @@ -27,7 +27,7 @@ var _ = Describe("Services", func() { }) AfterEach(func() { - broker.NewCatalogDeleter(sharedData.RootNamespace).ForBrokerName(brokerName).Delete() + broker.NewDeleter(sharedData.RootNamespace).ForBrokerName(brokerName).Delete() }) Describe("cf create-service", func() { diff --git a/tests/smoke/unbind_service_test.go b/tests/smoke/unbind_service_test.go index d2bb982b7..9445c579f 100644 --- a/tests/smoke/unbind_service_test.go +++ b/tests/smoke/unbind_service_test.go @@ -2,6 +2,7 @@ package smoke_test import ( "code.cloudfoundry.org/korifi/tests/helpers" + "code.cloudfoundry.org/korifi/tests/helpers/broker" "github.com/google/uuid" . "github.com/onsi/ginkgo/v2" @@ -49,6 +50,9 @@ var _ = Describe("cf unbind-service", func() { "broker-password", sharedData.BrokerURL, )).To(Exit(0)) + DeferCleanup(func() { + broker.NewDeleter(sharedData.RootNamespace).ForBrokerName(brokerName).Delete() + }) Expect(helpers.Cf("enable-service-access", "sample-service", "-b", brokerName)).To(Exit(0)) session := helpers.Cf("create-service", "sample-service", "sample", serviceName, "-b", brokerName)