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