github.com/sleungcy/cli@v7.1.0+incompatible/integration/v7/isolated/unset_label_command_test.go (about)

     1  package isolated
     2  
     3  import (
     4  	"fmt"
     5  	"regexp"
     6  
     7  	"code.cloudfoundry.org/cli/integration/helpers/servicebrokerstub"
     8  
     9  	. "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers"
    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("unset-label command", func() {
    18  	When("--help flag is set", func() {
    19  		It("appears in cf help -a", func() {
    20  			session := helpers.CF("help", "-a")
    21  			Eventually(session).Should(Exit(0))
    22  			Expect(session).To(HaveCommandInCategoryWithDescription("unset-label", "METADATA", "Unset a label (key-value pairs) for an API resource"))
    23  		})
    24  
    25  		It("Displays command usage to output", func() {
    26  			session := helpers.CF("unset-label", "--help")
    27  
    28  			Eventually(session).Should(Say("NAME:"))
    29  			Eventually(session).Should(Say(`\s+unset-label - Unset a label \(key-value pairs\) for an API resource`))
    30  			Eventually(session).Should(Say("USAGE:"))
    31  			Eventually(session).Should(Say(`\s+cf unset-label RESOURCE RESOURCE_NAME KEY...`))
    32  			Eventually(session).Should(Say("EXAMPLES:"))
    33  			Eventually(session).Should(Say(`\s+cf unset-label app dora ci_signature_sha2`))
    34  			Eventually(session).Should(Say(`\s+cf unset-label org business pci public-facing`))
    35  			Eventually(session).Should(Say(`\s+cf unset-label buildpack go_buildpack go -s cflinuxfs3`))
    36  			Eventually(session).Should(Say("RESOURCES:"))
    37  			Eventually(session).Should(Say(`\s+app`))
    38  			Eventually(session).Should(Say(`\s+buildpack`))
    39  			Eventually(session).Should(Say(`\s+domain`))
    40  			Eventually(session).Should(Say(`\s+org`))
    41  			Eventually(session).Should(Say(`\s+route`))
    42  			Eventually(session).Should(Say(`\s+service-broker`))
    43  			Eventually(session).Should(Say(`\s+service-offering`))
    44  			Eventually(session).Should(Say(`\s+service-plan`))
    45  			Eventually(session).Should(Say(`\s+space`))
    46  			Eventually(session).Should(Say(`\s+stack`))
    47  			Eventually(session).Should(Say("OPTIONS:"))
    48  			Eventually(session).Should(Say(`\s+--stack, -s\s+Specify stack to disambiguate buildpacks with the same name`))
    49  			Eventually(session).Should(Say(`\s+--broker, -b\s+Specify a service broker to disambiguate service offerings or service plans with the same name`))
    50  			Eventually(session).Should(Say(`\s+--offering, -e\s+Specify a service offering to disambiguate service plans with the same name`))
    51  			Eventually(session).Should(Say("SEE ALSO:"))
    52  			Eventually(session).Should(Say(`\s+labels, set-label`))
    53  			Eventually(session).Should(Exit(0))
    54  		})
    55  	})
    56  
    57  	When("the environment is set up correctly", func() {
    58  		var (
    59  			orgName   string
    60  			spaceName string
    61  			username  string
    62  		)
    63  
    64  		BeforeEach(func() {
    65  			username, _ = helpers.GetCredentials()
    66  			helpers.LoginCF()
    67  			orgName = helpers.NewOrgName()
    68  		})
    69  
    70  		When("unsetting labels from an app", func() {
    71  			var appName string
    72  
    73  			BeforeEach(func() {
    74  				spaceName = helpers.NewSpaceName()
    75  				appName = helpers.PrefixedRandomName("app")
    76  
    77  				helpers.SetupCF(orgName, spaceName)
    78  				helpers.WithHelloWorldApp(func(appDir string) {
    79  					Eventually(helpers.CF("push", appName, "-p", appDir, "--no-start")).Should(Exit(0))
    80  				})
    81  
    82  				session := helpers.CF("set-label", "app", appName, "some-key=some-value", "some-other-key=some-other-value", "some-third-key=other")
    83  				Eventually(session).Should(Exit(0))
    84  			})
    85  			AfterEach(func() {
    86  				helpers.QuickDeleteOrg(orgName)
    87  			})
    88  
    89  			It("unsets the specified labels on the app", func() {
    90  				session := helpers.CF("unset-label", "app", appName, "some-other-key", "some-third-key")
    91  				Eventually(session).Should(Exit(0))
    92  				Expect(session).Should(Say(regexp.QuoteMeta(`Removing label(s) for app %s in org %s / space %s as %s...`), appName, orgName, spaceName, username))
    93  				Expect(session).Should(Say("OK"))
    94  
    95  				helpers.CheckExpectedLabels(fmt.Sprintf("/v3/apps/%s", helpers.AppGUID(appName)), false, helpers.MetadataLabels{
    96  					"some-key": "some-value",
    97  				})
    98  			})
    99  		})
   100  
   101  		When("unsetting labels from a buildpack", func() {
   102  			var (
   103  				buildpackName string
   104  				buildpackGUID string
   105  				stacks        []string
   106  			)
   107  			BeforeEach(func() {
   108  				helpers.LoginCF()
   109  				buildpackName = helpers.NewBuildpackName()
   110  			})
   111  
   112  			When("there is only one instance of the given buildpack", func() {
   113  
   114  				BeforeEach(func() {
   115  					stacks = helpers.FetchStacks()
   116  					helpers.BuildpackWithStack(func(buildpackPath string) {
   117  						session := helpers.CF("create-buildpack", buildpackName, buildpackPath, "98")
   118  						Eventually(session).Should(Exit(0))
   119  					}, stacks[0])
   120  					buildpackGUID = helpers.BuildpackGUIDByNameAndStack(buildpackName, stacks[0])
   121  					session := helpers.CF("set-label", "buildpack", buildpackName, "pci=true", "public-facing=false", "a-third-label=some-value")
   122  					Eventually(session).Should(Exit(0))
   123  				})
   124  				AfterEach(func() {
   125  					deleteResourceByGUID(buildpackGUID, "buildpacks")
   126  				})
   127  
   128  				It("unsets the specified labels on the buildpack", func() {
   129  					session := helpers.CF("unset-label", "buildpack", buildpackName, "public-facing", "pci")
   130  					Eventually(session).Should(Exit(0))
   131  					Expect(session).Should(Say(regexp.QuoteMeta(`Removing label(s) for buildpack %s as %s...`), buildpackName, username))
   132  					Expect(session).Should(Say("OK"))
   133  
   134  					helpers.CheckExpectedLabels(fmt.Sprintf("/v3/buildpacks/%s", buildpackGUID), false, helpers.MetadataLabels{
   135  						"a-third-label": "some-value",
   136  					})
   137  				})
   138  			})
   139  
   140  			When("the buildpack is unknown", func() {
   141  				It("displays an error", func() {
   142  					session := helpers.CF("unset-label", "buildpack", "non-existent-buildpack", "some-key=some-value")
   143  					Eventually(session).Should(Exit(1))
   144  					Expect(session.Err).Should(Say("Buildpack 'non-existent-buildpack' not found"))
   145  					Expect(session).Should(Say("FAILED"))
   146  				})
   147  			})
   148  
   149  			When("the buildpack exists for multiple stacks", func() {
   150  				var buildpackGUIDs [2]string
   151  				BeforeEach(func() {
   152  					stacks = []string{helpers.PreferredStack(), helpers.CreateStack()}
   153  					for i := 0; i < 2; i++ {
   154  						helpers.BuildpackWithStack(func(buildpackPath string) {
   155  							createSession := helpers.CF("create-buildpack", buildpackName, buildpackPath, fmt.Sprintf("%d", 98+i))
   156  							Eventually(createSession).Should(Exit(0))
   157  						}, stacks[i])
   158  						buildpackGUIDs[i] = helpers.BuildpackGUIDByNameAndStack(buildpackName, stacks[i])
   159  						session := helpers.CF("set-label", "buildpack",
   160  							buildpackName, "-s", stacks[i],
   161  							"pci=true",
   162  							fmt.Sprintf("public-facing%d=false", i))
   163  						Eventually(session).Should(Exit(0))
   164  					}
   165  				})
   166  				AfterEach(func() {
   167  					for i := 0; i < 2; i++ {
   168  						deleteResourceByGUID(buildpackGUIDs[i], "buildpacks")
   169  					}
   170  					helpers.DeleteStack(stacks[1])
   171  				})
   172  
   173  				When("stack is not specified", func() {
   174  					It("displays an error", func() {
   175  						session := helpers.CF("unset-label", "buildpack", buildpackName, "pci")
   176  						Eventually(session).Should(Exit(1))
   177  						Expect(session.Err).Should(Say(fmt.Sprintf("Multiple buildpacks named %s found. Specify a stack name by using a '-s' flag.", buildpackName)))
   178  						Expect(session).Should(Say("FAILED"))
   179  					})
   180  				})
   181  
   182  				When("stack is specified", func() {
   183  					When("the label is invalid", func() {
   184  						It("gives an error message", func() {
   185  							const badLabel = "^^snorky"
   186  							session := helpers.CF("unset-label", "buildpack", buildpackName, badLabel, "--stack", stacks[0])
   187  							Eventually(session).Should(Exit(1))
   188  							Expect(session).Should(Say(regexp.QuoteMeta(fmt.Sprintf("Removing label(s) for buildpack %s with stack %s as %s...", buildpackName, stacks[0], username))))
   189  							Expect(session.Err).Should(Say(regexp.QuoteMeta(fmt.Sprintf("Metadata label key error: '%s' contains invalid characters", badLabel))))
   190  							Expect(session).Should(Say("FAILED"))
   191  						})
   192  					})
   193  
   194  					It("deletes the specified labels from the correct buildpack", func() {
   195  						session := helpers.CF("unset-label", "buildpack", buildpackName, "pci", "--stack", stacks[0])
   196  						Eventually(session).Should(Exit(0))
   197  						Expect(session).Should(Say(regexp.QuoteMeta(`Removing label(s) for buildpack %s with stack %s as %s...`), buildpackName, stacks[0], username))
   198  						Expect(session).Should(Say("OK"))
   199  
   200  						helpers.CheckExpectedLabels(fmt.Sprintf("/v3/buildpacks/%s", buildpackGUIDs[0]), false, helpers.MetadataLabels{
   201  							"public-facing0": "false",
   202  						})
   203  
   204  						helpers.CheckExpectedLabels(fmt.Sprintf("/v3/buildpacks/%s", buildpackGUIDs[1]), false, helpers.MetadataLabels{
   205  							"public-facing1": "false",
   206  							"pci":            "true",
   207  						})
   208  					})
   209  				})
   210  			})
   211  		})
   212  
   213  		When("unsetting labels from a domain", func() {
   214  
   215  			var (
   216  				domainName string
   217  				domain     helpers.Domain
   218  			)
   219  
   220  			BeforeEach(func() {
   221  				domainName = helpers.NewDomainName("labels")
   222  				domain = helpers.NewDomain(orgName, domainName)
   223  
   224  				helpers.SetupCFWithOrgOnly(orgName)
   225  				domain.CreatePrivate()
   226  
   227  				session := helpers.CF("set-label", "domain", domainName,
   228  					"some-key=some-value", "some-other-key=some-other-value", "some-third-key=other")
   229  				Eventually(session).Should(Exit(0))
   230  			})
   231  
   232  			AfterEach(func() {
   233  				domain.DeletePrivate()
   234  				helpers.QuickDeleteOrg(orgName)
   235  			})
   236  
   237  			It("unsets the specified labels on the domain", func() {
   238  				session := helpers.CF("unset-label", "domain", domainName, "some-other-key", "some-third-key")
   239  				Eventually(session).Should(Exit(0))
   240  				Expect(session).Should(Say(regexp.QuoteMeta(`Removing label(s) for domain %s as %s...`), domainName, username))
   241  				Expect(session).Should(Say("OK"))
   242  
   243  				helpers.CheckExpectedLabels(fmt.Sprintf("/v3/domains?names=%s", domainName), true, helpers.MetadataLabels{
   244  					"some-key": "some-value",
   245  				})
   246  			})
   247  		})
   248  
   249  		When("unsetting labels from an org", func() {
   250  			BeforeEach(func() {
   251  				helpers.SetupCFWithOrgOnly(orgName)
   252  				session := helpers.CF("set-label", "org", orgName, "pci=true", "public-facing=false")
   253  				Eventually(session).Should(Exit(0))
   254  			})
   255  			AfterEach(func() {
   256  				helpers.QuickDeleteOrg(orgName)
   257  			})
   258  
   259  			It("unsets the specified labels on the org", func() {
   260  				session := helpers.CF("unset-label", "org", orgName, "public-facing")
   261  				Eventually(session).Should(Exit(0))
   262  				Expect(session).Should(Say(regexp.QuoteMeta(`Removing label(s) for org %s as %s...`), orgName, username))
   263  				Expect(session).Should(Say("OK"))
   264  
   265  				helpers.CheckExpectedLabels(fmt.Sprintf("/v3/organizations/%s", helpers.GetOrgGUID(orgName)), false, helpers.MetadataLabels{
   266  					"pci": "true",
   267  				})
   268  			})
   269  		})
   270  
   271  		When("unsetting labels from a route", func() {
   272  			var (
   273  				orgGUID    string
   274  				routeName  string
   275  				domainName string
   276  				domain     helpers.Domain
   277  			)
   278  			BeforeEach(func() {
   279  				orgName = helpers.NewOrgName()
   280  				spaceName = helpers.NewSpaceName()
   281  				helpers.SetupCF(orgName, spaceName)
   282  
   283  				orgGUID = helpers.GetOrgGUID(orgName)
   284  				domainName = helpers.NewDomainName()
   285  				domain = helpers.NewDomain(orgName, domainName)
   286  				domain.Create()
   287  				Eventually(helpers.CF("create-route", domainName)).Should(Exit(0))
   288  				routeName = domainName
   289  
   290  				session := helpers.CF("set-label", "route", routeName, "some-key=some-value", "some-other-key=some-other-value")
   291  				Eventually(session).Should(Exit(0))
   292  				Expect(session).Should(Say(regexp.QuoteMeta(`Setting label(s) for route %s in org %s / space %s as %s...`), routeName, orgName, spaceName, username))
   293  				Expect(session).Should(Say("OK"))
   294  			})
   295  
   296  			AfterEach(func() {
   297  				Eventually(helpers.CF("delete-route", domainName, "-f")).Should(Exit(0))
   298  				domain.Delete()
   299  				helpers.QuickDeleteOrg(orgName)
   300  			})
   301  
   302  			It("unsets the specified labels on the route", func() {
   303  				session := helpers.CF("unset-label", "route", routeName, "some-key")
   304  				Eventually(session).Should(Exit(0))
   305  				Expect(session).Should(Say(regexp.QuoteMeta(`Removing label(s) for route %s in org %s / space %s as %s...`), routeName, orgName, spaceName, username))
   306  				Expect(session).Should(Say("OK"))
   307  
   308  				helpers.CheckExpectedLabels(fmt.Sprintf("/v3/routes?organization_guids=%s", orgGUID), true, helpers.MetadataLabels{
   309  					"some-other-key": "some-other-value",
   310  				})
   311  			})
   312  		})
   313  
   314  		When("unsetting labels from a space", func() {
   315  			BeforeEach(func() {
   316  				spaceName = helpers.NewSpaceName()
   317  				helpers.SetupCF(orgName, spaceName)
   318  				session := helpers.CF("set-label", "space", spaceName, "pci=true", "public-facing=false")
   319  				Eventually(session).Should(Exit(0))
   320  			})
   321  			AfterEach(func() {
   322  				helpers.QuickDeleteOrg(orgName)
   323  			})
   324  
   325  			It("unsets the specified labels on the space", func() {
   326  				session := helpers.CF("unset-label", "space", spaceName, "public-facing")
   327  				Eventually(session).Should(Exit(0))
   328  				Expect(session).Should(Say(regexp.QuoteMeta(`Removing label(s) for space %s in org %s as %s...`), spaceName, orgName, username))
   329  				Expect(session).Should(Say("OK"))
   330  
   331  				helpers.CheckExpectedLabels(fmt.Sprintf("/v3/spaces/%s", helpers.GetSpaceGUID(spaceName)), false, helpers.MetadataLabels{
   332  					"pci": "true",
   333  				})
   334  			})
   335  		})
   336  
   337  		When("unsetting labels from a stack", func() {
   338  			var (
   339  				stackGUID string
   340  				stackName string
   341  			)
   342  
   343  			BeforeEach(func() {
   344  				helpers.LoginCF()
   345  				stackName, stackGUID = helpers.CreateStackWithGUID()
   346  				session := helpers.CF("set-label", "stack", stackName, "pci=true", "public-facing=false")
   347  				Eventually(session).Should(Exit(0))
   348  			})
   349  
   350  			AfterEach(func() {
   351  				deleteResourceByGUID(stackGUID, "stacks")
   352  			})
   353  
   354  			It("unsets the specified labels on the stack", func() {
   355  				session := helpers.CF("unset-label", "stack", stackName, "public-facing")
   356  				Eventually(session).Should(Exit(0))
   357  				Expect(session).Should(Say(regexp.QuoteMeta(`Removing label(s) for stack %s as %s...`), stackName, username))
   358  				Expect(session).Should(Say("OK"))
   359  
   360  				helpers.CheckExpectedLabels(fmt.Sprintf("/v3/stacks/%s", stackGUID), false, helpers.MetadataLabels{
   361  					"pci": "true",
   362  				})
   363  			})
   364  		})
   365  
   366  		When("unsetting labels from a service-broker", func() {
   367  			var broker *servicebrokerstub.ServiceBrokerStub
   368  
   369  			BeforeEach(func() {
   370  				spaceName = helpers.NewSpaceName()
   371  				helpers.SetupCF(orgName, spaceName)
   372  				broker = servicebrokerstub.Register()
   373  
   374  				session := helpers.CF("set-label", "service-broker", broker.Name, "pci=true", "public-facing=false")
   375  				Eventually(session).Should(Exit(0))
   376  			})
   377  
   378  			AfterEach(func() {
   379  				helpers.QuickDeleteOrg(orgName)
   380  				broker.Forget()
   381  			})
   382  
   383  			It("unsets the specified labels on the service-broker", func() {
   384  				session := helpers.CF("unset-label", "service-broker", broker.Name, "public-facing")
   385  				Eventually(session).Should(Exit(0))
   386  				Expect(session).Should(Say(regexp.QuoteMeta(`Removing label(s) for service-broker %s as %s...`), broker.Name, username))
   387  				Expect(session).Should(Say("OK"))
   388  
   389  				helpers.CheckExpectedLabels(fmt.Sprintf("/v3/service_brokers?names=%s", broker.Name), true, helpers.MetadataLabels{
   390  					"pci": "true",
   391  				})
   392  			})
   393  		})
   394  
   395  		When("unsetting labels from a service-offering", func() {
   396  			var (
   397  				broker              *servicebrokerstub.ServiceBrokerStub
   398  				serviceOfferingName string
   399  			)
   400  
   401  			BeforeEach(func() {
   402  				spaceName = helpers.NewSpaceName()
   403  				helpers.SetupCF(orgName, spaceName)
   404  				broker = servicebrokerstub.Register()
   405  				serviceOfferingName = broker.Services[0].Name
   406  
   407  				session := helpers.CF("set-label", "service-offering", serviceOfferingName, "pci=true", "public-facing=false")
   408  				Eventually(session).Should(Exit(0))
   409  			})
   410  
   411  			AfterEach(func() {
   412  				helpers.QuickDeleteOrg(orgName)
   413  				broker.Forget()
   414  			})
   415  
   416  			It("unsets the specified labels", func() {
   417  				session := helpers.CF("unset-label", "service-offering", serviceOfferingName, "public-facing")
   418  				Eventually(session).Should(Exit(0))
   419  				Expect(session).Should(Say(regexp.QuoteMeta(`Removing label(s) for service-offering %s as %s...`), serviceOfferingName, username))
   420  				Expect(session).Should(Say("OK"))
   421  
   422  				helpers.CheckExpectedLabels(fmt.Sprintf("/v3/service_offerings?names=%s", serviceOfferingName), true, helpers.MetadataLabels{
   423  					"pci": "true",
   424  				})
   425  			})
   426  
   427  			When("the service broker name is specified", func() {
   428  				It("unsets the specified labels", func() {
   429  					session := helpers.CF("unset-label", "-b", broker.Name, "service-offering", serviceOfferingName, "public-facing")
   430  					Eventually(session).Should(Exit(0))
   431  					Expect(session).Should(Say(regexp.QuoteMeta(`Removing label(s) for service-offering %s from service broker %s as %s...`), serviceOfferingName, broker.Name, username))
   432  					Expect(session).Should(Say("OK"))
   433  
   434  					helpers.CheckExpectedLabels(fmt.Sprintf("/v3/service_offerings?names=%s", serviceOfferingName), true, helpers.MetadataLabels{
   435  						"pci": "true",
   436  					})
   437  				})
   438  			})
   439  		})
   440  
   441  		When("unsetting labels from a service-plan", func() {
   442  			var (
   443  				broker              *servicebrokerstub.ServiceBrokerStub
   444  				servicePlanName     string
   445  				serviceOfferingName string
   446  			)
   447  
   448  			BeforeEach(func() {
   449  				spaceName = helpers.NewSpaceName()
   450  				helpers.SetupCF(orgName, spaceName)
   451  				broker = servicebrokerstub.Register()
   452  				servicePlanName = broker.Services[0].Plans[0].Name
   453  				serviceOfferingName = broker.Services[0].Name
   454  
   455  				session := helpers.CF("set-label", "service-plan", servicePlanName, "pci=true", "public-facing=false")
   456  				Eventually(session).Should(Exit(0))
   457  			})
   458  
   459  			AfterEach(func() {
   460  				helpers.QuickDeleteOrg(orgName)
   461  				broker.Forget()
   462  			})
   463  
   464  			It("unsets the specified labels", func() {
   465  				session := helpers.CF("unset-label", "service-plan", servicePlanName, "-b", broker.Name, "-e", serviceOfferingName, "public-facing")
   466  				Eventually(session).Should(Say(regexp.QuoteMeta(`Removing label(s) for service-plan %s from service offering %s / service broker %s as %s...`), servicePlanName, serviceOfferingName, broker.Name, username))
   467  				Eventually(session).Should(Exit(0))
   468  				Expect(session).Should(Say("OK"))
   469  
   470  				helpers.CheckExpectedLabels(fmt.Sprintf("/v3/service_plans?names=%s", servicePlanName), true, helpers.MetadataLabels{
   471  					"pci": "true",
   472  				})
   473  			})
   474  		})
   475  	})
   476  })