github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/command/v7/labels_command_test.go (about)

     1  package v7_test
     2  
     3  import (
     4  	"errors"
     5  	"regexp"
     6  
     7  	"strings"
     8  
     9  	"code.cloudfoundry.org/cli/actor/v7action"
    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/v7"
    14  	"code.cloudfoundry.org/cli/command/v7/v7fakes"
    15  	"code.cloudfoundry.org/cli/types"
    16  	"code.cloudfoundry.org/cli/util/configv3"
    17  	"code.cloudfoundry.org/cli/util/ui"
    18  	. "github.com/onsi/ginkgo"
    19  	. "github.com/onsi/gomega"
    20  	. "github.com/onsi/gomega/gbytes"
    21  )
    22  
    23  var _ = Describe("labels command", func() {
    24  	var (
    25  		cmd             LabelsCommand
    26  		fakeLabelsActor *v7fakes.FakeLabelsActor
    27  		fakeConfig      *commandfakes.FakeConfig
    28  		fakeSharedActor *commandfakes.FakeSharedActor
    29  		testUI          *ui.UI
    30  
    31  		executeErr error
    32  	)
    33  
    34  	BeforeEach(func() {
    35  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    36  		fakeLabelsActor = new(v7fakes.FakeLabelsActor)
    37  		fakeConfig = new(commandfakes.FakeConfig)
    38  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    39  		cmd = LabelsCommand{
    40  			Actor:       fakeLabelsActor,
    41  			UI:          testUI,
    42  			Config:      fakeConfig,
    43  			SharedActor: fakeSharedActor,
    44  		}
    45  	})
    46  
    47  	JustBeforeEach(func() {
    48  		executeErr = cmd.Execute(nil)
    49  	})
    50  
    51  	Describe("listing labels", func() {
    52  		Describe("for apps", func() {
    53  			BeforeEach(func() {
    54  				fakeConfig.CurrentUserNameReturns("some-user", nil)
    55  				fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: "fake-org"})
    56  				fakeConfig.TargetedSpaceReturns(configv3.Space{Name: "fake-space", GUID: "some-space-guid"})
    57  				cmd.RequiredArgs = flag.LabelsArgs{
    58  					ResourceType: "app",
    59  					ResourceName: "dora",
    60  				}
    61  				fakeLabelsActor.GetApplicationLabelsReturns(
    62  					map[string]types.NullString{
    63  						"some-other-label": types.NewNullString("some-other-value"),
    64  						"some-label":       types.NewNullString("some-value"),
    65  					},
    66  					v7action.Warnings{},
    67  					nil)
    68  			})
    69  
    70  			It("doesn't error", func() {
    71  				Expect(executeErr).ToNot(HaveOccurred())
    72  			})
    73  
    74  			It("checks that the user is logged in and targeted to an org and space", func() {
    75  				Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    76  				checkOrg, checkSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    77  				Expect(checkOrg).To(BeTrue())
    78  				Expect(checkSpace).To(BeTrue())
    79  			})
    80  
    81  			It("displays a message that it is retrieving the labels", func() {
    82  				Expect(executeErr).ToNot(HaveOccurred())
    83  				Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    84  				Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Getting labels for app dora in org fake-org / space fake-space as some-user...`)))
    85  			})
    86  
    87  			It("retrieves the labels associated with the application", func() {
    88  				Expect(fakeLabelsActor.GetApplicationLabelsCallCount()).To(Equal(1))
    89  				appName, spaceGUID := fakeLabelsActor.GetApplicationLabelsArgsForCall(0)
    90  				Expect(appName).To(Equal("dora"))
    91  				Expect(spaceGUID).To(Equal("some-space-guid"))
    92  			})
    93  
    94  			It("displays the labels that are associated with the application, alphabetically", func() {
    95  				Expect(testUI.Out).To(Say(`key\s+value`))
    96  				Expect(testUI.Out).To(Say(`some-label\s+some-value`))
    97  				Expect(testUI.Out).To(Say(`some-other-label\s+some-other-value`))
    98  			})
    99  
   100  			When("CAPI returns warnings", func() {
   101  				BeforeEach(func() {
   102  					fakeLabelsActor.GetApplicationLabelsReturns(
   103  						map[string]types.NullString{
   104  							"some-other-label": types.NewNullString("some-other-value"),
   105  							"some-label":       types.NewNullString("some-value"),
   106  						},
   107  						v7action.Warnings([]string{"some-warning-1", "some-warning-2"}),
   108  						nil)
   109  				})
   110  
   111  				It("prints all warnings", func() {
   112  					Expect(testUI.Err).To(Say("some-warning-1"))
   113  					Expect(testUI.Err).To(Say("some-warning-2"))
   114  				})
   115  			})
   116  
   117  			When("there is an error retrieving the application", func() {
   118  				BeforeEach(func() {
   119  					fakeLabelsActor.GetApplicationLabelsReturns(
   120  						map[string]types.NullString{},
   121  						v7action.Warnings([]string{"some-warning-1", "some-warning-2"}),
   122  						errors.New("boom"))
   123  				})
   124  
   125  				It("returns the error", func() {
   126  					Expect(executeErr).To(MatchError("boom"))
   127  				})
   128  
   129  				It("still prints all warnings", func() {
   130  					Expect(testUI.Err).To(Say("some-warning-1"))
   131  					Expect(testUI.Err).To(Say("some-warning-2"))
   132  				})
   133  
   134  				It("doesn't say ok", func() {
   135  					Expect(testUI.Out).ToNot(Say("OK"))
   136  				})
   137  			})
   138  
   139  			When("checking targeted org and space fails", func() {
   140  				BeforeEach(func() {
   141  					fakeSharedActor.CheckTargetReturns(errors.New("nope"))
   142  				})
   143  
   144  				It("returns an error", func() {
   145  					Expect(executeErr).To(MatchError("nope"))
   146  				})
   147  			})
   148  
   149  			When("fetching the current user's name fails", func() {
   150  				BeforeEach(func() {
   151  					fakeConfig.CurrentUserNameReturns("some-user", errors.New("boom"))
   152  				})
   153  
   154  				It("returns an error", func() {
   155  					Expect(executeErr).To(MatchError("boom"))
   156  				})
   157  			})
   158  
   159  			When("the resource type argument is not lowercase", func() {
   160  				BeforeEach(func() {
   161  					cmd.RequiredArgs = flag.LabelsArgs{
   162  						ResourceType: "aPp",
   163  						ResourceName: "dora",
   164  					}
   165  				})
   166  
   167  				It("retrieves the labels associated with the app", func() {
   168  					Expect(fakeLabelsActor.GetApplicationLabelsCallCount()).To(Equal(1))
   169  					appName, spaceGUID := fakeLabelsActor.GetApplicationLabelsArgsForCall(0)
   170  					Expect(appName).To(Equal("dora"))
   171  					Expect(spaceGUID).To(Equal("some-space-guid"))
   172  				})
   173  			})
   174  		})
   175  
   176  		Describe("for domains", func() {
   177  			BeforeEach(func() {
   178  				fakeConfig.CurrentUserNameReturns("some-user", nil)
   179  				cmd.RequiredArgs = flag.LabelsArgs{
   180  					ResourceType: "domain",
   181  					ResourceName: "example.com",
   182  				}
   183  				fakeLabelsActor.GetDomainLabelsReturns(
   184  					map[string]types.NullString{
   185  						"some-other-label": types.NewNullString("some-other-value"),
   186  						"some-label":       types.NewNullString("some-value"),
   187  					},
   188  					v7action.Warnings{},
   189  					nil)
   190  			})
   191  
   192  			It("doesn't error", func() {
   193  				Expect(executeErr).ToNot(HaveOccurred())
   194  			})
   195  
   196  			It("checks that the user is logged in", func() {
   197  				Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
   198  				checkOrg, checkSpace := fakeSharedActor.CheckTargetArgsForCall(0)
   199  				Expect(checkOrg).To(BeFalse())
   200  				Expect(checkSpace).To(BeFalse())
   201  			})
   202  
   203  			It("displays a message that it is retrieving the labels", func() {
   204  				Expect(executeErr).ToNot(HaveOccurred())
   205  				Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Getting labels for domain example.com as some-user...`)))
   206  			})
   207  
   208  			It("retrieves the labels associated with the domain", func() {
   209  				Expect(fakeLabelsActor.GetDomainLabelsCallCount()).To(Equal(1))
   210  				domainName := fakeLabelsActor.GetDomainLabelsArgsForCall(0)
   211  				Expect(domainName).To(Equal("example.com"))
   212  			})
   213  
   214  			It("displays the labels that are associated with the domain, alphabetically", func() {
   215  				Expect(testUI.Out).To(Say(`key\s+value`))
   216  				Expect(testUI.Out).To(Say(`some-label\s+some-value`))
   217  				Expect(testUI.Out).To(Say(`some-other-label\s+some-other-value`))
   218  			})
   219  
   220  			When("CAPI returns warnings", func() {
   221  				BeforeEach(func() {
   222  					fakeLabelsActor.GetDomainLabelsReturns(
   223  						map[string]types.NullString{
   224  							"some-other-label": types.NewNullString("some-other-value"),
   225  							"some-label":       types.NewNullString("some-value"),
   226  						},
   227  						v7action.Warnings([]string{"some-warning-1", "some-warning-2"}),
   228  						nil)
   229  				})
   230  
   231  				It("prints all warnings", func() {
   232  					Expect(testUI.Err).To(Say("some-warning-1"))
   233  					Expect(testUI.Err).To(Say("some-warning-2"))
   234  				})
   235  			})
   236  
   237  			When("there is an error retrieving the domain", func() {
   238  				BeforeEach(func() {
   239  					fakeLabelsActor.GetDomainLabelsReturns(
   240  						map[string]types.NullString{},
   241  						v7action.Warnings([]string{"some-warning-1", "some-warning-2"}),
   242  						errors.New("boom"))
   243  				})
   244  
   245  				It("returns the error", func() {
   246  					Expect(executeErr).To(MatchError("boom"))
   247  				})
   248  
   249  				It("still prints all warnings", func() {
   250  					Expect(testUI.Err).To(Say("some-warning-1"))
   251  					Expect(testUI.Err).To(Say("some-warning-2"))
   252  				})
   253  
   254  				It("doesn't say ok", func() {
   255  					Expect(testUI.Out).ToNot(Say("OK"))
   256  				})
   257  			})
   258  
   259  			When("checking the user is logged-in fails", func() {
   260  				BeforeEach(func() {
   261  					fakeSharedActor.CheckTargetReturns(errors.New("nope"))
   262  				})
   263  
   264  				It("returns an error", func() {
   265  					Expect(executeErr).To(MatchError("nope"))
   266  				})
   267  			})
   268  
   269  			When("fetching the current user's name fails", func() {
   270  				BeforeEach(func() {
   271  					fakeConfig.CurrentUserNameReturns("some-user", errors.New("boom"))
   272  				})
   273  
   274  				It("returns an error", func() {
   275  					Expect(executeErr).To(MatchError("boom"))
   276  				})
   277  			})
   278  
   279  			When("the resource type argument is not lowercase", func() {
   280  				BeforeEach(func() {
   281  					cmd.RequiredArgs = flag.LabelsArgs{
   282  						ResourceType: "DoMaiN",
   283  						ResourceName: "example.com",
   284  					}
   285  				})
   286  
   287  				It("retrieves the labels associated with the domain", func() {
   288  					Expect(fakeLabelsActor.GetDomainLabelsCallCount()).To(Equal(1))
   289  					domainName := fakeLabelsActor.GetDomainLabelsArgsForCall(0)
   290  					Expect(domainName).To(Equal("example.com"))
   291  				})
   292  			})
   293  		})
   294  
   295  		Describe("for orgs", func() {
   296  			BeforeEach(func() {
   297  				fakeConfig.CurrentUserNameReturns("some-user", nil)
   298  				cmd.RequiredArgs = flag.LabelsArgs{
   299  					ResourceType: "org",
   300  					ResourceName: "fake-org",
   301  				}
   302  				fakeLabelsActor.GetOrganizationLabelsReturns(
   303  					map[string]types.NullString{
   304  						"some-other-label": types.NewNullString("some-other-value"),
   305  						"some-label":       types.NewNullString("some-value"),
   306  					},
   307  					v7action.Warnings{},
   308  					nil)
   309  			})
   310  
   311  			It("doesn't error", func() {
   312  				Expect(executeErr).ToNot(HaveOccurred())
   313  			})
   314  
   315  			It("checks that the user is logged in", func() {
   316  				Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
   317  				checkOrg, checkSpace := fakeSharedActor.CheckTargetArgsForCall(0)
   318  				Expect(checkOrg).To(BeFalse())
   319  				Expect(checkSpace).To(BeFalse())
   320  			})
   321  
   322  			It("displays a message that it is retrieving the labels", func() {
   323  				Expect(executeErr).ToNot(HaveOccurred())
   324  				Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
   325  				Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Getting labels for org fake-org as some-user...`)))
   326  			})
   327  
   328  			It("retrieves the labels associated with the organization", func() {
   329  				Expect(fakeLabelsActor.GetOrganizationLabelsCallCount()).To(Equal(1))
   330  			})
   331  
   332  			It("displays the labels that are associated with the organization, alphabetically", func() {
   333  				Expect(testUI.Out).To(Say(`key\s+value`))
   334  				Expect(testUI.Out).To(Say(`some-label\s+some-value`))
   335  				Expect(testUI.Out).To(Say(`some-other-label\s+some-other-value`))
   336  			})
   337  
   338  			When("CAPI returns warnings", func() {
   339  				BeforeEach(func() {
   340  					fakeLabelsActor.GetOrganizationLabelsReturns(
   341  						map[string]types.NullString{
   342  							"some-other-label": types.NewNullString("some-other-value"),
   343  							"some-label":       types.NewNullString("some-value"),
   344  						},
   345  						v7action.Warnings([]string{"some-warning-1", "some-warning-2"}),
   346  						nil)
   347  				})
   348  
   349  				It("prints all warnings", func() {
   350  					Expect(testUI.Err).To(Say("some-warning-1"))
   351  					Expect(testUI.Err).To(Say("some-warning-2"))
   352  				})
   353  			})
   354  
   355  			When("there is an error retrieving the organization", func() {
   356  				BeforeEach(func() {
   357  					fakeLabelsActor.GetOrganizationLabelsReturns(
   358  						map[string]types.NullString{},
   359  						v7action.Warnings([]string{"some-warning-1", "some-warning-2"}),
   360  						errors.New("boom"))
   361  				})
   362  
   363  				It("returns the error", func() {
   364  					Expect(executeErr).To(MatchError("boom"))
   365  				})
   366  
   367  				It("still prints all warnings", func() {
   368  					Expect(testUI.Err).To(Say("some-warning-1"))
   369  					Expect(testUI.Err).To(Say("some-warning-2"))
   370  				})
   371  
   372  				It("doesn't say ok", func() {
   373  					Expect(testUI.Out).ToNot(Say("OK"))
   374  				})
   375  			})
   376  
   377  			When("checking targeted org and space fails", func() {
   378  				BeforeEach(func() {
   379  					fakeSharedActor.CheckTargetReturns(errors.New("nope"))
   380  				})
   381  
   382  				It("returns an error", func() {
   383  					Expect(executeErr).To(MatchError("nope"))
   384  				})
   385  			})
   386  
   387  			When("fetching the current user's name fails", func() {
   388  				BeforeEach(func() {
   389  					fakeConfig.CurrentUserNameReturns("some-user", errors.New("boom"))
   390  				})
   391  
   392  				It("returns an error", func() {
   393  					Expect(executeErr).To(MatchError("boom"))
   394  				})
   395  			})
   396  
   397  			When("the resource type argument is not lowercase", func() {
   398  				BeforeEach(func() {
   399  					cmd.RequiredArgs = flag.LabelsArgs{
   400  						ResourceType: "OrG",
   401  						ResourceName: "fake-org",
   402  					}
   403  				})
   404  
   405  				It("retrieves the labels associated with the org", func() {
   406  					Expect(fakeLabelsActor.GetOrganizationLabelsCallCount()).To(Equal(1))
   407  					orgName := fakeLabelsActor.GetOrganizationLabelsArgsForCall(0)
   408  					Expect(orgName).To(Equal("fake-org"))
   409  				})
   410  			})
   411  		})
   412  
   413  		Describe("for routes", func() {
   414  			BeforeEach(func() {
   415  				fakeConfig.CurrentUserNameReturns("some-user", nil)
   416  				fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: "fake-org"})
   417  				fakeConfig.TargetedSpaceReturns(configv3.Space{Name: "fake-space", GUID: "some-space-guid"})
   418  				cmd.RequiredArgs = flag.LabelsArgs{
   419  					ResourceType: "route",
   420  					ResourceName: "foo.example.com/the-path",
   421  				}
   422  				fakeLabelsActor.GetRouteLabelsReturns(
   423  					map[string]types.NullString{
   424  						"some-other-label": types.NewNullString("some-other-value"),
   425  						"some-label":       types.NewNullString("some-value"),
   426  					},
   427  					v7action.Warnings{},
   428  					nil)
   429  			})
   430  
   431  			It("doesn't error", func() {
   432  				Expect(executeErr).ToNot(HaveOccurred())
   433  			})
   434  
   435  			It("checks that the user is logged in", func() {
   436  				Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
   437  				checkOrg, checkSpace := fakeSharedActor.CheckTargetArgsForCall(0)
   438  				Expect(checkOrg).To(BeTrue())
   439  				Expect(checkSpace).To(BeTrue())
   440  			})
   441  
   442  			It("displays a message that it is retrieving the labels", func() {
   443  				Expect(executeErr).ToNot(HaveOccurred())
   444  				Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Getting labels for route foo.example.com/the-path in org fake-org / space fake-space as some-user...`)))
   445  			})
   446  
   447  			It("retrieves the labels associated with the route", func() {
   448  				Expect(fakeLabelsActor.GetRouteLabelsCallCount()).To(Equal(1))
   449  				routeName, spaceGUID := fakeLabelsActor.GetRouteLabelsArgsForCall(0)
   450  				Expect(routeName).To(Equal("foo.example.com/the-path"))
   451  				Expect(spaceGUID).To(Equal("some-space-guid"))
   452  			})
   453  
   454  			It("displays the labels that are associated with the route, alphabetically", func() {
   455  				Expect(testUI.Out).To(Say(`key\s+value`))
   456  				Expect(testUI.Out).To(Say(`some-label\s+some-value`))
   457  				Expect(testUI.Out).To(Say(`some-other-label\s+some-other-value`))
   458  			})
   459  
   460  			When("CAPI returns warnings", func() {
   461  				BeforeEach(func() {
   462  					fakeLabelsActor.GetRouteLabelsReturns(
   463  						map[string]types.NullString{
   464  							"some-other-label": types.NewNullString("some-other-value"),
   465  							"some-label":       types.NewNullString("some-value"),
   466  						},
   467  						v7action.Warnings([]string{"some-warning-1", "some-warning-2"}),
   468  						nil)
   469  				})
   470  
   471  				It("prints all warnings", func() {
   472  					Expect(testUI.Err).To(Say("some-warning-1"))
   473  					Expect(testUI.Err).To(Say("some-warning-2"))
   474  				})
   475  			})
   476  
   477  			When("there is an error retrieving the route", func() {
   478  				BeforeEach(func() {
   479  					fakeLabelsActor.GetRouteLabelsReturns(
   480  						map[string]types.NullString{},
   481  						v7action.Warnings([]string{"some-warning-1", "some-warning-2"}),
   482  						errors.New("boom"))
   483  				})
   484  
   485  				It("returns the error", func() {
   486  					Expect(executeErr).To(MatchError("boom"))
   487  				})
   488  
   489  				It("still prints all warnings", func() {
   490  					Expect(testUI.Err).To(Say("some-warning-1"))
   491  					Expect(testUI.Err).To(Say("some-warning-2"))
   492  				})
   493  
   494  				It("doesn't say ok", func() {
   495  					Expect(testUI.Out).ToNot(Say("OK"))
   496  				})
   497  			})
   498  
   499  			When("checking the user is logged-in fails", func() {
   500  				BeforeEach(func() {
   501  					fakeSharedActor.CheckTargetReturns(errors.New("nope"))
   502  				})
   503  
   504  				It("returns an error", func() {
   505  					Expect(executeErr).To(MatchError("nope"))
   506  				})
   507  			})
   508  
   509  			When("fetching the current user's name fails", func() {
   510  				BeforeEach(func() { fakeConfig.CurrentUserNameReturns("some-user", errors.New("boom")) })
   511  				It("returns an error", func() {
   512  					Expect(executeErr).To(MatchError("boom"))
   513  				})
   514  			})
   515  
   516  			When("the resource type argument is not lowercase", func() {
   517  				BeforeEach(func() {
   518  					cmd.RequiredArgs = flag.LabelsArgs{
   519  						ResourceType: "RoUtE",
   520  						ResourceName: "example.com",
   521  					}
   522  				})
   523  
   524  				It("retrieves the labels associated with the route", func() {
   525  					Expect(fakeLabelsActor.GetRouteLabelsCallCount()).To(Equal(1))
   526  					routeName, _ := fakeLabelsActor.GetRouteLabelsArgsForCall(0)
   527  					Expect(routeName).To(Equal("example.com"))
   528  				})
   529  			})
   530  		})
   531  
   532  		Describe("for spaces", func() {
   533  			BeforeEach(func() {
   534  				fakeConfig.CurrentUserNameReturns("some-user", nil)
   535  				fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: "fake-org", GUID: "some-org-guid"})
   536  				cmd.RequiredArgs = flag.LabelsArgs{
   537  					ResourceType: "space",
   538  					ResourceName: "fake-space",
   539  				}
   540  				fakeLabelsActor.GetSpaceLabelsReturns(
   541  					map[string]types.NullString{
   542  						"some-other-label": types.NewNullString("some-other-value"),
   543  						"some-label":       types.NewNullString("some-value"),
   544  					},
   545  					v7action.Warnings{},
   546  					nil)
   547  			})
   548  
   549  			It("doesn't error", func() {
   550  				Expect(executeErr).ToNot(HaveOccurred())
   551  			})
   552  
   553  			It("checks that the user is logged in and targetted to an org", func() {
   554  				Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
   555  				checkOrg, checkSpace := fakeSharedActor.CheckTargetArgsForCall(0)
   556  				Expect(checkOrg).To(BeTrue())
   557  				Expect(checkSpace).To(BeFalse())
   558  			})
   559  
   560  			It("displays a message that it is retrieving the labels", func() {
   561  				Expect(executeErr).ToNot(HaveOccurred())
   562  				Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
   563  				Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Getting labels for space fake-space in org fake-org as some-user...`)))
   564  			})
   565  
   566  			It("retrieves the labels associated with the space", func() {
   567  				Expect(fakeLabelsActor.GetSpaceLabelsCallCount()).To(Equal(1))
   568  			})
   569  
   570  			It("displays the labels that are associated with the space, alphabetically", func() {
   571  				Expect(testUI.Out).To(Say(`key\s+value`))
   572  				Expect(testUI.Out).To(Say(`some-label\s+some-value`))
   573  				Expect(testUI.Out).To(Say(`some-other-label\s+some-other-value`))
   574  			})
   575  
   576  			When("CAPI returns warnings", func() {
   577  				BeforeEach(func() {
   578  					fakeLabelsActor.GetSpaceLabelsReturns(
   579  						map[string]types.NullString{
   580  							"some-other-label": types.NewNullString("some-other-value"),
   581  							"some-label":       types.NewNullString("some-value"),
   582  						},
   583  						v7action.Warnings([]string{"some-warning-1", "some-warning-2"}),
   584  						nil)
   585  				})
   586  
   587  				It("prints all warnings", func() {
   588  					Expect(testUI.Err).To(Say("some-warning-1"))
   589  					Expect(testUI.Err).To(Say("some-warning-2"))
   590  				})
   591  			})
   592  
   593  			When("there is an error retrieving the space", func() {
   594  				BeforeEach(func() {
   595  					fakeLabelsActor.GetSpaceLabelsReturns(
   596  						map[string]types.NullString{},
   597  						v7action.Warnings([]string{"some-warning-1", "some-warning-2"}),
   598  						errors.New("boom"))
   599  				})
   600  
   601  				It("returns the error", func() {
   602  					Expect(executeErr).To(MatchError("boom"))
   603  				})
   604  
   605  				It("still prints all warnings", func() {
   606  					Expect(testUI.Err).To(Say("some-warning-1"))
   607  					Expect(testUI.Err).To(Say("some-warning-2"))
   608  				})
   609  
   610  				It("doesn't say ok", func() {
   611  					Expect(testUI.Out).ToNot(Say("OK"))
   612  				})
   613  			})
   614  
   615  			When("checking targeted org and space fails", func() {
   616  				BeforeEach(func() {
   617  					fakeSharedActor.CheckTargetReturns(errors.New("nope"))
   618  				})
   619  
   620  				It("returns an error", func() {
   621  					Expect(executeErr).To(MatchError("nope"))
   622  				})
   623  			})
   624  
   625  			When("fetching the current user's name fails", func() {
   626  				BeforeEach(func() {
   627  					fakeConfig.CurrentUserNameReturns("some-user", errors.New("boom"))
   628  				})
   629  
   630  				It("returns an error", func() {
   631  					Expect(executeErr).To(MatchError("boom"))
   632  				})
   633  			})
   634  
   635  			When("the resource type argument is not lowercase", func() {
   636  				BeforeEach(func() {
   637  					cmd.RequiredArgs = flag.LabelsArgs{
   638  						ResourceType: "SpAcE",
   639  						ResourceName: "fake-space",
   640  					}
   641  				})
   642  
   643  				It("retrieves the labels associated with the space", func() {
   644  					Expect(fakeLabelsActor.GetSpaceLabelsCallCount()).To(Equal(1))
   645  					spaceName, orgGUID := fakeLabelsActor.GetSpaceLabelsArgsForCall(0)
   646  					Expect(spaceName).To(Equal("fake-space"))
   647  					Expect(orgGUID).To(Equal("some-org-guid"))
   648  				})
   649  			})
   650  		})
   651  
   652  		Describe("for stacks", func() {
   653  			BeforeEach(func() {
   654  				fakeConfig.CurrentUserNameReturns("some-user", nil)
   655  				cmd.RequiredArgs = flag.LabelsArgs{
   656  					ResourceType: "stack",
   657  					ResourceName: "fake-stack",
   658  				}
   659  				fakeLabelsActor.GetStackLabelsReturns(
   660  					map[string]types.NullString{
   661  						"some-other-label": types.NewNullString("some-other-value"),
   662  						"some-label":       types.NewNullString("some-value"),
   663  					},
   664  					v7action.Warnings{},
   665  					nil)
   666  			})
   667  
   668  			It("doesn't error", func() {
   669  				Expect(executeErr).ToNot(HaveOccurred())
   670  			})
   671  
   672  			It("checks that the user is logged in", func() {
   673  				Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
   674  				checkOrg, checkSpace := fakeSharedActor.CheckTargetArgsForCall(0)
   675  				Expect(checkOrg).To(BeFalse())
   676  				Expect(checkSpace).To(BeFalse())
   677  			})
   678  
   679  			It("displays a message that it is retrieving the labels", func() {
   680  				Expect(executeErr).ToNot(HaveOccurred())
   681  				Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
   682  				Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Getting labels for stack fake-stack as some-user...`)))
   683  			})
   684  
   685  			It("retrieves the labels associated with the stack", func() {
   686  				Expect(fakeLabelsActor.GetStackLabelsCallCount()).To(Equal(1))
   687  			})
   688  
   689  			It("displays the labels that are associated with the stack, alphabetically", func() {
   690  				Expect(testUI.Out).To(Say(`key\s+value`))
   691  				Expect(testUI.Out).To(Say(`some-label\s+some-value`))
   692  				Expect(testUI.Out).To(Say(`some-other-label\s+some-other-value`))
   693  			})
   694  
   695  			When("CAPI returns warnings", func() {
   696  				BeforeEach(func() {
   697  					fakeLabelsActor.GetStackLabelsReturns(
   698  						map[string]types.NullString{
   699  							"some-other-label": types.NewNullString("some-other-value"),
   700  							"some-label":       types.NewNullString("some-value"),
   701  						},
   702  						v7action.Warnings([]string{"some-warning-1", "some-warning-2"}),
   703  						nil)
   704  				})
   705  
   706  				It("prints all warnings", func() {
   707  					Expect(testUI.Err).To(Say("some-warning-1"))
   708  					Expect(testUI.Err).To(Say("some-warning-2"))
   709  				})
   710  			})
   711  
   712  			When("there is an error retrieving the stack", func() {
   713  				BeforeEach(func() {
   714  					fakeLabelsActor.GetStackLabelsReturns(
   715  						map[string]types.NullString{},
   716  						v7action.Warnings([]string{"some-warning-1", "some-warning-2"}),
   717  						errors.New("boom"))
   718  				})
   719  
   720  				It("returns the error", func() {
   721  					Expect(executeErr).To(MatchError("boom"))
   722  				})
   723  
   724  				It("still prints all warnings", func() {
   725  					Expect(testUI.Err).To(Say("some-warning-1"))
   726  					Expect(testUI.Err).To(Say("some-warning-2"))
   727  				})
   728  
   729  				It("doesn't say ok", func() {
   730  					Expect(testUI.Out).ToNot(Say("OK"))
   731  				})
   732  			})
   733  
   734  			When("checking targeted stack and space fails", func() {
   735  				BeforeEach(func() {
   736  					fakeSharedActor.CheckTargetReturns(errors.New("nope"))
   737  				})
   738  
   739  				It("returns an error", func() {
   740  					Expect(executeErr).To(MatchError("nope"))
   741  				})
   742  			})
   743  
   744  			When("fetching the current user's name fails", func() {
   745  				BeforeEach(func() {
   746  					fakeConfig.CurrentUserNameReturns("some-user", errors.New("boom"))
   747  				})
   748  
   749  				It("returns an error", func() {
   750  					Expect(executeErr).To(MatchError("boom"))
   751  				})
   752  			})
   753  
   754  			When("the resource type argument is not lowercase", func() {
   755  				BeforeEach(func() {
   756  					cmd.RequiredArgs = flag.LabelsArgs{
   757  						ResourceType: "sTack",
   758  						ResourceName: "fake-stack",
   759  					}
   760  				})
   761  
   762  				It("retrieves the labels associated with the stack", func() {
   763  					Expect(fakeLabelsActor.GetStackLabelsCallCount()).To(Equal(1))
   764  					stackName := fakeLabelsActor.GetStackLabelsArgsForCall(0)
   765  					Expect(stackName).To(Equal("fake-stack"))
   766  				})
   767  			})
   768  		})
   769  
   770  		Describe("for buildpacks", func() {
   771  			BeforeEach(func() {
   772  				fakeConfig.CurrentUserNameReturns("some-user", nil)
   773  				cmd.RequiredArgs = flag.LabelsArgs{
   774  					ResourceType: "buildpack",
   775  					ResourceName: "my-buildpack",
   776  				}
   777  				fakeLabelsActor.GetBuildpackLabelsReturns(
   778  					map[string]types.NullString{
   779  						"some-other-label": types.NewNullString("some-other-value"),
   780  						"some-label":       types.NewNullString("some-value"),
   781  					},
   782  					v7action.Warnings{},
   783  					nil)
   784  			})
   785  
   786  			It("doesn't error", func() {
   787  				Expect(executeErr).ToNot(HaveOccurred())
   788  			})
   789  
   790  			It("checks that the user is logged in", func() {
   791  				Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
   792  				checkOrg, checkSpace := fakeSharedActor.CheckTargetArgsForCall(0)
   793  				Expect(checkOrg).To(BeFalse())
   794  				Expect(checkSpace).To(BeFalse())
   795  			})
   796  
   797  			Describe("the getting-labels message", func() {
   798  				When("the buildpack stack is not specified", func() {
   799  					BeforeEach(func() {
   800  						cmd.BuildpackStack = ""
   801  					})
   802  
   803  					It("displays a message that it is retrieving the labels", func() {
   804  						Expect(executeErr).ToNot(HaveOccurred())
   805  						Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
   806  						Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Getting labels for buildpack my-buildpack as some-user...`)))
   807  					})
   808  				})
   809  
   810  				When("the buildpack stack is specified", func() {
   811  					BeforeEach(func() {
   812  						cmd.BuildpackStack = "omelette"
   813  					})
   814  
   815  					It("displays a message that it is retrieving the labels", func() {
   816  						Expect(executeErr).ToNot(HaveOccurred())
   817  						Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
   818  						Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Getting labels for buildpack my-buildpack with stack omelette as some-user...`)))
   819  					})
   820  				})
   821  			})
   822  
   823  			It("retrieves the labels associated with the buildpack", func() {
   824  				Expect(fakeLabelsActor.GetBuildpackLabelsCallCount()).To(Equal(1))
   825  			})
   826  
   827  			It("displays the labels that are associated with the buildpack, alphabetically", func() {
   828  				Expect(testUI.Out).To(Say(`key\s+value`))
   829  				Expect(testUI.Out).To(Say(`some-label\s+some-value`))
   830  				Expect(testUI.Out).To(Say(`some-other-label\s+some-other-value`))
   831  			})
   832  
   833  			When("CAPI returns warnings", func() {
   834  				BeforeEach(func() {
   835  					fakeLabelsActor.GetBuildpackLabelsReturns(
   836  						map[string]types.NullString{
   837  							"some-other-label": types.NewNullString("some-other-value"),
   838  							"some-label":       types.NewNullString("some-value"),
   839  						},
   840  						v7action.Warnings([]string{"some-warning-1", "some-warning-2"}),
   841  						nil)
   842  				})
   843  
   844  				It("prints all warnings", func() {
   845  					Expect(testUI.Err).To(Say("some-warning-1"))
   846  					Expect(testUI.Err).To(Say("some-warning-2"))
   847  				})
   848  			})
   849  
   850  			When("there is an error retrieving the buildpack", func() {
   851  				BeforeEach(func() {
   852  					fakeLabelsActor.GetBuildpackLabelsReturns(
   853  						map[string]types.NullString{},
   854  						v7action.Warnings([]string{"some-warning-1", "some-warning-2"}),
   855  						errors.New("boom"))
   856  				})
   857  
   858  				It("returns the error", func() {
   859  					Expect(executeErr).To(MatchError("boom"))
   860  				})
   861  
   862  				It("still prints all warnings", func() {
   863  					Expect(testUI.Err).To(Say("some-warning-1"))
   864  					Expect(testUI.Err).To(Say("some-warning-2"))
   865  				})
   866  
   867  				It("doesn't say ok", func() {
   868  					Expect(testUI.Out).ToNot(Say("OK"))
   869  				})
   870  			})
   871  
   872  			When("checking targeted org and space fails", func() {
   873  				BeforeEach(func() {
   874  					fakeSharedActor.CheckTargetReturns(errors.New("nope"))
   875  				})
   876  
   877  				It("returns an error", func() {
   878  					Expect(executeErr).To(MatchError("nope"))
   879  				})
   880  			})
   881  
   882  			When("fetching the current user's name fails", func() {
   883  				BeforeEach(func() {
   884  					fakeConfig.CurrentUserNameReturns("some-user", errors.New("boom"))
   885  				})
   886  
   887  				It("returns an error", func() {
   888  					Expect(executeErr).To(MatchError("boom"))
   889  				})
   890  			})
   891  
   892  			When("the resource type argument is not lowercase", func() {
   893  				BeforeEach(func() {
   894  					cmd.RequiredArgs = flag.LabelsArgs{
   895  						ResourceType: "buildPack",
   896  						ResourceName: "fake-buildpack",
   897  					}
   898  					cmd.BuildpackStack = "fake-stack"
   899  				})
   900  
   901  				It("retrieves the labels associated with the buildpack", func() {
   902  					Expect(fakeLabelsActor.GetBuildpackLabelsCallCount()).To(Equal(1))
   903  					buildpackName, stackName := fakeLabelsActor.GetBuildpackLabelsArgsForCall(0)
   904  					Expect(buildpackName).To(Equal("fake-buildpack"))
   905  					Expect(stackName).To(Equal("fake-stack"))
   906  				})
   907  			})
   908  		})
   909  	})
   910  
   911  	Describe("disallowed --stack option", func() {
   912  		When("specifying --stack", func() {
   913  			It("errors with a disallowed resource type", func() {
   914  				names := []string{"app", "space", "stack", "org"}
   915  				for _, name := range names {
   916  					cmd.RequiredArgs = flag.LabelsArgs{
   917  						ResourceType: name,
   918  						ResourceName: "oshkosh",
   919  					}
   920  					cmd.BuildpackStack = "cflinuxfs3"
   921  					executeErr := cmd.Execute(nil)
   922  					argumentCombinationError := translatableerror.ArgumentCombinationError{
   923  						Args: []string{strings.ToLower(cmd.RequiredArgs.ResourceType), "--stack, -s"},
   924  					}
   925  					Expect(executeErr).To(MatchError(argumentCombinationError), "'%s' resource did complain", name)
   926  				}
   927  			})
   928  
   929  			It("retrieves the labels when resource type is buildpack", func() {
   930  				cmd.RequiredArgs = flag.LabelsArgs{
   931  					ResourceType: "buildpack",
   932  					ResourceName: "oshkosh",
   933  				}
   934  				cmd.BuildpackStack = "cflinuxfs3"
   935  				executeErr := cmd.Execute(nil)
   936  				Expect(executeErr).ToNot(HaveOccurred())
   937  				Expect(fakeLabelsActor.GetBuildpackLabelsCallCount()).To(Equal(1))
   938  				buildpackName, stackName := fakeLabelsActor.GetBuildpackLabelsArgsForCall(0)
   939  				Expect(buildpackName).To(Equal("oshkosh"))
   940  				Expect(stackName).To(Equal("cflinuxfs3"))
   941  			})
   942  		})
   943  	})
   944  })