github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/command/v7/target_command_test.go (about) 1 package v7_test 2 3 import ( 4 "errors" 5 6 "code.cloudfoundry.org/cli/actor/v7action" 7 v7 "code.cloudfoundry.org/cli/command/v7" 8 "code.cloudfoundry.org/cli/command/v7/v7fakes" 9 10 "code.cloudfoundry.org/cli/actor/actionerror" 11 "code.cloudfoundry.org/cli/command/commandfakes" 12 "code.cloudfoundry.org/cli/command/translatableerror" 13 "code.cloudfoundry.org/cli/util/configv3" 14 "code.cloudfoundry.org/cli/util/ui" 15 . "github.com/onsi/ginkgo" 16 . "github.com/onsi/gomega" 17 . "github.com/onsi/gomega/gbytes" 18 ) 19 20 var _ = Describe("target Command", func() { 21 var ( 22 cmd v7.TargetCommand 23 testUI *ui.UI 24 fakeConfig *commandfakes.FakeConfig 25 fakeSharedActor *commandfakes.FakeSharedActor 26 fakeActor *v7fakes.FakeTargetActor 27 binaryName string 28 apiVersion string 29 minCLIVersion 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(v7fakes.FakeTargetActor) 38 39 cmd = v7.TargetCommand{ 40 UI: testUI, 41 Config: fakeConfig, 42 SharedActor: fakeSharedActor, 43 Actor: fakeActor, 44 } 45 46 binaryName = "faceman" 47 fakeConfig.BinaryNameReturns(binaryName) 48 apiVersion = "1.2.3" 49 fakeActor.CloudControllerAPIVersionReturns(apiVersion) 50 minCLIVersion = "1.0.0" 51 fakeConfig.MinCLIVersionReturns(minCLIVersion) 52 fakeConfig.BinaryVersionReturns("1.0.0") 53 }) 54 55 JustBeforeEach(func() { 56 executeErr = cmd.Execute(nil) 57 }) 58 59 When("a cloud controller API endpoint is set", func() { 60 BeforeEach(func() { 61 fakeConfig.TargetReturns("some-api-target") 62 }) 63 64 When("checking the cloud controller minimum version warning", func() { 65 var binaryVersion string 66 67 When("the CLI version is less than the recommended minimum", func() { 68 BeforeEach(func() { 69 binaryVersion = "0.0.0" 70 fakeConfig.BinaryVersionReturns(binaryVersion) 71 }) 72 73 It("displays a recommendation to update the CLI version", func() { 74 Expect(testUI.Err).To(Say("Cloud Foundry API version %s requires CLI version %s. You are currently on version %s. To upgrade your CLI, please visit: https://github.com/cloudfoundry/cli#downloads", apiVersion, minCLIVersion, binaryVersion)) 75 }) 76 }) 77 78 When("the CLI version is greater or equal to the recommended minimum", func() { 79 BeforeEach(func() { 80 binaryVersion = "1.0.0" 81 fakeConfig.BinaryVersionReturns(binaryVersion) 82 }) 83 84 It("does not display a recommendation to update the CLI version", func() { 85 Expect(testUI.Err).NotTo(Say("Cloud Foundry API version %s requires CLI version %s. You are currently on version %s. To upgrade your CLI, please visit: https://github.com/cloudfoundry/cli#downloads", apiVersion, minCLIVersion, binaryVersion)) 86 }) 87 }) 88 89 When("an error is encountered while parsing the semver versions", func() { 90 BeforeEach(func() { 91 fakeConfig.BinaryVersionReturns("&#%") 92 }) 93 94 It("does not recommend to update the CLI version", func() { 95 Expect(testUI.Err).NotTo(Say("Cloud Foundry API version %s requires CLI version %s.", apiVersion, minCLIVersion)) 96 }) 97 }) 98 99 When("the CLI version is invalid", func() { 100 BeforeEach(func() { 101 fakeConfig.BinaryVersionReturns("&#%") 102 }) 103 104 It("returns an error", func() { 105 Expect(executeErr.Error()).To(Equal("No Major.Minor.Patch elements found")) 106 }) 107 }) 108 }) 109 110 When("checking target fails", func() { 111 BeforeEach(func() { 112 fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName}) 113 }) 114 115 It("returns an error", func() { 116 Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName})) 117 118 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 119 checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0) 120 Expect(checkTargetedOrg).To(BeFalse()) 121 Expect(checkTargetedSpace).To(BeFalse()) 122 123 Expect(fakeConfig.UnsetOrganizationAndSpaceInformationCallCount()).To(Equal(0)) 124 Expect(fakeConfig.UnsetSpaceInformationCallCount()).To(Equal(0)) 125 }) 126 }) 127 128 When("the user is logged in", func() { 129 When("getting the current user returns an error", func() { 130 var someErr error 131 132 BeforeEach(func() { 133 someErr = errors.New("some-current-user-error") 134 fakeConfig.CurrentUserReturns(configv3.User{}, someErr) 135 }) 136 137 It("returns the same error", func() { 138 Expect(executeErr).To(MatchError(someErr)) 139 140 Expect(fakeConfig.UnsetOrganizationAndSpaceInformationCallCount()).To(Equal(0)) 141 Expect(fakeConfig.UnsetSpaceInformationCallCount()).To(Equal(0)) 142 }) 143 }) 144 145 When("getting the current user does not return an error", func() { 146 BeforeEach(func() { 147 fakeConfig.CurrentUserReturns( 148 configv3.User{Name: "some-user"}, 149 nil) 150 }) 151 152 When("no arguments are provided", func() { 153 When("no org or space are targeted", func() { 154 It("displays how to target an org and space", func() { 155 Expect(executeErr).ToNot(HaveOccurred()) 156 157 Expect(testUI.Out).To(Say("api endpoint: some-api-target")) 158 Expect(testUI.Out).To(Say("api version: 1.2.3")) 159 Expect(testUI.Out).To(Say("user: some-user")) 160 Expect(testUI.Out).To(Say("No org or space targeted, use '%s target -o ORG -s SPACE'", binaryName)) 161 }) 162 }) 163 164 When("an org but no space is targeted", func() { 165 BeforeEach(func() { 166 fakeConfig.HasTargetedOrganizationReturns(true) 167 fakeConfig.TargetedOrganizationReturns(configv3.Organization{ 168 GUID: "some-org-guid", 169 Name: "some-org", 170 }) 171 }) 172 173 It("displays the org and tip to target space", func() { 174 Expect(executeErr).ToNot(HaveOccurred()) 175 176 Expect(testUI.Out).To(Say("api endpoint: some-api-target")) 177 Expect(testUI.Out).To(Say("api version: 1.2.3")) 178 Expect(testUI.Out).To(Say("user: some-user")) 179 Expect(testUI.Out).To(Say("org: some-org")) 180 Expect(testUI.Out).To(Say("No space targeted, use '%s target -s SPACE'", binaryName)) 181 }) 182 }) 183 184 When("an org and space are targeted", func() { 185 BeforeEach(func() { 186 fakeConfig.HasTargetedOrganizationReturns(true) 187 fakeConfig.TargetedOrganizationReturns(configv3.Organization{ 188 GUID: "some-org-guid", 189 Name: "some-org", 190 }) 191 fakeConfig.HasTargetedSpaceReturns(true) 192 fakeConfig.TargetedSpaceReturns(configv3.Space{ 193 GUID: "some-space-guid", 194 Name: "some-space", 195 }) 196 }) 197 198 It("displays the org and space targeted ", func() { 199 Expect(executeErr).ToNot(HaveOccurred()) 200 201 Expect(testUI.Out).To(Say("api endpoint: some-api-target")) 202 Expect(testUI.Out).To(Say("api version: 1.2.3")) 203 Expect(testUI.Out).To(Say("user: some-user")) 204 Expect(testUI.Out).To(Say("org: some-org")) 205 Expect(testUI.Out).To(Say("space: some-space")) 206 }) 207 }) 208 }) 209 210 When("space is provided", func() { 211 BeforeEach(func() { 212 cmd.Space = "some-space" 213 }) 214 215 When("an org is already targeted", func() { 216 BeforeEach(func() { 217 fakeConfig.HasTargetedOrganizationReturns(true) 218 fakeConfig.TargetedOrganizationReturns(configv3.Organization{GUID: "some-org-guid"}) 219 }) 220 221 When("the space exists", func() { 222 BeforeEach(func() { 223 fakeActor.GetSpaceByNameAndOrganizationReturns( 224 v7action.Space{ 225 GUID: "some-space-guid", 226 Name: "some-space", 227 }, 228 v7action.Warnings{}, 229 nil) 230 }) 231 232 It("targets the space", func() { 233 Expect(executeErr).ToNot(HaveOccurred()) 234 235 Expect(fakeConfig.V7SetSpaceInformationCallCount()).To(Equal(1)) 236 spaceGUID, spaceName := fakeConfig.V7SetSpaceInformationArgsForCall(0) 237 Expect(spaceGUID).To(Equal("some-space-guid")) 238 Expect(spaceName).To(Equal("some-space")) 239 }) 240 }) 241 242 When("the space does not exist", func() { 243 BeforeEach(func() { 244 fakeActor.GetSpaceByNameAndOrganizationReturns( 245 v7action.Space{}, 246 v7action.Warnings{}, 247 actionerror.SpaceNotFoundError{Name: "some-space"}) 248 }) 249 250 It("returns a SpaceNotFoundError and clears existing space", func() { 251 Expect(executeErr).To(MatchError(actionerror.SpaceNotFoundError{Name: "some-space"})) 252 253 Expect(fakeConfig.SetSpaceInformationCallCount()).To(Equal(0)) 254 Expect(fakeConfig.UnsetOrganizationAndSpaceInformationCallCount()).To(Equal(0)) 255 }) 256 }) 257 }) 258 259 When("no org is targeted", func() { 260 It("returns NoOrgTargeted error and clears existing space", func() { 261 Expect(executeErr).To(MatchError(translatableerror.NoOrganizationTargetedError{BinaryName: "faceman"})) 262 263 Expect(fakeConfig.SetSpaceInformationCallCount()).To(Equal(0)) 264 Expect(fakeConfig.UnsetOrganizationAndSpaceInformationCallCount()).To(Equal(0)) 265 }) 266 }) 267 }) 268 269 When("org is provided", func() { 270 BeforeEach(func() { 271 cmd.Organization = "some-org" 272 }) 273 274 When("the org does not exist", func() { 275 BeforeEach(func() { 276 fakeActor.GetOrganizationByNameReturns( 277 v7action.Organization{}, 278 nil, 279 actionerror.OrganizationNotFoundError{Name: "some-org"}) 280 }) 281 282 It("displays all warnings,returns an org target error, and clears existing targets", func() { 283 Expect(executeErr).To(MatchError(actionerror.OrganizationNotFoundError{Name: "some-org"})) 284 285 Expect(fakeConfig.SetOrganizationInformationCallCount()).To(Equal(0)) 286 Expect(fakeConfig.UnsetOrganizationAndSpaceInformationCallCount()).To(Equal(1)) 287 }) 288 }) 289 290 When("the org exists", func() { 291 BeforeEach(func() { 292 fakeConfig.HasTargetedOrganizationReturns(true) 293 fakeConfig.TargetedOrganizationReturns(configv3.Organization{ 294 GUID: "some-org-guid", 295 Name: "some-org", 296 }) 297 fakeActor.GetOrganizationByNameReturns( 298 v7action.Organization{GUID: "some-org-guid"}, 299 v7action.Warnings{"warning-1", "warning-2"}, 300 nil) 301 }) 302 303 When("getting the organization's spaces returns an error", func() { 304 var err error 305 306 BeforeEach(func() { 307 err = errors.New("get-org-spaces-error") 308 fakeActor.GetOrganizationSpacesReturns( 309 []v7action.Space{}, 310 v7action.Warnings{ 311 "warning-3", 312 }, 313 err) 314 }) 315 316 It("displays all warnings, returns a get org spaces error and clears existing targets", func() { 317 Expect(executeErr).To(MatchError(err)) 318 319 Expect(fakeActor.GetOrganizationSpacesCallCount()).To(Equal(1)) 320 orgGUID := fakeActor.GetOrganizationSpacesArgsForCall(0) 321 Expect(orgGUID).To(Equal("some-org-guid")) 322 323 Expect(testUI.Err).To(Say("warning-1")) 324 Expect(testUI.Err).To(Say("warning-2")) 325 Expect(testUI.Err).To(Say("warning-3")) 326 327 Expect(fakeConfig.SetOrganizationInformationCallCount()).To(Equal(1)) 328 orgGUID, orgName := fakeConfig.SetOrganizationInformationArgsForCall(0) 329 Expect(orgGUID).To(Equal("some-org-guid")) 330 Expect(orgName).To(Equal("some-org")) 331 Expect(fakeConfig.SetSpaceInformationCallCount()).To(Equal(0)) 332 333 Expect(fakeConfig.UnsetOrganizationAndSpaceInformationCallCount()).To(Equal(1)) 334 }) 335 }) 336 337 When("there are no spaces in the targeted org", func() { 338 It("displays all warnings", func() { 339 Expect(executeErr).ToNot(HaveOccurred()) 340 341 Expect(testUI.Err).To(Say("warning-1")) 342 Expect(testUI.Err).To(Say("warning-2")) 343 }) 344 345 It("sets the org and unsets the space in the config", func() { 346 Expect(executeErr).ToNot(HaveOccurred()) 347 348 Expect(fakeConfig.SetOrganizationInformationCallCount()).To(Equal(1)) 349 orgGUID, orgName := fakeConfig.SetOrganizationInformationArgsForCall(0) 350 Expect(orgGUID).To(Equal("some-org-guid")) 351 Expect(orgName).To(Equal("some-org")) 352 353 Expect(fakeConfig.UnsetSpaceInformationCallCount()).To(Equal(1)) 354 Expect(fakeConfig.SetSpaceInformationCallCount()).To(Equal(0)) 355 }) 356 }) 357 358 When("there is only 1 space in the targeted org", func() { 359 BeforeEach(func() { 360 fakeActor.GetOrganizationSpacesReturns( 361 []v7action.Space{{ 362 GUID: "some-space-guid", 363 Name: "some-space", 364 }}, 365 v7action.Warnings{"warning-3"}, 366 nil, 367 ) 368 }) 369 370 It("displays all warnings", func() { 371 Expect(executeErr).ToNot(HaveOccurred()) 372 373 Expect(testUI.Err).To(Say("warning-1")) 374 Expect(testUI.Err).To(Say("warning-2")) 375 Expect(testUI.Err).To(Say("warning-3")) 376 }) 377 378 It("targets the org and space", func() { 379 Expect(executeErr).ToNot(HaveOccurred()) 380 381 Expect(fakeConfig.SetOrganizationInformationCallCount()).To(Equal(1)) 382 orgGUID, orgName := fakeConfig.SetOrganizationInformationArgsForCall(0) 383 Expect(orgGUID).To(Equal("some-org-guid")) 384 Expect(orgName).To(Equal("some-org")) 385 386 Expect(fakeConfig.UnsetSpaceInformationCallCount()).To(Equal(1)) 387 388 Expect(fakeConfig.V7SetSpaceInformationCallCount()).To(Equal(1)) 389 spaceGUID, spaceName := fakeConfig.V7SetSpaceInformationArgsForCall(0) 390 Expect(spaceGUID).To(Equal("some-space-guid")) 391 Expect(spaceName).To(Equal("some-space")) 392 }) 393 }) 394 395 When("there are multiple spaces in the targeted org", func() { 396 BeforeEach(func() { 397 fakeActor.GetOrganizationSpacesReturns( 398 []v7action.Space{ 399 { 400 GUID: "some-space-guid", 401 Name: "some-space", 402 }, 403 { 404 GUID: "another-space-space-guid", 405 Name: "another-space", 406 }, 407 }, 408 v7action.Warnings{"warning-3"}, 409 nil, 410 ) 411 }) 412 413 It("displays all warnings, sets the org, and clears the existing targetted space from the config", func() { 414 Expect(executeErr).ToNot(HaveOccurred()) 415 416 Expect(testUI.Err).To(Say("warning-1")) 417 Expect(testUI.Err).To(Say("warning-2")) 418 419 Expect(fakeConfig.SetOrganizationInformationCallCount()).To(Equal(1)) 420 orgGUID, orgName := fakeConfig.SetOrganizationInformationArgsForCall(0) 421 Expect(orgGUID).To(Equal("some-org-guid")) 422 Expect(orgName).To(Equal("some-org")) 423 424 Expect(fakeConfig.UnsetSpaceInformationCallCount()).To(Equal(1)) 425 Expect(fakeConfig.SetSpaceInformationCallCount()).To(Equal(0)) 426 }) 427 }) 428 429 When("getting the spaces in org returns an error", func() { 430 var err error 431 432 BeforeEach(func() { 433 err = errors.New("get-org-spaces-error") 434 fakeActor.GetOrganizationSpacesReturns( 435 []v7action.Space{}, 436 v7action.Warnings{ 437 "warning-3", 438 }, 439 err) 440 }) 441 442 It("displays all warnings, returns the error, and clears existing targets", func() { 443 Expect(executeErr).To(MatchError(err)) 444 445 Expect(testUI.Err).To(Say("warning-1")) 446 Expect(testUI.Err).To(Say("warning-2")) 447 Expect(testUI.Err).To(Say("warning-3")) 448 449 Expect(fakeConfig.UnsetOrganizationAndSpaceInformationCallCount()).To(Equal(1)) 450 }) 451 }) 452 }) 453 }) 454 455 When("org and space arguments are provided", func() { 456 BeforeEach(func() { 457 cmd.Space = "some-space" 458 cmd.Organization = "some-org" 459 }) 460 461 When("the org exists", func() { 462 BeforeEach(func() { 463 fakeActor.GetOrganizationByNameReturns( 464 v7action.Organization{ 465 GUID: "some-org-guid", 466 Name: "some-org", 467 }, 468 v7action.Warnings{ 469 "warning-1", 470 }, 471 nil) 472 473 fakeConfig.HasTargetedOrganizationReturns(true) 474 fakeConfig.TargetedOrganizationReturns(configv3.Organization{ 475 GUID: "some-org-guid", 476 Name: "some-org", 477 }) 478 479 }) 480 481 When("the space exists", func() { 482 BeforeEach(func() { 483 fakeActor.GetSpaceByNameAndOrganizationReturns( 484 v7action.Space{ 485 GUID: "some-space-guid", 486 Name: "some-space", 487 }, 488 v7action.Warnings{ 489 "warning-2", 490 }, 491 nil) 492 }) 493 494 It("sets the target org and space", func() { 495 Expect(fakeConfig.SetOrganizationInformationCallCount()).To(Equal(1)) 496 orgGUID, orgName := fakeConfig.SetOrganizationInformationArgsForCall(0) 497 Expect(orgGUID).To(Equal("some-org-guid")) 498 Expect(orgName).To(Equal("some-org")) 499 500 Expect(fakeConfig.V7SetSpaceInformationCallCount()).To(Equal(1)) 501 spaceGUID, spaceName := fakeConfig.V7SetSpaceInformationArgsForCall(0) 502 Expect(spaceGUID).To(Equal("some-space-guid")) 503 Expect(spaceName).To(Equal("some-space")) 504 }) 505 506 It("displays all warnings", func() { 507 Expect(testUI.Err).To(Say("warning-1")) 508 Expect(testUI.Err).To(Say("warning-2")) 509 }) 510 }) 511 512 When("the space does not exist", func() { 513 BeforeEach(func() { 514 fakeActor.GetSpaceByNameAndOrganizationReturns( 515 v7action.Space{}, 516 nil, 517 actionerror.SpaceNotFoundError{Name: "some-space"}) 518 }) 519 520 It("returns an error and clears existing targets", func() { 521 Expect(executeErr).To(MatchError(actionerror.SpaceNotFoundError{Name: "some-space"})) 522 523 Expect(fakeConfig.SetOrganizationInformationCallCount()).To(Equal(1)) 524 Expect(fakeConfig.SetSpaceInformationCallCount()).To(Equal(0)) 525 526 Expect(fakeConfig.UnsetOrganizationAndSpaceInformationCallCount()).To(Equal(1)) 527 }) 528 }) 529 }) 530 531 When("the org does not exist", func() { 532 BeforeEach(func() { 533 fakeActor.GetOrganizationByNameReturns( 534 v7action.Organization{}, 535 nil, 536 actionerror.OrganizationNotFoundError{Name: "some-org"}) 537 }) 538 539 It("returns an error and clears existing targets", func() { 540 Expect(executeErr).To(MatchError(actionerror.OrganizationNotFoundError{Name: "some-org"})) 541 542 Expect(fakeConfig.SetOrganizationInformationCallCount()).To(Equal(0)) 543 Expect(fakeConfig.SetSpaceInformationCallCount()).To(Equal(0)) 544 545 Expect(fakeConfig.UnsetOrganizationAndSpaceInformationCallCount()).To(Equal(1)) 546 }) 547 }) 548 }) 549 }) 550 }) 551 }) 552 })