github.com/franc20/ayesa_sap@v7.0.0-beta.28.0.20200124003224-302d4d52fa6c+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  var _ = Describe("LabelUpdater", func() {
    23  
    24  	var (
    25  		cmd             LabelUpdater
    26  		fakeActor       *v7fakes.FakeSetLabelActor
    27  		fakeConfig      *commandfakes.FakeConfig
    28  		fakeSharedActor *commandfakes.FakeSharedActor
    29  		testUI          *ui.UI
    30  		targetResource  TargetResource
    31  		labels          map[string]types.NullString
    32  	)
    33  
    34  	BeforeEach(func() {
    35  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    36  		fakeConfig = new(commandfakes.FakeConfig)
    37  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    38  		fakeActor = new(v7fakes.FakeSetLabelActor)
    39  		cmd = LabelUpdater{
    40  			UI:          testUI,
    41  			Config:      fakeConfig,
    42  			SharedActor: fakeSharedActor,
    43  			Actor:       fakeActor,
    44  		}
    45  	})
    46  
    47  	Context("shared validations", func() {
    48  		var resourceName string
    49  
    50  		BeforeEach(func() {
    51  			testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    52  			fakeActor = new(v7fakes.FakeSetLabelActor)
    53  			fakeConfig = new(commandfakes.FakeConfig)
    54  			fakeSharedActor = new(commandfakes.FakeSharedActor)
    55  			cmd = LabelUpdater{
    56  				Actor:       fakeActor,
    57  				UI:          testUI,
    58  				Config:      fakeConfig,
    59  				SharedActor: fakeSharedActor,
    60  			}
    61  		})
    62  
    63  		When("fetching the current user's name fails", func() {
    64  			BeforeEach(func() {
    65  				targetResource = TargetResource{
    66  					ResourceType: "anything",
    67  					ResourceName: resourceName,
    68  				}
    69  				fakeConfig.CurrentUserNameReturns("some-user", errors.New("boom"))
    70  			})
    71  
    72  			It("returns an error", func() {
    73  				err := cmd.Execute(targetResource, labels)
    74  				Expect(err).To(MatchError("boom"))
    75  			})
    76  		})
    77  
    78  		When("an unrecognized resource is specified", func() {
    79  			BeforeEach(func() {
    80  				resourceName = "some-unrecognized-resource"
    81  				cmd = LabelUpdater{
    82  					Actor:       fakeActor,
    83  					UI:          testUI,
    84  					Config:      fakeConfig,
    85  					SharedActor: fakeSharedActor,
    86  				}
    87  				targetResource = TargetResource{
    88  					ResourceType: "unrecognized-resource",
    89  					ResourceName: resourceName,
    90  				}
    91  			})
    92  
    93  			It("errors", func() {
    94  				executeErr := cmd.Execute(targetResource, labels)
    95  
    96  				Expect(executeErr).To(MatchError("Unsupported resource type of 'unrecognized-resource'"))
    97  			})
    98  		})
    99  
   100  		DescribeTable(
   101  			"Combination of --stack with resource type",
   102  			func(resourceType string) {
   103  				targetResource = TargetResource{
   104  					ResourceType:   resourceType,
   105  					BuildpackStack: "cflinuxfs3",
   106  				}
   107  
   108  				err := cmd.Execute(targetResource, nil)
   109  
   110  				argumentCombinationError := translatableerror.ArgumentCombinationError{
   111  					Args: []string{strings.ToLower(resourceType), "--stack, -s"},
   112  				}
   113  				Expect(err).To(MatchError(argumentCombinationError))
   114  			},
   115  			Entry("app", "app"),
   116  			Entry("domains", "domain"),
   117  			Entry("orgs", "org"),
   118  			Entry("routes", "route"),
   119  			Entry("spaces", "space"),
   120  			Entry("stacks", "stack"),
   121  			Entry("service brokers", "service-broker"),
   122  		)
   123  
   124  		DescribeTable(
   125  			"when checking the target fails",
   126  			func(resourceType string) {
   127  				fakeSharedActor.CheckTargetReturns(errors.New("Target not found"))
   128  				targetResource = TargetResource{
   129  					ResourceType: resourceType,
   130  				}
   131  
   132  				err := cmd.Execute(targetResource, nil)
   133  				Expect(err).To(MatchError("Target not found"))
   134  			},
   135  			Entry("app", "app"),
   136  			Entry("buildpack", "buildpack"),
   137  			// domain - does not check target
   138  			Entry("org", "org"),
   139  			Entry("route", "route"),
   140  			Entry("service-broker", "service-broker"),
   141  			Entry("space", "space"),
   142  			Entry("stack", "stack"),
   143  		)
   144  	})
   145  
   146  	When("updating labels on apps", func() {
   147  		var (
   148  			appName     string
   149  			executeErr  error
   150  			expectedMap map[string]types.NullString
   151  		)
   152  
   153  		BeforeEach(func() {
   154  			appName = "some-app"
   155  			targetResource = TargetResource{
   156  				ResourceType: "app",
   157  				ResourceName: appName,
   158  			}
   159  
   160  			fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: "fake-org"})
   161  			fakeConfig.TargetedSpaceReturns(configv3.Space{Name: "fake-space", GUID: "some-space-guid"})
   162  			fakeConfig.CurrentUserNameReturns("some-user", nil)
   163  
   164  			expectedMap = map[string]types.NullString{
   165  				"some-label":     types.NewNullString("some-value"),
   166  				"some-other-key": types.NewNullString()}
   167  			labels = expectedMap
   168  		})
   169  
   170  		JustBeforeEach(func() {
   171  			executeErr = cmd.Execute(targetResource, labels)
   172  		})
   173  
   174  		It("checks that the user is logged in and targeted to an org and space", func() {
   175  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
   176  			checkOrg, checkSpace := fakeSharedActor.CheckTargetArgsForCall(0)
   177  			Expect(checkOrg).To(BeTrue())
   178  			Expect(checkSpace).To(BeTrue())
   179  		})
   180  
   181  		When("updating the app labels succeeds", func() {
   182  			BeforeEach(func() {
   183  				fakeActor.UpdateApplicationLabelsByApplicationNameReturns(v7action.Warnings{"some-warning-1", "some-warning-2"},
   184  					nil)
   185  
   186  			})
   187  
   188  			It("prints all warnings and does not return an error ", func() {
   189  				Expect(executeErr).ToNot(HaveOccurred())
   190  				Expect(testUI.Err).To(Say("some-warning-1"))
   191  				Expect(testUI.Err).To(Say("some-warning-2"))
   192  			})
   193  
   194  			It("passes the correct parameters into the actor", func() {
   195  				Expect(fakeActor.UpdateApplicationLabelsByApplicationNameCallCount()).To(Equal(1))
   196  				actualAppName, spaceGUID, labelsMap := fakeActor.UpdateApplicationLabelsByApplicationNameArgsForCall(0)
   197  				Expect(actualAppName).To(Equal(appName))
   198  				Expect(spaceGUID).To(Equal("some-space-guid"))
   199  				Expect(labelsMap).To(Equal(expectedMap))
   200  			})
   201  		})
   202  		// FIXME maybe checking it calls the right method is enough?
   203  
   204  		When("the resource type argument is not lowercase", func() {
   205  			BeforeEach(func() {
   206  				targetResource.ResourceType = "aPp"
   207  			})
   208  
   209  			It("passes the correct parameters into the actor", func() {
   210  				Expect(executeErr).ToNot(HaveOccurred())
   211  
   212  				Expect(fakeActor.UpdateApplicationLabelsByApplicationNameCallCount()).To(Equal(1))
   213  				actualAppName, spaceGUID, labelsMap := fakeActor.UpdateApplicationLabelsByApplicationNameArgsForCall(0)
   214  				Expect(actualAppName).To(Equal(appName))
   215  				Expect(spaceGUID).To(Equal("some-space-guid"))
   216  				Expect(labelsMap).To(Equal(expectedMap))
   217  			})
   218  
   219  			It("displays a message in the right casing", func() {
   220  				Expect(testUI.Out).To(Say("(.*) label\\(s\\) for app (.*)"))
   221  				Expect(testUI.Out).To(Say("OK"))
   222  			})
   223  		})
   224  
   225  		When("updating the app labels fails", func() {
   226  			BeforeEach(func() {
   227  				fakeActor.UpdateApplicationLabelsByApplicationNameReturns(v7action.Warnings{"some-warning-1", "some-warning-2"},
   228  					errors.New("api call failed"))
   229  			})
   230  
   231  			It("prints all warnings", func() {
   232  				Expect(testUI.Err).To(Say("some-warning-1"))
   233  				Expect(testUI.Err).To(Say("some-warning-2"))
   234  			})
   235  
   236  			It("returns the error", func() {
   237  				Expect(executeErr).To(MatchError("api call failed"))
   238  			})
   239  		})
   240  
   241  		Context("Shows the right update message with org and space", func() {
   242  			When("Unsetting labels", func() {
   243  				BeforeEach(func() {
   244  					cmd.Action = Unset
   245  					//FIXME do we want to change the labels to all have nil values?
   246  				})
   247  				It("shows 'Removing' as action", func() {
   248  					Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Removing label(s) for app %s in org fake-org / space fake-space as some-user...`), appName))
   249  				})
   250  			})
   251  			When("Setting labels", func() {
   252  				BeforeEach(func() {
   253  					cmd.Action = Set
   254  					//FIXME do we want to change the labels to all have not nil values?
   255  				})
   256  
   257  				It("shows 'Setting' as action", func() {
   258  					Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Setting label(s) for app %s in org fake-org / space fake-space as some-user...`), appName))
   259  
   260  				})
   261  			})
   262  
   263  		})
   264  	})
   265  
   266  	When("updating labels on buildpacks", func() {
   267  		var resourceName string
   268  		var expectedMap map[string]types.NullString
   269  		var executeErr error
   270  
   271  		BeforeEach(func() {
   272  			resourceName = "buildpack-name"
   273  			targetResource = TargetResource{
   274  				ResourceType: "buildpack",
   275  				ResourceName: resourceName,
   276  			}
   277  
   278  			fakeSharedActor.CheckTargetReturns(nil)
   279  			fakeConfig.CurrentUserNameReturns("some-user", nil)
   280  
   281  			expectedMap = map[string]types.NullString{
   282  				"some-label":     types.NewNullString("some-value"),
   283  				"some-other-key": types.NewNullString()}
   284  			labels = expectedMap
   285  
   286  			fakeActor.UpdateBuildpackLabelsByBuildpackNameAndStackReturns(
   287  				v7action.Warnings([]string{"some-warning-1", "some-warning-2"}),
   288  				nil,
   289  			)
   290  
   291  		})
   292  
   293  		JustBeforeEach(func() {
   294  			executeErr = cmd.Execute(targetResource, labels)
   295  		})
   296  
   297  		It("checks that the user is logged in", func() {
   298  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
   299  			checkOrg, checkSpace := fakeSharedActor.CheckTargetArgsForCall(0)
   300  			Expect(checkOrg).To(BeFalse())
   301  			Expect(checkSpace).To(BeFalse())
   302  		})
   303  
   304  		When("updating the buildpack labels succeeds", func() {
   305  			When("the stack is specified", func() {
   306  				BeforeEach(func() {
   307  					targetResource.BuildpackStack = "globinski"
   308  				})
   309  
   310  				It("passes the right parameters", func() {
   311  					Expect(fakeActor.UpdateBuildpackLabelsByBuildpackNameAndStackCallCount()).To(Equal(1))
   312  					name, stack, labels := fakeActor.UpdateBuildpackLabelsByBuildpackNameAndStackArgsForCall(0)
   313  					Expect(name).To(Equal(resourceName), "failed to pass buildpack name")
   314  					Expect(stack).To(Equal("globinski"), "failed to pass stack name")
   315  					Expect(labels).To(BeEquivalentTo(expectedMap))
   316  				})
   317  
   318  				It("prints all warnings and does not return an argument combination error ", func() {
   319  					Expect(executeErr).ToNot(HaveOccurred())
   320  					Expect(testUI.Err).To(Say("some-warning-1"))
   321  					Expect(testUI.Err).To(Say("some-warning-2"))
   322  				})
   323  			})
   324  
   325  			When("the stack is not specified", func() {
   326  				It("passes the right parameters", func() {
   327  					Expect(fakeActor.UpdateBuildpackLabelsByBuildpackNameAndStackCallCount()).To(Equal(1))
   328  					name, stack, labels := fakeActor.UpdateBuildpackLabelsByBuildpackNameAndStackArgsForCall(0)
   329  					Expect(name).To(Equal(resourceName), "failed to pass buildpack name")
   330  					Expect(stack).To(Equal(""), "failed to pass stack name")
   331  					Expect(labels).To(BeEquivalentTo(expectedMap))
   332  				})
   333  			})
   334  		})
   335  
   336  		When("the resource type is not lowercase", func() {
   337  			BeforeEach(func() {
   338  				targetResource = TargetResource{
   339  					ResourceType: "bUiLdPaCk",
   340  					ResourceName: resourceName,
   341  				}
   342  				expectedMap = map[string]types.NullString{
   343  					"some-label":     types.NewNullString("some-value"),
   344  					"some-other-key": types.NewNullString()}
   345  			})
   346  
   347  			When("updating the buildpack labels succeeds", func() {
   348  				When("the stack is specified", func() {
   349  					BeforeEach(func() {
   350  						targetResource.BuildpackStack = "globinski"
   351  					})
   352  
   353  					It("passes the right parameters", func() {
   354  						Expect(executeErr).ToNot(HaveOccurred())
   355  						Expect(fakeActor.UpdateBuildpackLabelsByBuildpackNameAndStackCallCount()).To(Equal(1))
   356  						name, stack, labels := fakeActor.UpdateBuildpackLabelsByBuildpackNameAndStackArgsForCall(0)
   357  						Expect(name).To(Equal(resourceName), "failed to pass buildpack name")
   358  						Expect(stack).To(Equal("globinski"), "failed to pass stack name")
   359  						Expect(labels).To(BeEquivalentTo(expectedMap))
   360  					})
   361  
   362  					It("displays a message in the right casing", func() {
   363  						Expect(testUI.Out).To(Say("(.*) label\\(s\\) for buildpack (.*)"))
   364  						Expect(testUI.Out).To(Say("OK"))
   365  					})
   366  
   367  				})
   368  
   369  			})
   370  		})
   371  
   372  		When("updating the buildpack labels fails", func() {
   373  			BeforeEach(func() {
   374  				fakeActor.UpdateBuildpackLabelsByBuildpackNameAndStackReturns(v7action.Warnings{"some-warning-1", "some-warning-2"},
   375  					errors.New("api call failed"))
   376  			})
   377  
   378  			It("returns the error and prints all warnings", func() {
   379  				Expect(executeErr).To(MatchError("api call failed"))
   380  				Expect(testUI.Err).To(Say("some-warning-1"))
   381  				Expect(testUI.Err).To(Say("some-warning-2"))
   382  			})
   383  
   384  		})
   385  
   386  		Context("Shows the right update message with correct stack and action", func() {
   387  			When("Unsetting labels", func() {
   388  				BeforeEach(func() {
   389  					cmd.Action = Unset
   390  					//FIXME do we want to change the labels to all have nil values?
   391  				})
   392  
   393  				When("stack is passed", func() {
   394  					BeforeEach(func() {
   395  						targetResource.BuildpackStack = "globinski"
   396  					})
   397  					It("shows 'Removing' as action", func() {
   398  						Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Removing label(s) for buildpack %s with stack %s as some-user...`), resourceName, targetResource.BuildpackStack))
   399  					})
   400  				})
   401  
   402  				When("stack is not passed", func() {
   403  					It("shows 'Removing' as action", func() {
   404  						Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Removing label(s) for buildpack %s as some-user...`), resourceName))
   405  					})
   406  				})
   407  
   408  			})
   409  			When("Setting labels", func() {
   410  				BeforeEach(func() {
   411  					cmd.Action = Set
   412  					//FIXME do we want to change the labels to all have not nil values?
   413  				})
   414  
   415  				When("stack is passed", func() {
   416  					BeforeEach(func() {
   417  						targetResource.BuildpackStack = "globinski"
   418  					})
   419  					It("shows 'Setting' as action", func() {
   420  						Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Setting label(s) for buildpack %s with stack %s as some-user...`), resourceName, targetResource.BuildpackStack))
   421  					})
   422  				})
   423  
   424  				When("stack is not passed", func() {
   425  					It("shows 'Setting' as action", func() {
   426  						Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Setting label(s) for buildpack %s as some-user...`), resourceName))
   427  					})
   428  				})
   429  			})
   430  
   431  		})
   432  	})
   433  
   434  	When("updating labels in domains", func() {
   435  		var (
   436  			domainName  string
   437  			executeErr  error
   438  			expectedMap map[string]types.NullString
   439  		)
   440  
   441  		BeforeEach(func() {
   442  			domainName = "example.com"
   443  			targetResource = TargetResource{
   444  				ResourceType: "domain",
   445  				ResourceName: domainName,
   446  			}
   447  			expectedMap = map[string]types.NullString{
   448  				"some-label":     types.NewNullString("some-value"),
   449  				"some-other-key": types.NewNullString()}
   450  			labels = expectedMap
   451  
   452  			fakeConfig.CurrentUserNameReturns("some-user", nil)
   453  			fakeActor.UpdateDomainLabelsByDomainNameReturns(
   454  				v7action.Warnings{"some-warning-1", "some-warning-2"},
   455  				nil,
   456  			)
   457  		})
   458  
   459  		JustBeforeEach(func() {
   460  			executeErr = cmd.Execute(targetResource, labels)
   461  		})
   462  
   463  		It("doesn't check that the user is logged in", func() {
   464  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(0))
   465  		})
   466  
   467  		When("updating the app labels succeeds", func() {
   468  			It("prints all warnings and does not return an error ", func() {
   469  				Expect(executeErr).ToNot(HaveOccurred())
   470  				Expect(testUI.Err).To(Say("some-warning-1"))
   471  				Expect(testUI.Err).To(Say("some-warning-2"))
   472  			})
   473  
   474  			It("passes the correct parameters into the actor", func() {
   475  				Expect(fakeActor.UpdateDomainLabelsByDomainNameCallCount()).To(Equal(1))
   476  				name, labels := fakeActor.UpdateDomainLabelsByDomainNameArgsForCall(0)
   477  				Expect(name).To(Equal(domainName), "failed to pass domain name")
   478  				Expect(labels).To(BeEquivalentTo(expectedMap))
   479  			})
   480  		})
   481  
   482  		When("the resource type argument is not lowercase", func() {
   483  			BeforeEach(func() {
   484  				targetResource.ResourceType = "DoMaiN"
   485  			})
   486  
   487  			It("passes the correct parameters into the actor", func() {
   488  				Expect(executeErr).ToNot(HaveOccurred())
   489  
   490  				Expect(fakeActor.UpdateDomainLabelsByDomainNameCallCount()).To(Equal(1))
   491  				name, labels := fakeActor.UpdateDomainLabelsByDomainNameArgsForCall(0)
   492  				Expect(name).To(Equal(domainName), "failed to pass domain name")
   493  				Expect(labels).To(BeEquivalentTo(expectedMap))
   494  			})
   495  
   496  			It("displays a message in the right casing", func() {
   497  				Expect(testUI.Out).To(Say("(.*) label\\(s\\) for domain (.*)"))
   498  				Expect(testUI.Out).To(Say("OK"))
   499  			})
   500  		})
   501  
   502  		When("updating the domain labels fails", func() {
   503  			BeforeEach(func() {
   504  				fakeActor.UpdateDomainLabelsByDomainNameReturns(
   505  					v7action.Warnings{"some-warning-1", "some-warning-2"},
   506  					errors.New("api call failed"))
   507  			})
   508  
   509  			It("returns the error and prints all warnings", func() {
   510  				Expect(executeErr).To(MatchError("api call failed"))
   511  				Expect(testUI.Err).To(Say("some-warning-1"))
   512  				Expect(testUI.Err).To(Say("some-warning-2"))
   513  			})
   514  		})
   515  
   516  		Context("Shows the right update message", func() {
   517  			When("Unsetting labels", func() {
   518  				BeforeEach(func() {
   519  					cmd.Action = Unset
   520  				})
   521  				It("shows 'Removing' as action", func() {
   522  					Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Removing label(s) for domain %s as some-user...`), domainName))
   523  				})
   524  			})
   525  			When("Setting labels", func() {
   526  				BeforeEach(func() {
   527  					cmd.Action = Set
   528  				})
   529  
   530  				It("shows 'Setting' as action", func() {
   531  					Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Setting label(s) for domain %s as some-user...`), domainName))
   532  				})
   533  			})
   534  		})
   535  	})
   536  
   537  	When("updating labels on orgs", func() {
   538  		var (
   539  			executeErr  error
   540  			orgName     = "some-org"
   541  			expectedMap map[string]types.NullString
   542  		)
   543  
   544  		BeforeEach(func() {
   545  			targetResource = TargetResource{
   546  				ResourceType: "org",
   547  				ResourceName: orgName,
   548  			}
   549  			fakeSharedActor.CheckTargetReturns(nil)
   550  			fakeConfig.CurrentUserNameReturns("some-user", nil)
   551  
   552  			expectedMap = map[string]types.NullString{
   553  				"some-label":     types.NewNullString("some-value"),
   554  				"some-other-key": types.NewNullString()}
   555  			labels = expectedMap
   556  
   557  			fakeActor.UpdateOrganizationLabelsByOrganizationNameReturns(
   558  				v7action.Warnings{"some-warning-1", "some-warning-2"},
   559  				nil)
   560  		})
   561  
   562  		JustBeforeEach(func() {
   563  			executeErr = cmd.Execute(targetResource, labels)
   564  		})
   565  
   566  		It("checks that the user is logged in", func() {
   567  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
   568  			checkOrg, checkSpace := fakeSharedActor.CheckTargetArgsForCall(0)
   569  			Expect(checkOrg).To(BeFalse())
   570  			Expect(checkSpace).To(BeFalse())
   571  		})
   572  
   573  		When("updating the orgs labels succeeds", func() {
   574  			It("does not return an error and prints all warnings", func() {
   575  				Expect(executeErr).ToNot(HaveOccurred())
   576  				Expect(testUI.Err).To(Say("some-warning-1"))
   577  				Expect(testUI.Err).To(Say("some-warning-2"))
   578  			})
   579  
   580  			It("passes the correct parameters into the actor", func() {
   581  				Expect(fakeActor.UpdateOrganizationLabelsByOrganizationNameCallCount()).To(Equal(1))
   582  				actualOrgName, labelsMap := fakeActor.UpdateOrganizationLabelsByOrganizationNameArgsForCall(0)
   583  				Expect(actualOrgName).To(Equal(orgName))
   584  				Expect(labelsMap).To(Equal(expectedMap))
   585  			})
   586  		})
   587  
   588  		When("the resource type argument is not lowercase", func() {
   589  			BeforeEach(func() {
   590  				targetResource.ResourceType = "OrG"
   591  			})
   592  
   593  			It("passes the correct parameters into the actor", func() {
   594  				Expect(executeErr).ToNot(HaveOccurred())
   595  				Expect(fakeActor.UpdateOrganizationLabelsByOrganizationNameCallCount()).To(Equal(1))
   596  				actualOrgName, labelsMap := fakeActor.UpdateOrganizationLabelsByOrganizationNameArgsForCall(0)
   597  				Expect(actualOrgName).To(Equal(orgName))
   598  				Expect(labelsMap).To(Equal(expectedMap))
   599  			})
   600  
   601  			It("displays a message in the right casing", func() {
   602  				Expect(testUI.Out).To(Say("(.*) label\\(s\\) for org (.*)"))
   603  				Expect(testUI.Out).To(Say("OK"))
   604  			})
   605  		})
   606  
   607  		When("updating the org labels fails", func() {
   608  			BeforeEach(func() {
   609  				fakeActor.UpdateOrganizationLabelsByOrganizationNameReturns(
   610  					v7action.Warnings{"some-warning-1", "some-warning-2"},
   611  					errors.New("api call failed"))
   612  			})
   613  
   614  			It("returns the error and prints all warnings", func() {
   615  				Expect(executeErr).To(MatchError("api call failed"))
   616  				Expect(testUI.Err).To(Say("some-warning-1"))
   617  				Expect(testUI.Err).To(Say("some-warning-2"))
   618  			})
   619  
   620  		})
   621  
   622  		Context("Shows the right update message", func() {
   623  			When("Unsetting labels", func() {
   624  				BeforeEach(func() {
   625  					cmd.Action = Unset
   626  				})
   627  				It("shows 'Removing' as action", func() {
   628  					Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Removing label(s) for org %s as some-user...`), orgName))
   629  				})
   630  			})
   631  			When("Setting labels", func() {
   632  				BeforeEach(func() {
   633  					cmd.Action = Set
   634  				})
   635  
   636  				It("shows 'Setting' as action", func() {
   637  					Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Setting label(s) for org %s as some-user...`), orgName))
   638  				})
   639  			})
   640  		})
   641  	})
   642  
   643  	When("updating labels on routes", func() {
   644  		var (
   645  			resourceName string
   646  			expectedMap  map[string]types.NullString
   647  			executeErr   error
   648  		)
   649  
   650  		BeforeEach(func() {
   651  			resourceName = "some-route.example.com"
   652  			targetResource = TargetResource{
   653  				ResourceType: "route",
   654  				ResourceName: resourceName,
   655  			}
   656  
   657  			expectedMap = map[string]types.NullString{
   658  				"some-label":     types.NewNullString("some-value"),
   659  				"some-other-key": types.NewNullString()}
   660  			labels = expectedMap
   661  
   662  			fakeConfig.CurrentUserNameReturns("some-user", nil)
   663  			fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: "fake-org"})
   664  			fakeConfig.TargetedSpaceReturns(configv3.Space{Name: "fake-space", GUID: "space-guid"})
   665  			fakeActor.UpdateRouteLabelsReturns(v7action.Warnings{"some-warning-1", "some-warning-2"},
   666  				nil)
   667  		})
   668  
   669  		JustBeforeEach(func() {
   670  			executeErr = cmd.Execute(targetResource, labels)
   671  		})
   672  
   673  		It("checks that the user is logged in", func() {
   674  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
   675  			checkOrg, checkSpace := fakeSharedActor.CheckTargetArgsForCall(0)
   676  			Expect(checkOrg).To(BeTrue())
   677  			Expect(checkSpace).To(BeTrue())
   678  		})
   679  
   680  		When("updating the route labels succeeds", func() {
   681  			It("doesn't error and prints all warnings", func() {
   682  				Expect(executeErr).ToNot(HaveOccurred())
   683  				Expect(testUI.Err).To(Say("some-warning-1"))
   684  				Expect(testUI.Err).To(Say("some-warning-2"))
   685  			})
   686  
   687  			It("passes the right parameters to the actor", func() {
   688  				Expect(fakeActor.UpdateRouteLabelsCallCount()).To(Equal(1))
   689  				name, spaceGUID, labels := fakeActor.UpdateRouteLabelsArgsForCall(0)
   690  				Expect(name).To(Equal(resourceName), "failed to pass route name")
   691  				Expect(labels).To(BeEquivalentTo(expectedMap))
   692  				Expect(spaceGUID).To(Equal("space-guid"))
   693  			})
   694  		})
   695  
   696  		When("the resource type argument is not lowercase", func() {
   697  			BeforeEach(func() {
   698  				targetResource.ResourceType = "rouTE"
   699  			})
   700  
   701  			It("passes the correct parameters into the actor", func() {
   702  				Expect(executeErr).ToNot(HaveOccurred())
   703  
   704  				Expect(fakeActor.UpdateRouteLabelsCallCount()).To(Equal(1))
   705  
   706  				name, spaceGUID, labels := fakeActor.UpdateRouteLabelsArgsForCall(0)
   707  
   708  				Expect(name).To(Equal(resourceName), "failed to pass route name")
   709  				Expect(spaceGUID).To(Equal("space-guid"))
   710  				Expect(labels).To(BeEquivalentTo(expectedMap))
   711  			})
   712  
   713  			It("displays a message in the right casing", func() {
   714  				Expect(testUI.Out).To(Say("(.*) label\\(s\\) for route (.*)"))
   715  				Expect(testUI.Out).To(Say("OK"))
   716  			})
   717  		})
   718  
   719  		When("updating the route labels fails", func() {
   720  			BeforeEach(func() {
   721  				fakeActor.UpdateRouteLabelsReturns(v7action.Warnings{"some-warning-1", "some-warning-2"},
   722  					errors.New("api call failed"))
   723  			})
   724  
   725  			It("returns the error and prints all warnings", func() {
   726  				Expect(executeErr).To(MatchError("api call failed"))
   727  				Expect(testUI.Err).To(Say("some-warning-1"))
   728  				Expect(testUI.Err).To(Say("some-warning-2"))
   729  			})
   730  
   731  		})
   732  
   733  		Context("Shows the right update message", func() {
   734  			When("Unsetting labels", func() {
   735  				BeforeEach(func() {
   736  					cmd.Action = Unset
   737  				})
   738  				It("shows 'Removing' as action", func() {
   739  					Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Removing label(s) for route %s in org fake-org / space fake-space as some-user...`), resourceName))
   740  				})
   741  			})
   742  			When("Setting labels", func() {
   743  				BeforeEach(func() {
   744  					cmd.Action = Set
   745  				})
   746  
   747  				It("shows 'Setting' as action", func() {
   748  					Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Setting label(s) for route %s in org fake-org / space fake-space as some-user...`), resourceName))
   749  				})
   750  			})
   751  		})
   752  	})
   753  
   754  	When("updating labels on service-broker", func() {
   755  		expectedServiceBrokerName := "my-broker"
   756  		var (
   757  			expectedMap map[string]types.NullString
   758  			executeErr  error
   759  		)
   760  
   761  		BeforeEach(func() {
   762  			targetResource = TargetResource{
   763  				ResourceType: "service-broker",
   764  				ResourceName: expectedServiceBrokerName,
   765  			}
   766  
   767  			fakeConfig.CurrentUserNameReturns("some-user", nil)
   768  			expectedMap = map[string]types.NullString{
   769  				"some-label":     types.NewNullString("some-value"),
   770  				"some-other-key": types.NewNullString(),
   771  			}
   772  			labels = expectedMap
   773  		})
   774  
   775  		JustBeforeEach(func() {
   776  			executeErr = cmd.Execute(targetResource, labels)
   777  		})
   778  
   779  		It("checks that the user is logged in", func() {
   780  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
   781  			checkOrg, checkSpace := fakeSharedActor.CheckTargetArgsForCall(0)
   782  			Expect(checkOrg).To(BeFalse())
   783  			Expect(checkSpace).To(BeFalse())
   784  		})
   785  
   786  		When("updating the service-broker labels succeeds", func() {
   787  			BeforeEach(func() {
   788  				fakeActor.UpdateServiceBrokerLabelsByServiceBrokerNameReturns(v7action.Warnings{"some-warning-1", "some-warning-2"},
   789  					nil)
   790  			})
   791  
   792  			It("prints all warnings and does not return an error", func() {
   793  				Expect(executeErr).ToNot(HaveOccurred())
   794  				Expect(testUI.Err).To(Say("some-warning-1"))
   795  				Expect(testUI.Err).To(Say("some-warning-2"))
   796  			})
   797  
   798  			It("passes the correct parameters into the actor", func() {
   799  				Expect(fakeActor.UpdateServiceBrokerLabelsByServiceBrokerNameCallCount()).To(Equal(1))
   800  				serviceBrokerName, labelsMap := fakeActor.UpdateServiceBrokerLabelsByServiceBrokerNameArgsForCall(0)
   801  				Expect(serviceBrokerName).To(Equal(expectedServiceBrokerName))
   802  				Expect(labelsMap).To(Equal(expectedMap))
   803  			})
   804  		})
   805  
   806  		When("the resource type argument is not lowercase", func() {
   807  			BeforeEach(func() {
   808  				fakeActor.UpdateServiceBrokerLabelsByServiceBrokerNameReturns(v7action.Warnings{"some-warning-1", "some-warning-2"},
   809  					nil)
   810  
   811  				targetResource.ResourceType = "sErVice-BroKer"
   812  			})
   813  
   814  			It("passes the correct parameters into the actor", func() {
   815  				Expect(executeErr).ToNot(HaveOccurred())
   816  
   817  				Expect(fakeActor.UpdateServiceBrokerLabelsByServiceBrokerNameCallCount()).To(Equal(1))
   818  				serviceBrokerName, labelsMap := fakeActor.UpdateServiceBrokerLabelsByServiceBrokerNameArgsForCall(0)
   819  				Expect(serviceBrokerName).To(Equal(expectedServiceBrokerName))
   820  				Expect(labelsMap).To(Equal(expectedMap))
   821  			})
   822  
   823  			It("displays a message in the right casing", func() {
   824  				Expect(testUI.Out).To(Say("(.*) label\\(s\\) for service-broker (.*)"))
   825  				Expect(testUI.Out).To(Say("OK"))
   826  			})
   827  		})
   828  
   829  		When("updating the service-broker labels fails", func() {
   830  			BeforeEach(func() {
   831  				fakeActor.UpdateServiceBrokerLabelsByServiceBrokerNameReturns(v7action.Warnings{"some-warning-1", "some-warning-2"},
   832  					errors.New("api call failed"))
   833  			})
   834  
   835  			It("prints all warnings and returns the error", func() {
   836  				Expect(executeErr).To(MatchError("api call failed"))
   837  				Expect(testUI.Err).To(Say("some-warning-1"))
   838  				Expect(testUI.Err).To(Say("some-warning-2"))
   839  			})
   840  		})
   841  
   842  		Context("Shows the right update message", func() {
   843  			When("Unsetting labels", func() {
   844  				BeforeEach(func() {
   845  					cmd.Action = Unset
   846  				})
   847  				It("shows 'Removing' as action", func() {
   848  					Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Removing label(s) for service-broker %s as some-user...`), expectedServiceBrokerName))
   849  					Expect(testUI.Out).To(Say("OK"))
   850  				})
   851  			})
   852  			When("Setting labels", func() {
   853  				BeforeEach(func() {
   854  					cmd.Action = Set
   855  				})
   856  
   857  				It("shows 'Setting' as action", func() {
   858  					Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Setting label(s) for service-broker %s as some-user...`), expectedServiceBrokerName))
   859  					Expect(testUI.Out).To(Say("OK"))
   860  				})
   861  			})
   862  		})
   863  	})
   864  
   865  	When("updating labels on spaces", func() {
   866  		var (
   867  			executeErr  error
   868  			spaceName   string
   869  			expectedMap map[string]types.NullString
   870  		)
   871  
   872  		BeforeEach(func() {
   873  			spaceName = "spiff"
   874  			targetResource = TargetResource{
   875  				ResourceType: "space",
   876  				ResourceName: spaceName,
   877  			}
   878  			expectedMap = map[string]types.NullString{
   879  				"some-label":     types.NewNullString("some-value"),
   880  				"some-other-key": types.NewNullString()}
   881  			labels = expectedMap
   882  
   883  			fakeConfig.CurrentUserNameReturns("some-user", nil)
   884  			fakeConfig.TargetedOrganizationReturns(
   885  				configv3.Organization{Name: "fake-org", GUID: "some-org-guid"})
   886  
   887  			fakeActor.UpdateSpaceLabelsBySpaceNameReturns(
   888  				v7action.Warnings{"some-warning-1", "some-warning-2"},
   889  				nil)
   890  		})
   891  
   892  		JustBeforeEach(func() {
   893  			executeErr = cmd.Execute(targetResource, labels)
   894  		})
   895  
   896  		It("checks that the user is logged in and targeted to an org and space", func() {
   897  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
   898  			checkOrg, checkSpace := fakeSharedActor.CheckTargetArgsForCall(0)
   899  			Expect(checkOrg).To(BeTrue())
   900  			Expect(checkSpace).To(BeFalse())
   901  		})
   902  
   903  		When("updating the space 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.UpdateSpaceLabelsBySpaceNameCallCount()).To(Equal(1))
   912  				actualSpaceName, orgGUID, labelsMap := fakeActor.UpdateSpaceLabelsBySpaceNameArgsForCall(0)
   913  				Expect(actualSpaceName).To(Equal(spaceName))
   914  				Expect(orgGUID).To(Equal("some-org-guid"))
   915  				Expect(labelsMap).To(Equal(expectedMap))
   916  			})
   917  
   918  		})
   919  
   920  		When("the resource type argument is not lowercase", func() {
   921  			BeforeEach(func() {
   922  				targetResource.ResourceType = "SpAcE"
   923  			})
   924  
   925  			It("passes the right parameters to the actor", func() {
   926  				Expect(executeErr).ToNot(HaveOccurred())
   927  				Expect(fakeActor.UpdateSpaceLabelsBySpaceNameCallCount()).To(Equal(1))
   928  				actualSpaceName, orgGUID, labelsMap := fakeActor.UpdateSpaceLabelsBySpaceNameArgsForCall(0)
   929  				Expect(actualSpaceName).To(Equal(spaceName))
   930  				Expect(orgGUID).To(Equal("some-org-guid"))
   931  				Expect(labelsMap).To(Equal(expectedMap))
   932  			})
   933  
   934  			It("displays a message in the right casing", func() {
   935  				Expect(testUI.Out).To(Say("(.*) label\\(s\\) for space (.*)"))
   936  				Expect(testUI.Out).To(Say("OK"))
   937  			})
   938  		})
   939  
   940  		When("updating the space labels fails", func() {
   941  			BeforeEach(func() {
   942  				fakeActor.UpdateSpaceLabelsBySpaceNameReturns(
   943  					v7action.Warnings{"some-warning-1", "some-warning-2"},
   944  					errors.New("api call failed"),
   945  				)
   946  			})
   947  
   948  			It("returns the error and prints all warnings", func() {
   949  				Expect(executeErr).To(MatchError("api call failed"))
   950  				Expect(testUI.Err).To(Say("some-warning-1"))
   951  				Expect(testUI.Err).To(Say("some-warning-2"))
   952  			})
   953  		})
   954  
   955  		Context("Shows the right update message", func() {
   956  			When("Unsetting labels", func() {
   957  				BeforeEach(func() {
   958  					cmd.Action = Unset
   959  				})
   960  				It("shows 'Removing' as action", func() {
   961  					Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Removing label(s) for space %s in org fake-org as some-user...`), spaceName))
   962  					Expect(testUI.Out).To(Say("OK"))
   963  				})
   964  			})
   965  
   966  			When("Setting labels", func() {
   967  				BeforeEach(func() {
   968  					cmd.Action = Set
   969  				})
   970  
   971  				It("shows 'Setting' as action", func() {
   972  					Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Setting label(s) for space %s in org fake-org as some-user...`), spaceName))
   973  					Expect(testUI.Out).To(Say("OK"))
   974  				})
   975  			})
   976  		})
   977  	})
   978  
   979  	When("updating labels on stacks", func() {
   980  		var (
   981  			executeErr  error
   982  			expectedMap map[string]types.NullString
   983  		)
   984  
   985  		stackName := "some-stack"
   986  		BeforeEach(func() {
   987  			targetResource = TargetResource{
   988  				ResourceType: "stack",
   989  				ResourceName: stackName,
   990  			}
   991  			fakeConfig.CurrentUserNameReturns("some-user", nil)
   992  			fakeSharedActor.CheckTargetReturns(nil)
   993  			expectedMap = map[string]types.NullString{
   994  				"some-label":     types.NewNullString("some-value"),
   995  				"some-other-key": types.NewNullString(),
   996  			}
   997  			labels = expectedMap
   998  
   999  		})
  1000  
  1001  		JustBeforeEach(func() {
  1002  			executeErr = cmd.Execute(targetResource, labels)
  1003  		})
  1004  
  1005  		It("checks that the user is logged in but not necessarily targeted to an org", func() {
  1006  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
  1007  			checkOrg, checkSpace := fakeSharedActor.CheckTargetArgsForCall(0)
  1008  			Expect(checkOrg).To(BeFalse())
  1009  			Expect(checkSpace).To(BeFalse())
  1010  		})
  1011  
  1012  		When("updating the stack labels succeeds", func() {
  1013  			BeforeEach(func() {
  1014  				fakeActor.UpdateStackLabelsByStackNameReturns(v7action.Warnings{"some-warning-1", "some-warning-2"},
  1015  					nil)
  1016  			})
  1017  
  1018  			It("does not return an error and prints all warnings", func() {
  1019  				Expect(executeErr).ToNot(HaveOccurred())
  1020  				Expect(testUI.Err).To(Say("some-warning-1"))
  1021  				Expect(testUI.Err).To(Say("some-warning-2"))
  1022  
  1023  			})
  1024  
  1025  			It("passes the correct parameters into the actor", func() {
  1026  
  1027  				Expect(fakeActor.UpdateStackLabelsByStackNameCallCount()).To(Equal(1))
  1028  				actualStackName, labelsMap := fakeActor.UpdateStackLabelsByStackNameArgsForCall(0)
  1029  				Expect(actualStackName).To(Equal(stackName))
  1030  				Expect(labelsMap).To(Equal(expectedMap))
  1031  			})
  1032  
  1033  		})
  1034  
  1035  		When("the resource type argument is not lowercase", func() {
  1036  			BeforeEach(func() {
  1037  				targetResource.ResourceType = "sTaCk"
  1038  			})
  1039  			It("passes the correct parameters into the actor", func() {
  1040  				Expect(executeErr).ToNot(HaveOccurred())
  1041  				Expect(fakeActor.UpdateStackLabelsByStackNameCallCount()).To(Equal(1))
  1042  				actualStackName, labelsMap := fakeActor.UpdateStackLabelsByStackNameArgsForCall(0)
  1043  				Expect(actualStackName).To(Equal(stackName))
  1044  				Expect(labelsMap).To(Equal(expectedMap))
  1045  			})
  1046  
  1047  			It("displays a message in the right casing", func() {
  1048  				Expect(testUI.Out).To(Say("(.*) label\\(s\\) for stack (.*)"))
  1049  				Expect(testUI.Out).To(Say("OK"))
  1050  			})
  1051  
  1052  		})
  1053  
  1054  		When("updating the stack labels fails", func() {
  1055  			BeforeEach(func() {
  1056  				fakeActor.UpdateStackLabelsByStackNameReturns(
  1057  					v7action.Warnings{"some-warning-1", "some-warning-2"},
  1058  					errors.New("api call failed"),
  1059  				)
  1060  			})
  1061  
  1062  			It("returns the error and prints all warnings", func() {
  1063  				Expect(executeErr).To(MatchError("api call failed"))
  1064  				Expect(testUI.Err).To(Say("some-warning-1"))
  1065  				Expect(testUI.Err).To(Say("some-warning-2"))
  1066  			})
  1067  		})
  1068  
  1069  		Context("Shows the right update message", func() {
  1070  			When("Unsetting labels", func() {
  1071  				BeforeEach(func() {
  1072  					cmd.Action = Unset
  1073  				})
  1074  				It("shows 'Removing' as action", func() {
  1075  					Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Removing label(s) for stack %s as some-user...`), stackName))
  1076  					Expect(testUI.Out).To(Say("OK"))
  1077  				})
  1078  			})
  1079  
  1080  			When("Setting labels", func() {
  1081  				BeforeEach(func() {
  1082  					cmd.Action = Set
  1083  				})
  1084  
  1085  				It("shows 'Setting' as action", func() {
  1086  					Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Setting label(s) for stack %s as some-user...`), stackName))
  1087  					Expect(testUI.Out).To(Say("OK"))
  1088  				})
  1089  			})
  1090  		})
  1091  
  1092  	})
  1093  
  1094  })