github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/actor/v2action/service_binding_test.go (about) 1 package v2action_test 2 3 import ( 4 "errors" 5 6 "code.cloudfoundry.org/cli/actor/actionerror" 7 . "code.cloudfoundry.org/cli/actor/v2action" 8 "code.cloudfoundry.org/cli/actor/v2action/v2actionfakes" 9 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2" 10 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant" 11 12 . "github.com/onsi/ginkgo" 13 . "github.com/onsi/gomega" 14 ) 15 16 var _ = Describe("Service Binding Actions", func() { 17 var ( 18 actor *Actor 19 fakeCloudControllerClient *v2actionfakes.FakeCloudControllerClient 20 ) 21 22 BeforeEach(func() { 23 fakeCloudControllerClient = new(v2actionfakes.FakeCloudControllerClient) 24 actor = NewActor(fakeCloudControllerClient, nil, nil) 25 }) 26 27 Describe("BindServiceByApplicationAndServiceInstance", func() { 28 var ( 29 applicationGUID string 30 serviceInstanceGUID string 31 32 executeErr error 33 warnings Warnings 34 ) 35 BeforeEach(func() { 36 applicationGUID = "some-app-guid" 37 serviceInstanceGUID = "some-service-instance-guid" 38 }) 39 40 JustBeforeEach(func() { 41 warnings, executeErr = actor.BindServiceByApplicationAndServiceInstance(applicationGUID, serviceInstanceGUID) 42 }) 43 44 Context("when the binding is successful", func() { 45 BeforeEach(func() { 46 fakeCloudControllerClient.CreateServiceBindingReturns(ccv2.ServiceBinding{}, ccv2.Warnings{"some-warnings"}, nil) 47 }) 48 49 It("returns errors and warnings", func() { 50 Expect(executeErr).ToNot(HaveOccurred()) 51 Expect(warnings).To(ConsistOf("some-warnings")) 52 53 Expect(fakeCloudControllerClient.CreateServiceBindingCallCount()).To(Equal(1)) 54 inputAppGUID, inputServiceInstanceGUID, inputParameters := fakeCloudControllerClient.CreateServiceBindingArgsForCall(0) 55 Expect(inputAppGUID).To(Equal(applicationGUID)) 56 Expect(inputServiceInstanceGUID).To(Equal(serviceInstanceGUID)) 57 Expect(inputParameters).To(BeNil()) 58 }) 59 }) 60 61 Context("when the binding fails", func() { 62 BeforeEach(func() { 63 fakeCloudControllerClient.CreateServiceBindingReturns(ccv2.ServiceBinding{}, ccv2.Warnings{"some-warnings"}, errors.New("some-error")) 64 }) 65 66 It("returns errors and warnings", func() { 67 Expect(executeErr).To(MatchError("some-error")) 68 Expect(warnings).To(ConsistOf("some-warnings")) 69 }) 70 }) 71 }) 72 73 Describe("BindServiceBySpace", func() { 74 var ( 75 executeErr error 76 warnings Warnings 77 ) 78 79 JustBeforeEach(func() { 80 warnings, executeErr = actor.BindServiceBySpace("some-app-name", "some-service-instance-name", "some-space-guid", map[string]interface{}{"some-parameter": "some-value"}) 81 }) 82 83 Context("when getting the application errors", func() { 84 BeforeEach(func() { 85 fakeCloudControllerClient.GetApplicationsReturns( 86 nil, 87 ccv2.Warnings{"foo-1"}, 88 errors.New("some-error"), 89 ) 90 }) 91 92 It("returns the error", func() { 93 Expect(executeErr).To(MatchError(errors.New("some-error"))) 94 Expect(warnings).To(ConsistOf("foo-1")) 95 }) 96 }) 97 98 Context("when getting the application succeeds", func() { 99 BeforeEach(func() { 100 fakeCloudControllerClient.GetApplicationsReturns( 101 []ccv2.Application{{GUID: "some-app-guid"}}, 102 ccv2.Warnings{"foo-1"}, 103 nil, 104 ) 105 }) 106 107 Context("when getting the service instance errors", func() { 108 BeforeEach(func() { 109 fakeCloudControllerClient.GetSpaceServiceInstancesReturns( 110 []ccv2.ServiceInstance{}, 111 ccv2.Warnings{"foo-2"}, 112 errors.New("some-error"), 113 ) 114 }) 115 116 It("returns the error", func() { 117 Expect(executeErr).To(MatchError(errors.New("some-error"))) 118 Expect(warnings).To(ConsistOf("foo-1", "foo-2")) 119 }) 120 }) 121 122 Context("when getting the service instance succeeds", func() { 123 BeforeEach(func() { 124 fakeCloudControllerClient.GetSpaceServiceInstancesReturns( 125 []ccv2.ServiceInstance{{GUID: "some-service-instance-guid"}}, 126 ccv2.Warnings{"foo-2"}, 127 nil, 128 ) 129 }) 130 131 Context("when getting binding the service instance to the application errors", func() { 132 BeforeEach(func() { 133 fakeCloudControllerClient.CreateServiceBindingReturns( 134 ccv2.ServiceBinding{}, 135 ccv2.Warnings{"foo-3"}, 136 errors.New("some-error"), 137 ) 138 }) 139 140 It("returns the error", func() { 141 Expect(executeErr).To(MatchError(errors.New("some-error"))) 142 Expect(warnings).To(ConsistOf("foo-1", "foo-2", "foo-3")) 143 }) 144 }) 145 Context("when getting binding the service instance to the application succeeds", func() { 146 BeforeEach(func() { 147 fakeCloudControllerClient.CreateServiceBindingReturns( 148 ccv2.ServiceBinding{GUID: "some-service-binding-guid"}, 149 ccv2.Warnings{"foo-3"}, 150 nil, 151 ) 152 }) 153 154 It("returns all warnings", func() { 155 Expect(executeErr).ToNot(HaveOccurred()) 156 Expect(warnings).To(ConsistOf("foo-1", "foo-2", "foo-3")) 157 158 Expect(fakeCloudControllerClient.GetApplicationsCallCount()).To(Equal(1)) 159 160 Expect(fakeCloudControllerClient.GetSpaceServiceInstancesCallCount()).To(Equal(1)) 161 162 Expect(fakeCloudControllerClient.CreateServiceBindingCallCount()).To(Equal(1)) 163 appGUID, serviceInstanceGUID, parameters := fakeCloudControllerClient.CreateServiceBindingArgsForCall(0) 164 Expect(appGUID).To(Equal("some-app-guid")) 165 Expect(serviceInstanceGUID).To(Equal("some-service-instance-guid")) 166 Expect(parameters).To(Equal(map[string]interface{}{"some-parameter": "some-value"})) 167 }) 168 }) 169 }) 170 }) 171 }) 172 173 Describe("GetServiceBindingByApplicationAndServiceInstance", func() { 174 Context("when the service binding exists", func() { 175 BeforeEach(func() { 176 fakeCloudControllerClient.GetServiceBindingsReturns( 177 []ccv2.ServiceBinding{ 178 { 179 GUID: "some-service-binding-guid", 180 }, 181 }, 182 ccv2.Warnings{"foo"}, 183 nil, 184 ) 185 }) 186 187 It("returns the service binding and warnings", func() { 188 serviceBinding, warnings, err := actor.GetServiceBindingByApplicationAndServiceInstance("some-app-guid", "some-service-instance-guid") 189 Expect(err).ToNot(HaveOccurred()) 190 Expect(serviceBinding).To(Equal(ServiceBinding{ 191 GUID: "some-service-binding-guid", 192 })) 193 Expect(warnings).To(Equal(Warnings{"foo"})) 194 195 Expect(fakeCloudControllerClient.GetServiceBindingsCallCount()).To(Equal(1)) 196 Expect(fakeCloudControllerClient.GetServiceBindingsArgsForCall(0)).To(ConsistOf([]ccv2.Filter{ 197 ccv2.Filter{ 198 Type: constant.AppGUIDFilter, 199 Operator: constant.EqualOperator, 200 Values: []string{"some-app-guid"}, 201 }, 202 ccv2.Filter{ 203 Type: constant.ServiceInstanceGUIDFilter, 204 Operator: constant.EqualOperator, 205 Values: []string{"some-service-instance-guid"}, 206 }, 207 })) 208 }) 209 }) 210 211 Context("when the service binding does not exists", func() { 212 BeforeEach(func() { 213 fakeCloudControllerClient.GetServiceBindingsReturns([]ccv2.ServiceBinding{}, nil, nil) 214 }) 215 216 It("returns a ServiceBindingNotFoundError", func() { 217 _, _, err := actor.GetServiceBindingByApplicationAndServiceInstance("some-app-guid", "some-service-instance-guid") 218 Expect(err).To(MatchError(actionerror.ServiceBindingNotFoundError{ 219 AppGUID: "some-app-guid", 220 ServiceInstanceGUID: "some-service-instance-guid", 221 })) 222 }) 223 }) 224 225 Context("when the cloud controller client returns an error", func() { 226 var expectedError error 227 228 BeforeEach(func() { 229 expectedError = errors.New("I am a CloudControllerClient Error") 230 fakeCloudControllerClient.GetServiceBindingsReturns([]ccv2.ServiceBinding{}, nil, expectedError) 231 }) 232 233 It("returns the error", func() { 234 _, _, err := actor.GetServiceBindingByApplicationAndServiceInstance("some-app-guid", "some-service-instance-guid") 235 Expect(err).To(MatchError(expectedError)) 236 }) 237 }) 238 }) 239 240 Describe("UnbindServiceBySpace", func() { 241 Context("when the service binding exists", func() { 242 BeforeEach(func() { 243 fakeCloudControllerClient.GetApplicationsReturns( 244 []ccv2.Application{ 245 { 246 GUID: "some-app-guid", 247 Name: "some-app", 248 }, 249 }, 250 ccv2.Warnings{"foo-1"}, 251 nil, 252 ) 253 fakeCloudControllerClient.GetSpaceServiceInstancesReturns( 254 []ccv2.ServiceInstance{ 255 { 256 GUID: "some-service-instance-guid", 257 Name: "some-service-instance", 258 }, 259 }, 260 ccv2.Warnings{"foo-2"}, 261 nil, 262 ) 263 fakeCloudControllerClient.GetServiceBindingsReturns( 264 []ccv2.ServiceBinding{ 265 { 266 GUID: "some-service-binding-guid", 267 }, 268 }, 269 ccv2.Warnings{"foo-3"}, 270 nil, 271 ) 272 273 fakeCloudControllerClient.DeleteServiceBindingReturns( 274 ccv2.Warnings{"foo-4", "foo-5"}, 275 nil, 276 ) 277 }) 278 279 It("deletes the service binding", func() { 280 warnings, err := actor.UnbindServiceBySpace("some-app", "some-service-instance", "some-space-guid") 281 Expect(err).NotTo(HaveOccurred()) 282 Expect(warnings).To(ConsistOf(Warnings{"foo-1", "foo-2", "foo-3", "foo-4", "foo-5"})) 283 284 Expect(fakeCloudControllerClient.DeleteServiceBindingCallCount()).To(Equal(1)) 285 Expect(fakeCloudControllerClient.DeleteServiceBindingArgsForCall(0)).To(Equal("some-service-binding-guid")) 286 }) 287 288 Context("when the cloud controller API returns warnings and an error", func() { 289 var expectedError error 290 291 BeforeEach(func() { 292 expectedError = errors.New("I am a CC error") 293 fakeCloudControllerClient.DeleteServiceBindingReturns(ccv2.Warnings{"foo-4", "foo-5"}, expectedError) 294 }) 295 296 It("returns the warnings and the error", func() { 297 warnings, err := actor.UnbindServiceBySpace("some-app", "some-service-instance", "some-space-guid") 298 Expect(err).To(MatchError(expectedError)) 299 Expect(warnings).To(ConsistOf(Warnings{"foo-1", "foo-2", "foo-3", "foo-4", "foo-5"})) 300 }) 301 }) 302 }) 303 }) 304 305 Describe("GetServiceBindingsByServiceInstance", func() { 306 var ( 307 serviceBindings []ServiceBinding 308 serviceBindingsWarnings Warnings 309 serviceBindingsErr error 310 ) 311 312 JustBeforeEach(func() { 313 serviceBindings, serviceBindingsWarnings, serviceBindingsErr = actor.GetServiceBindingsByServiceInstance("some-service-instance-guid") 314 }) 315 316 Context("when no errors are encountered", func() { 317 BeforeEach(func() { 318 fakeCloudControllerClient.GetServiceInstanceServiceBindingsReturns( 319 []ccv2.ServiceBinding{ 320 {GUID: "some-service-binding-1-guid"}, 321 {GUID: "some-service-binding-2-guid"}, 322 }, 323 ccv2.Warnings{"get-service-bindings-warning"}, 324 nil, 325 ) 326 }) 327 328 It("returns service bindings and all warnings", func() { 329 Expect(serviceBindingsErr).ToNot(HaveOccurred()) 330 Expect(serviceBindings).To(ConsistOf( 331 ServiceBinding{GUID: "some-service-binding-1-guid"}, 332 ServiceBinding{GUID: "some-service-binding-2-guid"}, 333 )) 334 Expect(serviceBindingsWarnings).To(ConsistOf("get-service-bindings-warning")) 335 336 Expect(fakeCloudControllerClient.GetServiceInstanceServiceBindingsCallCount()).To(Equal(1)) 337 Expect(fakeCloudControllerClient.GetServiceInstanceServiceBindingsArgsForCall(0)).To(Equal("some-service-instance-guid")) 338 }) 339 }) 340 341 Context("when an error is encountered", func() { 342 var expectedErr error 343 344 BeforeEach(func() { 345 expectedErr = errors.New("get service bindings error") 346 fakeCloudControllerClient.GetServiceInstanceServiceBindingsReturns( 347 []ccv2.ServiceBinding{}, 348 ccv2.Warnings{"get-service-bindings-warning"}, 349 expectedErr, 350 ) 351 }) 352 353 It("returns the error and all warnings", func() { 354 Expect(serviceBindingsErr).To(MatchError(expectedErr)) 355 Expect(serviceBindingsWarnings).To(ConsistOf("get-service-bindings-warning")) 356 357 Expect(fakeCloudControllerClient.GetServiceInstanceServiceBindingsCallCount()).To(Equal(1)) 358 Expect(fakeCloudControllerClient.GetServiceInstanceServiceBindingsArgsForCall(0)).To(Equal("some-service-instance-guid")) 359 }) 360 }) 361 }) 362 363 Describe("GetServiceBindingsByUserProvidedServiceInstance", func() { 364 var ( 365 serviceBindings []ServiceBinding 366 serviceBindingsWarnings Warnings 367 serviceBindingsErr error 368 ) 369 370 JustBeforeEach(func() { 371 serviceBindings, serviceBindingsWarnings, serviceBindingsErr = actor.GetServiceBindingsByUserProvidedServiceInstance("some-user-provided-service-instance-guid") 372 }) 373 374 Context("when no errors are encountered", func() { 375 BeforeEach(func() { 376 fakeCloudControllerClient.GetUserProvidedServiceInstanceServiceBindingsReturns( 377 []ccv2.ServiceBinding{ 378 {GUID: "some-service-binding-1-guid"}, 379 {GUID: "some-service-binding-2-guid"}, 380 }, 381 ccv2.Warnings{"get-service-bindings-warning"}, 382 nil, 383 ) 384 }) 385 386 It("returns service bindings and all warnings", func() { 387 Expect(serviceBindingsErr).ToNot(HaveOccurred()) 388 Expect(serviceBindings).To(ConsistOf( 389 ServiceBinding{GUID: "some-service-binding-1-guid"}, 390 ServiceBinding{GUID: "some-service-binding-2-guid"}, 391 )) 392 Expect(serviceBindingsWarnings).To(ConsistOf("get-service-bindings-warning")) 393 394 Expect(fakeCloudControllerClient.GetUserProvidedServiceInstanceServiceBindingsCallCount()).To(Equal(1)) 395 Expect(fakeCloudControllerClient.GetUserProvidedServiceInstanceServiceBindingsArgsForCall(0)).To(Equal("some-user-provided-service-instance-guid")) 396 }) 397 }) 398 399 Context("when an error is encountered", func() { 400 var expectedErr error 401 402 BeforeEach(func() { 403 expectedErr = errors.New("get service bindings error") 404 fakeCloudControllerClient.GetUserProvidedServiceInstanceServiceBindingsReturns( 405 []ccv2.ServiceBinding{}, 406 ccv2.Warnings{"get-service-bindings-warning"}, 407 expectedErr, 408 ) 409 }) 410 411 It("returns the error and all warnings", func() { 412 Expect(serviceBindingsErr).To(MatchError(expectedErr)) 413 Expect(serviceBindingsWarnings).To(ConsistOf("get-service-bindings-warning")) 414 415 Expect(fakeCloudControllerClient.GetUserProvidedServiceInstanceServiceBindingsCallCount()).To(Equal(1)) 416 Expect(fakeCloudControllerClient.GetUserProvidedServiceInstanceServiceBindingsArgsForCall(0)).To(Equal("some-user-provided-service-instance-guid")) 417 }) 418 }) 419 }) 420 })