github.com/sleungcy-sap/cli@v7.1.0+incompatible/command/v7/labels_command_test.go (about)

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