github.com/ablease/cli@v6.37.1-0.20180613014814-3adbb7d7fb19+incompatible/command/v2/bind_security_group_command_test.go (about)

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