code.cloudfoundry.org/cli@v7.1.0+incompatible/actor/cfnetworkingaction/policy_test.go (about)

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