code.cloudfoundry.org/cli@v7.1.0+incompatible/command/v7/bind_security_group_command_test.go (about) 1 package v7_test 2 3 import ( 4 "errors" 5 6 "code.cloudfoundry.org/cli/actor/actionerror" 7 "code.cloudfoundry.org/cli/actor/v7action" 8 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant" 9 "code.cloudfoundry.org/cli/command/commandfakes" 10 "code.cloudfoundry.org/cli/command/flag" 11 . "code.cloudfoundry.org/cli/command/v7" 12 "code.cloudfoundry.org/cli/command/v7/v7fakes" 13 "code.cloudfoundry.org/cli/resources" 14 "code.cloudfoundry.org/cli/util/configv3" 15 "code.cloudfoundry.org/cli/util/ui" 16 . "github.com/onsi/ginkgo" 17 . "github.com/onsi/gomega" 18 . "github.com/onsi/gomega/gbytes" 19 ) 20 21 var _ = Describe("bind-security-group Command", func() { 22 var ( 23 cmd BindSecurityGroupCommand 24 testUI *ui.UI 25 fakeConfig *commandfakes.FakeConfig 26 fakeSharedActor *commandfakes.FakeSharedActor 27 fakeActor *v7fakes.FakeActor 28 binaryName string 29 executeErr error 30 getSecurityGroupWarning v7action.Warnings 31 ) 32 33 BeforeEach(func() { 34 testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer()) 35 fakeConfig = new(commandfakes.FakeConfig) 36 fakeSharedActor = new(commandfakes.FakeSharedActor) 37 fakeActor = new(v7fakes.FakeActor) 38 getSecurityGroupWarning = v7action.Warnings{"get security group warning"} 39 40 cmd = BindSecurityGroupCommand{ 41 BaseCommand: BaseCommand{ 42 UI: testUI, 43 Config: fakeConfig, 44 SharedActor: fakeSharedActor, 45 Actor: fakeActor, 46 }, 47 } 48 49 binaryName = "faceman" 50 fakeConfig.BinaryNameReturns(binaryName) 51 52 // Stubs for the happy path. 53 cmd.RequiredArgs.SecurityGroupName = "some-security-group" 54 cmd.RequiredArgs.OrganizationName = "some-org" 55 56 fakeConfig.CurrentUserReturns( 57 configv3.User{Name: "some-user"}, 58 nil) 59 fakeActor.GetSecurityGroupReturns( 60 resources.SecurityGroup{Name: "some-security-group", GUID: "some-security-group-guid"}, 61 getSecurityGroupWarning, 62 nil) 63 fakeActor.GetOrganizationByNameReturns( 64 resources.Organization{Name: "some-org", GUID: "some-org-guid"}, 65 v7action.Warnings{"get org warning"}, 66 nil) 67 }) 68 69 JustBeforeEach(func() { 70 executeErr = cmd.Execute(nil) 71 }) 72 73 When("lifecycle is 'running'", func() { 74 BeforeEach(func() { 75 cmd.Lifecycle = flag.SecurityGroupLifecycle(constant.SecurityGroupLifecycleRunning) 76 }) 77 78 When("checking target fails", func() { 79 BeforeEach(func() { 80 fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName}) 81 }) 82 83 It("returns an error", func() { 84 Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: "faceman"})) 85 86 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 87 checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0) 88 Expect(checkTargetedOrg).To(BeFalse()) 89 Expect(checkTargetedSpace).To(BeFalse()) 90 }) 91 }) 92 93 When("getting the current user returns an error", func() { 94 var expectedErr error 95 96 BeforeEach(func() { 97 expectedErr = errors.New("getting current user error") 98 fakeConfig.CurrentUserReturns( 99 configv3.User{}, 100 expectedErr) 101 }) 102 103 It("returns the error", func() { 104 Expect(executeErr).To(MatchError(expectedErr)) 105 }) 106 }) 107 108 It("Retrieves the security group information", func() { 109 Expect(fakeActor.GetSecurityGroupCallCount()).To(Equal(1)) 110 securityGroupName := fakeActor.GetSecurityGroupArgsForCall(0) 111 Expect(securityGroupName).To(Equal(cmd.RequiredArgs.SecurityGroupName)) 112 }) 113 114 It("prints the warnings", func() { 115 Expect(testUI.Err).To(Say(getSecurityGroupWarning[0])) 116 }) 117 118 When("an error is encountered getting the provided security group", func() { 119 var expectedErr error 120 121 BeforeEach(func() { 122 expectedErr = errors.New("get security group error") 123 fakeActor.GetSecurityGroupReturns( 124 resources.SecurityGroup{}, 125 v7action.Warnings{"get security group warning"}, 126 expectedErr) 127 }) 128 129 It("returns the error and displays all warnings", func() { 130 Expect(executeErr).To(MatchError(expectedErr)) 131 }) 132 }) 133 134 When("the provided org does not exist", func() { 135 BeforeEach(func() { 136 fakeActor.GetOrganizationByNameReturns( 137 resources.Organization{}, 138 v7action.Warnings{"get organization warning"}, 139 actionerror.OrganizationNotFoundError{Name: "some-org"}) 140 }) 141 142 It("returns an OrganizationNotFoundError and displays all warnings", func() { 143 Expect(executeErr).To(MatchError(actionerror.OrganizationNotFoundError{Name: "some-org"})) 144 Expect(testUI.Err).To(Say("get security group warning")) 145 Expect(testUI.Err).To(Say("get organization warning")) 146 }) 147 }) 148 149 When("an error is encountered getting the provided org", func() { 150 var expectedErr error 151 152 BeforeEach(func() { 153 expectedErr = errors.New("get org error") 154 fakeActor.GetOrganizationByNameReturns( 155 resources.Organization{}, 156 v7action.Warnings{"get org warning"}, 157 expectedErr) 158 }) 159 160 It("returns the error and displays all warnings", func() { 161 Expect(executeErr).To(MatchError(expectedErr)) 162 Expect(testUI.Err).To(Say("get security group warning")) 163 Expect(testUI.Err).To(Say("get org warning")) 164 }) 165 }) 166 167 When("a space is provided", func() { 168 BeforeEach(func() { 169 cmd.Space = "some-space" 170 }) 171 172 When("the space does not exist", func() { 173 BeforeEach(func() { 174 fakeActor.GetSpaceByNameAndOrganizationReturns( 175 resources.Space{}, 176 v7action.Warnings{"get space warning"}, 177 actionerror.SpaceNotFoundError{Name: "some-space"}) 178 }) 179 180 It("returns a SpaceNotFoundError", func() { 181 Expect(executeErr).To(MatchError(actionerror.SpaceNotFoundError{Name: "some-space"})) 182 Expect(testUI.Out).To(Say(`Assigning running security group some-security-group to space some-space in org some-org as some-user\.\.\.`)) 183 Expect(testUI.Err).To(Say("get security group warning")) 184 Expect(testUI.Err).To(Say("get org warning")) 185 Expect(testUI.Err).To(Say("get space warning")) 186 }) 187 }) 188 189 When("the space exists", func() { 190 BeforeEach(func() { 191 fakeActor.GetSpaceByNameAndOrganizationReturns( 192 resources.Space{ 193 GUID: "some-space-guid", 194 Name: "some-space", 195 }, 196 v7action.Warnings{"get space by org warning"}, 197 nil) 198 }) 199 200 When("no errors are encountered binding the security group to the space", func() { 201 BeforeEach(func() { 202 fakeActor.BindSecurityGroupToSpacesReturns( 203 v7action.Warnings{"bind security group to space warning"}, 204 nil) 205 }) 206 207 It("binds the security group to the space and displays all warnings", func() { 208 Expect(executeErr).NotTo(HaveOccurred()) 209 210 Expect(testUI.Out).To(Say(`Assigning running security group some-security-group to space some-space in org some-org as some-user\.\.\.`)) 211 // When space is provided, Assigning statement should only be printed once 212 Expect(testUI.Out).NotTo(Say(`Assigning running security group some-security-group to space some-space in org some-org as some-user\.\.\.`)) 213 Expect(testUI.Out).To(Say("OK")) 214 Expect(testUI.Out).To(Say(`TIP: Changes require an app restart \(for running\) or restage \(for staging\) to apply to existing applications\.`)) 215 216 Expect(testUI.Err).To(Say("get security group warning")) 217 Expect(testUI.Err).To(Say("get org warning")) 218 Expect(testUI.Err).To(Say("get space by org warning")) 219 Expect(testUI.Err).To(Say("bind security group to space warning")) 220 221 Expect(fakeActor.CloudControllerAPIVersionCallCount()).To(Equal(0)) 222 223 Expect(fakeActor.GetSecurityGroupCallCount()).To(Equal(1)) 224 Expect(fakeActor.GetSecurityGroupArgsForCall(0)).To(Equal("some-security-group")) 225 226 Expect(fakeActor.GetOrganizationByNameCallCount()).To(Equal(1)) 227 Expect(fakeActor.GetOrganizationByNameArgsForCall(0)).To(Equal("some-org")) 228 229 Expect(fakeActor.GetSpaceByNameAndOrganizationCallCount()).To(Equal(1)) 230 spaceName, orgGUID := fakeActor.GetSpaceByNameAndOrganizationArgsForCall(0) 231 Expect(orgGUID).To(Equal("some-org-guid")) 232 Expect(spaceName).To(Equal("some-space")) 233 234 Expect(fakeActor.BindSecurityGroupToSpacesCallCount()).To(Equal(1)) 235 securityGroupGUID, space, lifecycle := fakeActor.BindSecurityGroupToSpacesArgsForCall(0) 236 Expect(securityGroupGUID).To(Equal("some-security-group-guid")) 237 Expect(space[0].GUID).To(Equal("some-space-guid")) 238 Expect(lifecycle).To(Equal(constant.SecurityGroupLifecycleRunning)) 239 }) 240 }) 241 242 When("an error is encountered binding the security group to the space", func() { 243 var expectedErr error 244 245 BeforeEach(func() { 246 expectedErr = errors.New("bind error") 247 fakeActor.BindSecurityGroupToSpacesReturns( 248 v7action.Warnings{"bind security group to space warning"}, 249 expectedErr) 250 }) 251 252 It("returns the error and displays all warnings", func() { 253 Expect(executeErr).To(MatchError(expectedErr)) 254 255 Expect(testUI.Out).NotTo(Say("OK")) 256 257 Expect(testUI.Err).To(Say("get security group warning")) 258 Expect(testUI.Err).To(Say("get org warning")) 259 Expect(testUI.Err).To(Say("get space by org warning")) 260 Expect(testUI.Err).To(Say("bind security group to space warning")) 261 }) 262 }) 263 }) 264 }) 265 266 When("a space is not provided", func() { 267 When("there are no spaces in the org", func() { 268 BeforeEach(func() { 269 fakeActor.GetOrganizationSpacesReturns( 270 []resources.Space{}, 271 v7action.Warnings{"get org spaces warning"}, 272 nil) 273 }) 274 275 It("does not perform any bindings and displays all warnings", func() { 276 Expect(executeErr).NotTo(HaveOccurred()) 277 278 Expect(testUI.Out).To(Say("Assigning running security group %s to all spaces in org %s as some-user...", cmd.RequiredArgs.SecurityGroupName, cmd.RequiredArgs.OrganizationName)) 279 Expect(testUI.Out).To(Say("No spaces in org %s.", cmd.RequiredArgs.OrganizationName)) 280 Expect(testUI.Out).NotTo(Say("OK")) 281 Expect(testUI.Out).NotTo(Say(`TIP: Changes require an app restart \(for running\) or restage \(for staging\) to apply to existing applications\.`)) //TODO Why? 282 283 Expect(testUI.Err).To(Say("get security group warning")) 284 Expect(testUI.Err).To(Say("get org warning")) 285 Expect(testUI.Err).To(Say("get org spaces warning")) 286 }) 287 }) 288 289 When("there are spaces in the org", func() { 290 BeforeEach(func() { 291 fakeActor.GetOrganizationSpacesReturns( 292 []resources.Space{ 293 { 294 GUID: "some-space-guid-1", 295 Name: "some-space-1", 296 }, 297 { 298 GUID: "some-space-guid-2", 299 Name: "some-space-2", 300 }, 301 }, 302 v7action.Warnings{"get org spaces warning"}, 303 nil) 304 }) 305 306 When("no errors are encountered binding the security group to the spaces", func() { 307 BeforeEach(func() { 308 fakeActor.BindSecurityGroupToSpacesReturns( 309 v7action.Warnings{"bind security group to space warning 1"}, 310 nil) 311 }) 312 313 It("binds the security group to each space and displays all warnings", func() { 314 Expect(executeErr).NotTo(HaveOccurred()) 315 316 Expect(testUI.Out).To(Say(`Assigning running security group some-security-group to spaces some-space-1, some-space-2 in org some-org as some-user\.\.\.`)) 317 Expect(testUI.Out).To(Say("OK")) 318 Expect(testUI.Out).To(Say(`TIP: Changes require an app restart \(for running\) or restage \(for staging\) to apply to existing applications\.`)) 319 320 Expect(testUI.Err).To(Say("get security group warning")) 321 Expect(testUI.Err).To(Say("get org warning")) 322 Expect(testUI.Err).To(Say("get org spaces warning")) 323 Expect(testUI.Err).To(Say("bind security group to space warning 1")) 324 325 Expect(fakeActor.GetSecurityGroupCallCount()).To(Equal(1)) 326 Expect(fakeActor.GetSecurityGroupArgsForCall(0)).To(Equal("some-security-group")) 327 328 Expect(fakeActor.GetOrganizationByNameCallCount()).To(Equal(1)) 329 Expect(fakeActor.GetOrganizationByNameArgsForCall(0)).To(Equal("some-org")) 330 331 securityGroupGUID, spaceGUIDs, lifecycle := fakeActor.BindSecurityGroupToSpacesArgsForCall(0) 332 Expect(securityGroupGUID).To(Equal("some-security-group-guid")) 333 Expect(spaceGUIDs[0].GUID).To(Equal("some-space-guid-1")) 334 Expect(lifecycle).To(Equal(constant.SecurityGroupLifecycleRunning)) 335 }) 336 }) 337 338 When("an error is encountered binding the security group to a space", func() { 339 var expectedErr error 340 341 BeforeEach(func() { 342 expectedErr = errors.New("bind security group to space error") 343 fakeActor.BindSecurityGroupToSpacesReturns( 344 v7action.Warnings{"bind security group to space warning"}, 345 expectedErr) 346 }) 347 348 It("returns the error and displays all warnings", func() { 349 Expect(executeErr).To(MatchError(expectedErr)) 350 351 Expect(testUI.Out).NotTo(Say("OK")) 352 353 Expect(testUI.Err).To(Say("get security group warning")) 354 Expect(testUI.Err).To(Say("get org warning")) 355 Expect(testUI.Err).To(Say("get org spaces warning")) 356 Expect(testUI.Err).To(Say("bind security group to space warning")) 357 }) 358 }) 359 }) 360 361 When("an error is encountered getting spaces in the org", func() { 362 var expectedErr error 363 364 BeforeEach(func() { 365 expectedErr = errors.New("get org spaces error") 366 fakeActor.GetOrganizationSpacesReturns( 367 nil, 368 v7action.Warnings{"get org spaces warning"}, 369 expectedErr) 370 }) 371 372 It("returns the error and displays all warnings", func() { 373 Expect(executeErr).To(MatchError(expectedErr)) 374 Expect(testUI.Err).To(Say("get security group warning")) 375 Expect(testUI.Err).To(Say("get org warning")) 376 Expect(testUI.Err).To(Say("get org spaces warning")) 377 }) 378 }) 379 }) 380 }) 381 382 When("lifecycle is 'staging'", func() { 383 BeforeEach(func() { 384 cmd.Lifecycle = "staging" 385 }) 386 387 When("a space is provided", func() { 388 BeforeEach(func() { 389 cmd.Space = "some-space" 390 }) 391 392 When("the space exists", func() { 393 BeforeEach(func() { 394 fakeActor.GetSpaceByNameAndOrganizationReturns( 395 resources.Space{ 396 GUID: "some-space-guid", 397 Name: "some-space", 398 }, 399 v7action.Warnings{"get space by org warning"}, 400 nil) 401 }) 402 403 When("no errors are encountered binding the security group to the space", func() { 404 BeforeEach(func() { 405 fakeActor.BindSecurityGroupToSpacesReturns( 406 v7action.Warnings{"bind security group to space warning"}, 407 nil) 408 }) 409 410 It("binds the security group to the space and displays all warnings", func() { 411 Expect(executeErr).NotTo(HaveOccurred()) 412 413 Expect(testUI.Out).To(Say(`Assigning staging security group some-security-group to space some-space in org some-org as some-user\.\.\.`)) 414 Expect(testUI.Out).To(Say("OK")) 415 Expect(testUI.Out).To(Say(`TIP: Changes require an app restart \(for running\) or restage \(for staging\) to apply to existing applications\.`)) 416 417 Expect(testUI.Err).To(Say("get security group warning")) 418 Expect(testUI.Err).To(Say("get org warning")) 419 Expect(testUI.Err).To(Say("get space by org warning")) 420 Expect(testUI.Err).To(Say("bind security group to space warning")) 421 422 Expect(fakeActor.CloudControllerAPIVersionCallCount()).To(Equal(0)) 423 Expect(fakeActor.GetSecurityGroupCallCount()).To(Equal(1)) 424 Expect(fakeActor.GetSecurityGroupArgsForCall(0)).To(Equal("some-security-group")) 425 426 Expect(fakeActor.GetOrganizationByNameCallCount()).To(Equal(1)) 427 Expect(fakeActor.GetOrganizationByNameArgsForCall(0)).To(Equal("some-org")) 428 429 Expect(fakeActor.GetSpaceByNameAndOrganizationCallCount()).To(Equal(1)) 430 spaceName, orgGUID := fakeActor.GetSpaceByNameAndOrganizationArgsForCall(0) 431 Expect(orgGUID).To(Equal("some-org-guid")) 432 Expect(spaceName).To(Equal("some-space")) 433 434 Expect(fakeActor.BindSecurityGroupToSpacesCallCount()).To(Equal(1)) 435 securityGroupGUID, space, lifecycle := fakeActor.BindSecurityGroupToSpacesArgsForCall(0) 436 Expect(securityGroupGUID).To(Equal("some-security-group-guid")) 437 Expect(space[0].GUID).To(Equal("some-space-guid")) 438 Expect(lifecycle).To(Equal(constant.SecurityGroupLifecycleStaging)) 439 }) 440 }) 441 }) 442 443 When("the space does not exist", func() { 444 BeforeEach(func() { 445 fakeActor.GetSpaceByNameAndOrganizationReturns( 446 resources.Space{}, 447 v7action.Warnings{}, 448 actionerror.SpaceNotFoundError{Name: "some-space"}, 449 ) 450 }) 451 452 It("error and displays all warnings after displaying the flavor text", func() { 453 Expect(executeErr).To(MatchError(actionerror.SpaceNotFoundError{Name: "some-space"})) 454 Expect(testUI.Out).To(Say(`Assigning staging security group some-security-group to space some-space in org some-org as some-user\.\.\.`)) 455 }) 456 }) 457 }) 458 459 When("a space is not provided", func() { 460 When("there are spaces in the org", func() { 461 BeforeEach(func() { 462 fakeActor.GetOrganizationSpacesReturns( 463 []resources.Space{ 464 { 465 GUID: "some-space-guid-1", 466 Name: "some-space-1", 467 }, 468 { 469 GUID: "some-space-guid-2", 470 Name: "some-space-2", 471 }, 472 }, 473 v7action.Warnings{"get org spaces warning"}, 474 nil) 475 }) 476 477 When("no errors are encountered binding the security group to the spaces", func() { 478 BeforeEach(func() { 479 fakeActor.BindSecurityGroupToSpacesReturnsOnCall( 480 0, 481 v7action.Warnings{"bind security group to space warning 1"}, 482 nil) 483 }) 484 485 It("binds the security group to each space and displays all warnings", func() { 486 Expect(executeErr).NotTo(HaveOccurred()) 487 488 Expect(testUI.Out).To(Say(`Assigning staging security group some-security-group to spaces some-space-1, some-space-2 in org some-org as some-user\.\.\.`)) 489 Expect(testUI.Out).To(Say("OK")) 490 Expect(testUI.Out).To(Say(`TIP: Changes require an app restart \(for running\) or restage \(for staging\) to apply to existing applications\.`)) 491 492 Expect(testUI.Err).To(Say("get security group warning")) 493 Expect(testUI.Err).To(Say("get org warning")) 494 Expect(testUI.Err).To(Say("get org spaces warning")) 495 Expect(testUI.Err).To(Say("bind security group to space warning 1")) 496 497 Expect(fakeActor.GetSecurityGroupCallCount()).To(Equal(1)) 498 Expect(fakeActor.GetSecurityGroupArgsForCall(0)).To(Equal("some-security-group")) 499 500 Expect(fakeActor.GetOrganizationByNameCallCount()).To(Equal(1)) 501 Expect(fakeActor.GetOrganizationByNameArgsForCall(0)).To(Equal("some-org")) 502 503 Expect(fakeActor.BindSecurityGroupToSpacesCallCount()).To(Equal(1)) 504 securityGroupGUID, spaceGUIDs, lifecycle := fakeActor.BindSecurityGroupToSpacesArgsForCall(0) 505 Expect(securityGroupGUID).To(Equal("some-security-group-guid")) 506 Expect(spaceGUIDs[0].GUID).To(Equal("some-space-guid-1")) 507 Expect(lifecycle).To(Equal(constant.SecurityGroupLifecycleStaging)) 508 }) 509 }) 510 511 When("an error is encountered binding the security group to a space", func() { 512 var expectedErr error 513 514 BeforeEach(func() { 515 expectedErr = errors.New("bind security group to space error") 516 fakeActor.BindSecurityGroupToSpacesReturns( 517 v7action.Warnings{"bind security group to space warning"}, 518 expectedErr) 519 }) 520 521 It("returns the error and displays all warnings", func() { 522 Expect(executeErr).To(MatchError(expectedErr)) 523 524 Expect(testUI.Out).NotTo(Say("OK")) 525 526 Expect(testUI.Err).To(Say("get security group warning")) 527 Expect(testUI.Err).To(Say("get org warning")) 528 Expect(testUI.Err).To(Say("get org spaces warning")) 529 Expect(testUI.Err).To(Say("bind security group to space warning")) 530 }) 531 }) 532 }) 533 534 When("an error is encountered getting spaces in the org", func() { 535 var expectedErr error 536 537 BeforeEach(func() { 538 expectedErr = errors.New("get org spaces error") 539 fakeActor.GetOrganizationSpacesReturns( 540 nil, 541 v7action.Warnings{"get org spaces warning"}, 542 expectedErr) 543 }) 544 545 It("returns the error and displays all warnings", func() { 546 Expect(executeErr).To(MatchError(expectedErr)) 547 Expect(testUI.Err).To(Say("get security group warning")) 548 Expect(testUI.Err).To(Say("get org warning")) 549 Expect(testUI.Err).To(Say("get org spaces warning")) 550 }) 551 }) 552 }) 553 }) 554 })