github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/actor/v7action/route_binding_test.go (about) 1 package v7action_test 2 3 import ( 4 "code.cloudfoundry.org/cli/actor/actionerror" 5 . "code.cloudfoundry.org/cli/actor/v7action" 6 "code.cloudfoundry.org/cli/actor/v7action/v7actionfakes" 7 "code.cloudfoundry.org/cli/api/cloudcontroller/ccerror" 8 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3" 9 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant" 10 "code.cloudfoundry.org/cli/cf/errors" 11 "code.cloudfoundry.org/cli/resources" 12 "code.cloudfoundry.org/cli/types" 13 . "github.com/onsi/ginkgo" 14 . "github.com/onsi/gomega" 15 ) 16 17 var _ = Describe("Route Binding Action", func() { 18 var ( 19 actor *Actor 20 fakeCloudControllerClient *v7actionfakes.FakeCloudControllerClient 21 ) 22 23 BeforeEach(func() { 24 fakeCloudControllerClient = new(v7actionfakes.FakeCloudControllerClient) 25 actor = NewActor(fakeCloudControllerClient, nil, nil, nil, nil, nil) 26 }) 27 28 Describe("CreateRouteBinding", func() { 29 const ( 30 serviceInstanceName = "fake-service-instance-name" 31 serviceInstanceGUID = "fake-service-instance-guid" 32 domainName = "fake-domain-name" 33 domainGUID = "fake-domain-guid" 34 spaceGUID = "fake-space-guid" 35 hostname = "fake-hostname" 36 path = "fake-path" 37 routeGUID = "fake-route-guid" 38 fakeJobURL = ccv3.JobURL("fake-job-url") 39 ) 40 41 var ( 42 params CreateRouteBindingParams 43 warnings Warnings 44 executionError error 45 stream chan PollJobEvent 46 ) 47 48 BeforeEach(func() { 49 fakeCloudControllerClient.GetServiceInstanceByNameAndSpaceReturns( 50 resources.ServiceInstance{ 51 Name: serviceInstanceName, 52 GUID: serviceInstanceGUID, 53 Type: resources.ManagedServiceInstance, 54 }, 55 ccv3.IncludedResources{}, 56 ccv3.Warnings{"get instance warning"}, 57 nil, 58 ) 59 60 fakeCloudControllerClient.GetDomainsReturns( 61 []resources.Domain{{ 62 Name: domainName, 63 GUID: domainGUID, 64 }}, 65 ccv3.Warnings{"get domain warning"}, 66 nil, 67 ) 68 69 fakeCloudControllerClient.GetRoutesReturns( 70 []resources.Route{{ 71 GUID: routeGUID, 72 }}, 73 ccv3.Warnings{"get route warning"}, 74 nil, 75 ) 76 77 fakeCloudControllerClient.CreateRouteBindingReturns( 78 fakeJobURL, 79 ccv3.Warnings{"create binding warning"}, 80 nil, 81 ) 82 83 fakeStream := make(chan ccv3.PollJobEvent) 84 fakeCloudControllerClient.PollJobToEventStreamReturns(fakeStream) 85 go func() { 86 fakeStream <- ccv3.PollJobEvent{ 87 State: constant.JobPolling, 88 Warnings: ccv3.Warnings{"poll warning"}, 89 } 90 }() 91 92 params = CreateRouteBindingParams{ 93 SpaceGUID: spaceGUID, 94 ServiceInstanceName: serviceInstanceName, 95 DomainName: domainName, 96 Hostname: hostname, 97 Path: path, 98 Parameters: types.NewOptionalObject(map[string]interface{}{ 99 "foo": "bar", 100 }), 101 } 102 }) 103 104 JustBeforeEach(func() { 105 stream, warnings, executionError = actor.CreateRouteBinding(params) 106 }) 107 108 It("returns an event stream, warnings, and no errors", func() { 109 Expect(executionError).NotTo(HaveOccurred()) 110 111 Expect(warnings).To(ConsistOf(Warnings{ 112 "get instance warning", 113 "get domain warning", 114 "get route warning", 115 "create binding warning", 116 })) 117 118 Eventually(stream).Should(Receive(Equal(PollJobEvent{ 119 State: JobPolling, 120 Warnings: Warnings{"poll warning"}, 121 Err: nil, 122 }))) 123 }) 124 125 Describe("service instance lookup", func() { 126 It("makes the correct call", func() { 127 Expect(fakeCloudControllerClient.GetServiceInstanceByNameAndSpaceCallCount()).To(Equal(1)) 128 actualServiceInstanceName, actualSpaceGUID, actualQuery := fakeCloudControllerClient.GetServiceInstanceByNameAndSpaceArgsForCall(0) 129 Expect(actualServiceInstanceName).To(Equal(serviceInstanceName)) 130 Expect(actualSpaceGUID).To(Equal(spaceGUID)) 131 Expect(actualQuery).To(BeEmpty()) 132 }) 133 134 When("not found", func() { 135 BeforeEach(func() { 136 fakeCloudControllerClient.GetServiceInstanceByNameAndSpaceReturns( 137 resources.ServiceInstance{}, 138 ccv3.IncludedResources{}, 139 ccv3.Warnings{"get instance warning"}, 140 ccerror.ServiceInstanceNotFoundError{Name: serviceInstanceName}, 141 ) 142 }) 143 144 It("returns the error and warning", func() { 145 Expect(warnings).To(ContainElement("get instance warning")) 146 Expect(executionError).To(MatchError(actionerror.ServiceInstanceNotFoundError{Name: serviceInstanceName})) 147 }) 148 }) 149 150 When("fails", func() { 151 BeforeEach(func() { 152 fakeCloudControllerClient.GetServiceInstanceByNameAndSpaceReturns( 153 resources.ServiceInstance{}, 154 ccv3.IncludedResources{}, 155 ccv3.Warnings{"get instance warning"}, 156 errors.New("boof"), 157 ) 158 }) 159 160 It("returns the error and warning", func() { 161 Expect(warnings).To(ContainElement("get instance warning")) 162 Expect(executionError).To(MatchError("boof")) 163 }) 164 }) 165 }) 166 167 Describe("domain lookup", func() { 168 It("makes the correct call", func() { 169 Expect(fakeCloudControllerClient.GetDomainsCallCount()).To(Equal(1)) 170 Expect(fakeCloudControllerClient.GetDomainsArgsForCall(0)).To(ConsistOf( 171 ccv3.Query{Key: ccv3.NameFilter, Values: []string{domainName}}, 172 ccv3.Query{Key: ccv3.PerPage, Values: []string{"1"}}, 173 ccv3.Query{Key: ccv3.Page, Values: []string{"1"}}, 174 )) 175 }) 176 177 When("not found", func() { 178 BeforeEach(func() { 179 fakeCloudControllerClient.GetDomainsReturns( 180 []resources.Domain{}, 181 ccv3.Warnings{"get domain warning"}, 182 nil, 183 ) 184 }) 185 186 It("returns an error and warnings", func() { 187 Expect(warnings).To(ContainElement("get domain warning")) 188 Expect(executionError).To(MatchError(actionerror.DomainNotFoundError{Name: domainName})) 189 }) 190 }) 191 192 When("fails", func() { 193 BeforeEach(func() { 194 fakeCloudControllerClient.GetDomainsReturns( 195 []resources.Domain{}, 196 ccv3.Warnings{"get domain warning"}, 197 errors.New("boom"), 198 ) 199 }) 200 201 It("returns the error and warnings", func() { 202 Expect(warnings).To(ContainElement("get domain warning")) 203 Expect(executionError).To(MatchError("boom")) 204 }) 205 }) 206 }) 207 208 Describe("route lookup", func() { 209 It("makes the correct call", func() { 210 Expect(fakeCloudControllerClient.GetRoutesCallCount()).To(Equal(1)) 211 Expect(fakeCloudControllerClient.GetRoutesArgsForCall(0)).To(ConsistOf( 212 ccv3.Query{Key: ccv3.DomainGUIDFilter, Values: []string{domainGUID}}, 213 ccv3.Query{Key: ccv3.HostsFilter, Values: []string{hostname}}, 214 ccv3.Query{Key: ccv3.PathsFilter, Values: []string{path}}, 215 ccv3.Query{Key: ccv3.PerPage, Values: []string{"1"}}, 216 ccv3.Query{Key: ccv3.Page, Values: []string{"1"}}, 217 )) 218 }) 219 220 When("not found", func() { 221 BeforeEach(func() { 222 fakeCloudControllerClient.GetRoutesReturns( 223 []resources.Route{}, 224 ccv3.Warnings{"get route warning"}, 225 nil, 226 ) 227 228 params.Hostname = hostname 229 params.Path = path 230 }) 231 232 It("returns an error and warnings", func() { 233 Expect(warnings).To(ContainElement("get domain warning")) 234 Expect(executionError).To(MatchError(actionerror.RouteNotFoundError{ 235 Host: hostname, 236 DomainName: domainName, 237 Path: path, 238 })) 239 }) 240 }) 241 242 When("fails", func() { 243 BeforeEach(func() { 244 fakeCloudControllerClient.GetRoutesReturns( 245 []resources.Route{}, 246 ccv3.Warnings{"get route warning"}, 247 errors.New("pow"), 248 ) 249 }) 250 251 It("returns the error and warnings", func() { 252 Expect(warnings).To(ContainElement("get domain warning")) 253 Expect(executionError).To(MatchError("pow")) 254 }) 255 }) 256 }) 257 258 Describe("initiating the create", func() { 259 It("makes the correct call", func() { 260 Expect(fakeCloudControllerClient.CreateRouteBindingCallCount()).To(Equal(1)) 261 Expect(fakeCloudControllerClient.CreateRouteBindingArgsForCall(0)).To(Equal(resources.RouteBinding{ 262 ServiceInstanceGUID: serviceInstanceGUID, 263 RouteGUID: routeGUID, 264 Parameters: types.NewOptionalObject(map[string]interface{}{ 265 "foo": "bar", 266 }), 267 })) 268 }) 269 270 When("binding already exists", func() { 271 BeforeEach(func() { 272 fakeCloudControllerClient.CreateRouteBindingReturns( 273 "", 274 ccv3.Warnings{"create binding warning"}, 275 ccerror.ResourceAlreadyExistsError{ 276 Message: "The route and service instance are already bound.", 277 }, 278 ) 279 }) 280 281 It("returns an actionerror and warnings", func() { 282 Expect(warnings).To(ContainElement("create binding warning")) 283 Expect(executionError).To(MatchError(actionerror.ResourceAlreadyExistsError{ 284 Message: "The route and service instance are already bound.", 285 })) 286 }) 287 }) 288 289 When("fails", func() { 290 BeforeEach(func() { 291 fakeCloudControllerClient.CreateRouteBindingReturns( 292 "", 293 ccv3.Warnings{"create binding warning"}, 294 errors.New("boop"), 295 ) 296 }) 297 298 It("returns the error and warnings", func() { 299 Expect(warnings).To(ContainElement("create binding warning")) 300 Expect(executionError).To(MatchError("boop")) 301 }) 302 }) 303 }) 304 305 Describe("polling the job", func() { 306 It("polls the job", func() { 307 Expect(fakeCloudControllerClient.PollJobToEventStreamCallCount()).To(Equal(1)) 308 Expect(fakeCloudControllerClient.PollJobToEventStreamArgsForCall(0)).To(Equal(fakeJobURL)) 309 }) 310 }) 311 }) 312 313 Describe("DeleteRouteBinding", func() { 314 const ( 315 serviceInstanceName = "fake-service-instance-name" 316 serviceInstanceGUID = "fake-service-instance-guid" 317 domainName = "fake-domain-name" 318 domainGUID = "fake-domain-guid" 319 spaceGUID = "fake-space-guid" 320 hostname = "fake-hostname" 321 path = "fake-path" 322 routeGUID = "fake-route-guid" 323 routeBindingGUID = "fake-route-binding-guid" 324 fakeJobURL = ccv3.JobURL("fake-job-url") 325 ) 326 327 var ( 328 params DeleteRouteBindingParams 329 warnings Warnings 330 executionError error 331 stream chan PollJobEvent 332 ) 333 334 BeforeEach(func() { 335 fakeCloudControllerClient.GetServiceInstanceByNameAndSpaceReturns( 336 resources.ServiceInstance{ 337 Name: serviceInstanceName, 338 GUID: serviceInstanceGUID, 339 Type: resources.ManagedServiceInstance, 340 }, 341 ccv3.IncludedResources{}, 342 ccv3.Warnings{"get instance warning"}, 343 nil, 344 ) 345 346 fakeCloudControllerClient.GetDomainsReturns( 347 []resources.Domain{{ 348 Name: domainName, 349 GUID: domainGUID, 350 }}, 351 ccv3.Warnings{"get domain warning"}, 352 nil, 353 ) 354 355 fakeCloudControllerClient.GetRoutesReturns( 356 []resources.Route{{ 357 GUID: routeGUID, 358 }}, 359 ccv3.Warnings{"get route warning"}, 360 nil, 361 ) 362 363 fakeCloudControllerClient.GetRouteBindingsReturns( 364 []resources.RouteBinding{{ 365 GUID: routeBindingGUID, 366 }}, 367 ccv3.IncludedResources{}, 368 ccv3.Warnings{"get binding warning"}, 369 nil, 370 ) 371 372 fakeCloudControllerClient.DeleteRouteBindingReturns( 373 fakeJobURL, 374 ccv3.Warnings{"delete binding warning"}, 375 nil, 376 ) 377 378 fakeStream := make(chan ccv3.PollJobEvent) 379 fakeCloudControllerClient.PollJobToEventStreamReturns(fakeStream) 380 go func() { 381 fakeStream <- ccv3.PollJobEvent{ 382 State: constant.JobPolling, 383 Warnings: ccv3.Warnings{"poll warning"}, 384 } 385 }() 386 387 params = DeleteRouteBindingParams{ 388 SpaceGUID: spaceGUID, 389 ServiceInstanceName: serviceInstanceName, 390 DomainName: domainName, 391 Hostname: hostname, 392 Path: path, 393 } 394 }) 395 396 JustBeforeEach(func() { 397 stream, warnings, executionError = actor.DeleteRouteBinding(params) 398 }) 399 400 It("returns an event stream, warnings, and no errors", func() { 401 Expect(executionError).NotTo(HaveOccurred()) 402 403 Expect(warnings).To(ConsistOf(Warnings{ 404 "get instance warning", 405 "get domain warning", 406 "get route warning", 407 "get binding warning", 408 "delete binding warning", 409 })) 410 411 Eventually(stream).Should(Receive(Equal(PollJobEvent{ 412 State: JobPolling, 413 Warnings: Warnings{"poll warning"}, 414 Err: nil, 415 }))) 416 }) 417 418 Describe("service instance lookup", func() { 419 It("makes the correct call", func() { 420 Expect(fakeCloudControllerClient.GetServiceInstanceByNameAndSpaceCallCount()).To(Equal(1)) 421 actualServiceInstanceName, actualSpaceGUID, actualQuery := fakeCloudControllerClient.GetServiceInstanceByNameAndSpaceArgsForCall(0) 422 Expect(actualServiceInstanceName).To(Equal(serviceInstanceName)) 423 Expect(actualSpaceGUID).To(Equal(spaceGUID)) 424 Expect(actualQuery).To(BeEmpty()) 425 }) 426 427 When("not found", func() { 428 BeforeEach(func() { 429 fakeCloudControllerClient.GetServiceInstanceByNameAndSpaceReturns( 430 resources.ServiceInstance{}, 431 ccv3.IncludedResources{}, 432 ccv3.Warnings{"get instance warning"}, 433 ccerror.ServiceInstanceNotFoundError{Name: serviceInstanceName}, 434 ) 435 }) 436 437 It("returns the error and warning", func() { 438 Expect(warnings).To(ContainElement("get instance warning")) 439 Expect(executionError).To(MatchError(actionerror.ServiceInstanceNotFoundError{Name: serviceInstanceName})) 440 }) 441 }) 442 443 When("fails", func() { 444 BeforeEach(func() { 445 fakeCloudControllerClient.GetServiceInstanceByNameAndSpaceReturns( 446 resources.ServiceInstance{}, 447 ccv3.IncludedResources{}, 448 ccv3.Warnings{"get instance warning"}, 449 errors.New("boof"), 450 ) 451 }) 452 453 It("returns the error and warning", func() { 454 Expect(warnings).To(ContainElement("get instance warning")) 455 Expect(executionError).To(MatchError("boof")) 456 }) 457 }) 458 }) 459 460 Describe("domain lookup", func() { 461 It("makes the correct call", func() { 462 Expect(fakeCloudControllerClient.GetDomainsCallCount()).To(Equal(1)) 463 Expect(fakeCloudControllerClient.GetDomainsArgsForCall(0)).To(ConsistOf( 464 ccv3.Query{Key: ccv3.NameFilter, Values: []string{domainName}}, 465 ccv3.Query{Key: ccv3.PerPage, Values: []string{"1"}}, 466 ccv3.Query{Key: ccv3.Page, Values: []string{"1"}}, 467 )) 468 }) 469 470 When("not found", func() { 471 BeforeEach(func() { 472 fakeCloudControllerClient.GetDomainsReturns( 473 []resources.Domain{}, 474 ccv3.Warnings{"get domain warning"}, 475 nil, 476 ) 477 }) 478 479 It("returns an error and warnings", func() { 480 Expect(warnings).To(ContainElement("get domain warning")) 481 Expect(executionError).To(MatchError(actionerror.DomainNotFoundError{Name: domainName})) 482 }) 483 }) 484 485 When("fails", func() { 486 BeforeEach(func() { 487 fakeCloudControllerClient.GetDomainsReturns( 488 []resources.Domain{}, 489 ccv3.Warnings{"get domain warning"}, 490 errors.New("boom"), 491 ) 492 }) 493 494 It("returns the error and warnings", func() { 495 Expect(warnings).To(ContainElement("get domain warning")) 496 Expect(executionError).To(MatchError("boom")) 497 }) 498 }) 499 }) 500 501 Describe("route lookup", func() { 502 It("makes the correct call", func() { 503 Expect(fakeCloudControllerClient.GetRoutesCallCount()).To(Equal(1)) 504 Expect(fakeCloudControllerClient.GetRoutesArgsForCall(0)).To(ConsistOf( 505 ccv3.Query{Key: ccv3.DomainGUIDFilter, Values: []string{domainGUID}}, 506 ccv3.Query{Key: ccv3.HostsFilter, Values: []string{hostname}}, 507 ccv3.Query{Key: ccv3.PathsFilter, Values: []string{path}}, 508 ccv3.Query{Key: ccv3.PerPage, Values: []string{"1"}}, 509 ccv3.Query{Key: ccv3.Page, Values: []string{"1"}}, 510 )) 511 }) 512 513 When("not found", func() { 514 BeforeEach(func() { 515 fakeCloudControllerClient.GetRoutesReturns( 516 []resources.Route{}, 517 ccv3.Warnings{"get route warning"}, 518 nil, 519 ) 520 521 params.Hostname = hostname 522 params.Path = path 523 }) 524 525 It("returns an error and warnings", func() { 526 Expect(warnings).To(ContainElement("get domain warning")) 527 Expect(executionError).To(MatchError(actionerror.RouteNotFoundError{ 528 Host: hostname, 529 DomainName: domainName, 530 Path: path, 531 })) 532 }) 533 }) 534 535 When("fails", func() { 536 BeforeEach(func() { 537 fakeCloudControllerClient.GetRoutesReturns( 538 []resources.Route{}, 539 ccv3.Warnings{"get route warning"}, 540 errors.New("pow"), 541 ) 542 }) 543 544 It("returns the error and warnings", func() { 545 Expect(warnings).To(ContainElement("get domain warning")) 546 Expect(executionError).To(MatchError("pow")) 547 }) 548 }) 549 }) 550 551 Describe("route binding lookup", func() { 552 It("makes the correct call", func() { 553 Expect(fakeCloudControllerClient.GetRouteBindingsCallCount()).To(Equal(1)) 554 Expect(fakeCloudControllerClient.GetRouteBindingsArgsForCall(0)).To(ConsistOf( 555 ccv3.Query{Key: ccv3.RouteGUIDFilter, Values: []string{routeGUID}}, 556 ccv3.Query{Key: ccv3.ServiceInstanceGUIDFilter, Values: []string{serviceInstanceGUID}}, 557 ccv3.Query{Key: ccv3.PerPage, Values: []string{"1"}}, 558 ccv3.Query{Key: ccv3.Page, Values: []string{"1"}}, 559 )) 560 }) 561 562 When("not found", func() { 563 BeforeEach(func() { 564 fakeCloudControllerClient.GetRouteBindingsReturns( 565 []resources.RouteBinding{}, 566 ccv3.IncludedResources{}, 567 ccv3.Warnings{"get bindings warning"}, 568 nil, 569 ) 570 }) 571 572 It("returns an error and warnings", func() { 573 Expect(warnings).To(ContainElement("get bindings warning")) 574 Expect(executionError).To(MatchError(actionerror.RouteBindingNotFoundError{})) 575 }) 576 }) 577 578 When("fails", func() { 579 BeforeEach(func() { 580 fakeCloudControllerClient.GetRouteBindingsReturns( 581 []resources.RouteBinding{}, 582 ccv3.IncludedResources{}, 583 ccv3.Warnings{"get bindings warning"}, 584 errors.New("boom"), 585 ) 586 }) 587 588 It("returns an error and warnings", func() { 589 Expect(warnings).To(ContainElement("get bindings warning")) 590 Expect(executionError).To(MatchError("boom")) 591 }) 592 }) 593 }) 594 595 Describe("initiating the delete", func() { 596 It("makes the correct call", func() { 597 Expect(fakeCloudControllerClient.DeleteRouteBindingCallCount()).To(Equal(1)) 598 Expect(fakeCloudControllerClient.DeleteRouteBindingArgsForCall(0)).To(Equal(routeBindingGUID)) 599 }) 600 601 When("fails", func() { 602 BeforeEach(func() { 603 fakeCloudControllerClient.DeleteRouteBindingReturns( 604 "", 605 ccv3.Warnings{"delete binding warning"}, 606 errors.New("boop"), 607 ) 608 }) 609 610 It("returns the error and warnings", func() { 611 Expect(warnings).To(ContainElement("delete binding warning")) 612 Expect(executionError).To(MatchError("boop")) 613 }) 614 }) 615 }) 616 617 Describe("polling the job", func() { 618 It("polls the job", func() { 619 Expect(fakeCloudControllerClient.PollJobToEventStreamCallCount()).To(Equal(1)) 620 Expect(fakeCloudControllerClient.PollJobToEventStreamArgsForCall(0)).To(Equal(fakeJobURL)) 621 }) 622 }) 623 }) 624 })