github.com/jenspinney/cli@v6.42.1-0.20190207184520-7450c600020e+incompatible/actor/v2action/service_instance_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/ccerror" 10 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2" 11 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant" 12 13 . "github.com/onsi/ginkgo" 14 . "github.com/onsi/ginkgo/extensions/table" 15 . "github.com/onsi/gomega" 16 ) 17 18 var _ = Describe("Service Instance Actions", func() { 19 var ( 20 actor *Actor 21 fakeCloudControllerClient *v2actionfakes.FakeCloudControllerClient 22 ) 23 24 BeforeEach(func() { 25 fakeCloudControllerClient = new(v2actionfakes.FakeCloudControllerClient) 26 actor = NewActor(fakeCloudControllerClient, nil, nil) 27 }) 28 29 Describe("CreateServiceInstance", func() { 30 var ( 31 serviceInstance ServiceInstance 32 createServiceWarnings Warnings 33 createServiceErr error 34 ) 35 36 When("no broker name is specified", func() { 37 JustBeforeEach(func() { 38 serviceInstance, createServiceWarnings, createServiceErr = actor.CreateServiceInstance("space-guid", "service", "service-plan", "service-instance", "", map[string]interface{}{"some": "params"}, []string{"tag-1", "tag-2"}) 39 }) 40 41 When("there are no errors creating a service instance", func() { 42 var returnedInstance ccv2.ServiceInstance 43 44 BeforeEach(func() { 45 returnedInstance = ccv2.ServiceInstance{ 46 GUID: "some-service-guid", 47 Name: "service", 48 SpaceGUID: "space-guid", 49 ServiceGUID: "service-guid", 50 ServicePlanGUID: "service-plan-guid", 51 Type: constant.ServiceInstanceTypeManagedService, 52 Tags: []string{"some", "tags"}, 53 DashboardURL: "http://dashboard.com", 54 LastOperation: ccv2.LastOperation{}, 55 } 56 fakeCloudControllerClient.GetSpaceServicesReturns( 57 []ccv2.Service{{ 58 GUID: "a-service-guid", 59 }}, 60 nil, 61 nil, 62 ) 63 fakeCloudControllerClient.GetServicePlansReturns( 64 []ccv2.ServicePlan{ 65 { 66 GUID: "a-random-service-plan-guid", 67 Name: "service-plan-2", 68 }, 69 { 70 GUID: "the-service-plan-guid", 71 Name: "service-plan", 72 }, 73 }, 74 nil, 75 nil) 76 fakeCloudControllerClient.CreateServiceInstanceReturns( 77 returnedInstance, 78 ccv2.Warnings{"create-service-instance-warning"}, 79 nil) 80 }) 81 82 It("returns the service instance and warnings", func() { 83 By("getting the space services") 84 Expect(createServiceErr).ToNot(HaveOccurred()) 85 Expect(serviceInstance).To(Equal(ServiceInstance(returnedInstance))) 86 Expect(createServiceWarnings).To(ConsistOf("create-service-instance-warning")) 87 88 Expect(fakeCloudControllerClient.GetSpaceServicesCallCount()).To(Equal(1)) 89 spaceGUID, serviceFilter := fakeCloudControllerClient.GetSpaceServicesArgsForCall(0) 90 Expect(spaceGUID).Should(Equal("space-guid")) 91 Expect(serviceFilter).Should(Equal([]ccv2.Filter{{ 92 Type: constant.LabelFilter, 93 Operator: constant.EqualOperator, 94 Values: []string{"service"}, 95 }}, 96 )) 97 98 By("getting the plans of the service") 99 Expect(fakeCloudControllerClient.GetServicePlansCallCount()).To(Equal(1)) 100 planFilter := fakeCloudControllerClient.GetServicePlansArgsForCall(0) 101 Expect(planFilter).Should(Equal([]ccv2.Filter{{ 102 Type: constant.ServiceGUIDFilter, 103 Operator: constant.EqualOperator, 104 Values: []string{"a-service-guid"}, 105 }}, 106 )) 107 108 By("creating the service instance") 109 Expect(fakeCloudControllerClient.CreateServiceInstanceCallCount()).To(Equal(1)) 110 spaceGUID, planGUID, serviceInstanceName, parameters, tags := fakeCloudControllerClient.CreateServiceInstanceArgsForCall(0) 111 Expect(serviceInstanceName).To(Equal("service-instance")) 112 Expect(spaceGUID).To(Equal("space-guid")) 113 Expect(planGUID).To(Equal("the-service-plan-guid")) 114 Expect(parameters).To(Equal(map[string]interface{}{ 115 "some": "params", 116 })) 117 Expect(tags).To(ConsistOf("tag-1", "tag-2")) 118 }) 119 }) 120 121 When("there are errors creating a service instance", func() { 122 BeforeEach(func() { 123 fakeCloudControllerClient.GetSpaceServicesReturns( 124 []ccv2.Service{{ 125 GUID: "a-service-guid", 126 }}, 127 nil, 128 nil, 129 ) 130 fakeCloudControllerClient.GetServicePlansReturns( 131 []ccv2.ServicePlan{{ 132 GUID: "the-service-plan-guid", 133 Name: "service-plan", 134 }}, 135 nil, 136 nil) 137 fakeCloudControllerClient.CreateServiceInstanceReturns( 138 ccv2.ServiceInstance{}, 139 ccv2.Warnings{"create-service-instance-warning"}, 140 errors.New("boom")) 141 }) 142 143 It("returns the error and warnings", func() { 144 Expect(serviceInstance).To(Equal(ServiceInstance{})) 145 Expect(createServiceErr).To(MatchError("boom")) 146 Expect(createServiceWarnings).To(ConsistOf("create-service-instance-warning")) 147 148 Expect(fakeCloudControllerClient.CreateServiceInstanceCallCount()).To(Equal(1)) 149 }) 150 }) 151 152 When("there are no matching services", func() { 153 BeforeEach(func() { 154 fakeCloudControllerClient.GetSpaceServicesReturns( 155 []ccv2.Service{}, 156 ccv2.Warnings{"service-warnings"}, 157 nil, 158 ) 159 }) 160 161 It("returns the error and warnings", func() { 162 Expect(serviceInstance).To(Equal(ServiceInstance{})) 163 Expect(createServiceErr).To(MatchError(actionerror.ServiceNotFoundError{Name: "service"})) 164 Expect(createServiceWarnings).To(ConsistOf("service-warnings")) 165 166 Expect(fakeCloudControllerClient.CreateServiceInstanceCallCount()).To(Equal(0)) 167 }) 168 }) 169 170 When("getting services returns an error", func() { 171 BeforeEach(func() { 172 fakeCloudControllerClient.GetSpaceServicesReturns( 173 []ccv2.Service{}, 174 ccv2.Warnings{"service-warnings"}, 175 errors.New("boom"), 176 ) 177 }) 178 179 It("returns the error and warnings", func() { 180 Expect(serviceInstance).To(Equal(ServiceInstance{})) 181 Expect(createServiceErr).To(MatchError("boom")) 182 Expect(createServiceWarnings).To(ConsistOf("service-warnings")) 183 184 Expect(fakeCloudControllerClient.CreateServiceInstanceCallCount()).To(Equal(0)) 185 }) 186 }) 187 188 When("getting service plan returns an error", func() { 189 BeforeEach(func() { 190 fakeCloudControllerClient.GetSpaceServicesReturns( 191 []ccv2.Service{{ 192 GUID: "a-service-guid", 193 }}, 194 nil, 195 nil, 196 ) 197 fakeCloudControllerClient.GetServicePlansReturns( 198 []ccv2.ServicePlan{}, 199 ccv2.Warnings{"get-plan-warning"}, 200 errors.New("boom"), 201 ) 202 }) 203 204 It("returns the error and warnings", func() { 205 Expect(serviceInstance).To(Equal(ServiceInstance{})) 206 Expect(createServiceErr).To(MatchError("boom")) 207 Expect(createServiceWarnings).To(ConsistOf("get-plan-warning")) 208 209 Expect(fakeCloudControllerClient.CreateServiceInstanceCallCount()).To(Equal(0)) 210 }) 211 }) 212 213 When("no matching plan", func() { 214 BeforeEach(func() { 215 fakeCloudControllerClient.GetSpaceServicesReturns( 216 []ccv2.Service{{ 217 GUID: "a-service-guid", 218 }}, 219 nil, 220 nil, 221 ) 222 fakeCloudControllerClient.GetServicePlansReturns( 223 []ccv2.ServicePlan{{ 224 GUID: "the-service-plan-guid", 225 Name: "service-plan-2", 226 }}, 227 ccv2.Warnings{"get-plan-warning"}, 228 nil) 229 }) 230 231 It("returns the error and warnings", func() { 232 Expect(serviceInstance).To(Equal(ServiceInstance{})) 233 Expect(createServiceErr).To(MatchError(actionerror.ServicePlanNotFoundError{PlanName: "service-plan", ServiceName: "service"})) 234 Expect(createServiceWarnings).To(ConsistOf("get-plan-warning")) 235 236 Expect(fakeCloudControllerClient.CreateServiceInstanceCallCount()).To(Equal(0)) 237 }) 238 }) 239 240 When("there are warnings", func() { 241 BeforeEach(func() { 242 fakeCloudControllerClient.GetSpaceServicesReturns( 243 []ccv2.Service{ 244 {Label: "service-1", GUID: "service-guid-1"}, 245 }, 246 []string{"foo"}, 247 nil) 248 249 fakeCloudControllerClient.GetServicePlansReturns( 250 []ccv2.ServicePlan{ 251 {Name: "service-plan", GUID: "service-plan-guid-1"}, 252 }, 253 []string{"bar"}, 254 nil) 255 fakeCloudControllerClient.CreateServiceInstanceReturns( 256 ccv2.ServiceInstance{}, 257 []string{"baz", "qux"}, 258 nil) 259 }) 260 261 It("returns the warnings", func() { 262 Expect(createServiceWarnings).To(ConsistOf([]string{"foo", "bar", "baz", "qux"})) 263 }) 264 }) 265 266 When("there are multiple available services by the same name", func() { 267 BeforeEach(func() { 268 // two services get returned, indicating that there are two services with the 269 // same name (i.e. they are provided by different service brokers) 270 fakeCloudControllerClient.GetSpaceServicesReturns( 271 []ccv2.Service{ 272 { 273 GUID: "a-service-guid", 274 }, 275 { 276 GUID: "another-service-guid", 277 }, 278 }, 279 nil, 280 nil, 281 ) 282 }) 283 284 It("returns an error and warnings", func() { 285 Expect(fakeCloudControllerClient.GetSpaceServicesCallCount()).To(Equal(1)) 286 spaceGUID, serviceFilter := fakeCloudControllerClient.GetSpaceServicesArgsForCall(0) 287 Expect(spaceGUID).Should(Equal("space-guid")) 288 Expect(serviceFilter).Should(Equal([]ccv2.Filter{{ 289 Type: constant.LabelFilter, 290 Operator: constant.EqualOperator, 291 Values: []string{"service"}, 292 }}, 293 )) 294 295 Expect(createServiceErr).To(MatchError(actionerror.DuplicateServiceError{Name: "service"})) 296 Expect(fakeCloudControllerClient.CreateServiceInstanceCallCount()).To(Equal(0)) 297 }) 298 }) 299 }) 300 301 When("a broker name is specified", func() { 302 var brokerName string 303 304 BeforeEach(func() { 305 brokerName = "some-broker" 306 }) 307 308 JustBeforeEach(func() { 309 serviceInstance, createServiceWarnings, createServiceErr = actor.CreateServiceInstance("space-guid", "service", "service-plan", "service-instance", brokerName, map[string]interface{}{"some": "params"}, []string{"tag-1", "tag-2"}) 310 }) 311 312 When("the specified service broker exists", func() { 313 var returnedInstance ccv2.ServiceInstance 314 315 BeforeEach(func() { 316 returnedInstance = ccv2.ServiceInstance{ 317 GUID: "some-service-guid", 318 Name: "service", 319 SpaceGUID: "space-guid", 320 ServiceGUID: "service-guid", 321 ServicePlanGUID: "service-plan-guid", 322 Type: constant.ServiceInstanceTypeManagedService, 323 Tags: []string{"some", "tags"}, 324 DashboardURL: "http://dashboard.com", 325 LastOperation: ccv2.LastOperation{}, 326 } 327 fakeCloudControllerClient.GetServiceBrokersReturns( 328 []ccv2.ServiceBroker{{ 329 Name: "some-broker", 330 GUID: "a-broker-guid", 331 }}, 332 nil, 333 nil, 334 ) 335 fakeCloudControllerClient.GetSpaceServicesReturns( 336 []ccv2.Service{{ 337 GUID: "a-service-guid", 338 }}, 339 nil, 340 nil, 341 ) 342 fakeCloudControllerClient.GetServicePlansReturns( 343 []ccv2.ServicePlan{ 344 { 345 GUID: "a-random-service-plan-guid", 346 Name: "service-plan-2", 347 }, 348 { 349 GUID: "the-service-plan-guid", 350 Name: "service-plan", 351 }, 352 }, 353 nil, 354 nil) 355 fakeCloudControllerClient.CreateServiceInstanceReturns( 356 returnedInstance, 357 ccv2.Warnings{"create-service-instance-warning"}, 358 nil) 359 }) 360 361 It("returns the service instance and warnings", func() { 362 By("getting the service broker") 363 Expect(fakeCloudControllerClient.GetServiceBrokersCallCount()).To(Equal(1)) 364 brokerFilter := fakeCloudControllerClient.GetServiceBrokersArgsForCall(0) 365 Expect(brokerFilter).To(Equal([]ccv2.Filter{{ 366 Type: constant.NameFilter, 367 Operator: constant.EqualOperator, 368 Values: []string{brokerName}, 369 }}, 370 )) 371 372 By("getting the space services by service broker") 373 Expect(createServiceErr).ToNot(HaveOccurred()) 374 Expect(serviceInstance).To(Equal(ServiceInstance(returnedInstance))) 375 Expect(createServiceWarnings).To(ConsistOf("create-service-instance-warning")) 376 377 Expect(fakeCloudControllerClient.GetSpaceServicesCallCount()).To(Equal(1)) 378 spaceGUID, serviceFilters := fakeCloudControllerClient.GetSpaceServicesArgsForCall(0) 379 Expect(spaceGUID).To(Equal("space-guid")) 380 Expect(serviceFilters).To(Equal( 381 []ccv2.Filter{ 382 { 383 Type: constant.LabelFilter, 384 Operator: constant.EqualOperator, 385 Values: []string{"service"}, 386 }, 387 { 388 Type: constant.ServiceBrokerGUIDFilter, 389 Operator: constant.EqualOperator, 390 Values: []string{"a-broker-guid"}, 391 }, 392 }, 393 )) 394 395 By("getting the plans of the service") 396 Expect(fakeCloudControllerClient.GetServicePlansCallCount()).To(Equal(1)) 397 planFilter := fakeCloudControllerClient.GetServicePlansArgsForCall(0) 398 Expect(planFilter).To(Equal([]ccv2.Filter{{ 399 Type: constant.ServiceGUIDFilter, 400 Operator: constant.EqualOperator, 401 Values: []string{"a-service-guid"}, 402 }}, 403 )) 404 405 By("creating the service instance") 406 Expect(fakeCloudControllerClient.CreateServiceInstanceCallCount()).To(Equal(1)) 407 spaceGUID, planGUID, serviceInstanceName, parameters, tags := fakeCloudControllerClient.CreateServiceInstanceArgsForCall(0) 408 Expect(serviceInstanceName).To(Equal("service-instance")) 409 Expect(spaceGUID).To(Equal("space-guid")) 410 Expect(planGUID).To(Equal("the-service-plan-guid")) 411 Expect(parameters).To(Equal(map[string]interface{}{ 412 "some": "params", 413 })) 414 Expect(tags).To(ConsistOf("tag-1", "tag-2")) 415 }) 416 }) 417 418 When("the specified service broker does not exist", func() { 419 BeforeEach(func() { 420 fakeCloudControllerClient.GetServiceBrokersReturns( 421 []ccv2.ServiceBroker{}, 422 ccv2.Warnings{"get-service-broker-warning"}, 423 nil, 424 ) 425 }) 426 427 It("returns the error and warnings", func() { 428 Expect(serviceInstance).To(Equal(ServiceInstance{})) 429 Expect(createServiceErr).To(MatchError(actionerror.ServiceBrokerNotFoundError{Name: brokerName})) 430 Expect(createServiceWarnings).To(ConsistOf("get-service-broker-warning")) 431 432 Expect(fakeCloudControllerClient.CreateServiceInstanceCallCount()).To(Equal(0)) 433 }) 434 }) 435 436 When("there is an error getting the specified service broker", func() { 437 BeforeEach(func() { 438 fakeCloudControllerClient.GetServiceBrokersReturns( 439 []ccv2.ServiceBroker{}, 440 ccv2.Warnings{"get-service-broker-warning"}, 441 errors.New("oh no"), 442 ) 443 }) 444 445 It("returns the error and warnings", func() { 446 Expect(serviceInstance).To(Equal(ServiceInstance{})) 447 Expect(createServiceErr).To(MatchError("oh no")) 448 Expect(createServiceWarnings).To(ConsistOf("get-service-broker-warning")) 449 450 Expect(fakeCloudControllerClient.CreateServiceInstanceCallCount()).To(Equal(0)) 451 }) 452 }) 453 }) 454 }) 455 456 Describe("ServiceInstance", func() { 457 DescribeTable("IsManaged", 458 func(iType constant.ServiceInstanceType, expected bool) { 459 Expect(ServiceInstance{Type: iType}.IsManaged()).To(Equal(expected)) 460 }, 461 462 Entry("return true for managed service", constant.ServiceInstanceTypeManagedService, true), 463 Entry("return false for any other type of service", constant.ServiceInstanceTypeUserProvidedService, false), 464 ) 465 466 DescribeTable("IsUserProvided", 467 func(iType constant.ServiceInstanceType, expected bool) { 468 Expect(ServiceInstance{Type: iType}.IsUserProvided()).To(Equal(expected)) 469 }, 470 471 Entry("return true for UserProvidedService service", constant.ServiceInstanceTypeUserProvidedService, true), 472 Entry("return false for any other type of service", constant.ServiceInstanceTypeManagedService, false), 473 ) 474 }) 475 476 Describe("GetServiceInstance", func() { 477 var ( 478 serviceInstanceGUID string 479 480 serviceInstance ServiceInstance 481 warnings Warnings 482 executeErr error 483 ) 484 485 BeforeEach(func() { 486 serviceInstanceGUID = "service-instance-guid" 487 }) 488 489 JustBeforeEach(func() { 490 serviceInstance, warnings, executeErr = actor.GetServiceInstance(serviceInstanceGUID) 491 }) 492 493 When("the service instance exists", func() { 494 BeforeEach(func() { 495 fakeCloudControllerClient.GetServiceInstanceReturns(ccv2.ServiceInstance{Name: "some-service-instance", GUID: "service-instance-guid"}, ccv2.Warnings{"service-instance-warnings"}, nil) 496 }) 497 498 It("returns the service instance and warnings", func() { 499 Expect(executeErr).ToNot(HaveOccurred()) 500 Expect(serviceInstance).To(Equal(ServiceInstance{ 501 GUID: "service-instance-guid", 502 Name: "some-service-instance", 503 })) 504 Expect(warnings).To(ConsistOf("service-instance-warnings")) 505 506 Expect(fakeCloudControllerClient.GetServiceInstanceCallCount()).To(Equal(1)) 507 Expect(fakeCloudControllerClient.GetServiceInstanceArgsForCall(0)).To(Equal(serviceInstanceGUID)) 508 }) 509 }) 510 511 When("the service instance does not exist", func() { 512 BeforeEach(func() { 513 fakeCloudControllerClient.GetServiceInstanceReturns(ccv2.ServiceInstance{}, ccv2.Warnings{"service-instance-warnings-1"}, ccerror.ResourceNotFoundError{}) 514 }) 515 516 It("returns errors and warnings", func() { 517 Expect(executeErr).To(MatchError(actionerror.ServiceInstanceNotFoundError{GUID: serviceInstanceGUID})) 518 Expect(warnings).To(ConsistOf("service-instance-warnings-1")) 519 }) 520 }) 521 522 When("retrieving the application's bound services returns an error", func() { 523 var expectedErr error 524 525 BeforeEach(func() { 526 expectedErr = errors.New("this is indeed an error, kudos!") 527 fakeCloudControllerClient.GetServiceInstanceReturns(ccv2.ServiceInstance{}, ccv2.Warnings{"service-instance-warnings-1"}, expectedErr) 528 }) 529 530 It("returns errors and warnings", func() { 531 Expect(executeErr).To(MatchError(expectedErr)) 532 Expect(warnings).To(ConsistOf("service-instance-warnings-1")) 533 }) 534 }) 535 }) 536 537 Describe("GetServiceInstanceByNameAndSpace", func() { 538 When("the service instance exists", func() { 539 BeforeEach(func() { 540 fakeCloudControllerClient.GetSpaceServiceInstancesReturns( 541 []ccv2.ServiceInstance{ 542 { 543 GUID: "some-service-instance-guid", 544 Name: "some-service-instance", 545 }, 546 }, 547 ccv2.Warnings{"foo"}, 548 nil, 549 ) 550 }) 551 552 It("returns the service instance and warnings", func() { 553 serviceInstance, warnings, err := actor.GetServiceInstanceByNameAndSpace("some-service-instance", "some-space-guid") 554 Expect(err).ToNot(HaveOccurred()) 555 Expect(serviceInstance).To(Equal(ServiceInstance{ 556 GUID: "some-service-instance-guid", 557 Name: "some-service-instance", 558 })) 559 Expect(warnings).To(ConsistOf("foo")) 560 561 Expect(fakeCloudControllerClient.GetSpaceServiceInstancesCallCount()).To(Equal(1)) 562 563 spaceGUID, includeUserProvidedServices, queries := fakeCloudControllerClient.GetSpaceServiceInstancesArgsForCall(0) 564 Expect(spaceGUID).To(Equal("some-space-guid")) 565 Expect(includeUserProvidedServices).To(BeTrue()) 566 Expect(queries).To(ConsistOf([]ccv2.Filter{ 567 ccv2.Filter{ 568 Type: constant.NameFilter, 569 Operator: constant.EqualOperator, 570 Values: []string{"some-service-instance"}, 571 }, 572 })) 573 }) 574 }) 575 576 When("the service instance does not exists", func() { 577 BeforeEach(func() { 578 fakeCloudControllerClient.GetSpaceServiceInstancesReturns([]ccv2.ServiceInstance{}, nil, nil) 579 }) 580 581 It("returns a ServiceInstanceNotFoundError", func() { 582 _, _, err := actor.GetServiceInstanceByNameAndSpace("some-service-instance", "some-space-guid") 583 Expect(err).To(MatchError(actionerror.ServiceInstanceNotFoundError{Name: "some-service-instance"})) 584 }) 585 }) 586 587 When("the cloud controller client returns an error", func() { 588 var expectedError error 589 590 BeforeEach(func() { 591 expectedError = errors.New("I am a CloudControllerClient Error") 592 fakeCloudControllerClient.GetSpaceServiceInstancesReturns([]ccv2.ServiceInstance{}, nil, expectedError) 593 }) 594 595 It("returns the error", func() { 596 _, _, err := actor.GetServiceInstanceByNameAndSpace("some-service-instance", "some-space-guid") 597 Expect(err).To(MatchError(expectedError)) 598 }) 599 }) 600 }) 601 602 Describe("GetServiceInstancesByApplication", func() { 603 var ( 604 appGUID string 605 606 serviceInstances []ServiceInstance 607 warnings Warnings 608 executeErr error 609 ) 610 611 BeforeEach(func() { 612 appGUID = "some-app-guid" 613 }) 614 615 JustBeforeEach(func() { 616 serviceInstances, warnings, executeErr = actor.GetServiceInstancesByApplication(appGUID) 617 }) 618 619 When("the application has services bound", func() { 620 var serviceBindings []ccv2.ServiceBinding 621 622 BeforeEach(func() { 623 serviceBindings = []ccv2.ServiceBinding{ 624 {ServiceInstanceGUID: "service-instance-guid-1"}, 625 {ServiceInstanceGUID: "service-instance-guid-2"}, 626 {ServiceInstanceGUID: "service-instance-guid-3"}, 627 } 628 629 fakeCloudControllerClient.GetServiceBindingsReturns(serviceBindings, ccv2.Warnings{"service-bindings-warnings-1", "service-bindings-warnings-2"}, nil) 630 }) 631 632 When("retrieving the service instances is successful", func() { 633 BeforeEach(func() { 634 fakeCloudControllerClient.GetServiceInstanceReturnsOnCall(0, ccv2.ServiceInstance{Name: "some-service-instance-1"}, ccv2.Warnings{"service-instance-warnings-1"}, nil) 635 fakeCloudControllerClient.GetServiceInstanceReturnsOnCall(1, ccv2.ServiceInstance{Name: "some-service-instance-2"}, ccv2.Warnings{"service-instance-warnings-2"}, nil) 636 fakeCloudControllerClient.GetServiceInstanceReturnsOnCall(2, ccv2.ServiceInstance{Name: "some-service-instance-3"}, ccv2.Warnings{"service-instance-warnings-3"}, nil) 637 }) 638 639 It("returns the service instances and warnings", func() { 640 Expect(executeErr).ToNot(HaveOccurred()) 641 Expect(warnings).To(ConsistOf("service-bindings-warnings-1", "service-bindings-warnings-2", "service-instance-warnings-1", "service-instance-warnings-2", "service-instance-warnings-3")) 642 Expect(serviceInstances).To(ConsistOf( 643 ServiceInstance{Name: "some-service-instance-1"}, 644 ServiceInstance{Name: "some-service-instance-2"}, 645 ServiceInstance{Name: "some-service-instance-3"}, 646 )) 647 648 Expect(fakeCloudControllerClient.GetServiceInstanceCallCount()).To(Equal(3)) 649 Expect(fakeCloudControllerClient.GetServiceInstanceArgsForCall(0)).To(Equal("service-instance-guid-1")) 650 Expect(fakeCloudControllerClient.GetServiceInstanceArgsForCall(1)).To(Equal("service-instance-guid-2")) 651 Expect(fakeCloudControllerClient.GetServiceInstanceArgsForCall(2)).To(Equal("service-instance-guid-3")) 652 }) 653 }) 654 655 When("retrieving the service instances returns an error", func() { 656 var expectedErr error 657 658 BeforeEach(func() { 659 expectedErr = errors.New("this is indeed an error, kudos!") 660 fakeCloudControllerClient.GetServiceInstanceReturns(ccv2.ServiceInstance{}, ccv2.Warnings{"service-instance-warnings-1", "service-instance-warnings-2"}, expectedErr) 661 }) 662 663 It("returns errors and warnings", func() { 664 Expect(executeErr).To(MatchError(expectedErr)) 665 Expect(warnings).To(ConsistOf("service-bindings-warnings-1", "service-bindings-warnings-2", "service-instance-warnings-1", "service-instance-warnings-2")) 666 }) 667 }) 668 }) 669 670 When("the application has no services bound", func() { 671 BeforeEach(func() { 672 fakeCloudControllerClient.GetServiceBindingsReturns(nil, ccv2.Warnings{"service-bindings-warnings-1", "service-bindings-warnings-2"}, nil) 673 }) 674 675 It("returns an empty list and warnings", func() { 676 Expect(executeErr).ToNot(HaveOccurred()) 677 Expect(warnings).To(ConsistOf("service-bindings-warnings-1", "service-bindings-warnings-2")) 678 Expect(serviceInstances).To(BeEmpty()) 679 }) 680 }) 681 682 When("retrieving the application's bound services returns an error", func() { 683 var expectedErr error 684 685 BeforeEach(func() { 686 expectedErr = errors.New("this is indeed an error, kudos!") 687 fakeCloudControllerClient.GetServiceBindingsReturns(nil, ccv2.Warnings{"service-bindings-warnings-1", "service-bindings-warnings-2"}, expectedErr) 688 }) 689 690 It("returns errors and warnings", func() { 691 Expect(executeErr).To(MatchError(expectedErr)) 692 Expect(warnings).To(ConsistOf("service-bindings-warnings-1", "service-bindings-warnings-2")) 693 }) 694 }) 695 }) 696 697 Describe("GetServiceInstancesBySpace", func() { 698 When("there are service instances", func() { 699 BeforeEach(func() { 700 fakeCloudControllerClient.GetSpaceServiceInstancesReturns( 701 []ccv2.ServiceInstance{ 702 { 703 GUID: "some-service-instance-guid-1", 704 Name: "some-service-instance-1", 705 }, 706 { 707 GUID: "some-service-instance-guid-2", 708 Name: "some-service-instance-2", 709 }, 710 }, 711 ccv2.Warnings{"warning-1", "warning-2"}, 712 nil, 713 ) 714 }) 715 716 It("returns the service instances and warnings", func() { 717 serviceInstances, warnings, err := actor.GetServiceInstancesBySpace("some-space-guid") 718 Expect(err).ToNot(HaveOccurred()) 719 Expect(serviceInstances).To(ConsistOf( 720 ServiceInstance{ 721 GUID: "some-service-instance-guid-1", 722 Name: "some-service-instance-1", 723 }, 724 ServiceInstance{ 725 GUID: "some-service-instance-guid-2", 726 Name: "some-service-instance-2", 727 }, 728 )) 729 Expect(warnings).To(ConsistOf("warning-1", "warning-2")) 730 731 Expect(fakeCloudControllerClient.GetSpaceServiceInstancesCallCount()).To(Equal(1)) 732 733 spaceGUID, includeUserProvidedServices, queries := fakeCloudControllerClient.GetSpaceServiceInstancesArgsForCall(0) 734 Expect(spaceGUID).To(Equal("some-space-guid")) 735 Expect(includeUserProvidedServices).To(BeTrue()) 736 Expect(queries).To(BeNil()) 737 }) 738 }) 739 740 When("the cloud controller client returns an error", func() { 741 var expectedError error 742 743 BeforeEach(func() { 744 expectedError = errors.New("I am a CloudControllerClient Error") 745 fakeCloudControllerClient.GetSpaceServiceInstancesReturns( 746 []ccv2.ServiceInstance{}, 747 ccv2.Warnings{"warning-1", "warning-2"}, 748 expectedError) 749 }) 750 751 It("returns the error and warnings", func() { 752 _, warnings, err := actor.GetServiceInstancesBySpace("some-space-guid") 753 Expect(err).To(MatchError(expectedError)) 754 Expect(warnings).To(ConsistOf("warning-1", "warning-2")) 755 }) 756 }) 757 }) 758 })