github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/integration/v7/isolated/unset_label_command_test.go (about)

     1  package isolated
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"regexp"
     7  
     8  	. "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers"
     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("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+space`))
    43  			Eventually(session).Should(Say(`\s+stack`))
    44  			Eventually(session).Should(Say("OPTIONS:"))
    45  			Eventually(session).Should(Say(`\s+--stack, -s\s+Specify stack to disambiguate buildpacks with the same name`))
    46  			Eventually(session).Should(Say("SEE ALSO:"))
    47  			Eventually(session).Should(Say(`\s+labels, set-label`))
    48  			Eventually(session).Should(Exit(0))
    49  		})
    50  	})
    51  
    52  	When("the environment is set up correctly", func() {
    53  		var (
    54  			orgName   string
    55  			spaceName string
    56  			appName   string
    57  			username  string
    58  			stackName string
    59  		)
    60  
    61  		type commonResource struct {
    62  			Metadata struct {
    63  				Labels map[string]string
    64  			}
    65  		}
    66  
    67  		type resourceCollection struct {
    68  			Resources []commonResource
    69  		}
    70  
    71  		BeforeEach(func() {
    72  			username, _ = helpers.GetCredentials()
    73  			helpers.LoginCF()
    74  			orgName = helpers.NewOrgName()
    75  		})
    76  
    77  		When("unsetting labels from an app", func() {
    78  			BeforeEach(func() {
    79  				spaceName = helpers.NewSpaceName()
    80  				appName = helpers.PrefixedRandomName("app")
    81  
    82  				helpers.SetupCF(orgName, spaceName)
    83  				helpers.WithHelloWorldApp(func(appDir string) {
    84  					Eventually(helpers.CF("push", appName, "-p", appDir, "--no-start")).Should(Exit(0))
    85  				})
    86  
    87  				session := helpers.CF("set-label", "app", appName, "some-key=some-value", "some-other-key=some-other-value", "some-third-key=other")
    88  				Eventually(session).Should(Exit(0))
    89  			})
    90  			AfterEach(func() {
    91  				helpers.QuickDeleteOrg(orgName)
    92  			})
    93  
    94  			It("unsets the specified labels on the app", func() {
    95  				session := helpers.CF("unset-label", "app", appName, "some-other-key", "some-third-key")
    96  				Eventually(session).Should(Exit(0))
    97  				Expect(session).Should(Say(regexp.QuoteMeta(`Removing label(s) for app %s in org %s / space %s as %s...`), appName, orgName, spaceName, username))
    98  				Expect(session).Should(Say("OK"))
    99  
   100  				appGuid := helpers.AppGUID(appName)
   101  				session = helpers.CF("curl", fmt.Sprintf("/v3/apps/%s", appGuid))
   102  				Eventually(session).Should(Exit(0))
   103  				appJSON := session.Out.Contents()
   104  
   105  				var app commonResource
   106  				Expect(json.Unmarshal(appJSON, &app)).To(Succeed())
   107  				Expect(len(app.Metadata.Labels)).To(Equal(1))
   108  				Expect(app.Metadata.Labels["some-key"]).To(Equal("some-value"))
   109  			})
   110  		})
   111  
   112  		When("unsetting labels from a buildpack", func() {
   113  			var (
   114  				buildpackName string
   115  				buildpackGUID string
   116  				stacks        []string
   117  			)
   118  			BeforeEach(func() {
   119  				helpers.LoginCF()
   120  				buildpackName = helpers.NewBuildpackName()
   121  			})
   122  
   123  			When("there is only one instance of the given buildpack", func() {
   124  
   125  				BeforeEach(func() {
   126  					stacks = helpers.FetchStacks()
   127  					helpers.BuildpackWithStack(func(buildpackPath string) {
   128  						session := helpers.CF("create-buildpack", buildpackName, buildpackPath, "98")
   129  						Eventually(session).Should(Exit(0))
   130  					}, stacks[0])
   131  					buildpackGUID = helpers.BuildpackGUIDByNameAndStack(buildpackName, stacks[0])
   132  					session := helpers.CF("set-label", "buildpack", buildpackName, "pci=true", "public-facing=false")
   133  					Eventually(session).Should(Exit(0))
   134  				})
   135  				AfterEach(func() {
   136  					deleteResourceByGUID(buildpackGUID, "buildpacks")
   137  				})
   138  
   139  				It("unsets the specified labels on the buildpack", func() {
   140  					session := helpers.CF("unset-label", "buildpack", buildpackName, "public-facing", "pci")
   141  					Eventually(session).Should(Exit(0))
   142  					Expect(session).Should(Say(regexp.QuoteMeta(`Removing label(s) for buildpack %s as %s...`), buildpackName, username))
   143  					Expect(session).Should(Say("OK"))
   144  
   145  					// verify the labels are deleted
   146  					session = helpers.CF("curl", fmt.Sprintf("/v3/buildpacks/%s", buildpackGUID))
   147  					Eventually(session).Should(Exit(0))
   148  					buildpackJSON := session.Out.Contents()
   149  
   150  					var buildpack commonResource
   151  					Expect(json.Unmarshal(buildpackJSON, &buildpack)).To(Succeed())
   152  					Expect(len(buildpack.Metadata.Labels)).To(Equal(0))
   153  				})
   154  			})
   155  
   156  			When("the buildpack is unknown", func() {
   157  				It("displays an error", func() {
   158  					session := helpers.CF("unset-label", "buildpack", "non-existent-buildpack", "some-key=some-value")
   159  					Eventually(session).Should(Exit(1))
   160  					Expect(session.Err).Should(Say("Buildpack 'non-existent-buildpack' not found"))
   161  					Expect(session).Should(Say("FAILED"))
   162  				})
   163  			})
   164  
   165  			When("the buildpack exists for multiple stacks", func() {
   166  				var buildpackGUIDs [2]string
   167  				BeforeEach(func() {
   168  					stacks = []string{helpers.PreferredStack(), helpers.CreateStack()}
   169  					for i := 0; i < 2; i++ {
   170  						helpers.BuildpackWithStack(func(buildpackPath string) {
   171  							createSession := helpers.CF("create-buildpack", buildpackName, buildpackPath, fmt.Sprintf("%d", 98+i))
   172  							Eventually(createSession).Should(Exit(0))
   173  						}, stacks[i])
   174  						buildpackGUIDs[i] = helpers.BuildpackGUIDByNameAndStack(buildpackName, stacks[i])
   175  						session := helpers.CF("set-label", "buildpack",
   176  							buildpackName, "-s", stacks[i],
   177  							fmt.Sprintf("pci%d=true", i),
   178  							fmt.Sprintf("public-facing%d=false", i))
   179  						Eventually(session).Should(Exit(0))
   180  					}
   181  				})
   182  				AfterEach(func() {
   183  					for i := 0; i < 2; i++ {
   184  						deleteResourceByGUID(buildpackGUIDs[i], "buildpacks")
   185  					}
   186  					helpers.DeleteStack(stacks[1])
   187  				})
   188  
   189  				When("stack is not specified", func() {
   190  					It("displays an error", func() {
   191  						session := helpers.CF("unset-label", "buildpack", buildpackName, "pci1")
   192  						Eventually(session).Should(Exit(1))
   193  						Expect(session.Err).Should(Say(fmt.Sprintf("Multiple buildpacks named %s found. Specify a stack name by using a '-s' flag.", buildpackName)))
   194  						Expect(session).Should(Say("FAILED"))
   195  					})
   196  				})
   197  
   198  				When("stack is specified", func() {
   199  					When("the label is invalid", func() {
   200  						It("gives an error message", func() {
   201  							badLabel := "^^snorky"
   202  							session := helpers.CF("unset-label", "buildpack", buildpackName, badLabel, "--stack", stacks[0])
   203  							Eventually(session).Should(Exit(1))
   204  							Expect(session).Should(Say(regexp.QuoteMeta(fmt.Sprintf("Removing label(s) for buildpack %s with stack %s as %s...", buildpackName, stacks[0], username))))
   205  							Expect(session.Err).Should(Say(regexp.QuoteMeta(fmt.Sprintf("Metadata label key error: '%s' contains invalid characters", badLabel))))
   206  							Expect(session).Should(Say("FAILED"))
   207  						})
   208  					})
   209  
   210  					It("deletes the specified labels from the correct buildpack", func() {
   211  						var buildpack commonResource
   212  
   213  						session := helpers.CF("unset-label", "buildpack", buildpackName, "pci0", "--stack", stacks[0])
   214  						Eventually(session).Should(Exit(0))
   215  						Expect(session).Should(Say(regexp.QuoteMeta(`Removing label(s) for buildpack %s with stack %s as %s...`), buildpackName, stacks[0], username))
   216  						Expect(session).Should(Say("OK"))
   217  
   218  						session = helpers.CF("curl", fmt.Sprintf("/v3/buildpacks/%s", buildpackGUIDs[0]))
   219  						Eventually(session).Should(Exit(0))
   220  						buildpackJSON := session.Out.Contents()
   221  						Expect(json.Unmarshal(buildpackJSON, &buildpack)).To(Succeed())
   222  						Expect(len(buildpack.Metadata.Labels)).To(Equal(1))
   223  						Expect(buildpack.Metadata.Labels["public-facing0"]).To(Equal("false"))
   224  
   225  						session = helpers.CF("curl", fmt.Sprintf("/v3/buildpacks/%s", buildpackGUIDs[1]))
   226  						Eventually(session).Should(Exit(0))
   227  						buildpackJSON = session.Out.Contents()
   228  						buildpack = commonResource{}
   229  						Expect(json.Unmarshal(buildpackJSON, &buildpack)).To(Succeed())
   230  						Expect(len(buildpack.Metadata.Labels)).To(Equal(2))
   231  						Expect(buildpack.Metadata.Labels["pci1"]).To(Equal("true"))
   232  						Expect(buildpack.Metadata.Labels["public-facing1"]).To(Equal("false"))
   233  
   234  						session = helpers.CF("unset-label", "buildpack", buildpackName, "pci1", "--stack", stacks[1])
   235  						Eventually(session).Should(Exit(0))
   236  						Expect(session).Should(Say(regexp.QuoteMeta(`Removing label(s) for buildpack %s with stack %s as %s...`), buildpackName, stacks[1], username))
   237  						Expect(session).Should(Say("OK"))
   238  
   239  						session = helpers.CF("curl", fmt.Sprintf("/v3/buildpacks/%s", buildpackGUIDs[1]))
   240  						Eventually(session).Should(Exit(0))
   241  						buildpackJSON = session.Out.Contents()
   242  						buildpack = commonResource{}
   243  						Expect(json.Unmarshal(buildpackJSON, &buildpack)).To(Succeed())
   244  						Expect(len(buildpack.Metadata.Labels)).To(Equal(1))
   245  						Expect(buildpack.Metadata.Labels["public-facing1"]).To(Equal("false"))
   246  					})
   247  				})
   248  			})
   249  		})
   250  
   251  		When("unsetting labels from a domain", func() {
   252  
   253  			var (
   254  				domainName string
   255  				domain     helpers.Domain
   256  			)
   257  
   258  			BeforeEach(func() {
   259  				domainName = helpers.NewDomainName("labels")
   260  				domain = helpers.NewDomain(orgName, domainName)
   261  
   262  				helpers.SetupCFWithOrgOnly(orgName)
   263  				domain.CreatePrivate()
   264  
   265  				session := helpers.CF("set-label", "domain", domainName,
   266  					"some-key=some-value", "some-other-key=some-other-value", "some-third-key=other")
   267  				Eventually(session).Should(Exit(0))
   268  			})
   269  
   270  			AfterEach(func() {
   271  				domain.DeletePrivate()
   272  				helpers.QuickDeleteOrg(orgName)
   273  			})
   274  
   275  			It("unsets the specified labels on the domain", func() {
   276  				session := helpers.CF("unset-label", "domain", domainName,
   277  					"some-other-key", "some-third-key")
   278  				Eventually(session).Should(Exit(0))
   279  				Expect(session).Should(Say(regexp.QuoteMeta(`Removing label(s) for domain %s as %s...`), domainName, username))
   280  				Expect(session).Should(Say("OK"))
   281  
   282  				session = helpers.CF("curl", fmt.Sprintf("/v3/domains?names=%s", domainName))
   283  				Eventually(session).Should(Exit(0))
   284  				domainJSON := session.Out.Contents()
   285  				var domains resourceCollection
   286  				Expect(json.Unmarshal(domainJSON, &domains)).To(Succeed())
   287  				Expect(len(domains.Resources)).To(Equal(1))
   288  				Expect(len(domains.Resources[0].Metadata.Labels)).To(Equal(1))
   289  				Expect(domains.Resources[0].Metadata.Labels["some-key"]).To(Equal("some-value"))
   290  			})
   291  		})
   292  
   293  		When("unsetting labels from an org", func() {
   294  			BeforeEach(func() {
   295  				helpers.SetupCFWithOrgOnly(orgName)
   296  				session := helpers.CF("set-label", "org", orgName, "pci=true", "public-facing=false")
   297  				Eventually(session).Should(Exit(0))
   298  			})
   299  			AfterEach(func() {
   300  				helpers.QuickDeleteOrg(orgName)
   301  			})
   302  
   303  			It("unsets the specified labels on the org", func() {
   304  				session := helpers.CF("unset-label", "org", orgName, "public-facing")
   305  				Eventually(session).Should(Exit(0))
   306  				Expect(session).Should(Say(regexp.QuoteMeta(`Removing label(s) for org %s as %s...`), orgName, username))
   307  				Expect(session).Should(Say("OK"))
   308  
   309  				orgGUID := helpers.GetOrgGUID(orgName)
   310  				session = helpers.CF("curl", fmt.Sprintf("/v3/organizations/%s", orgGUID))
   311  				Eventually(session).Should(Exit(0))
   312  				orgJSON := session.Out.Contents()
   313  
   314  				var org commonResource
   315  				Expect(json.Unmarshal(orgJSON, &org)).To(Succeed())
   316  				Expect(len(org.Metadata.Labels)).To(Equal(1))
   317  				Expect(org.Metadata.Labels["pci"]).To(Equal("true"))
   318  			})
   319  		})
   320  
   321  		When("unsetting labels from a route", func() {
   322  			var (
   323  				orgGUID    string
   324  				routeName  string
   325  				domainName string
   326  				domain     helpers.Domain
   327  			)
   328  			BeforeEach(func() {
   329  				orgName = helpers.NewOrgName()
   330  				spaceName = helpers.NewSpaceName()
   331  				helpers.SetupCF(orgName, spaceName)
   332  
   333  				orgGUID = helpers.GetOrgGUID(orgName)
   334  				domainName = helpers.NewDomainName()
   335  				domain = helpers.NewDomain(orgName, domainName)
   336  				domain.Create()
   337  				Eventually(helpers.CF("create-route", domainName)).Should(Exit(0))
   338  				routeName = domainName
   339  
   340  				session := helpers.CF("set-label", "route", routeName, "some-key=some-value", "some-other-key=some-other-value")
   341  				Eventually(session).Should(Exit(0))
   342  				Expect(session).Should(Say(regexp.QuoteMeta(`Setting label(s) for route %s in org %s / space %s as %s...`), routeName, orgName, spaceName, username))
   343  				Expect(session).Should(Say("OK"))
   344  			})
   345  
   346  			AfterEach(func() {
   347  				Eventually(helpers.CF("delete-route", domainName, "-f")).Should(Exit(0))
   348  				domain.Delete()
   349  				helpers.QuickDeleteOrg(orgName)
   350  			})
   351  
   352  			It("unsets the specified labels on the route", func() {
   353  				session := helpers.CF("unset-label", "route", routeName, "some-key")
   354  				Eventually(session).Should(Exit(0))
   355  				Expect(session).Should(Say(regexp.QuoteMeta(`Removing label(s) for route %s in org %s / space %s as %s...`), routeName, orgName, spaceName, username))
   356  				Expect(session).Should(Say("OK"))
   357  
   358  				session = helpers.CF("curl", fmt.Sprintf("/v3/routes?organization_guids=%s", orgGUID))
   359  				Eventually(session).Should(Exit(0))
   360  
   361  				routeJSON := session.Out.Contents()
   362  				var routes resourceCollection
   363  
   364  				Expect(json.Unmarshal(routeJSON, &routes)).To(Succeed())
   365  				Expect(len(routes.Resources)).To(Equal(1))
   366  				Expect(len(routes.Resources[0].Metadata.Labels)).To(Equal(1))
   367  				Expect(routes.Resources[0].Metadata.Labels["some-other-key"]).To(Equal("some-other-value"))
   368  
   369  			})
   370  		})
   371  
   372  		When("unsetting labels from a space", func() {
   373  			BeforeEach(func() {
   374  				spaceName = helpers.NewSpaceName()
   375  				helpers.SetupCF(orgName, spaceName)
   376  				session := helpers.CF("set-label", "space", spaceName, "pci=true", "public-facing=false")
   377  				Eventually(session).Should(Exit(0))
   378  			})
   379  			AfterEach(func() {
   380  				helpers.QuickDeleteOrg(orgName)
   381  			})
   382  
   383  			It("unsets the specified labels on the space", func() {
   384  				session := helpers.CF("unset-label", "space", spaceName, "public-facing")
   385  				Eventually(session).Should(Exit(0))
   386  				Expect(session).Should(Say(regexp.QuoteMeta(`Removing label(s) for space %s in org %s as %s...`), spaceName, orgName, username))
   387  				Expect(session).Should(Say("OK"))
   388  
   389  				spaceGUID := helpers.GetSpaceGUID(spaceName)
   390  				session = helpers.CF("curl", fmt.Sprintf("/v3/spaces/%s", spaceGUID))
   391  				Eventually(session).Should(Exit(0))
   392  				spaceJSON := session.Out.Contents()
   393  
   394  				var space commonResource
   395  				Expect(json.Unmarshal(spaceJSON, &space)).To(Succeed())
   396  				Expect(len(space.Metadata.Labels)).To(Equal(1))
   397  				Expect(space.Metadata.Labels["pci"]).To(Equal("true"))
   398  			})
   399  		})
   400  
   401  		When("unsetting labels from a stack", func() {
   402  			var stackGUID string
   403  
   404  			BeforeEach(func() {
   405  				helpers.LoginCF()
   406  				stackName, stackGUID = helpers.CreateStackWithGUID()
   407  				session := helpers.CF("set-label", "stack", stackName, "pci=true", "public-facing=false")
   408  				Eventually(session).Should(Exit(0))
   409  			})
   410  
   411  			AfterEach(func() {
   412  				deleteResourceByGUID(stackGUID, "stacks")
   413  			})
   414  
   415  			It("unsets the specified labels on the stack", func() {
   416  				session := helpers.CF("unset-label", "stack", stackName, "public-facing")
   417  				Eventually(session).Should(Exit(0))
   418  				Expect(session).Should(Say(regexp.QuoteMeta(`Removing label(s) for stack %s as %s...`), stackName, username))
   419  				Expect(session).Should(Say("OK"))
   420  
   421  				session = helpers.CF("curl", fmt.Sprintf("/v3/stacks/%s", stackGUID))
   422  				Eventually(session).Should(Exit(0))
   423  				stackJSON := session.Out.Contents()
   424  
   425  				var stack commonResource
   426  				Expect(json.Unmarshal(stackJSON, &stack)).To(Succeed())
   427  				Expect(len(stack.Metadata.Labels)).To(Equal(1))
   428  				Expect(stack.Metadata.Labels["pci"]).To(Equal("true"))
   429  			})
   430  		})
   431  	})
   432  })