github.com/LukasHeimann/cloudfoundrycli/v8@v8.4.4/command/v7/bind_security_group_command_test.go (about) 1 package v7_test 2 3 import ( 4 "errors" 5 6 "github.com/LukasHeimann/cloudfoundrycli/v8/actor/actionerror" 7 "github.com/LukasHeimann/cloudfoundrycli/v8/actor/v7action" 8 "github.com/LukasHeimann/cloudfoundrycli/v8/api/cloudcontroller/ccv3/constant" 9 "github.com/LukasHeimann/cloudfoundrycli/v8/command/commandfakes" 10 "github.com/LukasHeimann/cloudfoundrycli/v8/command/flag" 11 . "github.com/LukasHeimann/cloudfoundrycli/v8/command/v7" 12 "github.com/LukasHeimann/cloudfoundrycli/v8/command/v7/v7fakes" 13 "github.com/LukasHeimann/cloudfoundrycli/v8/resources" 14 "github.com/LukasHeimann/cloudfoundrycli/v8/util/configv3" 15 "github.com/LukasHeimann/cloudfoundrycli/v8/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 fakeActor.GetCurrentUserReturns( 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 fakeActor.GetCurrentUserReturns( 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: If Dynamic ASG's are enabled, changes will automatically apply for running and staging applications. Otherwise, changes will 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.GetSecurityGroupCallCount()).To(Equal(1)) 222 Expect(fakeActor.GetSecurityGroupArgsForCall(0)).To(Equal("some-security-group")) 223 224 Expect(fakeActor.GetOrganizationByNameCallCount()).To(Equal(1)) 225 Expect(fakeActor.GetOrganizationByNameArgsForCall(0)).To(Equal("some-org")) 226 227 Expect(fakeActor.GetSpaceByNameAndOrganizationCallCount()).To(Equal(1)) 228 spaceName, orgGUID := fakeActor.GetSpaceByNameAndOrganizationArgsForCall(0) 229 Expect(orgGUID).To(Equal("some-org-guid")) 230 Expect(spaceName).To(Equal("some-space")) 231 232 Expect(fakeActor.BindSecurityGroupToSpacesCallCount()).To(Equal(1)) 233 securityGroupGUID, space, lifecycle := fakeActor.BindSecurityGroupToSpacesArgsForCall(0) 234 Expect(securityGroupGUID).To(Equal("some-security-group-guid")) 235 Expect(space[0].GUID).To(Equal("some-space-guid")) 236 Expect(lifecycle).To(Equal(constant.SecurityGroupLifecycleRunning)) 237 }) 238 }) 239 240 When("an error is encountered binding the security group to the space", func() { 241 var expectedErr error 242 243 BeforeEach(func() { 244 expectedErr = errors.New("bind error") 245 fakeActor.BindSecurityGroupToSpacesReturns( 246 v7action.Warnings{"bind security group to space warning"}, 247 expectedErr) 248 }) 249 250 It("returns the error and displays all warnings", func() { 251 Expect(executeErr).To(MatchError(expectedErr)) 252 253 Expect(testUI.Out).NotTo(Say("OK")) 254 255 Expect(testUI.Err).To(Say("get security group warning")) 256 Expect(testUI.Err).To(Say("get org warning")) 257 Expect(testUI.Err).To(Say("get space by org warning")) 258 Expect(testUI.Err).To(Say("bind security group to space warning")) 259 }) 260 }) 261 }) 262 }) 263 264 When("a space is not provided", func() { 265 When("there are no spaces in the org", func() { 266 BeforeEach(func() { 267 fakeActor.GetOrganizationSpacesReturns( 268 []resources.Space{}, 269 v7action.Warnings{"get org spaces warning"}, 270 nil) 271 }) 272 273 It("does not perform any bindings and displays all warnings", func() { 274 Expect(executeErr).NotTo(HaveOccurred()) 275 276 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)) 277 Expect(testUI.Out).To(Say("No spaces in org %s.", cmd.RequiredArgs.OrganizationName)) 278 Expect(testUI.Out).NotTo(Say("OK")) 279 Expect(testUI.Out).NotTo(Say(`TIP: If Dynamic ASG's are enabled, changes will automatically apply for running and staging applications. Otherwise, changes will require an app restart \(for running\) or restage \(for staging\) to apply to existing applications\.`)) //TODO Why? 280 281 Expect(testUI.Err).To(Say("get security group warning")) 282 Expect(testUI.Err).To(Say("get org warning")) 283 Expect(testUI.Err).To(Say("get org spaces warning")) 284 }) 285 }) 286 287 When("there are spaces in the org", func() { 288 BeforeEach(func() { 289 fakeActor.GetOrganizationSpacesReturns( 290 []resources.Space{ 291 { 292 GUID: "some-space-guid-1", 293 Name: "some-space-1", 294 }, 295 { 296 GUID: "some-space-guid-2", 297 Name: "some-space-2", 298 }, 299 }, 300 v7action.Warnings{"get org spaces warning"}, 301 nil) 302 }) 303 304 When("no errors are encountered binding the security group to the spaces", func() { 305 BeforeEach(func() { 306 fakeActor.BindSecurityGroupToSpacesReturns( 307 v7action.Warnings{"bind security group to space warning 1"}, 308 nil) 309 }) 310 311 It("binds the security group to each space and displays all warnings", func() { 312 Expect(executeErr).NotTo(HaveOccurred()) 313 314 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\.\.\.`)) 315 Expect(testUI.Out).To(Say("OK")) 316 Expect(testUI.Out).To(Say(`TIP: If Dynamic ASG's are enabled, changes will automatically apply for running and staging applications. Otherwise, changes will require an app restart \(for running\) or restage \(for staging\) to apply to existing applications\.`)) 317 318 Expect(testUI.Err).To(Say("get security group warning")) 319 Expect(testUI.Err).To(Say("get org warning")) 320 Expect(testUI.Err).To(Say("get org spaces warning")) 321 Expect(testUI.Err).To(Say("bind security group to space warning 1")) 322 323 Expect(fakeActor.GetSecurityGroupCallCount()).To(Equal(1)) 324 Expect(fakeActor.GetSecurityGroupArgsForCall(0)).To(Equal("some-security-group")) 325 326 Expect(fakeActor.GetOrganizationByNameCallCount()).To(Equal(1)) 327 Expect(fakeActor.GetOrganizationByNameArgsForCall(0)).To(Equal("some-org")) 328 329 securityGroupGUID, spaceGUIDs, lifecycle := fakeActor.BindSecurityGroupToSpacesArgsForCall(0) 330 Expect(securityGroupGUID).To(Equal("some-security-group-guid")) 331 Expect(spaceGUIDs[0].GUID).To(Equal("some-space-guid-1")) 332 Expect(lifecycle).To(Equal(constant.SecurityGroupLifecycleRunning)) 333 }) 334 }) 335 336 When("an error is encountered binding the security group to a space", func() { 337 var expectedErr error 338 339 BeforeEach(func() { 340 expectedErr = errors.New("bind security group to space error") 341 fakeActor.BindSecurityGroupToSpacesReturns( 342 v7action.Warnings{"bind security group to space warning"}, 343 expectedErr) 344 }) 345 346 It("returns the error and displays all warnings", func() { 347 Expect(executeErr).To(MatchError(expectedErr)) 348 349 Expect(testUI.Out).NotTo(Say("OK")) 350 351 Expect(testUI.Err).To(Say("get security group warning")) 352 Expect(testUI.Err).To(Say("get org warning")) 353 Expect(testUI.Err).To(Say("get org spaces warning")) 354 Expect(testUI.Err).To(Say("bind security group to space warning")) 355 }) 356 }) 357 }) 358 359 When("an error is encountered getting spaces in the org", func() { 360 var expectedErr error 361 362 BeforeEach(func() { 363 expectedErr = errors.New("get org spaces error") 364 fakeActor.GetOrganizationSpacesReturns( 365 nil, 366 v7action.Warnings{"get org spaces warning"}, 367 expectedErr) 368 }) 369 370 It("returns the error and displays all warnings", func() { 371 Expect(executeErr).To(MatchError(expectedErr)) 372 Expect(testUI.Err).To(Say("get security group warning")) 373 Expect(testUI.Err).To(Say("get org warning")) 374 Expect(testUI.Err).To(Say("get org spaces warning")) 375 }) 376 }) 377 }) 378 }) 379 380 When("lifecycle is 'staging'", func() { 381 BeforeEach(func() { 382 cmd.Lifecycle = "staging" 383 }) 384 385 When("a space is provided", func() { 386 BeforeEach(func() { 387 cmd.Space = "some-space" 388 }) 389 390 When("the space exists", func() { 391 BeforeEach(func() { 392 fakeActor.GetSpaceByNameAndOrganizationReturns( 393 resources.Space{ 394 GUID: "some-space-guid", 395 Name: "some-space", 396 }, 397 v7action.Warnings{"get space by org warning"}, 398 nil) 399 }) 400 401 When("no errors are encountered binding the security group to the space", func() { 402 BeforeEach(func() { 403 fakeActor.BindSecurityGroupToSpacesReturns( 404 v7action.Warnings{"bind security group to space warning"}, 405 nil) 406 }) 407 408 It("binds the security group to the space and displays all warnings", func() { 409 Expect(executeErr).NotTo(HaveOccurred()) 410 411 Expect(testUI.Out).To(Say(`Assigning staging security group some-security-group to space some-space in org some-org as some-user\.\.\.`)) 412 Expect(testUI.Out).To(Say("OK")) 413 Expect(testUI.Out).To(Say(`TIP: If Dynamic ASG's are enabled, changes will automatically apply for running and staging applications. Otherwise, changes will require an app restart \(for running\) or restage \(for staging\) to apply to existing applications\.`)) 414 415 Expect(testUI.Err).To(Say("get security group warning")) 416 Expect(testUI.Err).To(Say("get org warning")) 417 Expect(testUI.Err).To(Say("get space by org warning")) 418 Expect(testUI.Err).To(Say("bind security group to space warning")) 419 420 Expect(fakeActor.GetSecurityGroupCallCount()).To(Equal(1)) 421 Expect(fakeActor.GetSecurityGroupArgsForCall(0)).To(Equal("some-security-group")) 422 423 Expect(fakeActor.GetOrganizationByNameCallCount()).To(Equal(1)) 424 Expect(fakeActor.GetOrganizationByNameArgsForCall(0)).To(Equal("some-org")) 425 426 Expect(fakeActor.GetSpaceByNameAndOrganizationCallCount()).To(Equal(1)) 427 spaceName, orgGUID := fakeActor.GetSpaceByNameAndOrganizationArgsForCall(0) 428 Expect(orgGUID).To(Equal("some-org-guid")) 429 Expect(spaceName).To(Equal("some-space")) 430 431 Expect(fakeActor.BindSecurityGroupToSpacesCallCount()).To(Equal(1)) 432 securityGroupGUID, space, lifecycle := fakeActor.BindSecurityGroupToSpacesArgsForCall(0) 433 Expect(securityGroupGUID).To(Equal("some-security-group-guid")) 434 Expect(space[0].GUID).To(Equal("some-space-guid")) 435 Expect(lifecycle).To(Equal(constant.SecurityGroupLifecycleStaging)) 436 }) 437 }) 438 }) 439 440 When("the space does not exist", func() { 441 BeforeEach(func() { 442 fakeActor.GetSpaceByNameAndOrganizationReturns( 443 resources.Space{}, 444 v7action.Warnings{}, 445 actionerror.SpaceNotFoundError{Name: "some-space"}, 446 ) 447 }) 448 449 It("error and displays all warnings after displaying the flavor text", func() { 450 Expect(executeErr).To(MatchError(actionerror.SpaceNotFoundError{Name: "some-space"})) 451 Expect(testUI.Out).To(Say(`Assigning staging security group some-security-group to space some-space in org some-org as some-user\.\.\.`)) 452 }) 453 }) 454 }) 455 456 When("a space is not provided", func() { 457 When("there are spaces in the org", func() { 458 BeforeEach(func() { 459 fakeActor.GetOrganizationSpacesReturns( 460 []resources.Space{ 461 { 462 GUID: "some-space-guid-1", 463 Name: "some-space-1", 464 }, 465 { 466 GUID: "some-space-guid-2", 467 Name: "some-space-2", 468 }, 469 }, 470 v7action.Warnings{"get org spaces warning"}, 471 nil) 472 }) 473 474 When("no errors are encountered binding the security group to the spaces", func() { 475 BeforeEach(func() { 476 fakeActor.BindSecurityGroupToSpacesReturnsOnCall( 477 0, 478 v7action.Warnings{"bind security group to space warning 1"}, 479 nil) 480 }) 481 482 It("binds the security group to each space and displays all warnings", func() { 483 Expect(executeErr).NotTo(HaveOccurred()) 484 485 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\.\.\.`)) 486 Expect(testUI.Out).To(Say("OK")) 487 Expect(testUI.Out).To(Say(`TIP: If Dynamic ASG's are enabled, changes will automatically apply for running and staging applications. Otherwise, changes will require an app restart \(for running\) or restage \(for staging\) to apply to existing applications\.`)) 488 489 Expect(testUI.Err).To(Say("get security group warning")) 490 Expect(testUI.Err).To(Say("get org warning")) 491 Expect(testUI.Err).To(Say("get org spaces warning")) 492 Expect(testUI.Err).To(Say("bind security group to space warning 1")) 493 494 Expect(fakeActor.GetSecurityGroupCallCount()).To(Equal(1)) 495 Expect(fakeActor.GetSecurityGroupArgsForCall(0)).To(Equal("some-security-group")) 496 497 Expect(fakeActor.GetOrganizationByNameCallCount()).To(Equal(1)) 498 Expect(fakeActor.GetOrganizationByNameArgsForCall(0)).To(Equal("some-org")) 499 500 Expect(fakeActor.BindSecurityGroupToSpacesCallCount()).To(Equal(1)) 501 securityGroupGUID, spaceGUIDs, lifecycle := fakeActor.BindSecurityGroupToSpacesArgsForCall(0) 502 Expect(securityGroupGUID).To(Equal("some-security-group-guid")) 503 Expect(spaceGUIDs[0].GUID).To(Equal("some-space-guid-1")) 504 Expect(lifecycle).To(Equal(constant.SecurityGroupLifecycleStaging)) 505 }) 506 }) 507 508 When("an error is encountered binding the security group to a space", func() { 509 var expectedErr error 510 511 BeforeEach(func() { 512 expectedErr = errors.New("bind security group to space error") 513 fakeActor.BindSecurityGroupToSpacesReturns( 514 v7action.Warnings{"bind security group to space warning"}, 515 expectedErr) 516 }) 517 518 It("returns the error and displays all warnings", func() { 519 Expect(executeErr).To(MatchError(expectedErr)) 520 521 Expect(testUI.Out).NotTo(Say("OK")) 522 523 Expect(testUI.Err).To(Say("get security group warning")) 524 Expect(testUI.Err).To(Say("get org warning")) 525 Expect(testUI.Err).To(Say("get org spaces warning")) 526 Expect(testUI.Err).To(Say("bind security group to space warning")) 527 }) 528 }) 529 }) 530 531 When("an error is encountered getting spaces in the org", func() { 532 var expectedErr error 533 534 BeforeEach(func() { 535 expectedErr = errors.New("get org spaces error") 536 fakeActor.GetOrganizationSpacesReturns( 537 nil, 538 v7action.Warnings{"get org spaces warning"}, 539 expectedErr) 540 }) 541 542 It("returns the error and displays all warnings", func() { 543 Expect(executeErr).To(MatchError(expectedErr)) 544 Expect(testUI.Err).To(Say("get security group warning")) 545 Expect(testUI.Err).To(Say("get org warning")) 546 Expect(testUI.Err).To(Say("get org spaces warning")) 547 }) 548 }) 549 }) 550 }) 551 })