github.com/cloudfoundry-community/cloudfoundry-cli@v6.44.1-0.20240130060226-cda5ed8e89a5+incompatible/actor/v7action/service_instance_test.go (about) 1 package v7action_test 2 3 import ( 4 "code.cloudfoundry.org/cli/resources" 5 "errors" 6 7 "code.cloudfoundry.org/cli/actor/actionerror" 8 . "code.cloudfoundry.org/cli/actor/v7action" 9 "code.cloudfoundry.org/cli/actor/v7action/v7actionfakes" 10 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3" 11 12 . "github.com/onsi/ginkgo" 13 . "github.com/onsi/gomega" 14 ) 15 16 var _ = Describe("Service Instance Actions", func() { 17 var ( 18 actor *Actor 19 fakeCloudControllerClient *v7actionfakes.FakeCloudControllerClient 20 ) 21 22 BeforeEach(func() { 23 fakeCloudControllerClient = new(v7actionfakes.FakeCloudControllerClient) 24 actor = NewActor(fakeCloudControllerClient, nil, nil, nil) 25 }) 26 27 Describe("GetServiceInstanceByNameAndSpace", func() { 28 var ( 29 serviceInstanceName string 30 sourceSpaceGUID string 31 32 serviceInstance ServiceInstance 33 warnings Warnings 34 executionError error 35 ) 36 37 BeforeEach(func() { 38 serviceInstanceName = "some-service-instance" 39 sourceSpaceGUID = "some-source-space-guid" 40 }) 41 42 JustBeforeEach(func() { 43 serviceInstance, warnings, executionError = actor.GetServiceInstanceByNameAndSpace(serviceInstanceName, sourceSpaceGUID) 44 }) 45 46 When("the cloud controller request is successful", func() { 47 When("the cloud controller returns one service instance", func() { 48 BeforeEach(func() { 49 fakeCloudControllerClient.GetServiceInstancesReturns([]resources.ServiceInstance{ 50 { 51 Name: "some-service-instance", 52 GUID: "some-service-instance-guid", 53 }, 54 }, ccv3.Warnings{"some-service-instance-warning"}, nil) 55 }) 56 57 It("returns a service instance and warnings", func() { 58 Expect(executionError).NotTo(HaveOccurred()) 59 60 Expect(serviceInstance).To(Equal(ServiceInstance{Name: "some-service-instance", GUID: "some-service-instance-guid"})) 61 Expect(warnings).To(ConsistOf("some-service-instance-warning")) 62 Expect(fakeCloudControllerClient.GetServiceInstancesCallCount()).To(Equal(1)) 63 Expect(fakeCloudControllerClient.GetServiceInstancesArgsForCall(0)).To(ConsistOf( 64 ccv3.Query{Key: ccv3.NameFilter, Values: []string{serviceInstanceName}}, 65 ccv3.Query{Key: ccv3.SpaceGUIDFilter, Values: []string{sourceSpaceGUID}}, 66 )) 67 }) 68 }) 69 70 When("the cloud controller returns no service instances", func() { 71 BeforeEach(func() { 72 fakeCloudControllerClient.GetServiceInstancesReturns( 73 nil, 74 ccv3.Warnings{"some-service-instance-warning"}, 75 nil) 76 }) 77 78 It("returns an error and warnings", func() { 79 Expect(executionError).To(MatchError(actionerror.ServiceInstanceNotFoundError{Name: serviceInstanceName})) 80 81 Expect(warnings).To(ConsistOf("some-service-instance-warning")) 82 }) 83 }) 84 }) 85 86 When("the cloud controller returns an error", func() { 87 BeforeEach(func() { 88 fakeCloudControllerClient.GetServiceInstancesReturns( 89 nil, 90 ccv3.Warnings{"some-service-instance-warning"}, 91 errors.New("no service instance")) 92 }) 93 94 It("returns an error and warnings", func() { 95 Expect(executionError).To(MatchError("no service instance")) 96 Expect(warnings).To(ConsistOf("some-service-instance-warning")) 97 }) 98 }) 99 }) 100 101 Describe("UnshareServiceInstanceByServiceInstanceAndSpace", func() { 102 var ( 103 serviceInstanceGUID string 104 sharedToSpaceGUID string 105 106 warnings Warnings 107 executeErr error 108 ) 109 110 BeforeEach(func() { 111 serviceInstanceGUID = "some-service-instance-guid" 112 sharedToSpaceGUID = "some-other-space-guid" 113 }) 114 115 JustBeforeEach(func() { 116 warnings, executeErr = actor.UnshareServiceInstanceByServiceInstanceAndSpace(serviceInstanceGUID, sharedToSpaceGUID) 117 }) 118 119 When("no errors occur deleting the service instance share relationship", func() { 120 BeforeEach(func() { 121 fakeCloudControllerClient.DeleteServiceInstanceRelationshipsSharedSpaceReturns( 122 ccv3.Warnings{"delete-share-relationship-warning"}, 123 nil) 124 }) 125 126 It("returns no errors and all warnings", func() { 127 Expect(executeErr).ToNot(HaveOccurred()) 128 Expect(warnings).To(ConsistOf("delete-share-relationship-warning")) 129 130 Expect(fakeCloudControllerClient.DeleteServiceInstanceRelationshipsSharedSpaceCallCount()).To(Equal(1)) 131 serviceInstanceGUIDArg, sharedToSpaceGUIDArg := fakeCloudControllerClient.DeleteServiceInstanceRelationshipsSharedSpaceArgsForCall(0) 132 Expect(serviceInstanceGUIDArg).To(Equal(serviceInstanceGUID)) 133 Expect(sharedToSpaceGUIDArg).To(Equal(sharedToSpaceGUID)) 134 }) 135 }) 136 137 When("an error occurs deleting the service instance share relationship", func() { 138 var expectedErr error 139 140 BeforeEach(func() { 141 expectedErr = errors.New("delete share relationship error") 142 fakeCloudControllerClient.DeleteServiceInstanceRelationshipsSharedSpaceReturns( 143 ccv3.Warnings{"delete-share-relationship-warning"}, 144 expectedErr) 145 }) 146 147 It("returns the error and all warnings", func() { 148 Expect(executeErr).To(MatchError(expectedErr)) 149 Expect(warnings).To(ConsistOf("delete-share-relationship-warning")) 150 }) 151 }) 152 }) 153 })