github.com/randomtask1155/cli@v6.41.1-0.20181227003417-a98eed78cbde+incompatible/actor/cfnetworkingaction/policy_test.go (about) 1 package cfnetworkingaction_test 2 3 import ( 4 "errors" 5 6 "code.cloudfoundry.org/cfnetworking-cli-api/cfnetworking/cfnetv1" 7 "code.cloudfoundry.org/cli/actor/actionerror" 8 . "code.cloudfoundry.org/cli/actor/cfnetworkingaction" 9 "code.cloudfoundry.org/cli/actor/cfnetworkingaction/cfnetworkingactionfakes" 10 "code.cloudfoundry.org/cli/actor/v3action" 11 . "github.com/onsi/ginkgo" 12 . "github.com/onsi/gomega" 13 ) 14 15 var _ = Describe("Policy", func() { 16 var ( 17 actor *Actor 18 fakeV3Actor *cfnetworkingactionfakes.FakeV3Actor 19 fakeNetworkingClient *cfnetworkingactionfakes.FakeNetworkingClient 20 21 warnings Warnings 22 executeErr error 23 ) 24 25 BeforeEach(func() { 26 fakeV3Actor = new(cfnetworkingactionfakes.FakeV3Actor) 27 fakeNetworkingClient = new(cfnetworkingactionfakes.FakeNetworkingClient) 28 29 fakeV3Actor.GetApplicationByNameAndSpaceStub = func(appName string, spaceGUID string) (v3action.Application, v3action.Warnings, error) { 30 if appName == "appA" { 31 return v3action.Application{GUID: "appAGUID"}, []string{"v3ActorWarningA"}, nil 32 } else if appName == "appB" { 33 return v3action.Application{GUID: "appBGUID"}, []string{"v3ActorWarningB"}, nil 34 } 35 return v3action.Application{}, nil, nil 36 } 37 38 actor = NewActor(fakeNetworkingClient, fakeV3Actor) 39 }) 40 41 Describe("AddNetworkPolicy", func() { 42 JustBeforeEach(func() { 43 srcSpaceGuid := "src-space" 44 srcApp := "appA" 45 destSpaceGuid := "dst-space" 46 destApp := "appB" 47 protocol := "tcp" 48 startPort := 8080 49 endPort := 8090 50 warnings, executeErr = actor.AddNetworkPolicy(srcSpaceGuid, srcApp, destSpaceGuid, destApp, protocol, startPort, endPort) 51 }) 52 53 It("creates policies", func() { 54 Expect(warnings).To(ConsistOf("v3ActorWarningA", "v3ActorWarningB")) 55 Expect(executeErr).NotTo(HaveOccurred()) 56 57 Expect(fakeV3Actor.GetApplicationByNameAndSpaceCallCount()).To(Equal(2)) 58 sourceAppName, srcSpaceGUID := fakeV3Actor.GetApplicationByNameAndSpaceArgsForCall(0) 59 Expect(sourceAppName).To(Equal("appA")) 60 Expect(srcSpaceGUID).To(Equal("src-space")) 61 62 destAppName, destSpaceGUID := fakeV3Actor.GetApplicationByNameAndSpaceArgsForCall(1) 63 Expect(destAppName).To(Equal("appB")) 64 Expect(destSpaceGUID).To(Equal("dst-space")) 65 66 Expect(fakeNetworkingClient.CreatePoliciesCallCount()).To(Equal(1)) 67 Expect(fakeNetworkingClient.CreatePoliciesArgsForCall(0)).To(Equal([]cfnetv1.Policy{ 68 { 69 Source: cfnetv1.PolicySource{ 70 ID: "appAGUID", 71 }, 72 Destination: cfnetv1.PolicyDestination{ 73 ID: "appBGUID", 74 Protocol: "tcp", 75 Ports: cfnetv1.Ports{ 76 Start: 8080, 77 End: 8090, 78 }, 79 }, 80 }, 81 })) 82 }) 83 84 When("getting the source app fails ", func() { 85 BeforeEach(func() { 86 fakeV3Actor.GetApplicationByNameAndSpaceReturns(v3action.Application{}, []string{"v3ActorWarningA"}, errors.New("banana")) 87 }) 88 It("returns a sensible error", func() { 89 Expect(warnings).To(ConsistOf("v3ActorWarningA")) 90 Expect(executeErr).To(MatchError("banana")) 91 }) 92 }) 93 94 When("getting the destination app fails ", func() { 95 BeforeEach(func() { 96 fakeV3Actor.GetApplicationByNameAndSpaceStub = func(appName string, spaceGUID string) (v3action.Application, v3action.Warnings, error) { 97 if appName == "appB" { 98 return v3action.Application{}, []string{"v3ActorWarningB"}, errors.New("banana") 99 } 100 return v3action.Application{}, []string{"v3ActorWarningA"}, nil 101 } 102 }) 103 It("returns a sensible error", func() { 104 Expect(warnings).To(ConsistOf("v3ActorWarningA", "v3ActorWarningB")) 105 Expect(executeErr).To(MatchError("banana")) 106 }) 107 }) 108 109 When("creating the policy fails", func() { 110 BeforeEach(func() { 111 fakeNetworkingClient.CreatePoliciesReturns(errors.New("apple")) 112 }) 113 It("returns a sensible error", func() { 114 Expect(executeErr).To(MatchError("apple")) 115 }) 116 }) 117 }) 118 119 Describe("NetworkPoliciesBySpaceAndAppName", func() { 120 var ( 121 policies []Policy 122 srcApp string 123 ) 124 125 BeforeEach(func() { 126 fakeNetworkingClient.ListPoliciesReturns([]cfnetv1.Policy{{ 127 Source: cfnetv1.PolicySource{ 128 ID: "appAGUID", 129 }, 130 Destination: cfnetv1.PolicyDestination{ 131 ID: "appBGUID", 132 Protocol: "tcp", 133 Ports: cfnetv1.Ports{ 134 Start: 8080, 135 End: 8080, 136 }, 137 }, 138 }, { 139 Source: cfnetv1.PolicySource{ 140 ID: "appBGUID", 141 }, 142 Destination: cfnetv1.PolicyDestination{ 143 ID: "appBGUID", 144 Protocol: "tcp", 145 Ports: cfnetv1.Ports{ 146 Start: 8080, 147 End: 8080, 148 }, 149 }, 150 }, { 151 Source: cfnetv1.PolicySource{ 152 ID: "appCGUID", 153 }, 154 Destination: cfnetv1.PolicyDestination{ 155 ID: "appCGUID", 156 Protocol: "tcp", 157 Ports: cfnetv1.Ports{ 158 Start: 8080, 159 End: 8080, 160 }, 161 }, 162 }}, nil) 163 164 fakeV3Actor.GetApplicationsBySpaceStub = func(_ string) ([]v3action.Application, v3action.Warnings, error) { 165 return []v3action.Application{ 166 { 167 Name: "appA", 168 GUID: "appAGUID", 169 }, 170 { 171 Name: "appB", 172 GUID: "appBGUID", 173 }, 174 }, []string{"GetApplicationsBySpaceWarning"}, nil 175 } 176 177 }) 178 179 JustBeforeEach(func() { 180 srcSpaceGuid := "space" 181 policies, warnings, executeErr = actor.NetworkPoliciesBySpaceAndAppName(srcSpaceGuid, srcApp) 182 }) 183 184 When("listing policies based on a source app", func() { 185 BeforeEach(func() { 186 srcApp = "appA" 187 }) 188 189 It("lists only policies for which the app is a source", func() { 190 Expect(policies).To(Equal( 191 []Policy{{ 192 SourceName: "appA", 193 DestinationName: "appB", 194 Protocol: "tcp", 195 StartPort: 8080, 196 EndPort: 8080, 197 }}, 198 )) 199 }) 200 201 It("passes through the source app argument", func() { 202 Expect(warnings).To(ConsistOf("GetApplicationsBySpaceWarning", "v3ActorWarningA")) 203 Expect(executeErr).NotTo(HaveOccurred()) 204 205 Expect(fakeV3Actor.GetApplicationByNameAndSpaceCallCount()).To(Equal(1)) 206 sourceAppName, spaceGUID := fakeV3Actor.GetApplicationByNameAndSpaceArgsForCall(0) 207 Expect(sourceAppName).To(Equal("appA")) 208 Expect(spaceGUID).To(Equal("space")) 209 210 Expect(fakeNetworkingClient.ListPoliciesCallCount()).To(Equal(1)) 211 Expect(fakeNetworkingClient.ListPoliciesArgsForCall(0)).To(Equal([]string{"appAGUID"})) 212 }) 213 }) 214 215 When("getting the applications fails", func() { 216 BeforeEach(func() { 217 fakeV3Actor.GetApplicationsBySpaceReturns([]v3action.Application{}, []string{"GetApplicationsBySpaceWarning"}, errors.New("banana")) 218 }) 219 220 It("returns a sensible error", func() { 221 Expect(policies).To(Equal([]Policy{})) 222 Expect(warnings).To(ConsistOf("GetApplicationsBySpaceWarning")) 223 Expect(executeErr).To(MatchError("banana")) 224 }) 225 }) 226 227 When("getting the source app fails ", func() { 228 BeforeEach(func() { 229 fakeV3Actor.GetApplicationByNameAndSpaceStub = func(appName string, spaceGUID string) (v3action.Application, v3action.Warnings, error) { 230 if appName == "appA" { 231 return v3action.Application{}, []string{"v3ActorWarningA"}, errors.New("banana") 232 } 233 return v3action.Application{}, []string{"v3ActorWarningB"}, nil 234 } 235 236 srcApp = "appA" 237 }) 238 239 It("returns a sensible error", func() { 240 Expect(policies).To(Equal([]Policy{})) 241 Expect(warnings).To(ConsistOf("GetApplicationsBySpaceWarning", "v3ActorWarningA")) 242 Expect(executeErr).To(MatchError("banana")) 243 }) 244 }) 245 246 When("listing the policy fails", func() { 247 BeforeEach(func() { 248 fakeNetworkingClient.ListPoliciesReturns([]cfnetv1.Policy{}, errors.New("apple")) 249 }) 250 It("returns a sensible error", func() { 251 Expect(executeErr).To(MatchError("apple")) 252 }) 253 }) 254 }) 255 256 Describe("NetworkPoliciesBySpace", func() { 257 var ( 258 policies []Policy 259 ) 260 261 BeforeEach(func() { 262 fakeNetworkingClient.ListPoliciesReturns([]cfnetv1.Policy{{ 263 Source: cfnetv1.PolicySource{ 264 ID: "appAGUID", 265 }, 266 Destination: cfnetv1.PolicyDestination{ 267 ID: "appBGUID", 268 Protocol: "tcp", 269 Ports: cfnetv1.Ports{ 270 Start: 8080, 271 End: 8080, 272 }, 273 }, 274 }, { 275 Source: cfnetv1.PolicySource{ 276 ID: "appBGUID", 277 }, 278 Destination: cfnetv1.PolicyDestination{ 279 ID: "appBGUID", 280 Protocol: "tcp", 281 Ports: cfnetv1.Ports{ 282 Start: 8080, 283 End: 8080, 284 }, 285 }, 286 }, { 287 Source: cfnetv1.PolicySource{ 288 ID: "appCGUID", 289 }, 290 Destination: cfnetv1.PolicyDestination{ 291 ID: "appCGUID", 292 Protocol: "tcp", 293 Ports: cfnetv1.Ports{ 294 Start: 8080, 295 End: 8080, 296 }, 297 }, 298 }}, nil) 299 300 fakeV3Actor.GetApplicationsBySpaceStub = func(_ string) ([]v3action.Application, v3action.Warnings, error) { 301 return []v3action.Application{ 302 { 303 Name: "appA", 304 GUID: "appAGUID", 305 }, 306 { 307 Name: "appB", 308 GUID: "appBGUID", 309 }, 310 }, []string{"GetApplicationsBySpaceWarning"}, nil 311 } 312 313 }) 314 315 JustBeforeEach(func() { 316 spaceGuid := "space" 317 policies, warnings, executeErr = actor.NetworkPoliciesBySpace(spaceGuid) 318 }) 319 320 It("lists policies", func() { 321 Expect(policies).To(Equal( 322 []Policy{{ 323 SourceName: "appA", 324 DestinationName: "appB", 325 Protocol: "tcp", 326 StartPort: 8080, 327 EndPort: 8080, 328 }, { 329 SourceName: "appB", 330 DestinationName: "appB", 331 Protocol: "tcp", 332 StartPort: 8080, 333 EndPort: 8080, 334 }}, 335 )) 336 Expect(warnings).To(ConsistOf("GetApplicationsBySpaceWarning")) 337 Expect(executeErr).NotTo(HaveOccurred()) 338 339 Expect(fakeV3Actor.GetApplicationsBySpaceCallCount()).To(Equal(1)) 340 Expect(fakeV3Actor.GetApplicationByNameAndSpaceCallCount()).To(Equal(0)) 341 342 Expect(fakeNetworkingClient.ListPoliciesCallCount()).To(Equal(1)) 343 Expect(fakeNetworkingClient.ListPoliciesArgsForCall(0)).To(BeNil()) 344 }) 345 346 When("getting the applications fails", func() { 347 BeforeEach(func() { 348 fakeV3Actor.GetApplicationsBySpaceReturns([]v3action.Application{}, []string{"GetApplicationsBySpaceWarning"}, errors.New("banana")) 349 }) 350 351 It("returns a sensible error", func() { 352 Expect(policies).To(Equal([]Policy{})) 353 Expect(warnings).To(ConsistOf("GetApplicationsBySpaceWarning")) 354 Expect(executeErr).To(MatchError("banana")) 355 }) 356 }) 357 358 When("listing the policy fails", func() { 359 BeforeEach(func() { 360 fakeNetworkingClient.ListPoliciesReturns([]cfnetv1.Policy{}, errors.New("apple")) 361 }) 362 It("returns a sensible error", func() { 363 Expect(executeErr).To(MatchError("apple")) 364 }) 365 }) 366 }) 367 368 Describe("RemoveNetworkPolicy", func() { 369 BeforeEach(func() { 370 fakeNetworkingClient.ListPoliciesReturns([]cfnetv1.Policy{ 371 { 372 Source: cfnetv1.PolicySource{ 373 ID: "appAGUID", 374 }, 375 Destination: cfnetv1.PolicyDestination{ 376 ID: "appBGUID", 377 Protocol: "udp", 378 Ports: cfnetv1.Ports{ 379 Start: 123, 380 End: 345, 381 }, 382 }, 383 }, 384 }, nil) 385 }) 386 387 JustBeforeEach(func() { 388 srcSpaceGuid := "spaceA" 389 srcApp := "appA" 390 destSpaceGuid := "spaceB" 391 destApp := "appB" 392 protocol := "udp" 393 startPort := 123 394 endPort := 345 395 warnings, executeErr = actor.RemoveNetworkPolicy(srcSpaceGuid, srcApp, destSpaceGuid, destApp, protocol, startPort, endPort) 396 }) 397 It("removes policies", func() { 398 Expect(warnings).To(ConsistOf("v3ActorWarningA", "v3ActorWarningB")) 399 Expect(executeErr).NotTo(HaveOccurred()) 400 401 Expect(fakeV3Actor.GetApplicationByNameAndSpaceCallCount()).To(Equal(2)) 402 sourceAppName, spaceGUID := fakeV3Actor.GetApplicationByNameAndSpaceArgsForCall(0) 403 Expect(sourceAppName).To(Equal("appA")) 404 Expect(spaceGUID).To(Equal("spaceA")) 405 406 destAppName, spaceGUID := fakeV3Actor.GetApplicationByNameAndSpaceArgsForCall(1) 407 Expect(destAppName).To(Equal("appB")) 408 Expect(spaceGUID).To(Equal("spaceB")) 409 410 Expect(fakeNetworkingClient.ListPoliciesCallCount()).To(Equal(1)) 411 412 Expect(fakeNetworkingClient.RemovePoliciesCallCount()).To(Equal(1)) 413 Expect(fakeNetworkingClient.RemovePoliciesArgsForCall(0)).To(Equal([]cfnetv1.Policy{ 414 { 415 Source: cfnetv1.PolicySource{ 416 ID: "appAGUID", 417 }, 418 Destination: cfnetv1.PolicyDestination{ 419 ID: "appBGUID", 420 Protocol: "udp", 421 Ports: cfnetv1.Ports{ 422 Start: 123, 423 End: 345, 424 }, 425 }, 426 }, 427 })) 428 }) 429 430 When("the policy does not exist", func() { 431 BeforeEach(func() { 432 fakeNetworkingClient.ListPoliciesReturns([]cfnetv1.Policy{}, nil) 433 }) 434 435 It("returns an error", func() { 436 Expect(warnings).To(ConsistOf("v3ActorWarningA", "v3ActorWarningB")) 437 Expect(executeErr).To(MatchError(actionerror.PolicyDoesNotExistError{})) 438 }) 439 }) 440 441 When("getting the source app fails ", func() { 442 BeforeEach(func() { 443 fakeV3Actor.GetApplicationByNameAndSpaceReturns(v3action.Application{}, []string{"v3ActorWarningA"}, errors.New("banana")) 444 }) 445 It("returns a sensible error", func() { 446 Expect(warnings).To(ConsistOf("v3ActorWarningA")) 447 Expect(executeErr).To(MatchError("banana")) 448 }) 449 }) 450 451 When("getting the destination app fails ", func() { 452 BeforeEach(func() { 453 fakeV3Actor.GetApplicationByNameAndSpaceReturnsOnCall(0, v3action.Application{}, []string{"v3ActorWarningA"}, nil) 454 fakeV3Actor.GetApplicationByNameAndSpaceReturnsOnCall(1, v3action.Application{}, []string{"v3ActorWarningB"}, errors.New("banana")) 455 }) 456 457 It("returns a sensible error", func() { 458 Expect(warnings).To(ConsistOf("v3ActorWarningA", "v3ActorWarningB")) 459 Expect(executeErr).To(MatchError("banana")) 460 }) 461 }) 462 463 When("listing policies fails", func() { 464 BeforeEach(func() { 465 fakeNetworkingClient.ListPoliciesReturns([]cfnetv1.Policy{}, errors.New("apple")) 466 }) 467 It("returns a sensible error", func() { 468 Expect(executeErr).To(MatchError("apple")) 469 }) 470 }) 471 472 When("removing the policy fails", func() { 473 BeforeEach(func() { 474 fakeNetworkingClient.RemovePoliciesReturns(errors.New("apple")) 475 }) 476 It("returns a sensible error", func() { 477 Expect(executeErr).To(MatchError("apple")) 478 }) 479 }) 480 }) 481 })