github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/actor/cfnetworkingaction/policy_test.go (about) 1 package cfnetworkingaction_test 2 3 import ( 4 "errors" 5 "fmt" 6 7 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3" 8 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant" 9 "code.cloudfoundry.org/cli/resources" 10 "code.cloudfoundry.org/cli/util/batcher" 11 12 "code.cloudfoundry.org/cfnetworking-cli-api/cfnetworking/cfnetv1" 13 "code.cloudfoundry.org/cli/actor/actionerror" 14 . "code.cloudfoundry.org/cli/actor/cfnetworkingaction" 15 "code.cloudfoundry.org/cli/actor/cfnetworkingaction/cfnetworkingactionfakes" 16 . "github.com/onsi/ginkgo" 17 . "github.com/onsi/gomega" 18 ) 19 20 var _ = Describe("Policy", func() { 21 var ( 22 actor *Actor 23 fakeCloudControllerClient *cfnetworkingactionfakes.FakeCloudControllerClient 24 fakeNetworkingClient *cfnetworkingactionfakes.FakeNetworkingClient 25 26 warnings Warnings 27 executeErr error 28 ) 29 30 BeforeEach(func() { 31 fakeCloudControllerClient = new(cfnetworkingactionfakes.FakeCloudControllerClient) 32 fakeNetworkingClient = new(cfnetworkingactionfakes.FakeNetworkingClient) 33 34 fakeCloudControllerClient.GetApplicationByNameAndSpaceStub = func(appName string, spaceGUID string) (resources.Application, ccv3.Warnings, error) { 35 if appName == "appA" { 36 return resources.Application{GUID: "appAGUID"}, []string{"v3ActorWarningA"}, nil 37 } else if appName == "appB" { 38 return resources.Application{GUID: "appBGUID"}, []string{"v3ActorWarningB"}, nil 39 } 40 return resources.Application{}, nil, nil 41 } 42 43 actor = NewActor(fakeNetworkingClient, fakeCloudControllerClient) 44 }) 45 46 Describe("AddNetworkPolicy", func() { 47 JustBeforeEach(func() { 48 srcSpaceGuid := "src-space" 49 srcApp := "appA" 50 destSpaceGuid := "dst-space" 51 destApp := "appB" 52 protocol := "tcp" 53 startPort := 8080 54 endPort := 8090 55 warnings, executeErr = actor.AddNetworkPolicy(srcSpaceGuid, srcApp, destSpaceGuid, destApp, protocol, startPort, endPort) 56 }) 57 58 It("creates policies", func() { 59 Expect(warnings).To(ConsistOf("v3ActorWarningA", "v3ActorWarningB")) 60 Expect(executeErr).NotTo(HaveOccurred()) 61 62 Expect(fakeCloudControllerClient.GetApplicationByNameAndSpaceCallCount()).To(Equal(2)) 63 sourceAppName, srcSpaceGUID := fakeCloudControllerClient.GetApplicationByNameAndSpaceArgsForCall(0) 64 Expect(sourceAppName).To(Equal("appA")) 65 Expect(srcSpaceGUID).To(Equal("src-space")) 66 67 destAppName, destSpaceGUID := fakeCloudControllerClient.GetApplicationByNameAndSpaceArgsForCall(1) 68 Expect(destAppName).To(Equal("appB")) 69 Expect(destSpaceGUID).To(Equal("dst-space")) 70 71 Expect(fakeNetworkingClient.CreatePoliciesCallCount()).To(Equal(1)) 72 Expect(fakeNetworkingClient.CreatePoliciesArgsForCall(0)).To(Equal([]cfnetv1.Policy{ 73 { 74 Source: cfnetv1.PolicySource{ 75 ID: "appAGUID", 76 }, 77 Destination: cfnetv1.PolicyDestination{ 78 ID: "appBGUID", 79 Protocol: "tcp", 80 Ports: cfnetv1.Ports{ 81 Start: 8080, 82 End: 8090, 83 }, 84 }, 85 }, 86 })) 87 }) 88 89 When("getting the source app fails ", func() { 90 BeforeEach(func() { 91 fakeCloudControllerClient.GetApplicationByNameAndSpaceReturns(resources.Application{}, []string{"v3ActorWarningA"}, errors.New("banana")) 92 }) 93 It("returns a sensible error", func() { 94 Expect(warnings).To(ConsistOf("v3ActorWarningA")) 95 Expect(executeErr).To(MatchError("banana")) 96 }) 97 }) 98 99 When("getting the destination app fails ", func() { 100 BeforeEach(func() { 101 fakeCloudControllerClient.GetApplicationByNameAndSpaceStub = func(appName string, spaceGUID string) (resources.Application, ccv3.Warnings, error) { 102 if appName == "appB" { 103 return resources.Application{}, []string{"v3ActorWarningB"}, errors.New("banana") 104 } 105 return resources.Application{}, []string{"v3ActorWarningA"}, nil 106 } 107 }) 108 It("returns a sensible error", func() { 109 Expect(warnings).To(ConsistOf("v3ActorWarningA", "v3ActorWarningB")) 110 Expect(executeErr).To(MatchError("banana")) 111 }) 112 }) 113 114 When("creating the policy fails", func() { 115 BeforeEach(func() { 116 fakeNetworkingClient.CreatePoliciesReturns(errors.New("apple")) 117 }) 118 It("returns a sensible error", func() { 119 Expect(executeErr).To(MatchError("apple")) 120 }) 121 }) 122 }) 123 124 Describe("NetworkPoliciesBySpaceAndAppName", func() { 125 var ( 126 policies []Policy 127 srcApp string 128 ) 129 130 BeforeEach(func() { 131 fakeNetworkingClient.ListPoliciesReturns([]cfnetv1.Policy{{ 132 Source: cfnetv1.PolicySource{ 133 ID: "appAGUID", 134 }, 135 Destination: cfnetv1.PolicyDestination{ 136 ID: "appBGUID", 137 Protocol: "tcp", 138 Ports: cfnetv1.Ports{ 139 Start: 8080, 140 End: 8080, 141 }, 142 }, 143 }, { 144 Source: cfnetv1.PolicySource{ 145 ID: "appAGUID", 146 }, 147 Destination: cfnetv1.PolicyDestination{ 148 ID: "appCGUID", 149 Protocol: "tcp", 150 Ports: cfnetv1.Ports{ 151 Start: 8080, 152 End: 8080, 153 }, 154 }, 155 }}, nil) 156 157 fakeCloudControllerClient.GetApplicationByNameAndSpaceReturns(resources.Application{ 158 Name: "appA", 159 GUID: "appAGUID", 160 SpaceGUID: "spaceAGUID", 161 }, []string{"GetApplicationByNameAndSpaceWarning"}, nil) 162 163 fakeCloudControllerClient.GetApplicationsReturns([]resources.Application{ 164 { 165 Name: "appB", 166 GUID: "appBGUID", 167 SpaceGUID: "spaceAGUID", 168 }, 169 { 170 Name: "appC", 171 GUID: "appCGUID", 172 SpaceGUID: "spaceCGUID", 173 }, 174 }, []string{"GetApplicationsWarning"}, nil) 175 176 fakeCloudControllerClient.GetSpacesReturns([]resources.Space{ 177 { 178 Name: "spaceA", 179 GUID: "spaceAGUID", 180 Relationships: map[constant.RelationshipType]resources.Relationship{ 181 constant.RelationshipTypeOrganization: {GUID: "orgAGUID"}, 182 }, 183 }, 184 { 185 Name: "spaceC", 186 GUID: "spaceCGUID", 187 Relationships: map[constant.RelationshipType]resources.Relationship{ 188 constant.RelationshipTypeOrganization: {GUID: "orgCGUID"}, 189 }, 190 }, 191 }, ccv3.IncludedResources{}, []string{"GetSpacesWarning"}, nil) 192 193 fakeCloudControllerClient.GetOrganizationsReturns([]resources.Organization{ 194 { 195 Name: "orgA", 196 GUID: "orgAGUID", 197 }, 198 { 199 Name: "orgC", 200 GUID: "orgCGUID", 201 }, 202 }, []string{"GetOrganizationsWarning"}, nil) 203 }) 204 205 JustBeforeEach(func() { 206 srcSpaceGuid := "space" 207 policies, warnings, executeErr = actor.NetworkPoliciesBySpaceAndAppName(srcSpaceGuid, srcApp) 208 }) 209 210 When("listing policies based on a source app", func() { 211 BeforeEach(func() { 212 srcApp = "appA" 213 }) 214 215 It("lists only policies for which the app is a source", func() { 216 Expect(policies).To(Equal([]Policy{ 217 { 218 SourceName: "appA", 219 DestinationName: "appB", 220 Protocol: "tcp", 221 StartPort: 8080, 222 EndPort: 8080, 223 DestinationSpaceName: "spaceA", 224 DestinationOrgName: "orgA", 225 }, 226 { 227 SourceName: "appA", 228 DestinationName: "appC", 229 Protocol: "tcp", 230 StartPort: 8080, 231 EndPort: 8080, 232 DestinationSpaceName: "spaceC", 233 DestinationOrgName: "orgC", 234 }, 235 }, 236 )) 237 }) 238 239 It("passes through the source app argument", func() { 240 Expect(warnings).To(ConsistOf( 241 "GetApplicationByNameAndSpaceWarning", 242 "GetApplicationsWarning", 243 "GetSpacesWarning", 244 "GetOrganizationsWarning", 245 )) 246 Expect(executeErr).NotTo(HaveOccurred()) 247 248 Expect(fakeCloudControllerClient.GetApplicationByNameAndSpaceCallCount()).To(Equal(1)) 249 sourceAppName, spaceGUID := fakeCloudControllerClient.GetApplicationByNameAndSpaceArgsForCall(0) 250 Expect(sourceAppName).To(Equal("appA")) 251 Expect(spaceGUID).To(Equal("space")) 252 253 Expect(fakeNetworkingClient.ListPoliciesCallCount()).To(Equal(1)) 254 Expect(fakeNetworkingClient.ListPoliciesArgsForCall(0)).To(Equal([]string{"appAGUID"})) 255 256 Expect(fakeCloudControllerClient.GetApplicationsCallCount()).To(Equal(1)) 257 Expect(fakeCloudControllerClient.GetApplicationsArgsForCall(0)).To(Equal([]ccv3.Query{ 258 {Key: ccv3.GUIDFilter, Values: []string{"appBGUID", "appCGUID"}}, 259 })) 260 261 Expect(fakeCloudControllerClient.GetSpacesCallCount()).To(Equal(1)) 262 Expect(fakeCloudControllerClient.GetSpacesArgsForCall(0)).To(Equal([]ccv3.Query{ 263 {Key: ccv3.GUIDFilter, Values: []string{"spaceAGUID", "spaceCGUID"}}, 264 })) 265 }) 266 }) 267 268 When("getting the applications by name and space fails", func() { 269 BeforeEach(func() { 270 fakeCloudControllerClient.GetApplicationByNameAndSpaceReturns(resources.Application{}, []string{"GetApplicationsBySpaceWarning"}, errors.New("banana")) 271 }) 272 273 It("returns a sensible error", func() { 274 Expect(policies).To(Equal([]Policy{})) 275 Expect(warnings).To(ContainElement("GetApplicationsBySpaceWarning")) 276 Expect(executeErr).To(MatchError("banana")) 277 }) 278 }) 279 280 When("listing the policy fails", func() { 281 BeforeEach(func() { 282 fakeNetworkingClient.ListPoliciesReturns([]cfnetv1.Policy{}, errors.New("apple")) 283 }) 284 It("returns a sensible error", func() { 285 Expect(executeErr).To(MatchError("apple")) 286 }) 287 }) 288 289 When("getting the applications by guids fails", func() { 290 BeforeEach(func() { 291 fakeCloudControllerClient.GetApplicationsReturns([]resources.Application{}, []string{"GetApplicationsWarning"}, errors.New("banana")) 292 }) 293 294 It("returns a sensible error", func() { 295 Expect(policies).To(Equal([]Policy{})) 296 Expect(warnings).To(ContainElement("GetApplicationsWarning")) 297 Expect(executeErr).To(MatchError("banana")) 298 }) 299 }) 300 301 When("getting the spaces by guids fails", func() { 302 BeforeEach(func() { 303 fakeCloudControllerClient.GetSpacesReturns([]resources.Space{}, ccv3.IncludedResources{}, []string{"GetSpacesWarning"}, errors.New("banana")) 304 }) 305 306 It("returns a sensible error", func() { 307 Expect(policies).To(Equal([]Policy{})) 308 Expect(warnings).To(ContainElement("GetSpacesWarning")) 309 Expect(executeErr).To(MatchError("banana")) 310 }) 311 }) 312 }) 313 314 Describe("NetworkPoliciesBySpace", func() { 315 Describe("when the number of destination apps is small", func() { 316 var ( 317 policies []Policy 318 ) 319 320 BeforeEach(func() { 321 fakeNetworkingClient.ListPoliciesReturns([]cfnetv1.Policy{ 322 { 323 Source: cfnetv1.PolicySource{ 324 ID: "appAGUID", 325 }, 326 Destination: cfnetv1.PolicyDestination{ 327 ID: "appBGUID", 328 Protocol: "tcp", 329 Ports: cfnetv1.Ports{ 330 Start: 8080, 331 End: 8080, 332 }, 333 }, 334 }, 335 { 336 Source: cfnetv1.PolicySource{ 337 ID: "appBGUID", 338 }, 339 Destination: cfnetv1.PolicyDestination{ 340 ID: "appBGUID", 341 Protocol: "tcp", 342 Ports: cfnetv1.Ports{ 343 Start: 8080, 344 End: 8080, 345 }, 346 }, 347 }, 348 { 349 Source: cfnetv1.PolicySource{ 350 ID: "appAGUID", 351 }, 352 Destination: cfnetv1.PolicyDestination{ 353 ID: "appCGUID", 354 Protocol: "tcp", 355 Ports: cfnetv1.Ports{ 356 Start: 8080, 357 End: 8080, 358 }, 359 }, 360 }, 361 }, nil) 362 363 fakeCloudControllerClient.GetApplicationsReturnsOnCall(0, []resources.Application{ 364 { 365 Name: "appA", 366 GUID: "appAGUID", 367 SpaceGUID: "spaceAGUID", 368 }, 369 { 370 Name: "appB", 371 GUID: "appBGUID", 372 SpaceGUID: "spaceAGUID", 373 }, 374 { 375 Name: "appC", 376 GUID: "appCGUID", 377 SpaceGUID: "spaceCGUID", 378 }, 379 }, []string{"filter-apps-by-space-warning"}, nil) 380 381 fakeCloudControllerClient.GetApplicationsReturnsOnCall(1, []resources.Application{ 382 { 383 GUID: "appBGUID", 384 Name: "appB", 385 SpaceGUID: "spaceAGUID", 386 }, 387 { 388 GUID: "appCGUID", 389 Name: "appC", 390 SpaceGUID: "spaceCGUID", 391 }, 392 }, []string{"filter-apps-by-guid-warning"}, nil) 393 394 fakeCloudControllerClient.GetSpacesReturns([]resources.Space{ 395 { 396 GUID: "spaceAGUID", 397 Name: "spaceA", 398 Relationships: map[constant.RelationshipType]resources.Relationship{ 399 constant.RelationshipTypeOrganization: {GUID: "orgAGUID"}, 400 }, 401 }, 402 { 403 GUID: "spaceCGUID", 404 Name: "spaceC", 405 Relationships: map[constant.RelationshipType]resources.Relationship{ 406 constant.RelationshipTypeOrganization: {GUID: "orgCGUID"}, 407 }, 408 }, 409 }, ccv3.IncludedResources{}, []string{"GetSpaceWarning"}, nil) 410 411 fakeCloudControllerClient.GetOrganizationsReturns([]resources.Organization{ 412 { 413 GUID: "orgAGUID", 414 Name: "orgA", 415 }, 416 { 417 GUID: "orgCGUID", 418 Name: "orgC", 419 }, 420 }, []string{"GetOrganizationsWarning"}, nil) 421 }) 422 423 JustBeforeEach(func() { 424 spaceGuid := "space" 425 policies, warnings, executeErr = actor.NetworkPoliciesBySpace(spaceGuid) 426 }) 427 428 It("lists policies", func() { 429 Expect(policies).To(Equal( 430 []Policy{{ 431 SourceName: "appA", 432 DestinationName: "appB", 433 Protocol: "tcp", 434 StartPort: 8080, 435 EndPort: 8080, 436 DestinationSpaceName: "spaceA", 437 DestinationOrgName: "orgA", 438 }, { 439 SourceName: "appB", 440 DestinationName: "appB", 441 Protocol: "tcp", 442 StartPort: 8080, 443 EndPort: 8080, 444 DestinationSpaceName: "spaceA", 445 DestinationOrgName: "orgA", 446 }, { 447 SourceName: "appA", 448 DestinationName: "appC", 449 Protocol: "tcp", 450 StartPort: 8080, 451 EndPort: 8080, 452 DestinationSpaceName: "spaceC", 453 DestinationOrgName: "orgC", 454 }}, 455 )) 456 Expect(warnings).To(ConsistOf( 457 "filter-apps-by-space-warning", 458 "filter-apps-by-guid-warning", 459 "GetSpaceWarning", 460 "GetOrganizationsWarning", 461 )) 462 Expect(executeErr).NotTo(HaveOccurred()) 463 464 Expect(fakeNetworkingClient.ListPoliciesCallCount()).To(Equal(1)) 465 Expect(fakeNetworkingClient.ListPoliciesArgsForCall(0)).To(ConsistOf("appAGUID", "appBGUID", "appCGUID")) 466 467 Expect(fakeCloudControllerClient.GetApplicationsCallCount()).To(Equal(2)) 468 Expect(fakeCloudControllerClient.GetApplicationsArgsForCall(0)).To(Equal([]ccv3.Query{ 469 {Key: ccv3.SpaceGUIDFilter, Values: []string{"space"}}, 470 })) 471 Expect(fakeCloudControllerClient.GetApplicationsArgsForCall(1)).To(Equal([]ccv3.Query{ 472 {Key: ccv3.GUIDFilter, Values: []string{"appBGUID", "appCGUID"}}, 473 })) 474 475 Expect(fakeCloudControllerClient.GetSpacesCallCount()).To(Equal(1)) 476 Expect(fakeCloudControllerClient.GetSpacesArgsForCall(0)).To(Equal([]ccv3.Query{ 477 {Key: ccv3.GUIDFilter, Values: []string{"spaceAGUID", "spaceCGUID"}}, 478 })) 479 480 Expect(fakeCloudControllerClient.GetOrganizationsCallCount()).To(Equal(1)) 481 Expect(fakeCloudControllerClient.GetOrganizationsArgsForCall(0)).To(Equal([]ccv3.Query{ 482 {Key: ccv3.GUIDFilter, Values: []string{"orgAGUID", "orgCGUID"}}, 483 })) 484 }) 485 486 // policy server returns policies that match the give app guid in the source or destination 487 // we only care about the policies that match the source guid. 488 When("the policy server returns policies that have matching destination app guids", func() { 489 BeforeEach(func() { 490 fakeNetworkingClient.ListPoliciesReturns([]cfnetv1.Policy{{ 491 Source: cfnetv1.PolicySource{ 492 ID: "appDGUID", 493 }, 494 Destination: cfnetv1.PolicyDestination{ 495 ID: "appAGUID", 496 Protocol: "tcp", 497 Ports: cfnetv1.Ports{ 498 Start: 8080, 499 End: 8080, 500 }, 501 }, 502 }}, nil) 503 }) 504 505 It("filters them out ", func() { 506 Expect(policies).To(BeEmpty()) 507 }) 508 }) 509 510 When("getting the applications with a space guids filter fails", func() { 511 BeforeEach(func() { 512 fakeCloudControllerClient.GetApplicationsReturnsOnCall(0, []resources.Application{}, []string{"filter-apps-by-space-warning"}, errors.New("banana")) 513 }) 514 515 It("returns a sensible error", func() { 516 Expect(policies).To(Equal([]Policy{})) 517 Expect(warnings).To(ConsistOf("filter-apps-by-space-warning")) 518 Expect(executeErr).To(MatchError("banana")) 519 }) 520 }) 521 522 When("listing the policy fails", func() { 523 BeforeEach(func() { 524 fakeNetworkingClient.ListPoliciesReturns([]cfnetv1.Policy{}, errors.New("apple")) 525 }) 526 It("returns a sensible error", func() { 527 Expect(executeErr).To(MatchError("apple")) 528 }) 529 }) 530 531 When("getting the applications by guids fails", func() { 532 BeforeEach(func() { 533 fakeCloudControllerClient.GetApplicationsReturnsOnCall(1, []resources.Application{}, []string{"filter-apps-by-guid-warning"}, errors.New("banana")) 534 }) 535 536 It("returns a sensible error", func() { 537 Expect(policies).To(Equal([]Policy{})) 538 Expect(warnings).To(ContainElement("filter-apps-by-guid-warning")) 539 Expect(executeErr).To(MatchError("banana")) 540 }) 541 }) 542 543 When("getting the spaces by guids fails", func() { 544 BeforeEach(func() { 545 fakeCloudControllerClient.GetSpacesReturns([]resources.Space{}, ccv3.IncludedResources{}, []string{"GetSpacesWarning"}, errors.New("banana")) 546 }) 547 548 It("returns a sensible error", func() { 549 Expect(policies).To(Equal([]Policy{})) 550 Expect(warnings).To(ContainElement("GetSpacesWarning")) 551 Expect(executeErr).To(MatchError("banana")) 552 }) 553 }) 554 }) 555 556 Describe("when the number of destination apps is larger than the batch size", func() { 557 var ( 558 expectedPolicies []Policy 559 returnedPolicies []Policy 560 ) 561 562 BeforeEach(func() { 563 var ( 564 policies []cfnetv1.Policy 565 srcApps []resources.Application 566 destApps []resources.Application 567 ) 568 569 for i := 0; i < batcher.BatchSize*2; i++ { 570 srcApp := resources.Application{ 571 Name: fmt.Sprintf("src-app-%d", i), 572 GUID: fmt.Sprintf("src-app-%d-guid", i), 573 SpaceGUID: "space-guid", 574 } 575 576 destApp := resources.Application{ 577 Name: fmt.Sprintf("dest-app-%d", i), 578 GUID: fmt.Sprintf("dest-app-%d-guid", i), 579 SpaceGUID: "space-guid", 580 } 581 582 policy := cfnetv1.Policy{ 583 Source: cfnetv1.PolicySource{ 584 ID: srcApp.GUID, 585 }, 586 Destination: cfnetv1.PolicyDestination{ 587 ID: destApp.GUID, 588 Protocol: "tcp", 589 Ports: cfnetv1.Ports{ 590 Start: 8080, 591 End: 8080, 592 }, 593 }, 594 } 595 596 expectedPolicy := Policy{ 597 SourceName: srcApp.Name, 598 DestinationName: destApp.Name, 599 Protocol: "tcp", 600 StartPort: 8080, 601 EndPort: 8080, 602 DestinationSpaceName: "space", 603 DestinationOrgName: "org", 604 } 605 606 srcApps = append(srcApps, srcApp) 607 destApps = append(destApps, destApp) 608 policies = append(policies, policy) 609 expectedPolicies = append(expectedPolicies, expectedPolicy) 610 } 611 612 fakeNetworkingClient.ListPoliciesReturnsOnCall(0, policies[0:batcher.BatchSize], nil) 613 fakeNetworkingClient.ListPoliciesReturnsOnCall(1, policies[batcher.BatchSize:batcher.BatchSize*2], nil) 614 615 fakeCloudControllerClient.GetApplicationsReturnsOnCall(0, srcApps, []string{}, nil) 616 fakeCloudControllerClient.GetApplicationsReturnsOnCall(1, destApps[0:batcher.BatchSize], []string{}, nil) 617 fakeCloudControllerClient.GetApplicationsReturnsOnCall(2, destApps[batcher.BatchSize:batcher.BatchSize*2], []string{}, nil) 618 619 fakeCloudControllerClient.GetSpacesReturns([]resources.Space{ 620 { 621 GUID: "space-guid", 622 Name: "space", 623 Relationships: map[constant.RelationshipType]resources.Relationship{ 624 constant.RelationshipTypeOrganization: {GUID: "org-guid"}, 625 }, 626 }, 627 }, ccv3.IncludedResources{}, []string{}, nil) 628 629 fakeCloudControllerClient.GetOrganizationsReturns([]resources.Organization{ 630 { 631 GUID: "org-guid", 632 Name: "org", 633 }, 634 }, []string{}, nil) 635 }) 636 637 JustBeforeEach(func() { 638 spaceGuid := "space-guid" 639 returnedPolicies, warnings, executeErr = actor.NetworkPoliciesBySpace(spaceGuid) 640 }) 641 642 It("lists policies", func() { 643 Expect(returnedPolicies).To(Equal(expectedPolicies)) 644 Expect(warnings).To(BeEmpty()) 645 Expect(executeErr).NotTo(HaveOccurred()) 646 647 Expect(fakeNetworkingClient.ListPoliciesCallCount()).To(Equal(2)) 648 Expect(fakeCloudControllerClient.GetApplicationsCallCount()).To(Equal(3)) 649 Expect(fakeCloudControllerClient.GetSpacesCallCount()).To(Equal(1)) 650 Expect(fakeCloudControllerClient.GetOrganizationsCallCount()).To(Equal(1)) 651 }) 652 653 }) 654 }) 655 656 Describe("RemoveNetworkPolicy", func() { 657 BeforeEach(func() { 658 fakeNetworkingClient.ListPoliciesReturns([]cfnetv1.Policy{ 659 { 660 Source: cfnetv1.PolicySource{ 661 ID: "appAGUID", 662 }, 663 Destination: cfnetv1.PolicyDestination{ 664 ID: "appBGUID", 665 Protocol: "udp", 666 Ports: cfnetv1.Ports{ 667 Start: 123, 668 End: 345, 669 }, 670 }, 671 }, 672 }, nil) 673 }) 674 675 JustBeforeEach(func() { 676 srcSpaceGuid := "spaceA" 677 srcApp := "appA" 678 destSpaceGuid := "spaceB" 679 destApp := "appB" 680 protocol := "udp" 681 startPort := 123 682 endPort := 345 683 warnings, executeErr = actor.RemoveNetworkPolicy(srcSpaceGuid, srcApp, destSpaceGuid, destApp, protocol, startPort, endPort) 684 }) 685 It("removes policies", func() { 686 Expect(warnings).To(ConsistOf("v3ActorWarningA", "v3ActorWarningB")) 687 Expect(executeErr).NotTo(HaveOccurred()) 688 689 Expect(fakeCloudControllerClient.GetApplicationByNameAndSpaceCallCount()).To(Equal(2)) 690 sourceAppName, spaceGUID := fakeCloudControllerClient.GetApplicationByNameAndSpaceArgsForCall(0) 691 Expect(sourceAppName).To(Equal("appA")) 692 Expect(spaceGUID).To(Equal("spaceA")) 693 694 destAppName, spaceGUID := fakeCloudControllerClient.GetApplicationByNameAndSpaceArgsForCall(1) 695 Expect(destAppName).To(Equal("appB")) 696 Expect(spaceGUID).To(Equal("spaceB")) 697 698 Expect(fakeNetworkingClient.ListPoliciesCallCount()).To(Equal(1)) 699 700 Expect(fakeNetworkingClient.RemovePoliciesCallCount()).To(Equal(1)) 701 Expect(fakeNetworkingClient.RemovePoliciesArgsForCall(0)).To(Equal([]cfnetv1.Policy{ 702 { 703 Source: cfnetv1.PolicySource{ 704 ID: "appAGUID", 705 }, 706 Destination: cfnetv1.PolicyDestination{ 707 ID: "appBGUID", 708 Protocol: "udp", 709 Ports: cfnetv1.Ports{ 710 Start: 123, 711 End: 345, 712 }, 713 }, 714 }, 715 })) 716 }) 717 718 When("the policy does not exist", func() { 719 BeforeEach(func() { 720 fakeNetworkingClient.ListPoliciesReturns([]cfnetv1.Policy{}, nil) 721 }) 722 723 It("returns an error", func() { 724 Expect(warnings).To(ConsistOf("v3ActorWarningA", "v3ActorWarningB")) 725 Expect(executeErr).To(MatchError(actionerror.PolicyDoesNotExistError{})) 726 }) 727 }) 728 729 When("getting the source app fails ", func() { 730 BeforeEach(func() { 731 fakeCloudControllerClient.GetApplicationByNameAndSpaceReturns(resources.Application{}, []string{"v3ActorWarningA"}, errors.New("banana")) 732 }) 733 It("returns a sensible error", func() { 734 Expect(warnings).To(ConsistOf("v3ActorWarningA")) 735 Expect(executeErr).To(MatchError("banana")) 736 }) 737 }) 738 739 When("getting the destination app fails ", func() { 740 BeforeEach(func() { 741 fakeCloudControllerClient.GetApplicationByNameAndSpaceReturnsOnCall(0, resources.Application{}, []string{"v3ActorWarningA"}, nil) 742 fakeCloudControllerClient.GetApplicationByNameAndSpaceReturnsOnCall(1, resources.Application{}, []string{"v3ActorWarningB"}, errors.New("banana")) 743 }) 744 745 It("returns a sensible error", func() { 746 Expect(warnings).To(ConsistOf("v3ActorWarningA", "v3ActorWarningB")) 747 Expect(executeErr).To(MatchError("banana")) 748 }) 749 }) 750 751 When("listing policies fails", func() { 752 BeforeEach(func() { 753 fakeNetworkingClient.ListPoliciesReturns([]cfnetv1.Policy{}, errors.New("apple")) 754 }) 755 It("returns a sensible error", func() { 756 Expect(executeErr).To(MatchError("apple")) 757 }) 758 }) 759 760 When("removing the policy fails", func() { 761 BeforeEach(func() { 762 fakeNetworkingClient.RemovePoliciesReturns(errors.New("apple")) 763 }) 764 It("returns a sensible error", func() { 765 Expect(executeErr).To(MatchError("apple")) 766 }) 767 }) 768 }) 769 })