github.com/niteshexa/cloudfoundry_cli@v7.1.0+incompatible/command/v7/label_updater_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/translatableerror"
    11  	. "code.cloudfoundry.org/cli/command/v7"
    12  	"code.cloudfoundry.org/cli/command/v7/v7fakes"
    13  	"code.cloudfoundry.org/cli/types"
    14  	"code.cloudfoundry.org/cli/util/configv3"
    15  	"code.cloudfoundry.org/cli/util/ui"
    16  	. "github.com/onsi/ginkgo"
    17  	. "github.com/onsi/ginkgo/extensions/table"
    18  	. "github.com/onsi/gomega"
    19  	. "github.com/onsi/gomega/gbytes"
    20  )
    21  
    22  func labelSubcommands(subcommandsToRemove ...string) []TableEntry {
    23  	all := []string{
    24  		"app",
    25  		"buildpack",
    26  		"domain",
    27  		"org",
    28  		"route",
    29  		"space",
    30  		"stack",
    31  		"service-broker",
    32  		"service-offering",
    33  		"service-plan",
    34  	}
    35  	var entries []TableEntry
    36  	for _, labelSubcommand := range all {
    37  		remove := false
    38  		for _, subCommand := range subcommandsToRemove {
    39  			if labelSubcommand == subCommand {
    40  				remove = true
    41  				break
    42  			}
    43  		}
    44  		if !remove {
    45  			entries = append(entries, Entry(labelSubcommand, labelSubcommand))
    46  		}
    47  	}
    48  	return entries
    49  }
    50  
    51  var _ = Describe("LabelUpdater", func() {
    52  
    53  	var (
    54  		cmd             LabelUpdater
    55  		fakeActor       *v7fakes.FakeActor
    56  		fakeConfig      *commandfakes.FakeConfig
    57  		fakeSharedActor *commandfakes.FakeSharedActor
    58  		testUI          *ui.UI
    59  		targetResource  TargetResource
    60  		labels          map[string]types.NullString
    61  	)
    62  
    63  	BeforeEach(func() {
    64  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    65  		fakeConfig = new(commandfakes.FakeConfig)
    66  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    67  		fakeActor = new(v7fakes.FakeActor)
    68  		cmd = LabelUpdater{
    69  			UI:          testUI,
    70  			Config:      fakeConfig,
    71  			SharedActor: fakeSharedActor,
    72  			Actor:       fakeActor,
    73  		}
    74  	})
    75  
    76  	Context("shared validations", func() {
    77  		var resourceName string
    78  
    79  		BeforeEach(func() {
    80  			testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    81  			fakeActor = new(v7fakes.FakeActor)
    82  			fakeConfig = new(commandfakes.FakeConfig)
    83  			fakeSharedActor = new(commandfakes.FakeSharedActor)
    84  			cmd = LabelUpdater{
    85  				Actor:       fakeActor,
    86  				UI:          testUI,
    87  				Config:      fakeConfig,
    88  				SharedActor: fakeSharedActor,
    89  			}
    90  		})
    91  
    92  		When("fetching the current user's name fails", func() {
    93  			BeforeEach(func() {
    94  				targetResource = TargetResource{
    95  					ResourceType: "anything",
    96  					ResourceName: resourceName,
    97  				}
    98  				fakeConfig.CurrentUserNameReturns("some-user", errors.New("boom"))
    99  			})
   100  
   101  			It("returns an error", func() {
   102  				err := cmd.Execute(targetResource, labels)
   103  				Expect(err).To(MatchError("boom"))
   104  			})
   105  		})
   106  
   107  		When("an unrecognized resource type is specified", func() {
   108  			BeforeEach(func() {
   109  				resourceName = "some-unrecognized-resource"
   110  				cmd = LabelUpdater{
   111  					Actor:       fakeActor,
   112  					UI:          testUI,
   113  					Config:      fakeConfig,
   114  					SharedActor: fakeSharedActor,
   115  				}
   116  				targetResource = TargetResource{
   117  					ResourceType: "unrecognized-resource",
   118  					ResourceName: resourceName,
   119  				}
   120  			})
   121  
   122  			It("errors", func() {
   123  				executeErr := cmd.Execute(targetResource, labels)
   124  
   125  				Expect(executeErr).To(MatchError("Unsupported resource type of 'unrecognized-resource'"))
   126  			})
   127  		})
   128  
   129  		DescribeTable(
   130  			"Failure when --stack is combined with anything other than 'buildpack'",
   131  			func(resourceType string) {
   132  				targetResource = TargetResource{
   133  					ResourceType:   resourceType,
   134  					BuildpackStack: "cflinuxfs3",
   135  				}
   136  
   137  				err := cmd.Execute(targetResource, nil)
   138  
   139  				argumentCombinationError := translatableerror.ArgumentCombinationError{
   140  					Args: []string{strings.ToLower(resourceType), "--stack, -s"},
   141  				}
   142  				Expect(err).To(MatchError(argumentCombinationError))
   143  			},
   144  			labelSubcommands("buildpack")...,
   145  		)
   146  
   147  		DescribeTable(
   148  			"Failure when --broker is combined with anything other than 'service-offering' or 'service-plan'",
   149  			func(resourceType string) {
   150  				targetResource = TargetResource{
   151  					ResourceType:  resourceType,
   152  					ServiceBroker: "my-broker",
   153  				}
   154  
   155  				err := cmd.Execute(targetResource, nil)
   156  
   157  				argumentCombinationError := translatableerror.ArgumentCombinationError{
   158  					Args: []string{strings.ToLower(resourceType), "--broker, -b"},
   159  				}
   160  				Expect(err).To(MatchError(argumentCombinationError))
   161  			},
   162  			labelSubcommands("service-offering", "service-plan")...,
   163  		)
   164  
   165  		DescribeTable(
   166  			"Failure when --offering is combined with anything other than 'service-plan'",
   167  			func(resourceType string) {
   168  				targetResource = TargetResource{
   169  					ResourceType:    resourceType,
   170  					ServiceOffering: "my-service-offering",
   171  				}
   172  
   173  				err := cmd.Execute(targetResource, nil)
   174  
   175  				argumentCombinationError := translatableerror.ArgumentCombinationError{
   176  					Args: []string{strings.ToLower(resourceType), "--offering, -o"},
   177  				}
   178  				Expect(err).To(MatchError(argumentCombinationError))
   179  			},
   180  			labelSubcommands("service-plan")...,
   181  		)
   182  
   183  		DescribeTable(
   184  			"when checking the target fails",
   185  			func(resourceType string) {
   186  				fakeSharedActor.CheckTargetReturns(errors.New("Target not found"))
   187  				targetResource = TargetResource{
   188  					ResourceType: resourceType,
   189  				}
   190  
   191  				err := cmd.Execute(targetResource, nil)
   192  				Expect(err).To(MatchError("Target not found"))
   193  			},
   194  			labelSubcommands()...,
   195  		)
   196  
   197  		DescribeTable(
   198  			"checking that the user is logged in",
   199  			func(resourceType string) {
   200  				targetResource = TargetResource{
   201  					ResourceType: resourceType,
   202  				}
   203  				err := cmd.Execute(targetResource, nil)
   204  
   205  				Expect(err).NotTo(HaveOccurred())
   206  				Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
   207  				checkOrg, checkSpace := fakeSharedActor.CheckTargetArgsForCall(0)
   208  
   209  				switch resourceType {
   210  				case "app", "route":
   211  					Expect(checkOrg).To(BeTrue())
   212  					Expect(checkSpace).To(BeTrue())
   213  				case "space":
   214  					Expect(checkOrg).To(BeTrue())
   215  					Expect(checkSpace).To(BeFalse())
   216  				default:
   217  					Expect(checkOrg).To(BeFalse())
   218  					Expect(checkSpace).To(BeFalse())
   219  				}
   220  			},
   221  			labelSubcommands()...,
   222  		)
   223  	})
   224  
   225  	When("updating labels on apps", func() {
   226  		var (
   227  			appName     string
   228  			executeErr  error
   229  			expectedMap map[string]types.NullString
   230  		)
   231  
   232  		BeforeEach(func() {
   233  			appName = "some-app"
   234  			targetResource = TargetResource{
   235  				ResourceType: "app",
   236  				ResourceName: appName,
   237  			}
   238  
   239  			fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: "fake-org"})
   240  			fakeConfig.TargetedSpaceReturns(configv3.Space{Name: "fake-space", GUID: "some-space-guid"})
   241  			fakeConfig.CurrentUserNameReturns("some-user", nil)
   242  
   243  			expectedMap = map[string]types.NullString{
   244  				"some-label":     types.NewNullString("some-value"),
   245  				"some-other-key": types.NewNullString()}
   246  			labels = expectedMap
   247  		})
   248  
   249  		JustBeforeEach(func() {
   250  			executeErr = cmd.Execute(targetResource, labels)
   251  		})
   252  
   253  		When("updating the app labels succeeds", func() {
   254  			BeforeEach(func() {
   255  				fakeActor.UpdateApplicationLabelsByApplicationNameReturns(v7action.Warnings{"some-warning-1", "some-warning-2"}, nil)
   256  			})
   257  
   258  			It("prints all warnings and does not return an error ", func() {
   259  				Expect(executeErr).ToNot(HaveOccurred())
   260  				Expect(testUI.Err).To(Say("some-warning-1"))
   261  				Expect(testUI.Err).To(Say("some-warning-2"))
   262  			})
   263  
   264  			It("passes the correct parameters into the actor", func() {
   265  				Expect(fakeActor.UpdateApplicationLabelsByApplicationNameCallCount()).To(Equal(1))
   266  				actualAppName, spaceGUID, labelsMap := fakeActor.UpdateApplicationLabelsByApplicationNameArgsForCall(0)
   267  				Expect(actualAppName).To(Equal(appName))
   268  				Expect(spaceGUID).To(Equal("some-space-guid"))
   269  				Expect(labelsMap).To(Equal(expectedMap))
   270  			})
   271  		})
   272  
   273  		When("the resource type argument is not lowercase", func() {
   274  			BeforeEach(func() {
   275  				targetResource.ResourceType = "aPp"
   276  			})
   277  
   278  			It("calls the right actor", func() {
   279  				Expect(executeErr).ToNot(HaveOccurred())
   280  				Expect(fakeActor.UpdateApplicationLabelsByApplicationNameCallCount()).To(Equal(1))
   281  			})
   282  
   283  			It("displays a message in the right case", func() {
   284  				Expect(testUI.Out).To(Say("(.*) label\\(s\\) for app (.*)"))
   285  				Expect(testUI.Out).To(Say("OK"))
   286  			})
   287  		})
   288  
   289  		When("updating the app labels fails", func() {
   290  			BeforeEach(func() {
   291  				fakeActor.UpdateApplicationLabelsByApplicationNameReturns(v7action.Warnings{"some-warning-1", "some-warning-2"},
   292  					errors.New("api call failed"))
   293  			})
   294  
   295  			It("prints all warnings", func() {
   296  				Expect(testUI.Err).To(Say("some-warning-1"))
   297  				Expect(testUI.Err).To(Say("some-warning-2"))
   298  			})
   299  
   300  			It("returns the error", func() {
   301  				Expect(executeErr).To(MatchError("api call failed"))
   302  			})
   303  		})
   304  
   305  		Context("shows the right update message with org and space", func() {
   306  			When("Unsetting labels", func() {
   307  				BeforeEach(func() {
   308  					cmd.Action = Unset
   309  					//FIXME do we want to change the labels to all have nil values?
   310  				})
   311  				It("shows 'Removing' as action", func() {
   312  					Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Removing label(s) for app %s in org fake-org / space fake-space as some-user...`), appName))
   313  				})
   314  			})
   315  
   316  			When("Setting labels", func() {
   317  				BeforeEach(func() {
   318  					cmd.Action = Set
   319  					//FIXME do we want to change the labels to all have not nil values?
   320  				})
   321  
   322  				It("shows 'Setting' as action", func() {
   323  					Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Setting label(s) for app %s in org fake-org / space fake-space as some-user...`), appName))
   324  				})
   325  			})
   326  		})
   327  	})
   328  
   329  	When("updating labels on buildpacks", func() {
   330  		var resourceName string
   331  		var expectedMap map[string]types.NullString
   332  		var executeErr error
   333  
   334  		BeforeEach(func() {
   335  			resourceName = "buildpack-name"
   336  			targetResource = TargetResource{
   337  				ResourceType: "buildpack",
   338  				ResourceName: resourceName,
   339  			}
   340  
   341  			fakeSharedActor.CheckTargetReturns(nil)
   342  			fakeConfig.CurrentUserNameReturns("some-user", nil)
   343  
   344  			expectedMap = map[string]types.NullString{
   345  				"some-label":     types.NewNullString("some-value"),
   346  				"some-other-key": types.NewNullString()}
   347  			labels = expectedMap
   348  
   349  			fakeActor.UpdateBuildpackLabelsByBuildpackNameAndStackReturns(
   350  				v7action.Warnings([]string{"some-warning-1", "some-warning-2"}),
   351  				nil,
   352  			)
   353  
   354  		})
   355  
   356  		JustBeforeEach(func() {
   357  			executeErr = cmd.Execute(targetResource, labels)
   358  		})
   359  
   360  		When("updating the buildpack labels succeeds", func() {
   361  			When("the stack is specified", func() {
   362  				BeforeEach(func() {
   363  					targetResource.BuildpackStack = "globinski"
   364  				})
   365  
   366  				It("passes the right parameters", func() {
   367  					Expect(fakeActor.UpdateBuildpackLabelsByBuildpackNameAndStackCallCount()).To(Equal(1))
   368  					name, stack, labels := fakeActor.UpdateBuildpackLabelsByBuildpackNameAndStackArgsForCall(0)
   369  					Expect(name).To(Equal(resourceName), "failed to pass buildpack name")
   370  					Expect(stack).To(Equal("globinski"), "failed to pass stack name")
   371  					Expect(labels).To(BeEquivalentTo(expectedMap))
   372  				})
   373  
   374  				It("prints all warnings and does not error ", func() {
   375  					Expect(executeErr).ToNot(HaveOccurred())
   376  					Expect(testUI.Err).To(Say("some-warning-1"))
   377  					Expect(testUI.Err).To(Say("some-warning-2"))
   378  				})
   379  			})
   380  
   381  			When("the stack is not specified", func() {
   382  				It("passes the right parameters", func() {
   383  					Expect(fakeActor.UpdateBuildpackLabelsByBuildpackNameAndStackCallCount()).To(Equal(1))
   384  					name, stack, labels := fakeActor.UpdateBuildpackLabelsByBuildpackNameAndStackArgsForCall(0)
   385  					Expect(name).To(Equal(resourceName), "failed to pass buildpack name")
   386  					Expect(stack).To(Equal(""), "failed to pass stack name")
   387  					Expect(labels).To(BeEquivalentTo(expectedMap))
   388  				})
   389  
   390  				It("prints all warnings and does not error ", func() {
   391  					Expect(executeErr).ToNot(HaveOccurred())
   392  					Expect(testUI.Err).To(Say("some-warning-1"))
   393  					Expect(testUI.Err).To(Say("some-warning-2"))
   394  				})
   395  			})
   396  		})
   397  
   398  		When("the resource type is not lowercase", func() {
   399  			BeforeEach(func() {
   400  				targetResource = TargetResource{
   401  					ResourceType:   "bUiLdPaCk",
   402  					ResourceName:   resourceName,
   403  					BuildpackStack: "globinski",
   404  				}
   405  				expectedMap = map[string]types.NullString{
   406  					"some-label":     types.NewNullString("some-value"),
   407  					"some-other-key": types.NewNullString()}
   408  			})
   409  
   410  			It("calls the right actor", func() {
   411  				Expect(executeErr).ToNot(HaveOccurred())
   412  				Expect(fakeActor.UpdateBuildpackLabelsByBuildpackNameAndStackCallCount()).To(Equal(1))
   413  			})
   414  
   415  			It("displays a message in the right case", func() {
   416  				Expect(testUI.Out).To(Say("(.*) label\\(s\\) for buildpack (.*)"))
   417  				Expect(testUI.Out).To(Say("OK"))
   418  			})
   419  		})
   420  
   421  		When("updating the buildpack labels fails", func() {
   422  			BeforeEach(func() {
   423  				fakeActor.UpdateBuildpackLabelsByBuildpackNameAndStackReturns(v7action.Warnings{"some-warning-1", "some-warning-2"},
   424  					errors.New("api call failed"))
   425  			})
   426  
   427  			It("returns the error and prints all warnings", func() {
   428  				Expect(executeErr).To(MatchError("api call failed"))
   429  				Expect(testUI.Err).To(Say("some-warning-1"))
   430  				Expect(testUI.Err).To(Say("some-warning-2"))
   431  			})
   432  		})
   433  
   434  		Context("shows the right update message with correct stack and action", func() {
   435  			When("Unsetting labels", func() {
   436  				BeforeEach(func() {
   437  					cmd.Action = Unset
   438  				})
   439  
   440  				When("stack is passed", func() {
   441  					BeforeEach(func() {
   442  						targetResource.BuildpackStack = "globinski"
   443  					})
   444  					It("shows 'Removing' as action", func() {
   445  						Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Removing label(s) for buildpack %s with stack %s as some-user...`), resourceName, targetResource.BuildpackStack))
   446  					})
   447  				})
   448  
   449  				When("stack is not passed", func() {
   450  					It("shows 'Removing' as action", func() {
   451  						Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Removing label(s) for buildpack %s as some-user...`), resourceName))
   452  					})
   453  				})
   454  			})
   455  
   456  			When("Setting labels", func() {
   457  				BeforeEach(func() {
   458  					cmd.Action = Set
   459  					//FIXME do we want to change the labels to all have not nil values?
   460  				})
   461  
   462  				When("stack is passed", func() {
   463  					BeforeEach(func() {
   464  						targetResource.BuildpackStack = "globinski"
   465  					})
   466  					It("shows 'Setting' as action", func() {
   467  						Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Setting label(s) for buildpack %s with stack %s as some-user...`), resourceName, targetResource.BuildpackStack))
   468  					})
   469  				})
   470  
   471  				When("stack is not passed", func() {
   472  					It("shows 'Setting' as action", func() {
   473  						Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Setting label(s) for buildpack %s as some-user...`), resourceName))
   474  					})
   475  				})
   476  			})
   477  		})
   478  	})
   479  
   480  	When("updating labels in domains", func() {
   481  		var (
   482  			domainName  string
   483  			executeErr  error
   484  			expectedMap map[string]types.NullString
   485  		)
   486  
   487  		BeforeEach(func() {
   488  			domainName = "example.com"
   489  			targetResource = TargetResource{
   490  				ResourceType: "domain",
   491  				ResourceName: domainName,
   492  			}
   493  			expectedMap = map[string]types.NullString{
   494  				"some-label":     types.NewNullString("some-value"),
   495  				"some-other-key": types.NewNullString()}
   496  			labels = expectedMap
   497  
   498  			fakeConfig.CurrentUserNameReturns("some-user", nil)
   499  			fakeActor.UpdateDomainLabelsByDomainNameReturns(
   500  				v7action.Warnings{"some-warning-1", "some-warning-2"},
   501  				nil,
   502  			)
   503  		})
   504  
   505  		JustBeforeEach(func() {
   506  			executeErr = cmd.Execute(targetResource, labels)
   507  		})
   508  
   509  		When("updating the labels succeeds", func() {
   510  			It("prints all warnings and does not return an error ", func() {
   511  				Expect(executeErr).ToNot(HaveOccurred())
   512  				Expect(testUI.Err).To(Say("some-warning-1"))
   513  				Expect(testUI.Err).To(Say("some-warning-2"))
   514  			})
   515  
   516  			It("passes the correct parameters into the actor", func() {
   517  				Expect(fakeActor.UpdateDomainLabelsByDomainNameCallCount()).To(Equal(1))
   518  				name, labels := fakeActor.UpdateDomainLabelsByDomainNameArgsForCall(0)
   519  				Expect(name).To(Equal(domainName), "failed to pass domain name")
   520  				Expect(labels).To(BeEquivalentTo(expectedMap))
   521  			})
   522  		})
   523  
   524  		When("the resource type argument is not lowercase", func() {
   525  			BeforeEach(func() {
   526  				targetResource.ResourceType = "DoMaiN"
   527  			})
   528  
   529  			It("calls the right actor", func() {
   530  				Expect(executeErr).ToNot(HaveOccurred())
   531  				Expect(fakeActor.UpdateDomainLabelsByDomainNameCallCount()).To(Equal(1))
   532  			})
   533  
   534  			It("displays a message in the right case", func() {
   535  				Expect(testUI.Out).To(Say("(.*) label\\(s\\) for domain (.*)"))
   536  				Expect(testUI.Out).To(Say("OK"))
   537  			})
   538  		})
   539  
   540  		When("updating the domain labels fails", func() {
   541  			BeforeEach(func() {
   542  				fakeActor.UpdateDomainLabelsByDomainNameReturns(
   543  					v7action.Warnings{"some-warning-1", "some-warning-2"},
   544  					errors.New("api call failed"))
   545  			})
   546  
   547  			It("returns the error and prints all warnings", func() {
   548  				Expect(executeErr).To(MatchError("api call failed"))
   549  				Expect(testUI.Err).To(Say("some-warning-1"))
   550  				Expect(testUI.Err).To(Say("some-warning-2"))
   551  			})
   552  		})
   553  
   554  		Context("shows the right update message", func() {
   555  			When("Unsetting labels", func() {
   556  				BeforeEach(func() {
   557  					cmd.Action = Unset
   558  				})
   559  
   560  				It("shows 'Removing' as action", func() {
   561  					Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Removing label(s) for domain %s as some-user...`), domainName))
   562  				})
   563  			})
   564  
   565  			When("Setting labels", func() {
   566  				BeforeEach(func() {
   567  					cmd.Action = Set
   568  				})
   569  
   570  				It("shows 'Setting' as action", func() {
   571  					Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Setting label(s) for domain %s as some-user...`), domainName))
   572  				})
   573  			})
   574  		})
   575  	})
   576  
   577  	When("updating labels on orgs", func() {
   578  		var (
   579  			executeErr  error
   580  			orgName     = "some-org"
   581  			expectedMap map[string]types.NullString
   582  		)
   583  
   584  		BeforeEach(func() {
   585  			targetResource = TargetResource{
   586  				ResourceType: "org",
   587  				ResourceName: orgName,
   588  			}
   589  			fakeSharedActor.CheckTargetReturns(nil)
   590  			fakeConfig.CurrentUserNameReturns("some-user", nil)
   591  
   592  			expectedMap = map[string]types.NullString{
   593  				"some-label":     types.NewNullString("some-value"),
   594  				"some-other-key": types.NewNullString()}
   595  			labels = expectedMap
   596  
   597  			fakeActor.UpdateOrganizationLabelsByOrganizationNameReturns(
   598  				v7action.Warnings{"some-warning-1", "some-warning-2"},
   599  				nil)
   600  		})
   601  
   602  		JustBeforeEach(func() {
   603  			executeErr = cmd.Execute(targetResource, labels)
   604  		})
   605  
   606  		When("updating the orgs labels succeeds", func() {
   607  			It("does not return an error and prints all warnings", func() {
   608  				Expect(executeErr).ToNot(HaveOccurred())
   609  				Expect(testUI.Err).To(Say("some-warning-1"))
   610  				Expect(testUI.Err).To(Say("some-warning-2"))
   611  			})
   612  
   613  			It("passes the correct parameters into the actor", func() {
   614  				Expect(fakeActor.UpdateOrganizationLabelsByOrganizationNameCallCount()).To(Equal(1))
   615  				actualOrgName, labelsMap := fakeActor.UpdateOrganizationLabelsByOrganizationNameArgsForCall(0)
   616  				Expect(actualOrgName).To(Equal(orgName))
   617  				Expect(labelsMap).To(Equal(expectedMap))
   618  			})
   619  		})
   620  
   621  		When("the resource type argument is not lowercase", func() {
   622  			BeforeEach(func() {
   623  				targetResource.ResourceType = "OrG"
   624  			})
   625  
   626  			It("calls the right actor", func() {
   627  				Expect(executeErr).ToNot(HaveOccurred())
   628  				Expect(fakeActor.UpdateOrganizationLabelsByOrganizationNameCallCount()).To(Equal(1))
   629  			})
   630  
   631  			It("displays a message in the right case", func() {
   632  				Expect(testUI.Out).To(Say("(.*) label\\(s\\) for org (.*)"))
   633  				Expect(testUI.Out).To(Say("OK"))
   634  			})
   635  		})
   636  
   637  		When("updating the org labels fails", func() {
   638  			BeforeEach(func() {
   639  				fakeActor.UpdateOrganizationLabelsByOrganizationNameReturns(
   640  					v7action.Warnings{"some-warning-1", "some-warning-2"},
   641  					errors.New("api call failed"))
   642  			})
   643  
   644  			It("returns the error and prints all warnings", func() {
   645  				Expect(executeErr).To(MatchError("api call failed"))
   646  				Expect(testUI.Err).To(Say("some-warning-1"))
   647  				Expect(testUI.Err).To(Say("some-warning-2"))
   648  			})
   649  		})
   650  
   651  		Context("shows the right update message", func() {
   652  			When("Unsetting labels", func() {
   653  				BeforeEach(func() {
   654  					cmd.Action = Unset
   655  				})
   656  
   657  				It("shows 'Removing' as action", func() {
   658  					Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Removing label(s) for org %s as some-user...`), orgName))
   659  				})
   660  			})
   661  
   662  			When("Setting labels", func() {
   663  				BeforeEach(func() {
   664  					cmd.Action = Set
   665  				})
   666  
   667  				It("shows 'Setting' as action", func() {
   668  					Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Setting label(s) for org %s as some-user...`), orgName))
   669  				})
   670  			})
   671  		})
   672  	})
   673  
   674  	When("updating labels on routes", func() {
   675  		var (
   676  			resourceName string
   677  			expectedMap  map[string]types.NullString
   678  			executeErr   error
   679  		)
   680  
   681  		BeforeEach(func() {
   682  			resourceName = "some-route.example.com"
   683  			targetResource = TargetResource{
   684  				ResourceType: "route",
   685  				ResourceName: resourceName,
   686  			}
   687  
   688  			expectedMap = map[string]types.NullString{
   689  				"some-label":     types.NewNullString("some-value"),
   690  				"some-other-key": types.NewNullString()}
   691  			labels = expectedMap
   692  
   693  			fakeConfig.CurrentUserNameReturns("some-user", nil)
   694  			fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: "fake-org"})
   695  			fakeConfig.TargetedSpaceReturns(configv3.Space{Name: "fake-space", GUID: "space-guid"})
   696  			fakeActor.UpdateRouteLabelsReturns(v7action.Warnings{"some-warning-1", "some-warning-2"},
   697  				nil)
   698  		})
   699  
   700  		JustBeforeEach(func() {
   701  			executeErr = cmd.Execute(targetResource, labels)
   702  		})
   703  
   704  		When("updating the route labels succeeds", func() {
   705  			It("doesn't error and prints all warnings", func() {
   706  				Expect(executeErr).ToNot(HaveOccurred())
   707  				Expect(testUI.Err).To(Say("some-warning-1"))
   708  				Expect(testUI.Err).To(Say("some-warning-2"))
   709  			})
   710  
   711  			It("passes the right parameters to the actor", func() {
   712  				Expect(fakeActor.UpdateRouteLabelsCallCount()).To(Equal(1))
   713  				name, spaceGUID, labels := fakeActor.UpdateRouteLabelsArgsForCall(0)
   714  				Expect(name).To(Equal(resourceName), "failed to pass route name")
   715  				Expect(labels).To(BeEquivalentTo(expectedMap))
   716  				Expect(spaceGUID).To(Equal("space-guid"))
   717  			})
   718  		})
   719  
   720  		When("the resource type argument is not lowercase", func() {
   721  			BeforeEach(func() {
   722  				targetResource.ResourceType = "rouTE"
   723  			})
   724  
   725  			It("calls the right actor", func() {
   726  				Expect(executeErr).ToNot(HaveOccurred())
   727  				Expect(fakeActor.UpdateRouteLabelsCallCount()).To(Equal(1))
   728  			})
   729  
   730  			It("displays a message in the right case", func() {
   731  				Expect(testUI.Out).To(Say("(.*) label\\(s\\) for route (.*)"))
   732  				Expect(testUI.Out).To(Say("OK"))
   733  			})
   734  		})
   735  
   736  		When("updating the route labels fails", func() {
   737  			BeforeEach(func() {
   738  				fakeActor.UpdateRouteLabelsReturns(v7action.Warnings{"some-warning-1", "some-warning-2"},
   739  					errors.New("api call failed"))
   740  			})
   741  
   742  			It("returns the error and prints all warnings", func() {
   743  				Expect(executeErr).To(MatchError("api call failed"))
   744  				Expect(testUI.Err).To(Say("some-warning-1"))
   745  				Expect(testUI.Err).To(Say("some-warning-2"))
   746  			})
   747  		})
   748  
   749  		Context("shows the right update message", func() {
   750  			When("Unsetting labels", func() {
   751  				BeforeEach(func() {
   752  					cmd.Action = Unset
   753  				})
   754  
   755  				It("shows 'Removing' as action", func() {
   756  					Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Removing label(s) for route %s in org fake-org / space fake-space as some-user...`), resourceName))
   757  				})
   758  			})
   759  
   760  			When("Setting labels", func() {
   761  				BeforeEach(func() {
   762  					cmd.Action = Set
   763  				})
   764  
   765  				It("shows 'Setting' as action", func() {
   766  					Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Setting label(s) for route %s in org fake-org / space fake-space as some-user...`), resourceName))
   767  				})
   768  			})
   769  		})
   770  	})
   771  
   772  	When("updating labels on service-broker", func() {
   773  		const expectedServiceBrokerName = "my-broker"
   774  		var (
   775  			expectedMap map[string]types.NullString
   776  			executeErr  error
   777  		)
   778  
   779  		BeforeEach(func() {
   780  			targetResource = TargetResource{
   781  				ResourceType: "service-broker",
   782  				ResourceName: expectedServiceBrokerName,
   783  			}
   784  
   785  			fakeConfig.CurrentUserNameReturns("some-user", nil)
   786  			expectedMap = map[string]types.NullString{
   787  				"some-label":     types.NewNullString("some-value"),
   788  				"some-other-key": types.NewNullString(),
   789  			}
   790  			labels = expectedMap
   791  		})
   792  
   793  		JustBeforeEach(func() {
   794  			executeErr = cmd.Execute(targetResource, labels)
   795  		})
   796  
   797  		When("updating the service-broker labels succeeds", func() {
   798  			BeforeEach(func() {
   799  				fakeActor.UpdateServiceBrokerLabelsByServiceBrokerNameReturns(v7action.Warnings{"some-warning-1", "some-warning-2"},
   800  					nil)
   801  			})
   802  
   803  			It("prints all warnings and does not return an error", func() {
   804  				Expect(executeErr).ToNot(HaveOccurred())
   805  				Expect(testUI.Err).To(Say("some-warning-1"))
   806  				Expect(testUI.Err).To(Say("some-warning-2"))
   807  			})
   808  
   809  			It("passes the correct parameters into the actor", func() {
   810  				Expect(fakeActor.UpdateServiceBrokerLabelsByServiceBrokerNameCallCount()).To(Equal(1))
   811  				serviceBrokerName, labelsMap := fakeActor.UpdateServiceBrokerLabelsByServiceBrokerNameArgsForCall(0)
   812  				Expect(serviceBrokerName).To(Equal(expectedServiceBrokerName))
   813  				Expect(labelsMap).To(Equal(expectedMap))
   814  			})
   815  		})
   816  
   817  		When("the resource type argument is not lowercase", func() {
   818  			BeforeEach(func() {
   819  				fakeActor.UpdateServiceBrokerLabelsByServiceBrokerNameReturns(v7action.Warnings{"some-warning-1", "some-warning-2"},
   820  					nil)
   821  
   822  				targetResource.ResourceType = "sErVice-BroKer"
   823  			})
   824  
   825  			It("calls the right actor", func() {
   826  				Expect(executeErr).ToNot(HaveOccurred())
   827  				Expect(fakeActor.UpdateServiceBrokerLabelsByServiceBrokerNameCallCount()).To(Equal(1))
   828  			})
   829  
   830  			It("displays a message in the right case", func() {
   831  				Expect(testUI.Out).To(Say("(.*) label\\(s\\) for service-broker (.*)"))
   832  				Expect(testUI.Out).To(Say("OK"))
   833  			})
   834  		})
   835  
   836  		When("updating the service-broker labels fails", func() {
   837  			BeforeEach(func() {
   838  				fakeActor.UpdateServiceBrokerLabelsByServiceBrokerNameReturns(v7action.Warnings{"some-warning-1", "some-warning-2"},
   839  					errors.New("api call failed"))
   840  			})
   841  
   842  			It("prints all warnings and returns the error", func() {
   843  				Expect(executeErr).To(MatchError("api call failed"))
   844  				Expect(testUI.Err).To(Say("some-warning-1"))
   845  				Expect(testUI.Err).To(Say("some-warning-2"))
   846  			})
   847  		})
   848  
   849  		Context("shows the right update message", func() {
   850  			When("Unsetting labels", func() {
   851  				BeforeEach(func() {
   852  					cmd.Action = Unset
   853  				})
   854  
   855  				It("shows 'Removing' as action", func() {
   856  					Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Removing label(s) for service-broker %s as some-user...`), expectedServiceBrokerName))
   857  					Expect(testUI.Out).To(Say("OK"))
   858  				})
   859  			})
   860  
   861  			When("Setting labels", func() {
   862  				BeforeEach(func() {
   863  					cmd.Action = Set
   864  				})
   865  
   866  				It("shows 'Setting' as action", func() {
   867  					Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Setting label(s) for service-broker %s as some-user...`), expectedServiceBrokerName))
   868  					Expect(testUI.Out).To(Say("OK"))
   869  				})
   870  			})
   871  		})
   872  	})
   873  
   874  	When("updating labels on service-offering", func() {
   875  		var (
   876  			executeErr error
   877  		)
   878  
   879  		const serviceBrokerName = "brokerName"
   880  		const serviceOfferingName = "serviceOfferingName"
   881  
   882  		BeforeEach(func() {
   883  			targetResource = TargetResource{
   884  				ResourceType: "service-offering",
   885  				ResourceName: serviceOfferingName,
   886  			}
   887  			labels = map[string]types.NullString{
   888  				"some-label":     types.NewNullString("some-value"),
   889  				"some-other-key": types.NewNullString(),
   890  			}
   891  
   892  			fakeConfig.CurrentUserNameReturns("some-user", nil)
   893  			fakeActor.UpdateServiceOfferingLabelsReturns(
   894  				v7action.Warnings{"some-warning-1", "some-warning-2"},
   895  				nil,
   896  			)
   897  		})
   898  
   899  		JustBeforeEach(func() {
   900  			executeErr = cmd.Execute(targetResource, labels)
   901  		})
   902  
   903  		When("updating the labels succeeds", func() {
   904  			It("does not return an error and prints all warnings", func() {
   905  				Expect(executeErr).ToNot(HaveOccurred())
   906  				Expect(testUI.Err).To(Say("some-warning-1"))
   907  				Expect(testUI.Err).To(Say("some-warning-2"))
   908  			})
   909  
   910  			It("passes the correct parameters into the actor", func() {
   911  				Expect(fakeActor.UpdateServiceOfferingLabelsCallCount()).To(Equal(1))
   912  				gotServiceOfferingName, gotBrokerName, gotLabelsMap := fakeActor.UpdateServiceOfferingLabelsArgsForCall(0)
   913  				Expect(gotServiceOfferingName).To(Equal(serviceOfferingName))
   914  				Expect(gotBrokerName).To(BeEmpty())
   915  				Expect(gotLabelsMap).To(Equal(labels))
   916  			})
   917  
   918  			When("a service broker name is specified", func() {
   919  				BeforeEach(func() {
   920  					targetResource.ServiceBroker = serviceBrokerName
   921  				})
   922  
   923  				It("passes the broker name", func() {
   924  					Expect(fakeActor.UpdateServiceOfferingLabelsCallCount()).To(Equal(1))
   925  					_, gotBrokerName, _ := fakeActor.UpdateServiceOfferingLabelsArgsForCall(0)
   926  					Expect(gotBrokerName).To(Equal(serviceBrokerName))
   927  				})
   928  			})
   929  		})
   930  
   931  		When("the resource type argument is not lowercase", func() {
   932  			BeforeEach(func() {
   933  				targetResource.ResourceType = "Service-OffErinG"
   934  			})
   935  
   936  			It("calls the right actor", func() {
   937  				Expect(executeErr).ToNot(HaveOccurred())
   938  				Expect(fakeActor.UpdateServiceOfferingLabelsCallCount()).To(Equal(1))
   939  			})
   940  
   941  			It("displays a message in the right case", func() {
   942  				Expect(testUI.Out).To(Say("(.*) label\\(s\\) for service-offering (.*)"))
   943  				Expect(testUI.Out).To(Say("OK"))
   944  			})
   945  		})
   946  
   947  		When("updating the labels fails", func() {
   948  			BeforeEach(func() {
   949  				fakeActor.UpdateServiceOfferingLabelsReturns(
   950  					v7action.Warnings{"some-warning-1", "some-warning-2"},
   951  					errors.New("api call failed"),
   952  				)
   953  			})
   954  
   955  			It("returns the error and prints all warnings", func() {
   956  				Expect(executeErr).To(MatchError("api call failed"))
   957  				Expect(testUI.Err).To(Say("some-warning-1"))
   958  				Expect(testUI.Err).To(Say("some-warning-2"))
   959  			})
   960  		})
   961  
   962  		Context("shows the right update message", func() {
   963  			When("the broker name is not specified", func() {
   964  				When("Unsetting labels", func() {
   965  					BeforeEach(func() {
   966  						cmd.Action = Unset
   967  					})
   968  
   969  					It("shows 'Removing' as action", func() {
   970  						Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Removing label(s) for service-offering %s as some-user...`), serviceOfferingName))
   971  						Expect(testUI.Out).To(Say("OK"))
   972  					})
   973  				})
   974  
   975  				When("Setting labels", func() {
   976  					BeforeEach(func() {
   977  						cmd.Action = Set
   978  					})
   979  
   980  					It("shows 'Setting' as action", func() {
   981  						Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Setting label(s) for service-offering %s as some-user...`), serviceOfferingName))
   982  						Expect(testUI.Out).To(Say("OK"))
   983  					})
   984  				})
   985  			})
   986  
   987  			When("the broker name is specified", func() {
   988  				BeforeEach(func() {
   989  					targetResource.ServiceBroker = serviceBrokerName
   990  				})
   991  
   992  				When("Unsetting labels", func() {
   993  					BeforeEach(func() {
   994  						cmd.Action = Unset
   995  					})
   996  
   997  					It("shows 'Removing' as action", func() {
   998  						Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Removing label(s) for service-offering %s from service broker %s as some-user...`), serviceOfferingName, serviceBrokerName))
   999  						Expect(testUI.Out).To(Say("OK"))
  1000  					})
  1001  				})
  1002  
  1003  				When("Setting labels", func() {
  1004  					BeforeEach(func() {
  1005  						cmd.Action = Set
  1006  					})
  1007  
  1008  					It("shows 'Setting' as action", func() {
  1009  						Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Setting label(s) for service-offering %s from service broker %s as some-user...`), serviceOfferingName, serviceBrokerName))
  1010  						Expect(testUI.Out).To(Say("OK"))
  1011  					})
  1012  				})
  1013  			})
  1014  		})
  1015  	})
  1016  
  1017  	When("updating labels on service-plan", func() {
  1018  		var (
  1019  			executeErr error
  1020  		)
  1021  
  1022  		const serviceBrokerName = "brokerName"
  1023  		const serviceOfferingName = "serviceOfferingName"
  1024  		const servicePlanName = "servicePlanName"
  1025  
  1026  		BeforeEach(func() {
  1027  			targetResource = TargetResource{
  1028  				ResourceType: "service-plan",
  1029  				ResourceName: servicePlanName,
  1030  			}
  1031  			labels = map[string]types.NullString{
  1032  				"some-label":     types.NewNullString("some-value"),
  1033  				"some-other-key": types.NewNullString(),
  1034  			}
  1035  
  1036  			fakeConfig.CurrentUserNameReturns("some-user", nil)
  1037  			fakeActor.UpdateServicePlanLabelsReturns(
  1038  				v7action.Warnings{"some-warning-1", "some-warning-2"},
  1039  				nil,
  1040  			)
  1041  		})
  1042  
  1043  		JustBeforeEach(func() {
  1044  			executeErr = cmd.Execute(targetResource, labels)
  1045  		})
  1046  
  1047  		When("updating the labels succeeds", func() {
  1048  			It("does not return an error and prints all warnings", func() {
  1049  				Expect(executeErr).ToNot(HaveOccurred())
  1050  				Expect(testUI.Err).To(Say("some-warning-1"))
  1051  				Expect(testUI.Err).To(Say("some-warning-2"))
  1052  			})
  1053  
  1054  			It("passes the correct parameters into the actor", func() {
  1055  				Expect(fakeActor.UpdateServicePlanLabelsCallCount()).To(Equal(1))
  1056  				gotServicePlanName, gotServiceOfferingName, gotBrokerName, gotLabelsMap := fakeActor.UpdateServicePlanLabelsArgsForCall(0)
  1057  				Expect(gotServicePlanName).To(Equal(servicePlanName))
  1058  				Expect(gotServiceOfferingName).To(BeEmpty())
  1059  				Expect(gotBrokerName).To(BeEmpty())
  1060  				Expect(gotLabelsMap).To(Equal(labels))
  1061  			})
  1062  
  1063  			When("a service broker name is specified", func() {
  1064  				BeforeEach(func() {
  1065  					targetResource.ServiceBroker = serviceBrokerName
  1066  				})
  1067  
  1068  				It("passes the broker name", func() {
  1069  					Expect(fakeActor.UpdateServicePlanLabelsCallCount()).To(Equal(1))
  1070  					_, _, gotBrokerName, _ := fakeActor.UpdateServicePlanLabelsArgsForCall(0)
  1071  					Expect(gotBrokerName).To(Equal(serviceBrokerName))
  1072  				})
  1073  			})
  1074  
  1075  			When("a service offering name is specified", func() {
  1076  				BeforeEach(func() {
  1077  					targetResource.ServiceOffering = serviceOfferingName
  1078  				})
  1079  
  1080  				It("passes the broker name", func() {
  1081  					Expect(fakeActor.UpdateServicePlanLabelsCallCount()).To(Equal(1))
  1082  					_, gotOfferingName, _, _ := fakeActor.UpdateServicePlanLabelsArgsForCall(0)
  1083  					Expect(gotOfferingName).To(Equal(serviceOfferingName))
  1084  				})
  1085  			})
  1086  		})
  1087  
  1088  		When("the resource type argument is not lowercase", func() {
  1089  			BeforeEach(func() {
  1090  				targetResource.ResourceType = "Service-PlAN"
  1091  			})
  1092  
  1093  			It("calls the right actor", func() {
  1094  				Expect(executeErr).ToNot(HaveOccurred())
  1095  				Expect(fakeActor.UpdateServicePlanLabelsCallCount()).To(Equal(1))
  1096  			})
  1097  
  1098  			It("displays a message in the right case", func() {
  1099  				Expect(testUI.Out).To(Say("(.*) label\\(s\\) for service-plan (.*)"))
  1100  				Expect(testUI.Out).To(Say("OK"))
  1101  			})
  1102  		})
  1103  
  1104  		When("updating the labels fails", func() {
  1105  			BeforeEach(func() {
  1106  				fakeActor.UpdateServicePlanLabelsReturns(
  1107  					v7action.Warnings{"some-warning-1", "some-warning-2"},
  1108  					errors.New("api call failed"),
  1109  				)
  1110  			})
  1111  
  1112  			It("returns the error and prints all warnings", func() {
  1113  				Expect(executeErr).To(MatchError("api call failed"))
  1114  				Expect(testUI.Err).To(Say("some-warning-1"))
  1115  				Expect(testUI.Err).To(Say("some-warning-2"))
  1116  			})
  1117  		})
  1118  
  1119  		Context("shows the right update message", func() {
  1120  			When("no extra flags are specified", func() {
  1121  				When("Unsetting labels", func() {
  1122  					BeforeEach(func() {
  1123  						cmd.Action = Unset
  1124  					})
  1125  
  1126  					It("shows 'Removing' as action", func() {
  1127  						Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Removing label(s) for service-plan %s as some-user...`), servicePlanName))
  1128  						Expect(testUI.Out).To(Say("OK"))
  1129  					})
  1130  				})
  1131  
  1132  				When("Setting labels", func() {
  1133  					BeforeEach(func() {
  1134  						cmd.Action = Set
  1135  					})
  1136  
  1137  					It("shows 'Setting' as action", func() {
  1138  						Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Setting label(s) for service-plan %s as some-user...`), servicePlanName))
  1139  						Expect(testUI.Out).To(Say("OK"))
  1140  					})
  1141  				})
  1142  			})
  1143  
  1144  			When("the broker name is specified", func() {
  1145  				BeforeEach(func() {
  1146  					targetResource.ServiceBroker = serviceBrokerName
  1147  				})
  1148  
  1149  				When("Unsetting labels", func() {
  1150  					BeforeEach(func() {
  1151  						cmd.Action = Unset
  1152  					})
  1153  
  1154  					It("shows 'Removing' as action", func() {
  1155  						Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Removing label(s) for service-plan %s from service broker %s as some-user...`), servicePlanName, serviceBrokerName))
  1156  						Expect(testUI.Out).To(Say("OK"))
  1157  					})
  1158  				})
  1159  
  1160  				When("Setting labels", func() {
  1161  					BeforeEach(func() {
  1162  						cmd.Action = Set
  1163  					})
  1164  
  1165  					It("shows 'Setting' as action", func() {
  1166  						Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Setting label(s) for service-plan %s from service broker %s as some-user...`), servicePlanName, serviceBrokerName))
  1167  						Expect(testUI.Out).To(Say("OK"))
  1168  					})
  1169  				})
  1170  			})
  1171  
  1172  			When("the offering name is specified", func() {
  1173  				BeforeEach(func() {
  1174  					targetResource.ServiceOffering = serviceOfferingName
  1175  				})
  1176  
  1177  				When("Unsetting labels", func() {
  1178  					BeforeEach(func() {
  1179  						cmd.Action = Unset
  1180  					})
  1181  
  1182  					It("shows 'Removing' as action", func() {
  1183  						Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Removing label(s) for service-plan %s from service offering %s as some-user...`), servicePlanName, serviceOfferingName))
  1184  						Expect(testUI.Out).To(Say("OK"))
  1185  					})
  1186  				})
  1187  
  1188  				When("Setting labels", func() {
  1189  					BeforeEach(func() {
  1190  						cmd.Action = Set
  1191  					})
  1192  
  1193  					It("shows 'Setting' as action", func() {
  1194  						Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Setting label(s) for service-plan %s from service offering %s as some-user...`), servicePlanName, serviceOfferingName))
  1195  						Expect(testUI.Out).To(Say("OK"))
  1196  					})
  1197  				})
  1198  			})
  1199  
  1200  			When("both the offering name and the broker name are specified", func() {
  1201  				BeforeEach(func() {
  1202  					targetResource.ServiceBroker = serviceBrokerName
  1203  					targetResource.ServiceOffering = serviceOfferingName
  1204  				})
  1205  
  1206  				When("Unsetting labels", func() {
  1207  					BeforeEach(func() {
  1208  						cmd.Action = Unset
  1209  					})
  1210  
  1211  					It("shows 'Removing' as action", func() {
  1212  						Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Removing label(s) for service-plan %s from service offering %s / service broker %s as some-user...`), servicePlanName, serviceOfferingName, serviceBrokerName))
  1213  						Expect(testUI.Out).To(Say("OK"))
  1214  					})
  1215  				})
  1216  
  1217  				When("Setting labels", func() {
  1218  					BeforeEach(func() {
  1219  						cmd.Action = Set
  1220  					})
  1221  
  1222  					It("shows 'Setting' as action", func() {
  1223  						Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Setting label(s) for service-plan %s from service offering %s / service broker %s as some-user...`), servicePlanName, serviceOfferingName, serviceBrokerName))
  1224  						Expect(testUI.Out).To(Say("OK"))
  1225  					})
  1226  				})
  1227  			})
  1228  
  1229  		})
  1230  	})
  1231  
  1232  	When("updating labels on spaces", func() {
  1233  		var (
  1234  			executeErr  error
  1235  			spaceName   string
  1236  			expectedMap map[string]types.NullString
  1237  		)
  1238  
  1239  		BeforeEach(func() {
  1240  			spaceName = "spiff"
  1241  			targetResource = TargetResource{
  1242  				ResourceType: "space",
  1243  				ResourceName: spaceName,
  1244  			}
  1245  			expectedMap = map[string]types.NullString{
  1246  				"some-label":     types.NewNullString("some-value"),
  1247  				"some-other-key": types.NewNullString()}
  1248  			labels = expectedMap
  1249  
  1250  			fakeConfig.CurrentUserNameReturns("some-user", nil)
  1251  			fakeConfig.TargetedOrganizationReturns(
  1252  				configv3.Organization{Name: "fake-org", GUID: "some-org-guid"})
  1253  
  1254  			fakeActor.UpdateSpaceLabelsBySpaceNameReturns(
  1255  				v7action.Warnings{"some-warning-1", "some-warning-2"},
  1256  				nil)
  1257  		})
  1258  
  1259  		JustBeforeEach(func() {
  1260  			executeErr = cmd.Execute(targetResource, labels)
  1261  		})
  1262  
  1263  		When("updating the space labels succeeds", func() {
  1264  			It("does not return an error and prints all warnings", func() {
  1265  				Expect(executeErr).ToNot(HaveOccurred())
  1266  				Expect(testUI.Err).To(Say("some-warning-1"))
  1267  				Expect(testUI.Err).To(Say("some-warning-2"))
  1268  			})
  1269  
  1270  			It("passes the correct parameters into the actor", func() {
  1271  				Expect(fakeActor.UpdateSpaceLabelsBySpaceNameCallCount()).To(Equal(1))
  1272  				actualSpaceName, orgGUID, labelsMap := fakeActor.UpdateSpaceLabelsBySpaceNameArgsForCall(0)
  1273  				Expect(actualSpaceName).To(Equal(spaceName))
  1274  				Expect(orgGUID).To(Equal("some-org-guid"))
  1275  				Expect(labelsMap).To(Equal(expectedMap))
  1276  			})
  1277  		})
  1278  
  1279  		When("the resource type argument is not lowercase", func() {
  1280  			BeforeEach(func() {
  1281  				targetResource.ResourceType = "SpAcE"
  1282  			})
  1283  
  1284  			It("calls the right actor", func() {
  1285  				Expect(executeErr).ToNot(HaveOccurred())
  1286  				Expect(fakeActor.UpdateSpaceLabelsBySpaceNameCallCount()).To(Equal(1))
  1287  			})
  1288  
  1289  			It("displays a message in the right case", func() {
  1290  				Expect(testUI.Out).To(Say("(.*) label\\(s\\) for space (.*)"))
  1291  				Expect(testUI.Out).To(Say("OK"))
  1292  			})
  1293  		})
  1294  
  1295  		When("updating the space labels fails", func() {
  1296  			BeforeEach(func() {
  1297  				fakeActor.UpdateSpaceLabelsBySpaceNameReturns(
  1298  					v7action.Warnings{"some-warning-1", "some-warning-2"},
  1299  					errors.New("api call failed"),
  1300  				)
  1301  			})
  1302  
  1303  			It("returns the error and prints all warnings", func() {
  1304  				Expect(executeErr).To(MatchError("api call failed"))
  1305  				Expect(testUI.Err).To(Say("some-warning-1"))
  1306  				Expect(testUI.Err).To(Say("some-warning-2"))
  1307  			})
  1308  		})
  1309  
  1310  		Context("shows the right update message", func() {
  1311  			When("Unsetting labels", func() {
  1312  				BeforeEach(func() {
  1313  					cmd.Action = Unset
  1314  				})
  1315  
  1316  				It("shows 'Removing' as action", func() {
  1317  					Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Removing label(s) for space %s in org fake-org as some-user...`), spaceName))
  1318  					Expect(testUI.Out).To(Say("OK"))
  1319  				})
  1320  			})
  1321  
  1322  			When("Setting labels", func() {
  1323  				BeforeEach(func() {
  1324  					cmd.Action = Set
  1325  				})
  1326  
  1327  				It("shows 'Setting' as action", func() {
  1328  					Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Setting label(s) for space %s in org fake-org as some-user...`), spaceName))
  1329  					Expect(testUI.Out).To(Say("OK"))
  1330  				})
  1331  			})
  1332  		})
  1333  	})
  1334  
  1335  	When("updating labels on stacks", func() {
  1336  		const stackName = "some-stack"
  1337  
  1338  		var (
  1339  			executeErr  error
  1340  			expectedMap map[string]types.NullString
  1341  		)
  1342  
  1343  		BeforeEach(func() {
  1344  			targetResource = TargetResource{
  1345  				ResourceType: "stack",
  1346  				ResourceName: stackName,
  1347  			}
  1348  			fakeConfig.CurrentUserNameReturns("some-user", nil)
  1349  			fakeSharedActor.CheckTargetReturns(nil)
  1350  			expectedMap = map[string]types.NullString{
  1351  				"some-label":     types.NewNullString("some-value"),
  1352  				"some-other-key": types.NewNullString(),
  1353  			}
  1354  			labels = expectedMap
  1355  		})
  1356  
  1357  		JustBeforeEach(func() {
  1358  			executeErr = cmd.Execute(targetResource, labels)
  1359  		})
  1360  
  1361  		When("updating the stack labels succeeds", func() {
  1362  			BeforeEach(func() {
  1363  				fakeActor.UpdateStackLabelsByStackNameReturns(v7action.Warnings{"some-warning-1", "some-warning-2"},
  1364  					nil)
  1365  			})
  1366  
  1367  			It("does not return an error and prints all warnings", func() {
  1368  				Expect(executeErr).ToNot(HaveOccurred())
  1369  				Expect(testUI.Err).To(Say("some-warning-1"))
  1370  				Expect(testUI.Err).To(Say("some-warning-2"))
  1371  
  1372  			})
  1373  
  1374  			It("passes the correct parameters into the actor", func() {
  1375  				Expect(fakeActor.UpdateStackLabelsByStackNameCallCount()).To(Equal(1))
  1376  				actualStackName, labelsMap := fakeActor.UpdateStackLabelsByStackNameArgsForCall(0)
  1377  				Expect(actualStackName).To(Equal(stackName))
  1378  				Expect(labelsMap).To(Equal(expectedMap))
  1379  			})
  1380  		})
  1381  
  1382  		When("the resource type argument is not lowercase", func() {
  1383  			BeforeEach(func() {
  1384  				targetResource.ResourceType = "sTaCk"
  1385  			})
  1386  
  1387  			It("calls the right actor", func() {
  1388  				Expect(executeErr).ToNot(HaveOccurred())
  1389  				Expect(fakeActor.UpdateStackLabelsByStackNameCallCount()).To(Equal(1))
  1390  			})
  1391  
  1392  			It("displays a message in the right case", func() {
  1393  				Expect(testUI.Out).To(Say("(.*) label\\(s\\) for stack (.*)"))
  1394  				Expect(testUI.Out).To(Say("OK"))
  1395  			})
  1396  		})
  1397  
  1398  		When("updating the stack labels fails", func() {
  1399  			BeforeEach(func() {
  1400  				fakeActor.UpdateStackLabelsByStackNameReturns(
  1401  					v7action.Warnings{"some-warning-1", "some-warning-2"},
  1402  					errors.New("api call failed"),
  1403  				)
  1404  			})
  1405  
  1406  			It("returns the error and prints all warnings", func() {
  1407  				Expect(executeErr).To(MatchError("api call failed"))
  1408  				Expect(testUI.Err).To(Say("some-warning-1"))
  1409  				Expect(testUI.Err).To(Say("some-warning-2"))
  1410  			})
  1411  		})
  1412  
  1413  		Context("shows the right update message", func() {
  1414  			When("Unsetting labels", func() {
  1415  				BeforeEach(func() {
  1416  					cmd.Action = Unset
  1417  				})
  1418  
  1419  				It("shows 'Removing' as action", func() {
  1420  					Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Removing label(s) for stack %s as some-user...`), stackName))
  1421  					Expect(testUI.Out).To(Say("OK"))
  1422  				})
  1423  			})
  1424  
  1425  			When("Setting labels", func() {
  1426  				BeforeEach(func() {
  1427  					cmd.Action = Set
  1428  				})
  1429  
  1430  				It("shows 'Setting' as action", func() {
  1431  					Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Setting label(s) for stack %s as some-user...`), stackName))
  1432  					Expect(testUI.Out).To(Say("OK"))
  1433  				})
  1434  			})
  1435  		})
  1436  	})
  1437  })