From ae722ab68651a9fc14e8ff89d7f8f6c622737aeb Mon Sep 17 00:00:00 2001 From: Dimitar Draganov Date: Wed, 8 Jan 2025 15:45:48 +0200 Subject: [PATCH] Added requested changes --- api/handlers/service_binding_test.go | 26 +++++++++++ api/payloads/service_binding.go | 4 +- api/payloads/service_binding_test.go | 4 +- .../service_binding_repository_test.go | 43 +++++++++---------- 4 files changed, 50 insertions(+), 27 deletions(-) diff --git a/api/handlers/service_binding_test.go b/api/handlers/service_binding_test.go index 923c42f52..e3879bf93 100644 --- a/api/handlers/service_binding_test.go +++ b/api/handlers/service_binding_test.go @@ -105,6 +105,32 @@ var _ = Describe("ServiceBinding", func() { expectUnprocessableEntityError("Service credential bindings of type 'key' are not supported for user-provided service instances.") }) }) + + When("binding to a managed service", func() { + BeforeEach(func() { + serviceInstanceRepo.GetServiceInstanceReturns(repositories.ServiceInstanceRecord{ + GUID: "service-instance-guid", + SpaceGUID: "space-guid", + Type: korifiv1alpha1.ManagedType, + }, nil) + + serviceBindingRepo.CreateServiceBindingReturns(repositories.ServiceBindingRecord{ + GUID: "service-binding-guid", + Type: korifiv1alpha1.CFServiceBindingTypeKey, + }, nil) + }) + + It("creates a binding", func() { + Expect(serviceBindingRepo.CreateServiceBindingCallCount()).To(Equal(1)) + Expect(rr).To(HaveHTTPStatus(http.StatusAccepted)) + + _, actualAuthInfo, createServiceBindingMessage := serviceBindingRepo.CreateServiceBindingArgsForCall(0) + Expect(actualAuthInfo).To(Equal(authInfo)) + Expect(createServiceBindingMessage.ServiceInstanceGUID).To(Equal("service-instance-guid")) + Expect(createServiceBindingMessage.SpaceGUID).To(Equal("space-guid")) + Expect(createServiceBindingMessage.Type).To(Equal(korifiv1alpha1.CFServiceBindingTypeKey)) + }) + }) }) When("creating a service binding of type app", func() { diff --git a/api/payloads/service_binding.go b/api/payloads/service_binding.go index 73748aed4..767e9de2b 100644 --- a/api/payloads/service_binding.go +++ b/api/payloads/service_binding.go @@ -85,14 +85,12 @@ func (l ServiceBindingList) Validate() error { } func (l *ServiceBindingList) ToMessage() repositories.ListServiceBindingsMessage { - message := repositories.ListServiceBindingsMessage{ + return repositories.ListServiceBindingsMessage{ ServiceInstanceGUIDs: parse.ArrayParam(l.ServiceInstanceGUIDs), AppGUIDs: parse.ArrayParam(l.AppGUIDs), LabelSelector: l.LabelSelector, PlanGUIDs: parse.ArrayParam(l.PlanGUIDs), } - - return message } func (l *ServiceBindingList) SupportedKeys() []string { diff --git a/api/payloads/service_binding_test.go b/api/payloads/service_binding_test.go index 275936716..701f634ea 100644 --- a/api/payloads/service_binding_test.go +++ b/api/payloads/service_binding_test.go @@ -21,9 +21,11 @@ var _ = Describe("ServiceBindingList", func() { Expect(*actualServiceBindingList).To(Equal(expectedServiceBindingList)) }, Entry("type", "type=key", payloads.ServiceBindingList{Type: korifiv1alpha1.CFServiceBindingTypeKey}), + Entry("type", "type=app", payloads.ServiceBindingList{Type: korifiv1alpha1.CFServiceBindingTypeApp}), Entry("app_guids", "app_guids=app_guid", payloads.ServiceBindingList{AppGUIDs: "app_guid"}), Entry("service_instance_guids", "service_instance_guids=si_guid", payloads.ServiceBindingList{ServiceInstanceGUIDs: "si_guid"}), Entry("include", "include=app", payloads.ServiceBindingList{Include: "app"}), + Entry("include", "include=key", payloads.ServiceBindingList{Include: "key"}), Entry("label_selector=foo", "label_selector=foo", payloads.ServiceBindingList{LabelSelector: "foo"}), Entry("service_plan_guids=plan-guid", "service_plan_guids=plan-guid", payloads.ServiceBindingList{PlanGUIDs: "plan-guid"}), ) @@ -128,7 +130,7 @@ var _ = Describe("ServiceBindingCreate", func() { Expect(serviceBindingCreate).To(gstruct.PointTo(Equal(createPayload))) }) - When("binding is key and name field is omitted", func() { + When("name field is omitted", func() { BeforeEach(func() { createPayload.Name = "" }) diff --git a/api/repositories/service_binding_repository_test.go b/api/repositories/service_binding_repository_test.go index ec9b7f588..fbad8ccc7 100644 --- a/api/repositories/service_binding_repository_test.go +++ b/api/repositories/service_binding_repository_test.go @@ -289,7 +289,6 @@ var _ = Describe("ServiceBindingRepo", func() { It("creates a new CFServiceBinding resource and returns a record", func() { Expect(createErr).NotTo(HaveOccurred()) - Expect(serviceBindingRecord.GUID).NotTo(BeEmpty()) Expect(serviceBindingRecord.Type).To(Equal(korifiv1alpha1.CFServiceBindingTypeApp)) Expect(serviceBindingRecord.GUID).To(matchers.BeValidUUID()) Expect(serviceBindingRecord.Name).To(BeNil()) @@ -550,7 +549,7 @@ var _ = Describe("ServiceBindingRepo", func() { It("creates a new CFServiceBinding resource and returns a record", func() { Expect(createErr).NotTo(HaveOccurred()) - Expect(serviceBindingRecord.GUID).NotTo(BeEmpty()) + Expect(serviceBindingRecord.GUID).To(matchers.BeValidUUID()) Expect(serviceBindingRecord.Type).To(Equal(korifiv1alpha1.CFServiceBindingTypeApp)) Expect(*(serviceBindingRecord.Name)).To(Equal(serviceBindingName)) Expect(serviceBindingRecord.AppGUID).To(Equal(appGUID)) @@ -568,16 +567,22 @@ var _ = Describe("ServiceBindingRepo", func() { k8sClient.Get(ctx, types.NamespacedName{Name: serviceBindingRecord.GUID, Namespace: space.Name}, serviceBinding), ).To(Succeed()) - Expect(serviceBinding.Labels).To(HaveKeyWithValue("servicebinding.io/provisioned-service", "true")) - Expect(serviceBinding.Spec.Type).To(Equal(korifiv1alpha1.CFServiceBindingTypeApp)) - Expect(*(serviceBinding.Spec.DisplayName)).To(Equal(serviceBindingName)) - Expect(serviceBinding.Spec.Service).To(Equal(corev1.ObjectReference{ - Kind: "CFServiceInstance", - APIVersion: korifiv1alpha1.SchemeGroupVersion.Identifier(), - Name: cfServiceInstance.Name, - })) - Expect(serviceBinding.Spec.AppRef).To(Equal(corev1.LocalObjectReference{ - Name: appGUID, + Expect(*serviceBinding).To(MatchFields(IgnoreExtras, Fields{ + "ObjectMeta": MatchFields(IgnoreExtras, Fields{ + "Labels": HaveKeyWithValue("servicebinding.io/provisioned-service", "true"), + }), + "Spec": MatchFields(IgnoreExtras, Fields{ + "Type": Equal(korifiv1alpha1.CFServiceBindingTypeApp), + "DisplayName": PointTo(Equal(serviceBindingName)), + "Service": Equal(corev1.ObjectReference{ + Kind: "CFServiceInstance", + APIVersion: korifiv1alpha1.SchemeGroupVersion.Identifier(), + Name: cfServiceInstance.Name, + }), + "AppRef": Equal(corev1.LocalObjectReference{ + Name: appGUID, + }), + }), })) }) }) @@ -600,19 +605,11 @@ var _ = Describe("ServiceBindingRepo", func() { }) It("creates a key binding", func() { - Expect(createErr).NotTo(HaveOccurred()) - Expect(serviceBindingRecord.GUID).NotTo(BeEmpty()) + Expect(serviceBindingRecord.AppGUID).To(Equal("")) Expect(serviceBindingRecord.Type).To(Equal(korifiv1alpha1.CFServiceBindingTypeKey)) + Expect(serviceBindingRecord.Relationships()).To(HaveKeyWithValue("app", "")) Expect(*(serviceBindingRecord.Name)).To(Equal(serviceBindingName)) - Expect(serviceBindingRecord.AppGUID).To(Equal("")) - Expect(serviceBindingRecord.ServiceInstanceGUID).To(Equal(cfServiceInstance.Name)) - Expect(serviceBindingRecord.SpaceGUID).To(Equal(space.Name)) - Expect(serviceBindingRecord.CreatedAt).NotTo(BeZero()) - Expect(serviceBindingRecord.UpdatedAt).NotTo(BeNil()) - Expect(serviceBindingRecord.Relationships()).To(Equal(map[string]string{ - "app": "", - "service_instance": cfServiceInstance.Name, - })) + Expect(createErr).NotTo(HaveOccurred()) serviceBinding := new(korifiv1alpha1.CFServiceBinding) Expect(