github.com/cloudfoundry-community/cloudfoundry-cli@v6.44.1-0.20240130060226-cda5ed8e89a5+incompatible/integration/v7/isolated/set_label_command_test.go (about)

     1  package isolated
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"regexp"
     7  
     8  	"code.cloudfoundry.org/cli/integration/helpers"
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/gomega"
    11  	. "github.com/onsi/gomega/gbytes"
    12  	. "github.com/onsi/gomega/gexec"
    13  )
    14  
    15  var _ = Describe("set-label command", func() {
    16  	Describe("help", func() {
    17  		When("--help flag is set", func() {
    18  			It("Displays command usage to output", func() {
    19  				session := helpers.CF("set-label", "--help")
    20  
    21  				Eventually(session).Should(Say("NAME:"))
    22  				Eventually(session).Should(Say(`\s+set-label - Set a label \(key-value pairs\) for an API resource`))
    23  				Eventually(session).Should(Say("USAGE:"))
    24  				Eventually(session).Should(Say(`\s+cf set-label RESOURCE RESOURCE_NAME KEY=VALUE\.\.\.`))
    25  				Eventually(session).Should(Say("EXAMPLES:"))
    26  				Eventually(session).Should(Say(`\s+cf set-label app dora env=production`))
    27  				Eventually(session).Should(Say(`\s+cf set-label org business pci=true public-facing=false`))
    28  				Eventually(session).Should(Say(`\s+cf set-label space business_space public-facing=false owner=jane_doe`))
    29  				Eventually(session).Should(Say("RESOURCES:"))
    30  				Eventually(session).Should(Say(`\s+app`))
    31  				Eventually(session).Should(Say(`\s+org`))
    32  				Eventually(session).Should(Say(`\s+space`))
    33  				Eventually(session).Should(Say("SEE ALSO:"))
    34  				Eventually(session).Should(Say(`\s+delete-label, labels`))
    35  
    36  				Eventually(session).Should(Exit(0))
    37  			})
    38  		})
    39  	})
    40  
    41  	When("the environment is set up correctly", func() {
    42  		var (
    43  			orgName   string
    44  			spaceName string
    45  			appName   string
    46  			username  string
    47  		)
    48  
    49  		type commonResource struct {
    50  			Metadata struct {
    51  				Labels map[string]string
    52  			}
    53  		}
    54  
    55  		BeforeEach(func() {
    56  			username, _ = helpers.GetCredentials()
    57  			helpers.LoginCF()
    58  			orgName = helpers.NewOrgName()
    59  			helpers.CreateOrg(orgName)
    60  		})
    61  
    62  		When("assigning label to app", func() {
    63  			BeforeEach(func() {
    64  				spaceName = helpers.NewSpaceName()
    65  				appName = helpers.PrefixedRandomName("app")
    66  
    67  				helpers.SetupCF(orgName, spaceName)
    68  				helpers.WithHelloWorldApp(func(appDir string) {
    69  					Eventually(helpers.CF("push", appName, "-p", appDir)).Should(Exit(0))
    70  				})
    71  			})
    72  
    73  			It("sets the specified labels on the app", func() {
    74  				session := helpers.CF("set-label", "app", appName, "some-key=some-value", "some-other-key=some-other-value")
    75  				Eventually(session).Should(Say(regexp.QuoteMeta(`Setting label(s) for app %s in org %s / space %s as %s...`), appName, orgName, spaceName, username))
    76  				Eventually(session).Should(Say("OK"))
    77  				Eventually(session).Should(Exit(0))
    78  				appGUID := helpers.AppGUID(appName)
    79  				session = helpers.CF("curl", fmt.Sprintf("/v3/apps/%s", appGUID))
    80  				Eventually(session).Should(Exit(0))
    81  				appJSON := session.Out.Contents()
    82  				var app commonResource
    83  				Expect(json.Unmarshal(appJSON, &app)).To(Succeed())
    84  				Expect(len(app.Metadata.Labels)).To(Equal(2))
    85  				Expect(app.Metadata.Labels["some-key"]).To(Equal("some-value"))
    86  				Expect(app.Metadata.Labels["some-other-key"]).To(Equal("some-other-value"))
    87  			})
    88  
    89  			When("the app is unknown", func() {
    90  				It("displays an error", func() {
    91  					session := helpers.CF("set-label", "app", "non-existent-app", "some-key=some-value")
    92  					Eventually(session.Err).Should(Say("App 'non-existent-app' not found"))
    93  					Eventually(session).Should(Say("FAILED"))
    94  					Eventually(session).Should(Exit(1))
    95  				})
    96  			})
    97  
    98  			When("the label has an empty key and an invalid value", func() {
    99  				It("displays an error", func() {
   100  					session := helpers.CF("set-label", "app", appName, "=test", "sha2=108&eb90d734")
   101  					Eventually(session.Err).Should(Say("Metadata key error: label key cannot be empty string, Metadata value error: label '108&eb90d734' contains invalid characters"))
   102  					Eventually(session).Should(Say("FAILED"))
   103  					Eventually(session).Should(Exit(1))
   104  				})
   105  			})
   106  
   107  			When("the label does not include a '=' to separate the key and value", func() {
   108  				It("displays an error", func() {
   109  					session := helpers.CF("set-label", "app", appName, "test-label")
   110  					Eventually(session.Err).Should(Say("Metadata error: no value provided for label 'test-label'"))
   111  					Eventually(session).Should(Say("FAILED"))
   112  					Eventually(session).Should(Exit(1))
   113  				})
   114  			})
   115  
   116  			When("more than one value is provided for the same key", func() {
   117  				It("uses the last value", func() {
   118  					session := helpers.CF("set-label", "app", appName, "owner=sue", "owner=beth")
   119  					Eventually(session).Should(Exit(0))
   120  					appGUID := helpers.AppGUID(appName)
   121  					session = helpers.CF("curl", fmt.Sprintf("/v3/apps/%s", appGUID))
   122  					Eventually(session).Should(Exit(0))
   123  					appJSON := session.Out.Contents()
   124  					var app commonResource
   125  					Expect(json.Unmarshal(appJSON, &app)).To(Succeed())
   126  					Expect(len(app.Metadata.Labels)).To(Equal(1))
   127  					Expect(app.Metadata.Labels["owner"]).To(Equal("beth"))
   128  				})
   129  			})
   130  		})
   131  
   132  		When("assigning label to org", func() {
   133  			It("sets the specified labels on the org", func() {
   134  				session := helpers.CF("set-label", "org", orgName, "pci=true", "public-facing=false")
   135  				Eventually(session).Should(Say(regexp.QuoteMeta(`Setting label(s) for org %s as %s...`), orgName, username))
   136  				Eventually(session).Should(Say("OK"))
   137  				Eventually(session).Should(Exit(0))
   138  
   139  				orgGUID := helpers.GetOrgGUID(orgName)
   140  				session = helpers.CF("curl", fmt.Sprintf("/v3/organizations/%s", orgGUID))
   141  				Eventually(session).Should(Exit(0))
   142  				orgJSON := session.Out.Contents()
   143  				var org commonResource
   144  				Expect(json.Unmarshal(orgJSON, &org)).To(Succeed())
   145  				Expect(len(org.Metadata.Labels)).To(Equal(2))
   146  				Expect(org.Metadata.Labels["pci"]).To(Equal("true"))
   147  				Expect(org.Metadata.Labels["public-facing"]).To(Equal("false"))
   148  			})
   149  
   150  			When("the org is unknown", func() {
   151  				It("displays an error", func() {
   152  					session := helpers.CF("set-label", "org", "non-existent-org", "some-key=some-value")
   153  					Eventually(session.Err).Should(Say("Organization 'non-existent-org' not found"))
   154  					Eventually(session).Should(Say("FAILED"))
   155  					Eventually(session).Should(Exit(1))
   156  				})
   157  			})
   158  
   159  			When("the label has an empty key and an invalid value", func() {
   160  				It("displays an error", func() {
   161  					session := helpers.CF("set-label", "org", orgName, "=test", "sha2=108&eb90d734")
   162  					Eventually(session.Err).Should(Say("Metadata key error: label key cannot be empty string, Metadata value error: label '108&eb90d734' contains invalid characters"))
   163  					Eventually(session).Should(Say("FAILED"))
   164  					Eventually(session).Should(Exit(1))
   165  				})
   166  			})
   167  
   168  			When("the label does not include a '=' to separate the key and value", func() {
   169  				It("displays an error", func() {
   170  					session := helpers.CF("set-label", "org", orgName, "test-label")
   171  					Eventually(session.Err).Should(Say("Metadata error: no value provided for label 'test-label'"))
   172  					Eventually(session).Should(Say("FAILED"))
   173  					Eventually(session).Should(Exit(1))
   174  				})
   175  			})
   176  
   177  			When("more than one value is provided for the same key", func() {
   178  				It("uses the last value", func() {
   179  					session := helpers.CF("set-label", "org", orgName, "owner=sue", "owner=beth")
   180  					Eventually(session).Should(Exit(0))
   181  					orgGUID := helpers.GetOrgGUID(orgName)
   182  					session = helpers.CF("curl", fmt.Sprintf("/v3/organizations/%s", orgGUID))
   183  					Eventually(session).Should(Exit(0))
   184  					orgJSON := session.Out.Contents()
   185  					var org commonResource
   186  					Expect(json.Unmarshal(orgJSON, &org)).To(Succeed())
   187  					Expect(len(org.Metadata.Labels)).To(Equal(1))
   188  					Expect(org.Metadata.Labels["owner"]).To(Equal("beth"))
   189  				})
   190  			})
   191  		})
   192  	})
   193  })