github.com/loafoe/cli@v7.1.0+incompatible/command/v7/create_space_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 v7 "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("create-space Command", func() { 22 var ( 23 cmd v7.CreateSpaceCommand 24 testUI *ui.UI 25 fakeConfig *commandfakes.FakeConfig 26 fakeSharedActor *commandfakes.FakeSharedActor 27 fakeActor *v7fakes.FakeActor 28 binaryName string 29 executeErr error 30 31 spaceName string 32 spaceGUID string 33 orgGUID string 34 orgName string 35 userName string 36 quotaName string 37 ) 38 39 BeforeEach(func() { 40 testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer()) 41 fakeConfig = new(commandfakes.FakeConfig) 42 fakeSharedActor = new(commandfakes.FakeSharedActor) 43 fakeActor = new(v7fakes.FakeActor) 44 45 binaryName = "faceman" 46 fakeConfig.BinaryNameReturns(binaryName) 47 spaceName = "some-space" 48 spaceGUID = "some-space-guid" 49 orgGUID = "some-org-guid" 50 orgName = "" 51 quotaName = "" 52 userName = "some-user-name" 53 54 fakeConfig.TargetedOrganizationReturns(configv3.Organization{ 55 Name: "some-org-name", 56 GUID: "some-org-guid", 57 }) 58 fakeConfig.CurrentUserReturns(configv3.User{ 59 Name: userName, 60 Origin: "some-user-origin", 61 }, nil) 62 fakeActor.CreateSpaceReturns(resources.Space{ 63 Name: spaceName, 64 GUID: spaceGUID, 65 }, v7action.Warnings{}, nil) 66 67 }) 68 69 JustBeforeEach(func() { 70 cmd = v7.CreateSpaceCommand{ 71 BaseCommand: v7.BaseCommand{ 72 UI: testUI, 73 Config: fakeConfig, 74 SharedActor: fakeSharedActor, 75 Actor: fakeActor, 76 }, 77 RequiredArgs: flag.Space{Space: spaceName}, 78 Organization: orgName, 79 Quota: quotaName, 80 } 81 82 executeErr = cmd.Execute(nil) 83 }) 84 85 When("the environment is not set up correctly (CheckTarget fails)", func() { 86 BeforeEach(func() { 87 fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName}) 88 }) 89 90 It("returns an error", func() { 91 Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName})) 92 93 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 94 checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0) 95 Expect(checkTargetedOrg).To(BeFalse()) 96 Expect(checkTargetedSpace).To(BeFalse()) 97 }) 98 }) 99 100 When("passing in a organization", func() { 101 When("getting the passed-in organization returns warnings", func() { 102 BeforeEach(func() { 103 orgName = "some-other-org" 104 fakeActor.GetOrganizationByNameReturns( 105 resources.Organization{}, 106 v7action.Warnings{"get-org-warnings"}, 107 nil, 108 ) 109 }) 110 111 It("prints all warnings", func() { 112 Expect(testUI.Err).To(Say("get-org-warnings")) 113 }) 114 }) 115 116 When("the organization exists", func() { 117 BeforeEach(func() { 118 orgName = "some-other-org" 119 fakeActor.GetOrganizationByNameReturns( 120 resources.Organization{Name: "some-other-org", GUID: "some-other-org-guid"}, 121 v7action.Warnings{}, 122 nil, 123 ) 124 }) 125 126 It("does not error", func() { 127 Expect(executeErr).ToNot(HaveOccurred()) 128 }) 129 130 It("gets the org and passes it into the CreateSpaceActor", func() { 131 Expect(fakeActor.GetOrganizationByNameCallCount()).To(Equal(1)) 132 expectedOrgName := fakeActor.GetOrganizationByNameArgsForCall(0) 133 Expect(expectedOrgName).To(Equal(orgName)) 134 135 Expect(fakeActor.CreateSpaceCallCount()).To(Equal(1)) 136 expectedSpaceName, expectedOrgGUID := fakeActor.CreateSpaceArgsForCall(0) 137 Expect(expectedSpaceName).To(Equal(spaceName)) 138 Expect(expectedOrgGUID).To(Equal("some-other-org-guid")) 139 }) 140 }) 141 When("the organization doesn't exist", func() { 142 BeforeEach(func() { 143 orgName = "some-other-org" 144 fakeActor.GetOrganizationByNameReturns( 145 resources.Organization{}, 146 v7action.Warnings{}, 147 errors.New("get-organization-error"), 148 ) 149 }) 150 151 It("returns an error", func() { 152 Expect(executeErr).To(MatchError(errors.New("get-organization-error"))) 153 }) 154 155 It("does not create the space", func() { 156 Expect(fakeActor.GetOrganizationByNameCallCount()).To(Equal(1)) 157 expectedOrgName := fakeActor.GetOrganizationByNameArgsForCall(0) 158 Expect(expectedOrgName).To(Equal(orgName)) 159 160 Expect(fakeActor.CreateSpaceCallCount()).To(Equal(0)) 161 }) 162 }) 163 }) 164 165 It("prints text indicating it is creating a space", func() { 166 Expect(testUI.Out).To(Say(`Creating space %s in org %s as %s\.\.\.`, spaceName, "some-org-name", userName)) 167 }) 168 169 When("creating the space returns warnings", func() { 170 BeforeEach(func() { 171 fakeActor.CreateSpaceReturns( 172 resources.Space{}, 173 v7action.Warnings{"warnings-1", "warnings-2"}, 174 nil, 175 ) 176 }) 177 It("prints the warnings", func() { 178 Expect(testUI.Err).To(Say("warnings-1")) 179 Expect(testUI.Err).To(Say("warnings-2")) 180 }) 181 }) 182 183 When("the space already exists", func() { 184 BeforeEach(func() { 185 fakeActor.CreateSpaceReturns(resources.Space{}, v7action.Warnings{}, actionerror.SpaceAlreadyExistsError{Space: spaceName}) 186 }) 187 188 It("displays that the space already exists, and does not error", func() { 189 Expect(executeErr).ToNot(HaveOccurred()) 190 191 Expect(testUI.Out).To(Say(`Space '%s' already exists\.`, spaceName)) 192 Expect(testUI.Out).To(Say("OK")) 193 }) 194 }) 195 196 When("creating the space errors", func() { 197 BeforeEach(func() { 198 fakeActor.CreateSpaceReturns( 199 resources.Space{}, 200 v7action.Warnings{}, 201 errors.New("err-create-space"), 202 ) 203 }) 204 205 It("returns an error", func() { 206 Expect(executeErr).To(MatchError("err-create-space")) 207 }) 208 }) 209 210 When("passing in a quota", func() { 211 BeforeEach(func() { 212 quotaName = "some-quota" 213 }) 214 215 It("prints text indicating it is applying a quota to the space", func() { 216 Expect(testUI.Out).To(Say(`Setting space quota %s to space %s as %s\.\.\.`, quotaName, spaceName, userName)) 217 }) 218 219 When("setting the quota onto the space returns warnings", func() { 220 BeforeEach(func() { 221 fakeActor.ApplySpaceQuotaByNameReturns( 222 v7action.Warnings{"quota-warnings-1", "quota-warnings-2"}, 223 nil, 224 ) 225 }) 226 227 It("prints the warnings", func() { 228 Expect(testUI.Err).To(Say("quota-warnings-1")) 229 Expect(testUI.Err).To(Say("quota-warnings-2")) 230 }) 231 }) 232 233 When("the quota does not exist", func() { 234 BeforeEach(func() { 235 fakeActor.ApplySpaceQuotaByNameReturns(v7action.Warnings{}, actionerror.SpaceQuotaNotFoundForNameError{Name: quotaName}) 236 }) 237 238 It("returns an error and displays warnings", func() { 239 Expect(executeErr).To(MatchError(actionerror.SpaceQuotaNotFoundForNameError{Name: "some-quota"})) 240 Expect(fakeActor.ApplySpaceQuotaByNameCallCount()).To(Equal(1)) 241 Expect(fakeActor.CreateSpaceRoleCallCount()).To(Equal(0)) 242 }) 243 }) 244 245 When("the quota exists", func() { 246 BeforeEach(func() { 247 fakeActor.ApplySpaceQuotaByNameReturns(v7action.Warnings{}, nil) 248 }) 249 250 It("calls ApplySpaceQuotaByName() with correct parameters", func() { 251 quota, passedSpaceGUID, passedOrgGUID := fakeActor.ApplySpaceQuotaByNameArgsForCall(0) 252 Expect(quota).To(Equal(quotaName)) 253 Expect(passedSpaceGUID).To(Equal(spaceGUID)) 254 Expect(passedOrgGUID).To(Equal(orgGUID)) 255 }) 256 257 It("does not return an error and displays warnings", func() { 258 Expect(testUI.Out).To(Say("OK")) // create space 259 Expect(testUI.Out).To(Say("OK")) // apply quota 260 Expect(testUI.Out).To(Say("OK")) // assign spaceManager 261 Expect(testUI.Out).To(Say("OK")) // assign spaceDeveloper 262 Expect(executeErr).To(Not(HaveOccurred())) 263 }) 264 }) 265 }) 266 267 It("prints that it is assigning roles to the current user", func() { 268 Expect(testUI.Out).To(Say(`Assigning role SpaceManager to user %s in org %s / space %s as %s\.\.\.`, userName, "some-org-name", spaceName, userName)) 269 Expect(testUI.Out).To(Say(`Assigning role SpaceDeveloper to user %s in org %s / space %s as %s\.\.\.`, userName, "some-org-name", spaceName, userName)) 270 }) 271 272 When("setting roles returns warnings", func() { 273 BeforeEach(func() { 274 fakeActor.CreateSpaceRoleReturnsOnCall(0, 275 v7action.Warnings{"create-space-manager-role-warning"}, 276 nil, 277 ) 278 279 fakeActor.CreateSpaceRoleReturnsOnCall(1, 280 v7action.Warnings{"create-space-developer-role-warning"}, 281 nil, 282 ) 283 }) 284 285 It("displays the warnings", func() { 286 Expect(testUI.Err).To(Say("create-space-manager-role-warning")) 287 Expect(testUI.Err).To(Say("create-space-developer-role-warning")) 288 }) 289 }) 290 291 When("setting the space manager role fails", func() { 292 BeforeEach(func() { 293 fakeActor.CreateSpaceRoleReturnsOnCall(0, 294 v7action.Warnings{}, 295 errors.New("create-space-manager-role-error"), 296 ) 297 }) 298 299 It("displays warnings and returns the error", func() { 300 Expect(executeErr).To(MatchError("create-space-manager-role-error")) 301 }) 302 }) 303 304 When("setting the space developer role fails", func() { 305 BeforeEach(func() { 306 fakeActor.CreateSpaceRoleReturnsOnCall(1, 307 v7action.Warnings{}, 308 errors.New("create-space-developer-role-error"), 309 ) 310 }) 311 312 It("displays warnings and returns the error", func() { 313 Expect(executeErr).To(MatchError("create-space-developer-role-error")) 314 }) 315 }) 316 317 When("setting roles is successful", func() { 318 BeforeEach(func() { 319 fakeActor.CreateSpaceRoleReturnsOnCall(0, 320 v7action.Warnings{}, 321 nil, 322 ) 323 324 fakeActor.CreateSpaceRoleReturnsOnCall(1, 325 v7action.Warnings{}, 326 nil, 327 ) 328 }) 329 330 It("creates the space in the targeted organization", func() { 331 Expect(fakeActor.GetOrganizationByNameCallCount()).To(Equal(0)) 332 Expect(fakeActor.CreateSpaceCallCount()).To(Equal(1)) 333 expectedSpaceName, expectedOrgGUID := fakeActor.CreateSpaceArgsForCall(0) 334 Expect(expectedSpaceName).To(Equal(spaceName)) 335 Expect(expectedOrgGUID).To(Equal("some-org-guid")) 336 }) 337 338 It("sets the user as a space manager", func() { 339 Expect(fakeActor.CreateSpaceRoleCallCount()).To(Equal(2)) 340 givenRoleType, givenOrgGuid, givenSpaceGUID, givenUserName, givenOrigin, givenIsClient := fakeActor.CreateSpaceRoleArgsForCall(0) 341 Expect(givenRoleType).To(Equal(constant.SpaceManagerRole)) 342 Expect(givenOrgGuid).To(Equal("some-org-guid")) 343 Expect(givenSpaceGUID).To(Equal("some-space-guid")) 344 Expect(givenUserName).To(Equal("some-user-name")) 345 Expect(givenOrigin).To(Equal("some-user-origin")) 346 Expect(givenIsClient).To(BeFalse()) 347 }) 348 349 It("sets the user as a space developer", func() { 350 Expect(fakeActor.CreateSpaceRoleCallCount()).To(Equal(2)) 351 givenRoleType, givenOrgGuid, givenSpaceGUID, givenUserName, givenOrigin, givenIsClient := fakeActor.CreateSpaceRoleArgsForCall(1) 352 Expect(givenRoleType).To(Equal(constant.SpaceDeveloperRole)) 353 Expect(givenOrgGuid).To(Equal("some-org-guid")) 354 Expect(givenSpaceGUID).To(Equal("some-space-guid")) 355 Expect(givenUserName).To(Equal("some-user-name")) 356 Expect(givenOrigin).To(Equal("some-user-origin")) 357 Expect(givenIsClient).To(BeFalse()) 358 }) 359 360 It("prints ok and then a tip", func() { 361 Expect(executeErr).ToNot(HaveOccurred()) 362 Expect(testUI.Out).To(Say("OK")) // create space 363 Expect(testUI.Out).To(Say("OK")) // assign spaceManager 364 Expect(testUI.Out).To(Say("OK")) // assign spaceDeveloper 365 366 Expect(testUI.Out).To(Say(`TIP: Use 'cf target -o "%s" -s "%s"' to target new space`, "some-org-name", spaceName)) 367 }) 368 }) 369 })