github.com/arunkumar7540/cli@v6.45.0+incompatible/command/v7/target_command_test.go (about)

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