Skip to content

Commit

Permalink
Ensure e2e and smoke tests delete their brokers
Browse files Browse the repository at this point in the history
Also, rename `broker.CatalogDeleter` to `broker.Deleter` as the utility
deletes the broker, not just the catalog
  • Loading branch information
danail-branekov authored and georgethebeatle committed Jan 14, 2025
1 parent d73a05f commit 6f87111
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 32 deletions.
4 changes: 2 additions & 2 deletions tests/e2e/service_bindings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down
10 changes: 5 additions & 5 deletions tests/e2e/service_brokers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
})
})
Expand All @@ -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() {
Expand All @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down
4 changes: 2 additions & 2 deletions tests/e2e/service_instances_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/service_offerings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/service_plans_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
57 changes: 38 additions & 19 deletions tests/helpers/broker/deleter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -34,44 +35,60 @@ 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,
maxRetries: 50,
}
}

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{}
Expect(d.k8sClient.List(d.ctx, servicePlans, client.InNamespace(d.rootNamespace), d.catalogLabelSelector)).To(Succeed())
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(
Expand All @@ -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{
Expand All @@ -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())
Expand Down
4 changes: 4 additions & 0 deletions tests/smoke/bind_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tests/smoke/catalog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion tests/smoke/services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
4 changes: 4 additions & 0 deletions tests/smoke/unbind_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 6f87111

Please sign in to comment.