github.com/franc20/ayesa_sap@v7.0.0-beta.28.0.20200124003224-302d4d52fa6c+incompatible/integration/v7/isolated/set_label_command_test.go (about)

     1  package isolated
     2  
     3  import (
     4  	"fmt"
     5  	"regexp"
     6  
     7  	. "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers"
     8  	"code.cloudfoundry.org/cli/integration/helpers/fakeservicebroker"
     9  
    10  	"code.cloudfoundry.org/cli/integration/helpers"
    11  	. "github.com/onsi/ginkgo"
    12  	. "github.com/onsi/gomega"
    13  	. "github.com/onsi/gomega/gbytes"
    14  	. "github.com/onsi/gomega/gexec"
    15  )
    16  
    17  var _ = Describe("set-label command", func() {
    18  	Describe("help", func() {
    19  		When("--help flag is set", func() {
    20  			It("appears in cf help -a", func() {
    21  				session := helpers.CF("help", "-a")
    22  				Eventually(session).Should(Exit(0))
    23  				Expect(session).To(HaveCommandInCategoryWithDescription("set-label", "METADATA", "Set a label (key-value pairs) for an API resource"))
    24  			})
    25  
    26  			It("Displays command usage to output", func() {
    27  				session := helpers.CF("set-label", "--help")
    28  
    29  				Eventually(session).Should(Say("NAME:"))
    30  				Eventually(session).Should(Say(`\s+set-label - Set a label \(key-value pairs\) for an API resource`))
    31  				Eventually(session).Should(Say("USAGE:"))
    32  				Eventually(session).Should(Say(`\s+cf set-label RESOURCE RESOURCE_NAME KEY=VALUE\.\.\.`))
    33  				Eventually(session).Should(Say("EXAMPLES:"))
    34  				Eventually(session).Should(Say(`\s+cf set-label app dora env=production`))
    35  				Eventually(session).Should(Say(`\s+cf set-label org business pci=true public-facing=false`))
    36  				Eventually(session).Should(Say(`\s+cf set-label buildpack go_buildpack go=1.12 -s cflinuxfs3`))
    37  				Eventually(session).Should(Say("RESOURCES:"))
    38  				Eventually(session).Should(Say(`\s+app`))
    39  				Eventually(session).Should(Say(`\s+buildpack`))
    40  				Eventually(session).Should(Say(`\s+domain`))
    41  				Eventually(session).Should(Say(`\s+org`))
    42  				Eventually(session).Should(Say(`\s+route`))
    43  				Eventually(session).Should(Say(`\s+service-broker`))
    44  				Eventually(session).Should(Say(`\s+space`))
    45  				Eventually(session).Should(Say(`\s+stack`))
    46  				Eventually(session).Should(Say("OPTIONS:"))
    47  				Eventually(session).Should(Say(`\s+--stack, -s\s+Specify stack to disambiguate buildpacks with the same name`))
    48  				Eventually(session).Should(Say("SEE ALSO:"))
    49  				Eventually(session).Should(Say(`\s+labels, unset-label`))
    50  
    51  				Eventually(session).Should(Exit(0))
    52  			})
    53  		})
    54  	})
    55  
    56  	When("the environment is set up correctly", func() {
    57  		var (
    58  			orgName       string
    59  			spaceName     string
    60  			username      string
    61  			stackNameBase string
    62  		)
    63  
    64  		testExpectedBehaviors := func(resourceType, resourceTypeFormatted, resourceName string) {
    65  			By("checking the behavior when the resource does not exist", func() {
    66  				unknownResourceName := "non-existent-" + resourceType
    67  				session := helpers.CF("set-label", resourceType, unknownResourceName, "some-key=some-value")
    68  				Eventually(session.Err).Should(Say("%s '%s' not found", resourceTypeFormatted, unknownResourceName))
    69  				Eventually(session).Should(Say("FAILED"))
    70  				Eventually(session).Should(Exit(1))
    71  			})
    72  
    73  			By("checking the behavior when the label has an empty key and an invalid value", func() {
    74  				session := helpers.CF("set-label", resourceType, resourceName, "=test", "sha2=108&eb90d734")
    75  				Eventually(session.Err).Should(Say("Metadata label key error: key cannot be empty string, Metadata label value error: '108&eb90d734' contains invalid characters"))
    76  				Eventually(session).Should(Say("FAILED"))
    77  				Eventually(session).Should(Exit(1))
    78  			})
    79  
    80  			By("checking the behavior when the label does not include a '=' to separate the key and value", func() {
    81  				session := helpers.CF("set-label", resourceType, resourceName, "test-label")
    82  				Eventually(session.Err).Should(Say("Metadata error: no value provided for label 'test-label'"))
    83  				Eventually(session).Should(Say("FAILED"))
    84  				Eventually(session).Should(Exit(1))
    85  			})
    86  		}
    87  
    88  		BeforeEach(func() {
    89  			username, _ = helpers.GetCredentials()
    90  			orgName = helpers.NewOrgName()
    91  			stackNameBase = helpers.NewStackName()
    92  		})
    93  
    94  		When("assigning label to app", func() {
    95  			var appName string
    96  
    97  			BeforeEach(func() {
    98  				spaceName = helpers.NewSpaceName()
    99  				appName = helpers.PrefixedRandomName("app")
   100  
   101  				helpers.SetupCF(orgName, spaceName)
   102  				helpers.WithHelloWorldApp(func(appDir string) {
   103  					Eventually(helpers.CF("push", appName, "-p", appDir, "--no-start")).Should(Exit(0))
   104  				})
   105  			})
   106  
   107  			AfterEach(func() {
   108  				helpers.QuickDeleteOrg(orgName)
   109  			})
   110  
   111  			It("has the expected shared behaviors", func() {
   112  				testExpectedBehaviors("app", "App", appName)
   113  			})
   114  
   115  			It("sets the specified labels on the app", func() {
   116  				session := helpers.CF("set-label", "app", appName, "some-key=some-value", "some-other-key=some-other-value")
   117  				Eventually(session).Should(Say(regexp.QuoteMeta(`Setting label(s) for app %s in org %s / space %s as %s...`), appName, orgName, spaceName, username))
   118  				Eventually(session).Should(Say("OK"))
   119  				Eventually(session).Should(Exit(0))
   120  
   121  				helpers.CheckExpectedLabels(fmt.Sprintf("/v3/apps/%s", helpers.AppGUID(appName)), false, helpers.MetadataLabels{
   122  					"some-key":       "some-value",
   123  					"some-other-key": "some-other-value",
   124  				})
   125  			})
   126  
   127  			When("more than one value is provided for the same key", func() {
   128  				It("uses the last value", func() {
   129  					session := helpers.CF("set-label", "app", appName, "owner=sue", "owner=beth")
   130  					Eventually(session).Should(Exit(0))
   131  
   132  					helpers.CheckExpectedLabels(fmt.Sprintf("/v3/apps/%s", helpers.AppGUID(appName)), false, helpers.MetadataLabels{
   133  						"owner": "beth",
   134  					})
   135  				})
   136  			})
   137  		})
   138  
   139  		When("assigning label to domain", func() {
   140  			var (
   141  				domainName string
   142  				domain     helpers.Domain
   143  			)
   144  
   145  			BeforeEach(func() {
   146  				domainName = helpers.NewDomainName("labels")
   147  				domain = helpers.NewDomain(orgName, domainName)
   148  
   149  				helpers.SetupCFWithOrgOnly(orgName)
   150  				domain.CreatePrivate()
   151  			})
   152  
   153  			AfterEach(func() {
   154  				domain.DeletePrivate()
   155  				helpers.QuickDeleteOrg(orgName)
   156  			})
   157  
   158  			It("has the expected shared behaviors", func() {
   159  				testExpectedBehaviors("domain", "Domain", domainName)
   160  			})
   161  
   162  			It("sets the specified labels on the domain", func() {
   163  				session := helpers.CF("set-label", "domain", domainName, "some-key=some-value", "some-other-key=some-other-value")
   164  				Eventually(session).Should(Exit(0))
   165  				Expect(session).Should(Say(regexp.QuoteMeta(`Setting label(s) for domain %s as %s...`), domainName, username))
   166  				Expect(session).Should(Say("OK"))
   167  
   168  				helpers.CheckExpectedLabels(fmt.Sprintf("/v3/domains?names=%s", domainName), true, helpers.MetadataLabels{
   169  					"some-key":       "some-value",
   170  					"some-other-key": "some-other-value",
   171  				})
   172  			})
   173  
   174  			When("more than one value is provided for the same key", func() {
   175  				It("uses the last value", func() {
   176  					session := helpers.CF("set-label", "domain", domainName, "some-key=some-value", "some-key=some-other-value")
   177  					Eventually(session).Should(Exit(0))
   178  					Expect(session).Should(Say(regexp.QuoteMeta(`Setting label(s) for domain %s as %s...`), domainName, username))
   179  					Expect(session).Should(Say("OK"))
   180  
   181  					helpers.CheckExpectedLabels(fmt.Sprintf("/v3/domains?names=%s", domainName), true, helpers.MetadataLabels{
   182  						"some-key": "some-other-value",
   183  					})
   184  				})
   185  			})
   186  		})
   187  
   188  		When("assigning label to space", func() {
   189  			BeforeEach(func() {
   190  				spaceName = helpers.NewSpaceName()
   191  				helpers.SetupCF(orgName, spaceName)
   192  			})
   193  
   194  			AfterEach(func() {
   195  				helpers.QuickDeleteOrg(orgName)
   196  			})
   197  
   198  			It("has the expected shared behaviors", func() {
   199  				testExpectedBehaviors("space", "Space", spaceName)
   200  			})
   201  
   202  			It("sets the specified labels on the space", func() {
   203  				session := helpers.CF("set-label", "space", spaceName, "some-key=some-value", "some-other-key=some-other-value")
   204  				Eventually(session).Should(Say(regexp.QuoteMeta(`Setting label(s) for space %s in org %s as %s...`), spaceName, orgName, username))
   205  				Eventually(session).Should(Say("OK"))
   206  				Eventually(session).Should(Exit(0))
   207  
   208  				helpers.CheckExpectedLabels(fmt.Sprintf("/v3/spaces/%s", helpers.GetSpaceGUID(spaceName)), false, helpers.MetadataLabels{
   209  					"some-key":       "some-value",
   210  					"some-other-key": "some-other-value",
   211  				})
   212  			})
   213  
   214  			When("more than one value is provided for the same key", func() {
   215  				It("uses the last value", func() {
   216  					session := helpers.CF("set-label", "space", spaceName, "owner=sue", "owner=beth")
   217  					Eventually(session).Should(Exit(0))
   218  
   219  					helpers.CheckExpectedLabels(fmt.Sprintf("/v3/spaces/%s", helpers.GetSpaceGUID(spaceName)), false, helpers.MetadataLabels{
   220  						"owner": "beth",
   221  					})
   222  				})
   223  			})
   224  		})
   225  
   226  		When("assigning label to org", func() {
   227  			BeforeEach(func() {
   228  				helpers.SetupCFWithOrgOnly(orgName)
   229  			})
   230  
   231  			AfterEach(func() {
   232  				helpers.QuickDeleteOrg(orgName)
   233  			})
   234  
   235  			It("has the expected shared behaviors", func() {
   236  				testExpectedBehaviors("org", "Organization", orgName)
   237  			})
   238  
   239  			It("sets the specified labels on the org", func() {
   240  				session := helpers.CF("set-label", "org", orgName, "pci=true", "public-facing=false")
   241  				Eventually(session).Should(Say(regexp.QuoteMeta(`Setting label(s) for org %s as %s...`), orgName, username))
   242  				Eventually(session).Should(Say("OK"))
   243  				Eventually(session).Should(Exit(0))
   244  
   245  				helpers.CheckExpectedLabels(fmt.Sprintf("/v3/organizations/%s", helpers.GetOrgGUID(orgName)), false, helpers.MetadataLabels{
   246  					"pci":           "true",
   247  					"public-facing": "false",
   248  				})
   249  			})
   250  
   251  			When("more than one value is provided for the same key", func() {
   252  				It("uses the last value", func() {
   253  					session := helpers.CF("set-label", "org", orgName, "owner=sue", "owner=beth")
   254  					Eventually(session).Should(Exit(0))
   255  
   256  					helpers.CheckExpectedLabels(fmt.Sprintf("/v3/organizations/%s", helpers.GetOrgGUID(orgName)), false, helpers.MetadataLabels{
   257  						"owner": "beth",
   258  					})
   259  				})
   260  			})
   261  		})
   262  
   263  		When("assigning label to route", func() {
   264  			var (
   265  				orgGUID    string
   266  				routeName  string
   267  				domainName string
   268  				domain     helpers.Domain
   269  			)
   270  			BeforeEach(func() {
   271  				orgName = helpers.NewOrgName()
   272  				spaceName = helpers.NewSpaceName()
   273  				helpers.SetupCF(orgName, spaceName)
   274  
   275  				orgGUID = helpers.GetOrgGUID(orgName)
   276  				domainName = helpers.NewDomainName()
   277  				domain = helpers.NewDomain(orgName, domainName)
   278  				domain.Create()
   279  				Eventually(helpers.CF("create-route", domainName)).Should(Exit(0))
   280  				routeName = domainName
   281  			})
   282  
   283  			AfterEach(func() {
   284  				Eventually(helpers.CF("delete-route", domainName, "-f")).Should(Exit(0))
   285  				domain.Delete()
   286  				helpers.QuickDeleteOrg(orgName)
   287  			})
   288  
   289  			It("has the expected shared behaviors", func() {
   290  				// The Domain is checked first, hence why the error message says 'Domain' and not 'Route'
   291  				testExpectedBehaviors("route", "Domain", routeName)
   292  			})
   293  
   294  			When("the route is unknown", func() {
   295  				It("displays an error", func() {
   296  					invalidRoute := "non-existent-host." + domainName
   297  					session := helpers.CF("set-label", "route", invalidRoute, "some-key=some-value")
   298  					Eventually(session.Err).Should(Say(fmt.Sprintf("Route with host 'non-existent-host' and domain '%s' not found", domainName)))
   299  					Eventually(session).Should(Say("FAILED"))
   300  					Eventually(session).Should(Exit(1))
   301  				})
   302  			})
   303  
   304  			It("sets the specified labels on the route", func() {
   305  				session := helpers.CF("set-label", "route", routeName, "some-key=some-value", "some-other-key=some-other-value")
   306  
   307  				Eventually(session).Should(Say(regexp.QuoteMeta(`Setting label(s) for route %s in org %s / space %s as %s...`), routeName, orgName, spaceName, username))
   308  				Eventually(session).Should(Say("OK"))
   309  				Eventually(session).Should(Exit(0))
   310  
   311  				helpers.CheckExpectedLabels(fmt.Sprintf("/v3/routes?organization_guids=%s", orgGUID), true, helpers.MetadataLabels{
   312  					"some-key":       "some-value",
   313  					"some-other-key": "some-other-value",
   314  				})
   315  			})
   316  
   317  			When("more than one value is provided for the same key", func() {
   318  				It("uses the last value", func() {
   319  					session := helpers.CF("set-label", "route", routeName, "owner=sue", "owner=beth")
   320  					Eventually(session).Should(Exit(0))
   321  
   322  					helpers.CheckExpectedLabels(fmt.Sprintf("/v3/routes?organization_guids=%s", orgGUID), true, helpers.MetadataLabels{
   323  						"owner": "beth",
   324  					})
   325  				})
   326  			})
   327  		})
   328  
   329  		When("assigning label to buildpack", func() {
   330  			var (
   331  				buildpackName string
   332  			)
   333  
   334  			BeforeEach(func() {
   335  				helpers.LoginCF()
   336  				buildpackName = helpers.NewBuildpackName()
   337  			})
   338  
   339  			When("the buildpack exists for at most one stack", func() {
   340  				var (
   341  					currentStack string
   342  				)
   343  
   344  				BeforeEach(func() {
   345  					currentStack = helpers.PreferredStack()
   346  					helpers.BuildpackWithStack(func(buildpackPath string) {
   347  						session := helpers.CF("create-buildpack", buildpackName, buildpackPath, "98")
   348  						Eventually(session).Should(Exit(0))
   349  					}, currentStack)
   350  				})
   351  
   352  				AfterEach(func() {
   353  					helpers.CF("delete-buildpack", buildpackName, "-f", "-s", currentStack)
   354  				})
   355  
   356  				It("has the expected shared behaviors", func() {
   357  					testExpectedBehaviors("buildpack", "Buildpack", buildpackName)
   358  				})
   359  
   360  				It("sets the specified labels on the buildpack", func() {
   361  					session := helpers.CF("set-label", "buildpack", buildpackName, "pci=true", "public-facing=false")
   362  					Eventually(session).Should(Say(regexp.QuoteMeta(`Setting label(s) for buildpack %s as %s...`), buildpackName, username))
   363  					Eventually(session).Should(Say("OK"))
   364  					Eventually(session).Should(Exit(0))
   365  
   366  					buildpackGUID := helpers.BuildpackGUIDByNameAndStack(buildpackName, currentStack)
   367  					helpers.CheckExpectedLabels(fmt.Sprintf("/v3/buildpacks/%s", buildpackGUID), false, helpers.MetadataLabels{
   368  						"pci":           "true",
   369  						"public-facing": "false",
   370  					})
   371  				})
   372  
   373  				When("the buildpack exists for multiple stacks", func() {
   374  					var stacks []string
   375  
   376  					BeforeEach(func() {
   377  						stacks = []string{helpers.PreferredStack(), helpers.CreateStack()}
   378  
   379  						helpers.BuildpackWithStack(func(buildpackPath string) {
   380  							createSession := helpers.CF("create-buildpack", buildpackName, buildpackPath, "99")
   381  							Eventually(createSession).Should(Exit(0))
   382  						}, stacks[1])
   383  					})
   384  					AfterEach(func() {
   385  						helpers.CF("delete-buildpack", buildpackName, "-f", "-s", stacks[1])
   386  						helpers.DeleteStack(stacks[1])
   387  					})
   388  
   389  					When("stack is not specified", func() {
   390  						It("displays an error", func() {
   391  							session := helpers.CF("set-label", "buildpack", buildpackName, "some-key=some-value")
   392  							Eventually(session.Err).Should(Say(fmt.Sprintf("Multiple buildpacks named %s found. Specify a stack name by using a '-s' flag.", buildpackName)))
   393  							Eventually(session).Should(Say("FAILED"))
   394  							Eventually(session).Should(Exit(1))
   395  						})
   396  					})
   397  
   398  					When("stack is specified", func() {
   399  						It("sets the specified labels on the correct buildpack", func() {
   400  							session := helpers.CF("set-label", "buildpack", buildpackName, "pci=true", "public-facing=false", "--stack", stacks[1])
   401  							Eventually(session).Should(Say(regexp.QuoteMeta(`Setting label(s) for buildpack %s with stack %s as %s...`), buildpackName, stacks[1], username))
   402  							Eventually(session).Should(Say("OK"))
   403  							Eventually(session).Should(Exit(0))
   404  
   405  							buildpackGUID := helpers.BuildpackGUIDByNameAndStack(buildpackName, stacks[1])
   406  							helpers.CheckExpectedLabels(fmt.Sprintf("/v3/buildpacks/%s", buildpackGUID), false, helpers.MetadataLabels{
   407  								"pci":           "true",
   408  								"public-facing": "false",
   409  							})
   410  						})
   411  					})
   412  				})
   413  
   414  				When("the buildpack exists in general but does NOT exist for the specified stack", func() {
   415  					It("displays an error", func() {
   416  						session := helpers.CF("set-label", "buildpack", buildpackName, "some-key=some-value", "--stack", "FAKE")
   417  						Eventually(session.Err).Should(Say(fmt.Sprintf("Buildpack '%s' with stack 'FAKE' not found", buildpackName)))
   418  						Eventually(session).Should(Say("FAILED"))
   419  						Eventually(session).Should(Exit(1))
   420  					})
   421  				})
   422  
   423  				When("more than one value is provided for the same key", func() {
   424  					It("uses the last value", func() {
   425  						session := helpers.CF("set-label", "buildpack", buildpackName, "owner=sue", "owner=beth")
   426  						Eventually(session).Should(Exit(0))
   427  
   428  						buildpackGUID := helpers.BuildpackGUIDByNameAndStack(buildpackName, currentStack)
   429  						helpers.CheckExpectedLabels(fmt.Sprintf("/v3/buildpacks/%s", buildpackGUID), false, helpers.MetadataLabels{
   430  							"owner": "beth",
   431  						})
   432  					})
   433  				})
   434  			})
   435  
   436  			When("the buildpack exists for multiple stacks", func() {
   437  				var (
   438  					stacks             [3]string
   439  					buildpackGUIDs     [3]string
   440  					testWithStackCount int
   441  				)
   442  
   443  				BeforeEach(func() {
   444  					stacks[0] = helpers.PreferredStack()
   445  					testWithStackCount += 1
   446  					stacks[1] = helpers.CreateStack(fmt.Sprintf("%s-%d", stackNameBase, testWithStackCount))
   447  
   448  					for i := 0; i < 2; i++ {
   449  						helpers.BuildpackWithStack(func(buildpackPath string) {
   450  							createSession := helpers.CF("create-buildpack", buildpackName, buildpackPath,
   451  								fmt.Sprintf("%d", 95+i))
   452  							Eventually(createSession).Should(Exit(0))
   453  							buildpackGUIDs[i] = helpers.BuildpackGUIDByNameAndStack(buildpackName, stacks[i])
   454  						}, stacks[i])
   455  					}
   456  					helpers.CF("curl", "/v3/buildpacks?names="+buildpackName)
   457  				})
   458  
   459  				AfterEach(func() {
   460  					helpers.CF("delete-buildpack", buildpackName, "-f", "-s", stacks[0])
   461  					helpers.CF("delete-buildpack", buildpackName, "-f", "-s", stacks[1])
   462  					helpers.DeleteStack(stacks[1])
   463  				})
   464  
   465  				When("all buildpacks are stack-scoped", func() {
   466  					When("no stack is specified", func() {
   467  						It("displays an error", func() {
   468  							session := helpers.CF("set-label", "buildpack", buildpackName, "some-key=some-value")
   469  							Eventually(session.Err).Should(Say(fmt.Sprintf("Multiple buildpacks named %s found. Specify a stack name by using a '-s' flag.", buildpackName)))
   470  							Eventually(session).Should(Say("FAILED"))
   471  							Eventually(session).Should(Exit(1))
   472  						})
   473  					})
   474  
   475  					When("a non-existent stack is specified", func() {
   476  						It("displays an error", func() {
   477  							bogusStackName := stacks[0] + "-bogus-" + stacks[1]
   478  							session := helpers.CF("set-label", "buildpack", buildpackName, "olive=3", "mangosteen=4", "--stack", bogusStackName)
   479  							Eventually(session.Err).Should(Say(regexp.QuoteMeta(fmt.Sprintf("Buildpack '%s' with stack '%s' not found", buildpackName, bogusStackName))))
   480  							Eventually(session).Should(Say("FAILED"))
   481  							Eventually(session).Should(Exit(1))
   482  						})
   483  					})
   484  
   485  					When("an existing stack is specified", func() {
   486  						It("updates the correct buildpack", func() {
   487  							session := helpers.CF("set-label", "buildpack", buildpackName, "peach=5", "quince=6", "--stack", stacks[0])
   488  							Eventually(session).Should(Say(regexp.QuoteMeta(`Setting label(s) for buildpack %s with stack %s as %s...`), buildpackName, stacks[0], username))
   489  							Eventually(session).Should(Say("OK"))
   490  							Eventually(session).Should(Exit(0))
   491  
   492  							helpers.CheckExpectedLabels(fmt.Sprintf("/v3/buildpacks/%s", buildpackGUIDs[0]), false, helpers.MetadataLabels{
   493  								"peach":  "5",
   494  								"quince": "6",
   495  							})
   496  						})
   497  					})
   498  				})
   499  
   500  				When("one of the buildpacks is not stack-scoped", func() {
   501  					BeforeEach(func() {
   502  						helpers.BuildpackWithoutStack(func(buildpackPath string) {
   503  							createSession := helpers.CF("create-buildpack", buildpackName, buildpackPath, "97")
   504  							Eventually(createSession).Should(Exit(0))
   505  							buildpackGUIDs[2] = helpers.BuildpackGUIDByNameAndStack(buildpackName, "")
   506  						})
   507  					})
   508  					AfterEach(func() {
   509  						helpers.CF("delete-buildpack", buildpackName, "-f")
   510  					})
   511  
   512  					When("no stack is specified", func() {
   513  						It("updates the unscoped buildpack", func() {
   514  							session := helpers.CF("set-label", "buildpack", buildpackName, "mango=1", "figs=2")
   515  							Eventually(session).Should(Say(regexp.QuoteMeta(`Setting label(s) for buildpack %s as %s...`), buildpackName, username))
   516  							Eventually(session).Should(Say("OK"))
   517  							Eventually(session).Should(Exit(0))
   518  
   519  							helpers.CheckExpectedLabels(fmt.Sprintf("/v3/buildpacks/%s", buildpackGUIDs[2]), false, helpers.MetadataLabels{
   520  								"mango": "1",
   521  								"figs":  "2",
   522  							})
   523  						})
   524  					})
   525  
   526  					When("an existing stack is specified", func() {
   527  						It("updates the correct buildpack", func() {
   528  							session := helpers.CF("set-label", "buildpack", buildpackName, "tangelo=3", "lemon=4", "--stack", stacks[1])
   529  							Eventually(session).Should(Say(regexp.QuoteMeta(`Setting label(s) for buildpack %s with stack %s as %s...`), buildpackName, stacks[1], username))
   530  							Eventually(session).Should(Say("OK"))
   531  							Eventually(session).Should(Exit(0))
   532  
   533  							helpers.CheckExpectedLabels(fmt.Sprintf("/v3/buildpacks/%s", buildpackGUIDs[1]), false, helpers.MetadataLabels{
   534  								"tangelo": "3",
   535  								"lemon":   "4",
   536  							})
   537  						})
   538  					})
   539  
   540  					When("a non-existent stack is specified", func() {
   541  						It("displays an error", func() {
   542  							bogusStackName := stacks[0] + "-bogus-" + stacks[1]
   543  							session := helpers.CF("set-label", "buildpack", buildpackName, "olive=3", "mangosteen=4", "--stack", bogusStackName)
   544  							Eventually(session.Err).Should(Say(regexp.QuoteMeta(fmt.Sprintf("Buildpack '%s' with stack '%s' not found", buildpackName, bogusStackName))))
   545  							Eventually(session).Should(Say("FAILED"))
   546  							Eventually(session).Should(Exit(1))
   547  						})
   548  					})
   549  				})
   550  			})
   551  		})
   552  
   553  		When("assigning label to stack", func() {
   554  			var (
   555  				stackName string
   556  				stackGUID string
   557  			)
   558  
   559  			BeforeEach(func() {
   560  				helpers.LoginCF()
   561  				stackName, stackGUID = helpers.CreateStackWithGUID()
   562  			})
   563  
   564  			AfterEach(func() {
   565  				deleteResourceByGUID(stackGUID, "stacks")
   566  			})
   567  
   568  			It("has the expected shared behaviors", func() {
   569  				testExpectedBehaviors("stack", "Stack", stackName)
   570  			})
   571  
   572  			It("sets the specified labels on the stack", func() {
   573  				session := helpers.CF("set-label", "stack", stackName, "pci=true", "public-facing=false")
   574  				Eventually(session).Should(Say(regexp.QuoteMeta(`Setting label(s) for stack %s as %s...`), stackName, username))
   575  				Eventually(session).Should(Say("OK"))
   576  				Eventually(session).Should(Exit(0))
   577  
   578  				helpers.CheckExpectedLabels(fmt.Sprintf("/v3/stacks/%s", stackGUID), false, helpers.MetadataLabels{
   579  					"pci":           "true",
   580  					"public-facing": "false",
   581  				})
   582  			})
   583  
   584  			When("more than one value is provided for the same key", func() {
   585  				It("uses the last value", func() {
   586  					session := helpers.CF("set-label", "stack", stackName, "owner=sue", "owner=beth")
   587  					Eventually(session).Should(Exit(0))
   588  
   589  					helpers.CheckExpectedLabels(fmt.Sprintf("/v3/stacks/%s", stackGUID), false, helpers.MetadataLabels{
   590  						"owner": "beth",
   591  					})
   592  				})
   593  			})
   594  		})
   595  
   596  		When("assigning label to service-broker", func() {
   597  			var broker *fakeservicebroker.FakeServiceBroker
   598  
   599  			BeforeEach(func() {
   600  				orgName = helpers.NewOrgName()
   601  				spaceName = helpers.NewSpaceName()
   602  				helpers.SetupCF(orgName, spaceName)
   603  				broker = fakeservicebroker.New().EnsureBrokerIsAvailable()
   604  			})
   605  
   606  			AfterEach(func() {
   607  				broker.Destroy()
   608  				helpers.QuickDeleteOrg(orgName)
   609  			})
   610  
   611  			It("has the expected shared behaviors", func() {
   612  				testExpectedBehaviors("service-broker", "Service broker", broker.Name())
   613  			})
   614  
   615  			It("sets the specified labels on the service-broker", func() {
   616  				session := helpers.CF("set-label", "service-broker", broker.Name(), "some-key=some-value", "some-other-key=some-other-value")
   617  				Eventually(session).Should(Say(regexp.QuoteMeta(`Setting label(s) for service-broker %s as %s...`), broker.Name(), username))
   618  				Eventually(session).Should(Say("OK"))
   619  				Eventually(session).Should(Exit(0))
   620  
   621  				helpers.CheckExpectedLabels(fmt.Sprintf("/v3/service_brokers?names=%s", broker.Name()), true, helpers.MetadataLabels{
   622  					"some-key":       "some-value",
   623  					"some-other-key": "some-other-value",
   624  				})
   625  			})
   626  
   627  			When("more than one value is provided for the same key", func() {
   628  				It("uses the last value", func() {
   629  					session := helpers.CF("set-label", "service-broker", broker.Name(), "owner=sue", "owner=beth")
   630  					Eventually(session).Should(Exit(0))
   631  
   632  					helpers.CheckExpectedLabels(fmt.Sprintf("/v3/service_brokers?names=%s", broker.Name()), true, helpers.MetadataLabels{
   633  						"owner": "beth",
   634  					})
   635  				})
   636  			})
   637  		})
   638  	})
   639  })
   640  
   641  func deleteResourceByGUID(guid string, urlType string) {
   642  	session := helpers.CF("curl", "-v", "-X", "DELETE",
   643  		fmt.Sprintf("/v3/%s/%s", urlType, guid))
   644  	Eventually(session).Should(Exit(0))
   645  	Eventually(session).Should(Say(`(?:204 No Content|202 Accepted)`))
   646  }