github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/actor/v2action/service_instance_test.go (about) 1 package v2action_test 2 3 import ( 4 "errors" 5 6 . "code.cloudfoundry.org/cli/actor/v2action" 7 "code.cloudfoundry.org/cli/actor/v2action/v2actionfakes" 8 "code.cloudfoundry.org/cli/api/cloudcontroller/ccerror" 9 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2" 10 11 . "github.com/onsi/ginkgo" 12 . "github.com/onsi/gomega" 13 ) 14 15 var _ = Describe("Service Instance Actions", func() { 16 var ( 17 actor *Actor 18 fakeCloudControllerClient *v2actionfakes.FakeCloudControllerClient 19 ) 20 21 BeforeEach(func() { 22 fakeCloudControllerClient = new(v2actionfakes.FakeCloudControllerClient) 23 actor = NewActor(fakeCloudControllerClient, nil, nil) 24 }) 25 26 Describe("GetServiceInstance", func() { 27 var ( 28 serviceInstanceGUID string 29 30 serviceInstance ServiceInstance 31 warnings Warnings 32 executeErr error 33 ) 34 35 BeforeEach(func() { 36 serviceInstanceGUID = "service-instance-guid" 37 }) 38 39 JustBeforeEach(func() { 40 serviceInstance, warnings, executeErr = actor.GetServiceInstance(serviceInstanceGUID) 41 }) 42 43 Context("when the service instance exists", func() { 44 BeforeEach(func() { 45 fakeCloudControllerClient.GetServiceInstanceReturns(ccv2.ServiceInstance{Name: "some-service-instance", GUID: "service-instance-guid"}, ccv2.Warnings{"service-instance-warnings"}, nil) 46 }) 47 48 It("returns the service instance and warnings", func() { 49 Expect(executeErr).ToNot(HaveOccurred()) 50 Expect(serviceInstance).To(Equal(ServiceInstance{ 51 GUID: "service-instance-guid", 52 Name: "some-service-instance", 53 })) 54 Expect(warnings).To(Equal(Warnings{"service-instance-warnings"})) 55 56 Expect(fakeCloudControllerClient.GetServiceInstanceCallCount()).To(Equal(1)) 57 Expect(fakeCloudControllerClient.GetServiceInstanceArgsForCall(0)).To(Equal(serviceInstanceGUID)) 58 }) 59 }) 60 61 Context("when the service instance does not exist", func() { 62 BeforeEach(func() { 63 fakeCloudControllerClient.GetServiceInstanceReturns(ccv2.ServiceInstance{}, ccv2.Warnings{"service-instance-warnings-1"}, ccerror.ResourceNotFoundError{}) 64 }) 65 66 It("returns errors and warnings", func() { 67 Expect(executeErr).To(MatchError(ServiceInstanceNotFoundError{GUID: serviceInstanceGUID})) 68 Expect(warnings).To(ConsistOf("service-instance-warnings-1")) 69 }) 70 }) 71 72 Context("when retrieving the application's bound services returns an error", func() { 73 var expectedErr error 74 75 BeforeEach(func() { 76 expectedErr = errors.New("this is indeed an error, kudos!") 77 fakeCloudControllerClient.GetServiceInstanceReturns(ccv2.ServiceInstance{}, ccv2.Warnings{"service-instance-warnings-1"}, expectedErr) 78 }) 79 80 It("returns errors and warnings", func() { 81 Expect(executeErr).To(MatchError(expectedErr)) 82 Expect(warnings).To(ConsistOf("service-instance-warnings-1")) 83 }) 84 }) 85 }) 86 87 Describe("GetServiceInstanceByNameAndSpace", func() { 88 Context("when the service instance exists", func() { 89 BeforeEach(func() { 90 fakeCloudControllerClient.GetSpaceServiceInstancesReturns( 91 []ccv2.ServiceInstance{ 92 { 93 GUID: "some-service-instance-guid", 94 Name: "some-service-instance", 95 }, 96 }, 97 ccv2.Warnings{"foo"}, 98 nil, 99 ) 100 }) 101 102 It("returns the service instance and warnings", func() { 103 serviceInstance, warnings, err := actor.GetServiceInstanceByNameAndSpace("some-service-instance", "some-space-guid") 104 Expect(err).ToNot(HaveOccurred()) 105 Expect(serviceInstance).To(Equal(ServiceInstance{ 106 GUID: "some-service-instance-guid", 107 Name: "some-service-instance", 108 })) 109 Expect(warnings).To(Equal(Warnings{"foo"})) 110 111 Expect(fakeCloudControllerClient.GetSpaceServiceInstancesCallCount()).To(Equal(1)) 112 113 spaceGUID, includeUserProvidedServices, queries := fakeCloudControllerClient.GetSpaceServiceInstancesArgsForCall(0) 114 Expect(spaceGUID).To(Equal("some-space-guid")) 115 Expect(includeUserProvidedServices).To(BeTrue()) 116 Expect(queries).To(ConsistOf([]ccv2.Query{ 117 ccv2.Query{ 118 Filter: ccv2.NameFilter, 119 Operator: ccv2.EqualOperator, 120 Values: []string{"some-service-instance"}, 121 }, 122 })) 123 }) 124 }) 125 126 Context("when the service instance does not exists", func() { 127 BeforeEach(func() { 128 fakeCloudControllerClient.GetSpaceServiceInstancesReturns([]ccv2.ServiceInstance{}, nil, nil) 129 }) 130 131 It("returns a ServiceInstanceNotFoundError", func() { 132 _, _, err := actor.GetServiceInstanceByNameAndSpace("some-service-instance", "some-space-guid") 133 Expect(err).To(MatchError(ServiceInstanceNotFoundError{Name: "some-service-instance"})) 134 }) 135 }) 136 137 Context("when the cloud controller client returns an error", func() { 138 var expectedError error 139 140 BeforeEach(func() { 141 expectedError = errors.New("I am a CloudControllerClient Error") 142 fakeCloudControllerClient.GetSpaceServiceInstancesReturns([]ccv2.ServiceInstance{}, nil, expectedError) 143 }) 144 145 It("returns the error", func() { 146 _, _, err := actor.GetServiceInstanceByNameAndSpace("some-service-instance", "some-space-guid") 147 Expect(err).To(MatchError(expectedError)) 148 }) 149 }) 150 }) 151 152 Describe("GetServiceInstancesByApplication", func() { 153 var ( 154 appGUID string 155 156 serviceInstances []ServiceInstance 157 warnings Warnings 158 executeErr error 159 ) 160 161 BeforeEach(func() { 162 appGUID = "some-app-guid" 163 }) 164 165 JustBeforeEach(func() { 166 serviceInstances, warnings, executeErr = actor.GetServiceInstancesByApplication(appGUID) 167 }) 168 169 Context("when the application has services bound", func() { 170 var serviceBindings []ccv2.ServiceBinding 171 172 BeforeEach(func() { 173 serviceBindings = []ccv2.ServiceBinding{ 174 {ServiceInstanceGUID: "service-instance-guid-1"}, 175 {ServiceInstanceGUID: "service-instance-guid-2"}, 176 {ServiceInstanceGUID: "service-instance-guid-3"}, 177 } 178 179 fakeCloudControllerClient.GetServiceBindingsReturns(serviceBindings, ccv2.Warnings{"service-bindings-warnings-1", "service-bindings-warnings-2"}, nil) 180 }) 181 182 Context("when retrieving the service instances is successful", func() { 183 BeforeEach(func() { 184 fakeCloudControllerClient.GetServiceInstanceReturnsOnCall(0, ccv2.ServiceInstance{Name: "some-service-instance-1"}, ccv2.Warnings{"service-instance-warnings-1"}, nil) 185 fakeCloudControllerClient.GetServiceInstanceReturnsOnCall(1, ccv2.ServiceInstance{Name: "some-service-instance-2"}, ccv2.Warnings{"service-instance-warnings-2"}, nil) 186 fakeCloudControllerClient.GetServiceInstanceReturnsOnCall(2, ccv2.ServiceInstance{Name: "some-service-instance-3"}, ccv2.Warnings{"service-instance-warnings-3"}, nil) 187 }) 188 189 It("returns the service instances and warnings", func() { 190 Expect(executeErr).ToNot(HaveOccurred()) 191 Expect(warnings).To(ConsistOf("service-bindings-warnings-1", "service-bindings-warnings-2", "service-instance-warnings-1", "service-instance-warnings-2", "service-instance-warnings-3")) 192 Expect(serviceInstances).To(ConsistOf( 193 ServiceInstance{Name: "some-service-instance-1"}, 194 ServiceInstance{Name: "some-service-instance-2"}, 195 ServiceInstance{Name: "some-service-instance-3"}, 196 )) 197 198 Expect(fakeCloudControllerClient.GetServiceInstanceCallCount()).To(Equal(3)) 199 Expect(fakeCloudControllerClient.GetServiceInstanceArgsForCall(0)).To(Equal("service-instance-guid-1")) 200 Expect(fakeCloudControllerClient.GetServiceInstanceArgsForCall(1)).To(Equal("service-instance-guid-2")) 201 Expect(fakeCloudControllerClient.GetServiceInstanceArgsForCall(2)).To(Equal("service-instance-guid-3")) 202 }) 203 }) 204 205 Context("when retrieving the service instances returns an error", func() { 206 var expectedErr error 207 208 BeforeEach(func() { 209 expectedErr = errors.New("this is indeed an error, kudos!") 210 fakeCloudControllerClient.GetServiceInstanceReturns(ccv2.ServiceInstance{}, ccv2.Warnings{"service-instance-warnings-1", "service-instance-warnings-2"}, expectedErr) 211 }) 212 213 It("returns errors and warnings", func() { 214 Expect(executeErr).To(MatchError(expectedErr)) 215 Expect(warnings).To(ConsistOf("service-bindings-warnings-1", "service-bindings-warnings-2", "service-instance-warnings-1", "service-instance-warnings-2")) 216 }) 217 }) 218 }) 219 220 Context("when the application has no services bound", func() { 221 BeforeEach(func() { 222 fakeCloudControllerClient.GetServiceBindingsReturns(nil, ccv2.Warnings{"service-bindings-warnings-1", "service-bindings-warnings-2"}, nil) 223 }) 224 225 It("returns an empty list and warnings", func() { 226 Expect(executeErr).ToNot(HaveOccurred()) 227 Expect(warnings).To(ConsistOf("service-bindings-warnings-1", "service-bindings-warnings-2")) 228 Expect(serviceInstances).To(BeEmpty()) 229 }) 230 }) 231 232 Context("when retrieving the application's bound services returns an error", func() { 233 var expectedErr error 234 235 BeforeEach(func() { 236 expectedErr = errors.New("this is indeed an error, kudos!") 237 fakeCloudControllerClient.GetServiceBindingsReturns(nil, ccv2.Warnings{"service-bindings-warnings-1", "service-bindings-warnings-2"}, expectedErr) 238 }) 239 240 It("returns errors and warnings", func() { 241 Expect(executeErr).To(MatchError(expectedErr)) 242 Expect(warnings).To(ConsistOf("service-bindings-warnings-1", "service-bindings-warnings-2")) 243 }) 244 }) 245 }) 246 247 Describe("GetServiceInstancesBySpace", func() { 248 Context("when there are service instances", func() { 249 BeforeEach(func() { 250 fakeCloudControllerClient.GetSpaceServiceInstancesReturns( 251 []ccv2.ServiceInstance{ 252 { 253 GUID: "some-service-instance-guid-1", 254 Name: "some-service-instance-1", 255 }, 256 { 257 GUID: "some-service-instance-guid-2", 258 Name: "some-service-instance-2", 259 }, 260 }, 261 ccv2.Warnings{"warning-1", "warning-2"}, 262 nil, 263 ) 264 }) 265 266 It("returns the service instances and warnings", func() { 267 serviceInstances, warnings, err := actor.GetServiceInstancesBySpace("some-space-guid") 268 Expect(err).ToNot(HaveOccurred()) 269 Expect(serviceInstances).To(ConsistOf( 270 ServiceInstance{ 271 GUID: "some-service-instance-guid-1", 272 Name: "some-service-instance-1", 273 }, 274 ServiceInstance{ 275 GUID: "some-service-instance-guid-2", 276 Name: "some-service-instance-2", 277 }, 278 )) 279 Expect(warnings).To(ConsistOf("warning-1", "warning-2")) 280 281 Expect(fakeCloudControllerClient.GetSpaceServiceInstancesCallCount()).To(Equal(1)) 282 283 spaceGUID, includeUserProvidedServices, queries := fakeCloudControllerClient.GetSpaceServiceInstancesArgsForCall(0) 284 Expect(spaceGUID).To(Equal("some-space-guid")) 285 Expect(includeUserProvidedServices).To(BeTrue()) 286 Expect(queries).To(BeNil()) 287 }) 288 }) 289 290 Context("when the cloud controller client returns an error", func() { 291 var expectedError error 292 293 BeforeEach(func() { 294 expectedError = errors.New("I am a CloudControllerClient Error") 295 fakeCloudControllerClient.GetSpaceServiceInstancesReturns( 296 []ccv2.ServiceInstance{}, 297 ccv2.Warnings{"warning-1", "warning-2"}, 298 expectedError) 299 }) 300 301 It("returns the error and warnings", func() { 302 _, warnings, err := actor.GetServiceInstancesBySpace("some-space-guid") 303 Expect(err).To(MatchError(expectedError)) 304 Expect(warnings).To(ConsistOf("warning-1", "warning-2")) 305 }) 306 }) 307 }) 308 })