github.com/arunkumar7540/cli@v6.45.0+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 space", func() { 133 BeforeEach(func() { 134 spaceName = helpers.NewSpaceName() 135 136 helpers.SetupCF(orgName, spaceName) 137 }) 138 139 It("sets the specified labels on the space", func() { 140 session := helpers.CF("set-label", "space", spaceName, "some-key=some-value", "some-other-key=some-other-value") 141 Eventually(session).Should(Say(regexp.QuoteMeta(`Setting label(s) for space %s in org %s as %s...`), spaceName, orgName, username)) 142 Eventually(session).Should(Say("OK")) 143 Eventually(session).Should(Exit(0)) 144 spaceGUID := helpers.GetSpaceGUID(spaceName) 145 session = helpers.CF("curl", fmt.Sprintf("/v3/spaces/%s", spaceGUID)) 146 Eventually(session).Should(Exit(0)) 147 spaceJSON := session.Out.Contents() 148 var space commonResource 149 Expect(json.Unmarshal(spaceJSON, &space)).To(Succeed()) 150 Expect(len(space.Metadata.Labels)).To(Equal(2)) 151 Expect(space.Metadata.Labels["some-key"]).To(Equal("some-value")) 152 Expect(space.Metadata.Labels["some-other-key"]).To(Equal("some-other-value")) 153 }) 154 155 When("the space is unknown", func() { 156 It("displays an error", func() { 157 session := helpers.CF("set-label", "space", "non-existent-space", "some-key=some-value") 158 Eventually(session.Err).Should(Say("Space 'non-existent-space' not found")) 159 Eventually(session).Should(Say("FAILED")) 160 Eventually(session).Should(Exit(1)) 161 }) 162 }) 163 164 When("the label has an empty key and an invalid value", func() { 165 It("displays an error", func() { 166 session := helpers.CF("set-label", "space", spaceName, "=test", "sha2=108&eb90d734") 167 Eventually(session.Err).Should(Say("Metadata key error: label key cannot be empty string, Metadata value error: label '108&eb90d734' contains invalid characters")) 168 Eventually(session).Should(Say("FAILED")) 169 Eventually(session).Should(Exit(1)) 170 }) 171 }) 172 173 When("the label does not include a '=' to separate the key and value", func() { 174 It("displays an error", func() { 175 session := helpers.CF("set-label", "space", spaceName, "test-label") 176 Eventually(session.Err).Should(Say("Metadata error: no value provided for label 'test-label'")) 177 Eventually(session).Should(Say("FAILED")) 178 Eventually(session).Should(Exit(1)) 179 }) 180 }) 181 182 When("more than one value is provided for the same key", func() { 183 It("uses the last value", func() { 184 session := helpers.CF("set-label", "space", spaceName, "owner=sue", "owner=beth") 185 Eventually(session).Should(Exit(0)) 186 spaceGUID := helpers.GetSpaceGUID(spaceName) 187 session = helpers.CF("curl", fmt.Sprintf("/v3/spaces/%s", spaceGUID)) 188 Eventually(session).Should(Exit(0)) 189 spaceJSON := session.Out.Contents() 190 var space commonResource 191 Expect(json.Unmarshal(spaceJSON, &space)).To(Succeed()) 192 Expect(len(space.Metadata.Labels)).To(Equal(1)) 193 Expect(space.Metadata.Labels["owner"]).To(Equal("beth")) 194 }) 195 }) 196 }) 197 198 When("assigning label to org", func() { 199 It("sets the specified labels on the org", func() { 200 session := helpers.CF("set-label", "org", orgName, "pci=true", "public-facing=false") 201 Eventually(session).Should(Say(regexp.QuoteMeta(`Setting label(s) for org %s as %s...`), orgName, username)) 202 Eventually(session).Should(Say("OK")) 203 Eventually(session).Should(Exit(0)) 204 205 orgGUID := helpers.GetOrgGUID(orgName) 206 session = helpers.CF("curl", fmt.Sprintf("/v3/organizations/%s", orgGUID)) 207 Eventually(session).Should(Exit(0)) 208 orgJSON := session.Out.Contents() 209 var org commonResource 210 Expect(json.Unmarshal(orgJSON, &org)).To(Succeed()) 211 Expect(len(org.Metadata.Labels)).To(Equal(2)) 212 Expect(org.Metadata.Labels["pci"]).To(Equal("true")) 213 Expect(org.Metadata.Labels["public-facing"]).To(Equal("false")) 214 }) 215 216 When("the org is unknown", func() { 217 It("displays an error", func() { 218 session := helpers.CF("set-label", "org", "non-existent-org", "some-key=some-value") 219 Eventually(session.Err).Should(Say("Organization 'non-existent-org' not found")) 220 Eventually(session).Should(Say("FAILED")) 221 Eventually(session).Should(Exit(1)) 222 }) 223 }) 224 225 When("the label has an empty key and an invalid value", func() { 226 It("displays an error", func() { 227 session := helpers.CF("set-label", "org", orgName, "=test", "sha2=108&eb90d734") 228 Eventually(session.Err).Should(Say("Metadata key error: label key cannot be empty string, Metadata value error: label '108&eb90d734' contains invalid characters")) 229 Eventually(session).Should(Say("FAILED")) 230 Eventually(session).Should(Exit(1)) 231 }) 232 }) 233 234 When("the label does not include a '=' to separate the key and value", func() { 235 It("displays an error", func() { 236 session := helpers.CF("set-label", "org", orgName, "test-label") 237 Eventually(session.Err).Should(Say("Metadata error: no value provided for label 'test-label'")) 238 Eventually(session).Should(Say("FAILED")) 239 Eventually(session).Should(Exit(1)) 240 }) 241 }) 242 243 When("more than one value is provided for the same key", func() { 244 It("uses the last value", func() { 245 session := helpers.CF("set-label", "org", orgName, "owner=sue", "owner=beth") 246 Eventually(session).Should(Exit(0)) 247 orgGUID := helpers.GetOrgGUID(orgName) 248 session = helpers.CF("curl", fmt.Sprintf("/v3/organizations/%s", orgGUID)) 249 Eventually(session).Should(Exit(0)) 250 orgJSON := session.Out.Contents() 251 var org commonResource 252 Expect(json.Unmarshal(orgJSON, &org)).To(Succeed()) 253 Expect(len(org.Metadata.Labels)).To(Equal(1)) 254 Expect(org.Metadata.Labels["owner"]).To(Equal("beth")) 255 }) 256 }) 257 }) 258 }) 259 })