github.com/LukasHeimann/cloudfoundrycli/v8@v8.4.4/command/v7/labels_command_test.go (about)

     1  package v7_test
     2  
     3  import (
     4  	"errors"
     5  	"regexp"
     6  	"strings"
     7  
     8  	"github.com/LukasHeimann/cloudfoundrycli/v8/actor/v7action"
     9  	"github.com/LukasHeimann/cloudfoundrycli/v8/command/commandfakes"
    10  	"github.com/LukasHeimann/cloudfoundrycli/v8/command/flag"
    11  	"github.com/LukasHeimann/cloudfoundrycli/v8/command/translatableerror"
    12  	. "github.com/LukasHeimann/cloudfoundrycli/v8/command/v7"
    13  	"github.com/LukasHeimann/cloudfoundrycli/v8/command/v7/v7fakes"
    14  	"github.com/LukasHeimann/cloudfoundrycli/v8/types"
    15  	"github.com/LukasHeimann/cloudfoundrycli/v8/util/configv3"
    16  	"github.com/LukasHeimann/cloudfoundrycli/v8/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  				fakeLabelsActor.GetCurrentUserReturns(configv3.User{Name: "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", "service-instance":
   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  	Describe("listing labels", func() {
   188  		JustBeforeEach(func() {
   189  			executeErr = cmd.Execute(nil)
   190  		})
   191  
   192  		Describe("for apps", func() {
   193  			BeforeEach(func() {
   194  				fakeLabelsActor.GetCurrentUserReturns(configv3.User{Name: "some-user"}, nil)
   195  				fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: "fake-org"})
   196  				fakeConfig.TargetedSpaceReturns(configv3.Space{Name: "fake-space", GUID: "some-space-guid"})
   197  				cmd.RequiredArgs = flag.LabelsArgs{
   198  					ResourceType: "app",
   199  					ResourceName: "dora",
   200  				}
   201  				fakeLabelsActor.GetApplicationLabelsReturns(
   202  					map[string]types.NullString{
   203  						"some-other-label": types.NewNullString("some-other-value"),
   204  						"some-label":       types.NewNullString("some-value"),
   205  					},
   206  					v7action.Warnings{},
   207  					nil)
   208  			})
   209  
   210  			It("doesn't error", func() {
   211  				Expect(executeErr).ToNot(HaveOccurred())
   212  			})
   213  
   214  			It("displays a message that it is retrieving the labels", func() {
   215  				Expect(executeErr).ToNot(HaveOccurred())
   216  				Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Getting labels for app dora in org fake-org / space fake-space as some-user...`)))
   217  			})
   218  
   219  			It("retrieves the labels associated with the application", func() {
   220  				Expect(fakeLabelsActor.GetApplicationLabelsCallCount()).To(Equal(1))
   221  				appName, spaceGUID := fakeLabelsActor.GetApplicationLabelsArgsForCall(0)
   222  				Expect(appName).To(Equal("dora"))
   223  				Expect(spaceGUID).To(Equal("some-space-guid"))
   224  			})
   225  
   226  			It("displays the labels that are associated with the application, alphabetically", func() {
   227  				Expect(testUI.Out).To(Say(`key\s+value`))
   228  				Expect(testUI.Out).To(Say(`some-label\s+some-value`))
   229  				Expect(testUI.Out).To(Say(`some-other-label\s+some-other-value`))
   230  			})
   231  
   232  			When("CAPI returns warnings", func() {
   233  				BeforeEach(func() {
   234  					fakeLabelsActor.GetApplicationLabelsReturns(
   235  						map[string]types.NullString{
   236  							"some-other-label": types.NewNullString("some-other-value"),
   237  							"some-label":       types.NewNullString("some-value"),
   238  						},
   239  						v7action.Warnings([]string{"some-warning-1", "some-warning-2"}),
   240  						nil)
   241  				})
   242  
   243  				It("prints all warnings", func() {
   244  					Expect(testUI.Err).To(Say("some-warning-1"))
   245  					Expect(testUI.Err).To(Say("some-warning-2"))
   246  				})
   247  			})
   248  
   249  			When("there is an error retrieving the application", func() {
   250  				BeforeEach(func() {
   251  					fakeLabelsActor.GetApplicationLabelsReturns(
   252  						map[string]types.NullString{},
   253  						v7action.Warnings([]string{"some-warning-1", "some-warning-2"}),
   254  						errors.New("boom"))
   255  				})
   256  
   257  				It("returns the error", func() {
   258  					Expect(executeErr).To(MatchError("boom"))
   259  				})
   260  
   261  				It("still prints all warnings", func() {
   262  					Expect(testUI.Err).To(Say("some-warning-1"))
   263  					Expect(testUI.Err).To(Say("some-warning-2"))
   264  				})
   265  
   266  				It("doesn't say ok", func() {
   267  					Expect(testUI.Out).ToNot(Say("OK"))
   268  				})
   269  			})
   270  		})
   271  
   272  		Describe("for domains", func() {
   273  			BeforeEach(func() {
   274  				fakeLabelsActor.GetCurrentUserReturns(configv3.User{Name: "some-user"}, nil)
   275  				cmd.RequiredArgs = flag.LabelsArgs{
   276  					ResourceType: "domain",
   277  					ResourceName: "example.com",
   278  				}
   279  				fakeLabelsActor.GetDomainLabelsReturns(
   280  					map[string]types.NullString{
   281  						"some-other-label": types.NewNullString("some-other-value"),
   282  						"some-label":       types.NewNullString("some-value"),
   283  					},
   284  					v7action.Warnings{},
   285  					nil)
   286  			})
   287  
   288  			It("doesn't error", func() {
   289  				Expect(executeErr).ToNot(HaveOccurred())
   290  			})
   291  
   292  			It("displays a message that it is retrieving the labels", func() {
   293  				Expect(executeErr).ToNot(HaveOccurred())
   294  				Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Getting labels for domain example.com as some-user...`)))
   295  			})
   296  
   297  			It("retrieves the labels associated with the domain", func() {
   298  				Expect(fakeLabelsActor.GetDomainLabelsCallCount()).To(Equal(1))
   299  				domainName := fakeLabelsActor.GetDomainLabelsArgsForCall(0)
   300  				Expect(domainName).To(Equal("example.com"))
   301  			})
   302  
   303  			It("displays the labels that are associated with the domain, alphabetically", func() {
   304  				Expect(testUI.Out).To(Say(`key\s+value`))
   305  				Expect(testUI.Out).To(Say(`some-label\s+some-value`))
   306  				Expect(testUI.Out).To(Say(`some-other-label\s+some-other-value`))
   307  			})
   308  
   309  			When("CAPI returns warnings", func() {
   310  				BeforeEach(func() {
   311  					fakeLabelsActor.GetDomainLabelsReturns(
   312  						map[string]types.NullString{
   313  							"some-other-label": types.NewNullString("some-other-value"),
   314  							"some-label":       types.NewNullString("some-value"),
   315  						},
   316  						v7action.Warnings([]string{"some-warning-1", "some-warning-2"}),
   317  						nil)
   318  				})
   319  
   320  				It("prints all warnings", func() {
   321  					Expect(testUI.Err).To(Say("some-warning-1"))
   322  					Expect(testUI.Err).To(Say("some-warning-2"))
   323  				})
   324  			})
   325  
   326  			When("there is an error retrieving the domain", func() {
   327  				BeforeEach(func() {
   328  					fakeLabelsActor.GetDomainLabelsReturns(
   329  						map[string]types.NullString{},
   330  						v7action.Warnings([]string{"some-warning-1", "some-warning-2"}),
   331  						errors.New("boom"))
   332  				})
   333  
   334  				It("returns the error", func() {
   335  					Expect(executeErr).To(MatchError("boom"))
   336  				})
   337  
   338  				It("still prints all warnings", func() {
   339  					Expect(testUI.Err).To(Say("some-warning-1"))
   340  					Expect(testUI.Err).To(Say("some-warning-2"))
   341  				})
   342  
   343  				It("doesn't say ok", func() {
   344  					Expect(testUI.Out).ToNot(Say("OK"))
   345  				})
   346  			})
   347  		})
   348  
   349  		Describe("for orgs", func() {
   350  			BeforeEach(func() {
   351  				fakeLabelsActor.GetCurrentUserReturns(configv3.User{Name: "some-user"}, nil)
   352  				cmd.RequiredArgs = flag.LabelsArgs{
   353  					ResourceType: "org",
   354  					ResourceName: "fake-org",
   355  				}
   356  				fakeLabelsActor.GetOrganizationLabelsReturns(
   357  					map[string]types.NullString{
   358  						"some-other-label": types.NewNullString("some-other-value"),
   359  						"some-label":       types.NewNullString("some-value"),
   360  					},
   361  					v7action.Warnings{},
   362  					nil)
   363  			})
   364  
   365  			It("doesn't error", func() {
   366  				Expect(executeErr).ToNot(HaveOccurred())
   367  			})
   368  
   369  			It("displays a message that it is retrieving the labels", func() {
   370  				Expect(executeErr).ToNot(HaveOccurred())
   371  				Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Getting labels for org fake-org as some-user...`)))
   372  			})
   373  
   374  			It("retrieves the labels associated with the organization", func() {
   375  				Expect(fakeLabelsActor.GetOrganizationLabelsCallCount()).To(Equal(1))
   376  			})
   377  
   378  			It("displays the labels that are associated with the organization, alphabetically", func() {
   379  				Expect(testUI.Out).To(Say(`key\s+value`))
   380  				Expect(testUI.Out).To(Say(`some-label\s+some-value`))
   381  				Expect(testUI.Out).To(Say(`some-other-label\s+some-other-value`))
   382  			})
   383  
   384  			When("CAPI returns warnings", func() {
   385  				BeforeEach(func() {
   386  					fakeLabelsActor.GetOrganizationLabelsReturns(
   387  						map[string]types.NullString{
   388  							"some-other-label": types.NewNullString("some-other-value"),
   389  							"some-label":       types.NewNullString("some-value"),
   390  						},
   391  						v7action.Warnings([]string{"some-warning-1", "some-warning-2"}),
   392  						nil)
   393  				})
   394  
   395  				It("prints all warnings", func() {
   396  					Expect(testUI.Err).To(Say("some-warning-1"))
   397  					Expect(testUI.Err).To(Say("some-warning-2"))
   398  				})
   399  			})
   400  
   401  			When("there is an error retrieving the organization", func() {
   402  				BeforeEach(func() {
   403  					fakeLabelsActor.GetOrganizationLabelsReturns(
   404  						map[string]types.NullString{},
   405  						v7action.Warnings([]string{"some-warning-1", "some-warning-2"}),
   406  						errors.New("boom"))
   407  				})
   408  
   409  				It("returns the error", func() {
   410  					Expect(executeErr).To(MatchError("boom"))
   411  				})
   412  
   413  				It("still prints all warnings", func() {
   414  					Expect(testUI.Err).To(Say("some-warning-1"))
   415  					Expect(testUI.Err).To(Say("some-warning-2"))
   416  				})
   417  
   418  				It("doesn't say ok", func() {
   419  					Expect(testUI.Out).ToNot(Say("OK"))
   420  				})
   421  			})
   422  		})
   423  
   424  		Describe("for routes", func() {
   425  			BeforeEach(func() {
   426  				fakeLabelsActor.GetCurrentUserReturns(configv3.User{Name: "some-user"}, nil)
   427  				fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: "fake-org"})
   428  				fakeConfig.TargetedSpaceReturns(configv3.Space{Name: "fake-space", GUID: "some-space-guid"})
   429  				cmd.RequiredArgs = flag.LabelsArgs{
   430  					ResourceType: "route",
   431  					ResourceName: "foo.example.com/the-path",
   432  				}
   433  				fakeLabelsActor.GetRouteLabelsReturns(
   434  					map[string]types.NullString{
   435  						"some-other-label": types.NewNullString("some-other-value"),
   436  						"some-label":       types.NewNullString("some-value"),
   437  					},
   438  					v7action.Warnings{},
   439  					nil)
   440  			})
   441  
   442  			It("doesn't error", func() {
   443  				Expect(executeErr).ToNot(HaveOccurred())
   444  			})
   445  
   446  			It("displays a message that it is retrieving the labels", func() {
   447  				Expect(executeErr).ToNot(HaveOccurred())
   448  				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...`)))
   449  			})
   450  
   451  			It("retrieves the labels associated with the route", func() {
   452  				Expect(fakeLabelsActor.GetRouteLabelsCallCount()).To(Equal(1))
   453  				routeName, spaceGUID := fakeLabelsActor.GetRouteLabelsArgsForCall(0)
   454  				Expect(routeName).To(Equal("foo.example.com/the-path"))
   455  				Expect(spaceGUID).To(Equal("some-space-guid"))
   456  			})
   457  
   458  			It("displays the labels that are associated with the route, alphabetically", func() {
   459  				Expect(testUI.Out).To(Say(`key\s+value`))
   460  				Expect(testUI.Out).To(Say(`some-label\s+some-value`))
   461  				Expect(testUI.Out).To(Say(`some-other-label\s+some-other-value`))
   462  			})
   463  
   464  			When("CAPI returns warnings", func() {
   465  				BeforeEach(func() {
   466  					fakeLabelsActor.GetRouteLabelsReturns(
   467  						map[string]types.NullString{
   468  							"some-other-label": types.NewNullString("some-other-value"),
   469  							"some-label":       types.NewNullString("some-value"),
   470  						},
   471  						v7action.Warnings([]string{"some-warning-1", "some-warning-2"}),
   472  						nil)
   473  				})
   474  
   475  				It("prints all warnings", func() {
   476  					Expect(testUI.Err).To(Say("some-warning-1"))
   477  					Expect(testUI.Err).To(Say("some-warning-2"))
   478  				})
   479  			})
   480  
   481  			When("there is an error retrieving the route", func() {
   482  				BeforeEach(func() {
   483  					fakeLabelsActor.GetRouteLabelsReturns(
   484  						map[string]types.NullString{},
   485  						v7action.Warnings([]string{"some-warning-1", "some-warning-2"}),
   486  						errors.New("boom"))
   487  				})
   488  
   489  				It("returns the error", func() {
   490  					Expect(executeErr).To(MatchError("boom"))
   491  				})
   492  
   493  				It("still prints all warnings", func() {
   494  					Expect(testUI.Err).To(Say("some-warning-1"))
   495  					Expect(testUI.Err).To(Say("some-warning-2"))
   496  				})
   497  
   498  				It("doesn't say ok", func() {
   499  					Expect(testUI.Out).ToNot(Say("OK"))
   500  				})
   501  			})
   502  		})
   503  
   504  		Describe("for spaces", func() {
   505  			BeforeEach(func() {
   506  				fakeLabelsActor.GetCurrentUserReturns(configv3.User{Name: "some-user"}, nil)
   507  				fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: "fake-org", GUID: "some-org-guid"})
   508  				cmd.RequiredArgs = flag.LabelsArgs{
   509  					ResourceType: "space",
   510  					ResourceName: "fake-space",
   511  				}
   512  				fakeLabelsActor.GetSpaceLabelsReturns(
   513  					map[string]types.NullString{
   514  						"some-other-label": types.NewNullString("some-other-value"),
   515  						"some-label":       types.NewNullString("some-value"),
   516  					},
   517  					v7action.Warnings{},
   518  					nil)
   519  			})
   520  
   521  			It("doesn't error", func() {
   522  				Expect(executeErr).ToNot(HaveOccurred())
   523  			})
   524  
   525  			It("displays a message that it is retrieving the labels", func() {
   526  				Expect(executeErr).ToNot(HaveOccurred())
   527  				Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Getting labels for space fake-space in org fake-org as some-user...`)))
   528  			})
   529  
   530  			It("retrieves the labels associated with the space", func() {
   531  				Expect(fakeLabelsActor.GetSpaceLabelsCallCount()).To(Equal(1))
   532  			})
   533  
   534  			It("displays the labels that are associated with the space, alphabetically", func() {
   535  				Expect(testUI.Out).To(Say(`key\s+value`))
   536  				Expect(testUI.Out).To(Say(`some-label\s+some-value`))
   537  				Expect(testUI.Out).To(Say(`some-other-label\s+some-other-value`))
   538  			})
   539  
   540  			When("CAPI returns warnings", func() {
   541  				BeforeEach(func() {
   542  					fakeLabelsActor.GetSpaceLabelsReturns(
   543  						map[string]types.NullString{
   544  							"some-other-label": types.NewNullString("some-other-value"),
   545  							"some-label":       types.NewNullString("some-value"),
   546  						},
   547  						v7action.Warnings([]string{"some-warning-1", "some-warning-2"}),
   548  						nil)
   549  				})
   550  
   551  				It("prints all warnings", func() {
   552  					Expect(testUI.Err).To(Say("some-warning-1"))
   553  					Expect(testUI.Err).To(Say("some-warning-2"))
   554  				})
   555  			})
   556  
   557  			When("there is an error retrieving the space", func() {
   558  				BeforeEach(func() {
   559  					fakeLabelsActor.GetSpaceLabelsReturns(
   560  						map[string]types.NullString{},
   561  						v7action.Warnings([]string{"some-warning-1", "some-warning-2"}),
   562  						errors.New("boom"))
   563  				})
   564  
   565  				It("returns the error", func() {
   566  					Expect(executeErr).To(MatchError("boom"))
   567  				})
   568  
   569  				It("still prints all warnings", func() {
   570  					Expect(testUI.Err).To(Say("some-warning-1"))
   571  					Expect(testUI.Err).To(Say("some-warning-2"))
   572  				})
   573  
   574  				It("doesn't say ok", func() {
   575  					Expect(testUI.Out).ToNot(Say("OK"))
   576  				})
   577  			})
   578  		})
   579  
   580  		Describe("for stacks", func() {
   581  			BeforeEach(func() {
   582  				fakeLabelsActor.GetCurrentUserReturns(configv3.User{Name: "some-user"}, nil)
   583  				cmd.RequiredArgs = flag.LabelsArgs{
   584  					ResourceType: "stack",
   585  					ResourceName: "fake-stack",
   586  				}
   587  				fakeLabelsActor.GetStackLabelsReturns(
   588  					map[string]types.NullString{
   589  						"some-other-label": types.NewNullString("some-other-value"),
   590  						"some-label":       types.NewNullString("some-value"),
   591  					},
   592  					v7action.Warnings{},
   593  					nil)
   594  			})
   595  
   596  			It("doesn't error", func() {
   597  				Expect(executeErr).ToNot(HaveOccurred())
   598  			})
   599  
   600  			It("displays a message that it is retrieving the labels", func() {
   601  				Expect(executeErr).ToNot(HaveOccurred())
   602  				Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Getting labels for stack fake-stack as some-user...`)))
   603  			})
   604  
   605  			It("retrieves the labels associated with the stack", func() {
   606  				Expect(fakeLabelsActor.GetStackLabelsCallCount()).To(Equal(1))
   607  			})
   608  
   609  			It("displays the labels that are associated with the stack, alphabetically", func() {
   610  				Expect(testUI.Out).To(Say(`key\s+value`))
   611  				Expect(testUI.Out).To(Say(`some-label\s+some-value`))
   612  				Expect(testUI.Out).To(Say(`some-other-label\s+some-other-value`))
   613  			})
   614  
   615  			When("CAPI returns warnings", func() {
   616  				BeforeEach(func() {
   617  					fakeLabelsActor.GetStackLabelsReturns(
   618  						map[string]types.NullString{
   619  							"some-other-label": types.NewNullString("some-other-value"),
   620  							"some-label":       types.NewNullString("some-value"),
   621  						},
   622  						v7action.Warnings([]string{"some-warning-1", "some-warning-2"}),
   623  						nil)
   624  				})
   625  
   626  				It("prints all warnings", func() {
   627  					Expect(testUI.Err).To(Say("some-warning-1"))
   628  					Expect(testUI.Err).To(Say("some-warning-2"))
   629  				})
   630  			})
   631  
   632  			When("there is an error retrieving the stack", func() {
   633  				BeforeEach(func() {
   634  					fakeLabelsActor.GetStackLabelsReturns(
   635  						map[string]types.NullString{},
   636  						v7action.Warnings([]string{"some-warning-1", "some-warning-2"}),
   637  						errors.New("boom"))
   638  				})
   639  
   640  				It("returns the error", func() {
   641  					Expect(executeErr).To(MatchError("boom"))
   642  				})
   643  
   644  				It("still prints all warnings", func() {
   645  					Expect(testUI.Err).To(Say("some-warning-1"))
   646  					Expect(testUI.Err).To(Say("some-warning-2"))
   647  				})
   648  
   649  				It("doesn't say ok", func() {
   650  					Expect(testUI.Out).ToNot(Say("OK"))
   651  				})
   652  			})
   653  		})
   654  
   655  		Describe("for buildpacks", func() {
   656  			BeforeEach(func() {
   657  				fakeLabelsActor.GetCurrentUserReturns(configv3.User{Name: "some-user"}, nil)
   658  				cmd.RequiredArgs = flag.LabelsArgs{
   659  					ResourceType: "buildpack",
   660  					ResourceName: "my-buildpack",
   661  				}
   662  				fakeLabelsActor.GetBuildpackLabelsReturns(
   663  					map[string]types.NullString{
   664  						"some-other-label": types.NewNullString("some-other-value"),
   665  						"some-label":       types.NewNullString("some-value"),
   666  					},
   667  					v7action.Warnings{},
   668  					nil)
   669  			})
   670  
   671  			It("doesn't error", func() {
   672  				Expect(executeErr).ToNot(HaveOccurred())
   673  			})
   674  
   675  			Describe("the getting-labels message", func() {
   676  				When("the buildpack stack is not specified", func() {
   677  					BeforeEach(func() {
   678  						cmd.BuildpackStack = ""
   679  					})
   680  
   681  					It("displays a message that it is retrieving the labels", func() {
   682  						Expect(executeErr).ToNot(HaveOccurred())
   683  						Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Getting labels for buildpack my-buildpack as some-user...`)))
   684  					})
   685  				})
   686  
   687  				When("the buildpack stack is specified", func() {
   688  					BeforeEach(func() {
   689  						cmd.BuildpackStack = "omelette"
   690  					})
   691  
   692  					It("displays a message that it is retrieving the labels", func() {
   693  						Expect(executeErr).ToNot(HaveOccurred())
   694  						Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Getting labels for buildpack my-buildpack with stack omelette as some-user...`)))
   695  					})
   696  				})
   697  			})
   698  
   699  			It("retrieves the labels associated with the buildpack", func() {
   700  				Expect(fakeLabelsActor.GetBuildpackLabelsCallCount()).To(Equal(1))
   701  			})
   702  
   703  			It("displays the labels that are associated with the buildpack, alphabetically", func() {
   704  				Expect(testUI.Out).To(Say(`key\s+value`))
   705  				Expect(testUI.Out).To(Say(`some-label\s+some-value`))
   706  				Expect(testUI.Out).To(Say(`some-other-label\s+some-other-value`))
   707  			})
   708  
   709  			When("CAPI returns warnings", func() {
   710  				BeforeEach(func() {
   711  					fakeLabelsActor.GetBuildpackLabelsReturns(
   712  						map[string]types.NullString{
   713  							"some-other-label": types.NewNullString("some-other-value"),
   714  							"some-label":       types.NewNullString("some-value"),
   715  						},
   716  						v7action.Warnings([]string{"some-warning-1", "some-warning-2"}),
   717  						nil)
   718  				})
   719  
   720  				It("prints all warnings", func() {
   721  					Expect(testUI.Err).To(Say("some-warning-1"))
   722  					Expect(testUI.Err).To(Say("some-warning-2"))
   723  				})
   724  			})
   725  
   726  			When("there is an error retrieving the buildpack", func() {
   727  				BeforeEach(func() {
   728  					fakeLabelsActor.GetBuildpackLabelsReturns(
   729  						map[string]types.NullString{},
   730  						v7action.Warnings([]string{"some-warning-1", "some-warning-2"}),
   731  						errors.New("boom"))
   732  				})
   733  
   734  				It("returns the error", func() {
   735  					Expect(executeErr).To(MatchError("boom"))
   736  				})
   737  
   738  				It("still prints all warnings", func() {
   739  					Expect(testUI.Err).To(Say("some-warning-1"))
   740  					Expect(testUI.Err).To(Say("some-warning-2"))
   741  				})
   742  
   743  				It("doesn't say ok", func() {
   744  					Expect(testUI.Out).ToNot(Say("OK"))
   745  				})
   746  			})
   747  
   748  			When("specifying --stack", func() {
   749  				BeforeEach(func() {
   750  					cmd.RequiredArgs = flag.LabelsArgs{
   751  						ResourceType: "buildpack",
   752  						ResourceName: "oshkosh",
   753  					}
   754  					cmd.BuildpackStack = "cflinuxfs3"
   755  				})
   756  				It("retrieves the labels when resource type is buildpack", func() {
   757  					Expect(executeErr).ToNot(HaveOccurred())
   758  					Expect(fakeLabelsActor.GetBuildpackLabelsCallCount()).To(Equal(1))
   759  					buildpackName, stackName := fakeLabelsActor.GetBuildpackLabelsArgsForCall(0)
   760  					Expect(buildpackName).To(Equal("oshkosh"))
   761  					Expect(stackName).To(Equal("cflinuxfs3"))
   762  				})
   763  			})
   764  		})
   765  
   766  		Describe("for service-brokers", func() {
   767  			When("There is an error fetching the labels", func() {
   768  				BeforeEach(func() {
   769  					fakeLabelsActor.GetCurrentUserReturns(configv3.User{Name: "some-user"}, nil)
   770  
   771  					cmd.RequiredArgs = flag.LabelsArgs{
   772  						ResourceType: "service-broker",
   773  						ResourceName: "existent-broker",
   774  					}
   775  
   776  					fakeLabelsActor.GetServiceBrokerLabelsReturns(
   777  						map[string]types.NullString{},
   778  						v7action.Warnings([]string{"a warning"}),
   779  						errors.New("some random error"))
   780  				})
   781  
   782  				It("returns an error and prints all warnings", func() {
   783  					Expect(executeErr).To(MatchError("some random error"))
   784  					Expect(testUI.Err).To(Say("a warning"))
   785  				})
   786  
   787  				It("displays a message that it is retrieving the labels", func() {
   788  					Expect(testUI.Out).To(Say("Getting labels for service-broker existent-broker as some-user..."))
   789  				})
   790  			})
   791  
   792  			When("Service broker has labels", func() {
   793  				var labels map[string]types.NullString
   794  				BeforeEach(func() {
   795  					labels = map[string]types.NullString{
   796  						"some-other-label": types.NewNullString("some-other-value"),
   797  						"some-label":       types.NewNullString("some-value"),
   798  					}
   799  
   800  					fakeLabelsActor.GetCurrentUserReturns(configv3.User{Name: "some-user"}, nil)
   801  
   802  					cmd.RequiredArgs = flag.LabelsArgs{
   803  						ResourceType: "service-broker",
   804  						ResourceName: "a-broker",
   805  					}
   806  
   807  					fakeLabelsActor.GetServiceBrokerLabelsReturns(
   808  						labels,
   809  						v7action.Warnings([]string{"some-warning-1", "some-warning-2"}),
   810  						nil,
   811  					)
   812  				})
   813  
   814  				It("displays a message that it is retrieving the labels", func() {
   815  					Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Getting labels for service-broker a-broker as some-user...`)))
   816  				})
   817  
   818  				It("retrieves the labels associated with the broker, alphabetically", func() {
   819  					Expect(testUI.Out).To(Say(`key\s+value`))
   820  					Expect(testUI.Out).To(Say(`some-label\s+some-value`))
   821  					Expect(testUI.Out).To(Say(`some-other-label\s+some-other-value`))
   822  				})
   823  
   824  				It("prints all the warnings", func() {
   825  					Expect(testUI.Err).To(Say("some-warning-1"))
   826  					Expect(testUI.Err).To(Say("some-warning-2"))
   827  				})
   828  			})
   829  		})
   830  
   831  		Describe("for service-instances", func() {
   832  			BeforeEach(func() {
   833  				fakeLabelsActor.GetCurrentUserReturns(configv3.User{Name: "some-user"}, nil)
   834  				fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: "fake-org"})
   835  				fakeConfig.TargetedSpaceReturns(configv3.Space{Name: "fake-space", GUID: "some-space-guid"})
   836  
   837  				setPositionalFlags(&cmd, "service-instance", "my-service-instance")
   838  			})
   839  
   840  			When("error fetching labels", func() {
   841  				BeforeEach(func() {
   842  					fakeLabelsActor.GetServiceInstanceLabelsReturns(
   843  						map[string]types.NullString{},
   844  						[]string{"a warning"},
   845  						errors.New("some random error"),
   846  					)
   847  				})
   848  
   849  				It("returns an error and prints all warnings", func() {
   850  					Expect(executeErr).To(MatchError("some random error"))
   851  					Expect(testUI.Err).To(Say("a warning"))
   852  				})
   853  
   854  				It("displays a message that it is retrieving the labels", func() {
   855  					Expect(testUI.Out).To(Say("Getting labels for service-instance my-service-instance in org fake-org / space fake-space as some-user..."))
   856  				})
   857  			})
   858  
   859  			When("has labels", func() {
   860  				var labels map[string]types.NullString
   861  				BeforeEach(func() {
   862  					labels = map[string]types.NullString{
   863  						"some-other-label": types.NewNullString("some-other-value"),
   864  						"some-label":       types.NewNullString("some-value"),
   865  					}
   866  
   867  					fakeLabelsActor.GetServiceInstanceLabelsReturns(
   868  						labels,
   869  						[]string{"some-warning-1", "some-warning-2"},
   870  						nil,
   871  					)
   872  				})
   873  
   874  				It("queries the right names", func() {
   875  					Expect(fakeLabelsActor.GetServiceInstanceLabelsCallCount()).To(Equal(1))
   876  					serviceInstanceName, spaceGUID := fakeLabelsActor.GetServiceInstanceLabelsArgsForCall(0)
   877  					Expect(serviceInstanceName).To(Equal("my-service-instance"))
   878  					Expect(spaceGUID).To(Equal("some-space-guid"))
   879  				})
   880  
   881  				It("displays a message that it is retrieving the labels", func() {
   882  					Expect(testUI.Out).To(Say("Getting labels for service-instance my-service-instance in org fake-org / space fake-space as some-user..."))
   883  				})
   884  
   885  				It("retrieves the labels alphabetically", func() {
   886  					Expect(testUI.Out).To(Say(`key\s+value`))
   887  					Expect(testUI.Out).To(Say(`some-label\s+some-value`))
   888  					Expect(testUI.Out).To(Say(`some-other-label\s+some-other-value`))
   889  				})
   890  
   891  				It("prints all the warnings", func() {
   892  					Expect(testUI.Err).To(Say("some-warning-1"))
   893  					Expect(testUI.Err).To(Say("some-warning-2"))
   894  				})
   895  			})
   896  		})
   897  
   898  		Describe("for service-offerings", func() {
   899  			BeforeEach(func() {
   900  				fakeLabelsActor.GetCurrentUserReturns(configv3.User{Name: "some-user"}, nil)
   901  
   902  				cmd.RequiredArgs = flag.LabelsArgs{
   903  					ResourceType: "service-offering",
   904  					ResourceName: "my-service-offering",
   905  				}
   906  			})
   907  
   908  			When("There is an error fetching the labels", func() {
   909  				BeforeEach(func() {
   910  					fakeLabelsActor.GetServiceOfferingLabelsReturns(
   911  						map[string]types.NullString{},
   912  						v7action.Warnings([]string{"a warning"}),
   913  						errors.New("some random error"),
   914  					)
   915  				})
   916  
   917  				It("returns an error and prints all warnings", func() {
   918  					Expect(executeErr).To(MatchError("some random error"))
   919  					Expect(testUI.Err).To(Say("a warning"))
   920  				})
   921  
   922  				It("displays a message that it is retrieving the labels", func() {
   923  					Expect(testUI.Out).To(Say("Getting labels for service-offering my-service-offering as some-user..."))
   924  				})
   925  			})
   926  
   927  			When("Service offering has labels", func() {
   928  				var labels map[string]types.NullString
   929  				BeforeEach(func() {
   930  					labels = map[string]types.NullString{
   931  						"some-other-label": types.NewNullString("some-other-value"),
   932  						"some-label":       types.NewNullString("some-value"),
   933  					}
   934  
   935  					fakeLabelsActor.GetServiceOfferingLabelsReturns(
   936  						labels,
   937  						v7action.Warnings([]string{"some-warning-1", "some-warning-2"}),
   938  						nil,
   939  					)
   940  				})
   941  
   942  				It("queries the right names", func() {
   943  					Expect(fakeLabelsActor.GetServiceOfferingLabelsCallCount()).To(Equal(1))
   944  					serviceOfferingName, serviceBrokerName := fakeLabelsActor.GetServiceOfferingLabelsArgsForCall(0)
   945  					Expect(serviceOfferingName).To(Equal("my-service-offering"))
   946  					Expect(serviceBrokerName).To(Equal(""))
   947  				})
   948  
   949  				It("displays a message that it is retrieving the labels", func() {
   950  					Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Getting labels for service-offering my-service-offering as some-user...`)))
   951  				})
   952  
   953  				It("retrieves the labels alphabetically", func() {
   954  					Expect(testUI.Out).To(Say(`key\s+value`))
   955  					Expect(testUI.Out).To(Say(`some-label\s+some-value`))
   956  					Expect(testUI.Out).To(Say(`some-other-label\s+some-other-value`))
   957  				})
   958  
   959  				It("prints all the warnings", func() {
   960  					Expect(testUI.Err).To(Say("some-warning-1"))
   961  					Expect(testUI.Err).To(Say("some-warning-2"))
   962  				})
   963  
   964  				When("a service broker name is specified", func() {
   965  					BeforeEach(func() {
   966  						cmd.ServiceBroker = "my-service-broker"
   967  					})
   968  
   969  					It("queries the right names", func() {
   970  						Expect(fakeLabelsActor.GetServiceOfferingLabelsCallCount()).To(Equal(1))
   971  						serviceOfferingName, serviceBrokerName := fakeLabelsActor.GetServiceOfferingLabelsArgsForCall(0)
   972  						Expect(serviceOfferingName).To(Equal("my-service-offering"))
   973  						Expect(serviceBrokerName).To(Equal("my-service-broker"))
   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-offering my-service-offering from service broker my-service-broker as some-user...`)))
   978  					})
   979  				})
   980  			})
   981  		})
   982  
   983  		Describe("for service-plans", func() {
   984  			BeforeEach(func() {
   985  				fakeLabelsActor.GetCurrentUserReturns(configv3.User{Name: "some-user"}, nil)
   986  
   987  				cmd.RequiredArgs = flag.LabelsArgs{
   988  					ResourceType: "service-plan",
   989  					ResourceName: "my-service-plan",
   990  				}
   991  			})
   992  
   993  			When("there is an error fetching the labels", func() {
   994  				BeforeEach(func() {
   995  					fakeLabelsActor.GetServicePlanLabelsReturns(
   996  						map[string]types.NullString{},
   997  						v7action.Warnings([]string{"a warning"}),
   998  						errors.New("some random error"),
   999  					)
  1000  				})
  1001  
  1002  				It("returns an error and prints all warnings", func() {
  1003  					Expect(executeErr).To(MatchError("some random error"))
  1004  					Expect(testUI.Err).To(Say("a warning"))
  1005  				})
  1006  
  1007  				It("displays a message that it is retrieving the labels", func() {
  1008  					Expect(testUI.Out).To(Say("Getting labels for service-plan my-service-plan as some-user..."))
  1009  				})
  1010  			})
  1011  
  1012  			When("service plan has labels", func() {
  1013  				var labels map[string]types.NullString
  1014  				BeforeEach(func() {
  1015  					labels = map[string]types.NullString{
  1016  						"some-other-label": types.NewNullString("some-other-value"),
  1017  						"some-label":       types.NewNullString("some-value"),
  1018  					}
  1019  
  1020  					fakeLabelsActor.GetServicePlanLabelsReturns(
  1021  						labels,
  1022  						v7action.Warnings([]string{"some-warning-1", "some-warning-2"}),
  1023  						nil,
  1024  					)
  1025  				})
  1026  
  1027  				It("queries the right names", func() {
  1028  					Expect(fakeLabelsActor.GetServicePlanLabelsCallCount()).To(Equal(1))
  1029  					servicePlanName, serviceOfferingName, serviceBrokerName := fakeLabelsActor.GetServicePlanLabelsArgsForCall(0)
  1030  					Expect(servicePlanName).To(Equal("my-service-plan"))
  1031  					Expect(serviceOfferingName).To(Equal(""))
  1032  					Expect(serviceBrokerName).To(Equal(""))
  1033  				})
  1034  
  1035  				It("displays a message that it is retrieving the labels", func() {
  1036  					Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Getting labels for service-plan my-service-plan as some-user...`)))
  1037  				})
  1038  
  1039  				It("retrieves the labels alphabetically", func() {
  1040  					Expect(testUI.Out).To(Say(`key\s+value`))
  1041  					Expect(testUI.Out).To(Say(`some-label\s+some-value`))
  1042  					Expect(testUI.Out).To(Say(`some-other-label\s+some-other-value`))
  1043  				})
  1044  
  1045  				It("prints all the warnings", func() {
  1046  					Expect(testUI.Err).To(Say("some-warning-1"))
  1047  					Expect(testUI.Err).To(Say("some-warning-2"))
  1048  				})
  1049  
  1050  				Context("command options", func() {
  1051  					Context("service broker and service offering", func() {
  1052  						BeforeEach(func() {
  1053  							cmd.ServiceBroker = "my-service-broker"
  1054  							cmd.ServiceOffering = "my-service-offering"
  1055  						})
  1056  
  1057  						It("queries the right names", func() {
  1058  							Expect(fakeLabelsActor.GetServicePlanLabelsCallCount()).To(Equal(1))
  1059  							servicePlanName, serviceOfferingName, serviceBrokerName := fakeLabelsActor.GetServicePlanLabelsArgsForCall(0)
  1060  							Expect(servicePlanName).To(Equal("my-service-plan"))
  1061  							Expect(serviceBrokerName).To(Equal("my-service-broker"))
  1062  							Expect(serviceOfferingName).To(Equal("my-service-offering"))
  1063  						})
  1064  
  1065  						It("displays a message that it is retrieving the labels", func() {
  1066  							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...`)))
  1067  						})
  1068  					})
  1069  
  1070  					Context("service broker", func() {
  1071  						BeforeEach(func() {
  1072  							cmd.ServiceBroker = "my-service-broker"
  1073  						})
  1074  
  1075  						It("displays a message that it is retrieving the labels", func() {
  1076  							Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Getting labels for service-plan my-service-plan from service broker my-service-broker as some-user...`)))
  1077  						})
  1078  					})
  1079  
  1080  					Context("service offering", func() {
  1081  						BeforeEach(func() {
  1082  							cmd.ServiceOffering = "my-service-offering"
  1083  						})
  1084  
  1085  						It("displays a message that it is retrieving the labels", func() {
  1086  							Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Getting labels for service-plan my-service-plan from service offering my-service-offering as some-user...`)))
  1087  						})
  1088  					})
  1089  				})
  1090  			})
  1091  		})
  1092  	})
  1093  })