github.com/arunkumar7540/cli@v6.45.0+incompatible/command/v7/set_label_command_test.go (about) 1 package v7_test 2 3 import ( 4 "errors" 5 "regexp" 6 7 "code.cloudfoundry.org/cli/actor/v7action" 8 "code.cloudfoundry.org/cli/command/commandfakes" 9 "code.cloudfoundry.org/cli/command/flag" 10 . "code.cloudfoundry.org/cli/command/v7" 11 "code.cloudfoundry.org/cli/command/v7/v7fakes" 12 "code.cloudfoundry.org/cli/types" 13 "code.cloudfoundry.org/cli/util/configv3" 14 "code.cloudfoundry.org/cli/util/ui" 15 uuid "github.com/nu7hatch/gouuid" 16 . "github.com/onsi/ginkgo" 17 . "github.com/onsi/gomega" 18 . "github.com/onsi/gomega/gbytes" 19 ) 20 21 var _ = Describe("set-label command", func() { 22 var ( 23 cmd SetLabelCommand 24 resourceName string 25 fakeActor *v7fakes.FakeSetLabelActor 26 fakeConfig *commandfakes.FakeConfig 27 fakeSharedActor *commandfakes.FakeSharedActor 28 testUI *ui.UI 29 30 executeErr error 31 ) 32 33 When("setting labels on apps", func() { 34 35 BeforeEach(func() { 36 testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer()) 37 fakeActor = new(v7fakes.FakeSetLabelActor) 38 fakeConfig = new(commandfakes.FakeConfig) 39 fakeSharedActor = new(commandfakes.FakeSharedActor) 40 cmd = SetLabelCommand{ 41 Actor: fakeActor, 42 UI: testUI, 43 Config: fakeConfig, 44 SharedActor: fakeSharedActor, 45 } 46 cmd.RequiredArgs = flag.SetLabelArgs{ 47 ResourceType: "app", 48 } 49 }) 50 51 JustBeforeEach(func() { 52 executeErr = cmd.Execute(nil) 53 }) 54 55 It("doesn't error", func() { 56 Expect(executeErr).ToNot(HaveOccurred()) 57 }) 58 59 It("checks that the user is logged in and targeted to an org and space", func() { 60 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 61 checkOrg, checkSpace := fakeSharedActor.CheckTargetArgsForCall(0) 62 Expect(checkOrg).To(BeTrue()) 63 Expect(checkSpace).To(BeTrue()) 64 }) 65 66 When("checking target succeeds", func() { 67 BeforeEach(func() { 68 fakeSharedActor.CheckTargetReturns(nil) 69 }) 70 71 When("fetching current user's name succeeds", func() { 72 BeforeEach(func() { 73 fakeConfig.CurrentUserNameReturns("some-user", nil) 74 fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: "fake-org"}) 75 fakeConfig.TargetedSpaceReturns(configv3.Space{Name: "fake-space", GUID: "some-space-guid"}) 76 77 u, err := uuid.NewV4() 78 Expect(err).NotTo(HaveOccurred()) 79 resourceName = u.String() 80 }) 81 82 When("all the provided labels are valid", func() { 83 BeforeEach(func() { 84 cmd.RequiredArgs = flag.SetLabelArgs{ 85 ResourceType: "app", 86 ResourceName: resourceName, 87 Labels: []string{"FOO=BAR", "ENV=FAKE"}, 88 } 89 fakeActor.UpdateApplicationLabelsByApplicationNameReturns( 90 v7action.Warnings([]string{"some-warning-1", "some-warning-2"}), 91 nil, 92 ) 93 }) 94 95 When("updating the application labels succeeds", func() { 96 It("sets the provided labels on the app", func() { 97 name, spaceGUID, labels := fakeActor.UpdateApplicationLabelsByApplicationNameArgsForCall(0) 98 Expect(name).To(Equal(resourceName), "failed to pass app name") 99 Expect(spaceGUID).To(Equal("some-space-guid")) 100 Expect(labels).To(BeEquivalentTo(map[string]types.NullString{ 101 "FOO": types.NewNullString("BAR"), 102 "ENV": types.NewNullString("FAKE"), 103 })) 104 }) 105 106 It("displays a message", func() { 107 Expect(executeErr).ToNot(HaveOccurred()) 108 109 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 110 111 Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Setting label(s) for app %s in org fake-org / space fake-space as some-user...`), resourceName)) 112 Expect(testUI.Out).To(Say("OK")) 113 }) 114 115 It("prints all warnings", func() { 116 Expect(testUI.Err).To(Say("some-warning-1")) 117 Expect(testUI.Err).To(Say("some-warning-2")) 118 }) 119 }) 120 }) 121 122 When("updating the application labels fail", func() { 123 BeforeEach(func() { 124 cmd.RequiredArgs = flag.SetLabelArgs{ 125 ResourceType: "app", 126 ResourceName: resourceName, 127 Labels: []string{"FOO=BAR", "ENV=FAKE"}, 128 } 129 fakeActor.UpdateApplicationLabelsByApplicationNameReturns( 130 v7action.Warnings([]string{"some-warning-1", "some-warning-2"}), 131 errors.New("some-updating-error"), 132 ) 133 }) 134 It("displays warnings and an error message", func() { 135 Expect(testUI.Err).To(Say("some-warning-1")) 136 Expect(testUI.Err).To(Say("some-warning-2")) 137 Expect(executeErr).To(HaveOccurred()) 138 Expect(executeErr).To(MatchError("some-updating-error")) 139 }) 140 }) 141 142 When("some provided labels do not have a value part", func() { 143 BeforeEach(func() { 144 cmd.RequiredArgs = flag.SetLabelArgs{ 145 ResourceType: "app", 146 ResourceName: resourceName, 147 Labels: []string{"FOO=BAR", "MISSING_EQUALS", "ENV=FAKE"}, 148 } 149 }) 150 151 It("complains about the missing equal sign", func() { 152 Expect(executeErr).To(MatchError("Metadata error: no value provided for label 'MISSING_EQUALS'")) 153 }) 154 }) 155 }) 156 157 When("fetching the current user's name fails", func() { 158 BeforeEach(func() { 159 cmd.RequiredArgs = flag.SetLabelArgs{ 160 ResourceType: "app", 161 ResourceName: resourceName, 162 Labels: []string{"FOO=BAR", "ENV=FAKE"}, 163 } 164 fakeConfig.CurrentUserNameReturns("some-user", errors.New("boom")) 165 }) 166 167 It("returns an error", func() { 168 Expect(executeErr).To(MatchError("boom")) 169 }) 170 }) 171 }) 172 173 When("checking targeted org and space fails", func() { 174 BeforeEach(func() { 175 fakeSharedActor.CheckTargetReturns(errors.New("nope")) 176 }) 177 178 It("returns an error", func() { 179 Expect(executeErr).To(MatchError("nope")) 180 }) 181 }) 182 }) 183 184 When("setting labels on orgs", func() { 185 BeforeEach(func() { 186 testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer()) 187 fakeActor = new(v7fakes.FakeSetLabelActor) 188 fakeConfig = new(commandfakes.FakeConfig) 189 fakeSharedActor = new(commandfakes.FakeSharedActor) 190 resourceName = "some-org" 191 cmd = SetLabelCommand{ 192 Actor: fakeActor, 193 UI: testUI, 194 Config: fakeConfig, 195 SharedActor: fakeSharedActor, 196 } 197 cmd.RequiredArgs = flag.SetLabelArgs{ 198 ResourceType: "org", 199 ResourceName: resourceName, 200 } 201 }) 202 203 JustBeforeEach(func() { 204 executeErr = cmd.Execute(nil) 205 }) 206 207 It("doesn't error", func() { 208 Expect(executeErr).ToNot(HaveOccurred()) 209 }) 210 211 It("checks that the user is logged in", func() { 212 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 213 checkOrg, checkSpace := fakeSharedActor.CheckTargetArgsForCall(0) 214 Expect(checkOrg).To(BeFalse()) 215 Expect(checkSpace).To(BeFalse()) 216 }) 217 218 When("checking target succeeds", func() { 219 BeforeEach(func() { 220 fakeSharedActor.CheckTargetReturns(nil) 221 }) 222 223 When("fetching current user's name succeeds", func() { 224 BeforeEach(func() { 225 fakeConfig.CurrentUserNameReturns("some-user", nil) 226 }) 227 228 When("all the provided labels are valid", func() { 229 BeforeEach(func() { 230 cmd.RequiredArgs = flag.SetLabelArgs{ 231 ResourceType: "org", 232 ResourceName: resourceName, 233 Labels: []string{"FOO=BAR", "ENV=FAKE"}, 234 } 235 fakeActor.UpdateOrganizationLabelsByOrganizationNameReturns( 236 v7action.Warnings([]string{"some-warning-1", "some-warning-2"}), 237 nil, 238 ) 239 }) 240 241 When("updating the application labels succeeds", func() { 242 It("sets the provided labels on the app", func() { 243 Expect(fakeActor.UpdateOrganizationLabelsByOrganizationNameCallCount()).To(Equal(1)) 244 name, labels := fakeActor.UpdateOrganizationLabelsByOrganizationNameArgsForCall(0) 245 Expect(name).To(Equal(resourceName), "failed to pass app name") 246 Expect(labels).To(BeEquivalentTo(map[string]types.NullString{ 247 "FOO": types.NewNullString("BAR"), 248 "ENV": types.NewNullString("FAKE"), 249 })) 250 }) 251 252 It("displays a message", func() { 253 Expect(executeErr).ToNot(HaveOccurred()) 254 255 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 256 257 Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Setting label(s) for org %s as some-user...`), resourceName)) 258 Expect(testUI.Out).To(Say("OK")) 259 }) 260 261 It("prints all warnings", func() { 262 Expect(testUI.Err).To(Say("some-warning-1")) 263 Expect(testUI.Err).To(Say("some-warning-2")) 264 }) 265 }) 266 }) 267 268 When("updating the application labels fail", func() { 269 BeforeEach(func() { 270 cmd.RequiredArgs = flag.SetLabelArgs{ 271 ResourceType: "org", 272 ResourceName: resourceName, 273 Labels: []string{"FOO=BAR", "ENV=FAKE"}, 274 } 275 fakeActor.UpdateOrganizationLabelsByOrganizationNameReturns( 276 v7action.Warnings([]string{"some-warning-1", "some-warning-2"}), 277 errors.New("some-updating-error"), 278 ) 279 }) 280 281 It("displays warnings and an error message", func() { 282 Expect(testUI.Err).To(Say("some-warning-1")) 283 Expect(testUI.Err).To(Say("some-warning-2")) 284 Expect(executeErr).To(MatchError("some-updating-error")) 285 }) 286 }) 287 288 When("some provided labels do not have a value part", func() { 289 BeforeEach(func() { 290 cmd.RequiredArgs = flag.SetLabelArgs{ 291 ResourceType: "org", 292 ResourceName: resourceName, 293 Labels: []string{"FOO=BAR", "MISSING_EQUALS", "ENV=FAKE"}, 294 } 295 }) 296 297 It("complains about the missing equal sign", func() { 298 Expect(executeErr).To(MatchError("Metadata error: no value provided for label 'MISSING_EQUALS'")) 299 Expect(executeErr).To(HaveOccurred()) 300 }) 301 }) 302 }) 303 304 When("fetching the current user's name fails", func() { 305 BeforeEach(func() { 306 cmd.RequiredArgs = flag.SetLabelArgs{ 307 ResourceType: "org", 308 ResourceName: resourceName, 309 } 310 fakeConfig.CurrentUserNameReturns("some-user", errors.New("boom")) 311 }) 312 313 It("returns an error", func() { 314 Expect(executeErr).To(MatchError("boom")) 315 }) 316 }) 317 }) 318 319 When("checking targeted org and space fails", func() { 320 BeforeEach(func() { 321 fakeSharedActor.CheckTargetReturns(errors.New("nope")) 322 }) 323 324 It("returns an error", func() { 325 Expect(executeErr).To(MatchError("nope")) 326 }) 327 }) 328 }) 329 330 When("setting labels on spaces", func() { 331 BeforeEach(func() { 332 testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer()) 333 fakeActor = new(v7fakes.FakeSetLabelActor) 334 fakeConfig = new(commandfakes.FakeConfig) 335 fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: "fake-org", GUID: "some-org-guid"}) 336 337 fakeSharedActor = new(commandfakes.FakeSharedActor) 338 resourceName = "some-space" 339 cmd = SetLabelCommand{ 340 Actor: fakeActor, 341 UI: testUI, 342 Config: fakeConfig, 343 SharedActor: fakeSharedActor, 344 } 345 cmd.RequiredArgs = flag.SetLabelArgs{ 346 ResourceType: "space", 347 ResourceName: resourceName, 348 } 349 }) 350 351 JustBeforeEach(func() { 352 executeErr = cmd.Execute(nil) 353 }) 354 355 It("checks that the user is logged in and targeted to an org", func() { 356 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 357 checkOrg, checkSpace := fakeSharedActor.CheckTargetArgsForCall(0) 358 Expect(checkOrg).To(BeTrue()) 359 Expect(checkSpace).To(BeFalse()) 360 }) 361 362 When("checking target succeeds", func() { 363 BeforeEach(func() { 364 fakeSharedActor.CheckTargetReturns(nil) 365 }) 366 367 When("fetching current user's name succeeds", func() { 368 BeforeEach(func() { 369 fakeConfig.CurrentUserNameReturns("some-user", nil) 370 }) 371 372 When("all the provided labels are valid", func() { 373 BeforeEach(func() { 374 cmd.RequiredArgs = flag.SetLabelArgs{ 375 ResourceType: "space", 376 ResourceName: resourceName, 377 Labels: []string{"FOO=BAR", "ENV=FAKE"}, 378 } 379 fakeActor.UpdateSpaceLabelsBySpaceNameReturns( 380 v7action.Warnings([]string{"some-warning-1", "some-warning-2"}), 381 nil, 382 ) 383 }) 384 385 When("updating the space labels succeeds", func() { 386 It("sets the provided labels on the space", func() { 387 Expect(fakeActor.UpdateSpaceLabelsBySpaceNameCallCount()).To(Equal(1)) 388 spaceName, orgGUID, labels := fakeActor.UpdateSpaceLabelsBySpaceNameArgsForCall(0) 389 Expect(spaceName).To(Equal(resourceName), "failed to pass space name") 390 Expect(orgGUID).To(Equal("some-org-guid")) 391 Expect(labels).To(BeEquivalentTo(map[string]types.NullString{ 392 "FOO": types.NewNullString("BAR"), 393 "ENV": types.NewNullString("FAKE"), 394 })) 395 }) 396 397 It("displays a message", func() { 398 Expect(executeErr).ToNot(HaveOccurred()) 399 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 400 Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Setting label(s) for space %s in org fake-org as some-user...`), resourceName)) 401 Expect(testUI.Out).To(Say("OK")) 402 }) 403 404 It("prints all warnings", func() { 405 Expect(testUI.Err).To(Say("some-warning-1")) 406 Expect(testUI.Err).To(Say("some-warning-2")) 407 }) 408 }) 409 }) 410 411 When("updating the application labels fail", func() { 412 BeforeEach(func() { 413 cmd.RequiredArgs = flag.SetLabelArgs{ 414 ResourceType: "org", 415 ResourceName: resourceName, 416 Labels: []string{"FOO=BAR", "ENV=FAKE"}, 417 } 418 fakeActor.UpdateOrganizationLabelsByOrganizationNameReturns( 419 v7action.Warnings([]string{"some-warning-1", "some-warning-2"}), 420 errors.New("some-updating-error"), 421 ) 422 }) 423 424 It("displays warnings and an error message", func() { 425 Expect(testUI.Err).To(Say("some-warning-1")) 426 Expect(testUI.Err).To(Say("some-warning-2")) 427 Expect(executeErr).To(MatchError("some-updating-error")) 428 }) 429 }) 430 431 When("some provided labels do not have a value part", func() { 432 BeforeEach(func() { 433 cmd.RequiredArgs = flag.SetLabelArgs{ 434 ResourceType: "org", 435 ResourceName: resourceName, 436 Labels: []string{"FOO=BAR", "MISSING_EQUALS", "ENV=FAKE"}, 437 } 438 }) 439 440 It("complains about the missing equal sign", func() { 441 Expect(executeErr).To(MatchError("Metadata error: no value provided for label 'MISSING_EQUALS'")) 442 Expect(executeErr).To(HaveOccurred()) 443 }) 444 }) 445 }) 446 447 When("fetching the current user's name fails", func() { 448 BeforeEach(func() { 449 cmd.RequiredArgs = flag.SetLabelArgs{ 450 ResourceType: "org", 451 ResourceName: resourceName, 452 } 453 fakeConfig.CurrentUserNameReturns("some-user", errors.New("boom")) 454 }) 455 456 It("returns an error", func() { 457 Expect(executeErr).To(MatchError("boom")) 458 }) 459 }) 460 }) 461 462 When("checking targeted org and space fails", func() { 463 BeforeEach(func() { 464 fakeSharedActor.CheckTargetReturns(errors.New("nope")) 465 }) 466 467 It("returns an error", func() { 468 Expect(executeErr).To(MatchError("nope")) 469 }) 470 }) 471 }) 472 473 When("an unrecognized resource is specified", func() { 474 BeforeEach(func() { 475 testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer()) 476 fakeActor = new(v7fakes.FakeSetLabelActor) 477 fakeConfig = new(commandfakes.FakeConfig) 478 fakeSharedActor = new(commandfakes.FakeSharedActor) 479 resourceName = "some-unrecognized-resource" 480 cmd = SetLabelCommand{ 481 Actor: fakeActor, 482 UI: testUI, 483 Config: fakeConfig, 484 SharedActor: fakeSharedActor, 485 } 486 cmd.RequiredArgs = flag.SetLabelArgs{ 487 ResourceType: "unrecognized-resource", 488 ResourceName: resourceName, 489 } 490 }) 491 492 JustBeforeEach(func() { 493 executeErr = cmd.Execute(nil) 494 }) 495 496 It("errors", func() { 497 Expect(executeErr).To(MatchError("Unsupported resource type of 'unrecognized-resource'")) 498 }) 499 }) 500 501 })