github.com/arunkumar7540/cli@v6.45.0+incompatible/command/v7/labels_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 . "github.com/onsi/ginkgo" 16 . "github.com/onsi/gomega" 17 . "github.com/onsi/gomega/gbytes" 18 ) 19 20 var _ = Describe("labels command", func() { 21 var ( 22 cmd LabelsCommand 23 fakeLabelsActor *v7fakes.FakeLabelsActor 24 fakeConfig *commandfakes.FakeConfig 25 fakeSharedActor *commandfakes.FakeSharedActor 26 testUI *ui.UI 27 28 executeErr error 29 ) 30 31 BeforeEach(func() { 32 testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer()) 33 fakeLabelsActor = new(v7fakes.FakeLabelsActor) 34 fakeConfig = new(commandfakes.FakeConfig) 35 fakeSharedActor = new(commandfakes.FakeSharedActor) 36 cmd = LabelsCommand{ 37 Actor: fakeLabelsActor, 38 UI: testUI, 39 Config: fakeConfig, 40 SharedActor: fakeSharedActor, 41 } 42 }) 43 44 JustBeforeEach(func() { 45 executeErr = cmd.Execute(nil) 46 }) 47 48 Describe("listing labels", func() { 49 50 Describe("for apps", func() { 51 BeforeEach(func() { 52 fakeConfig.CurrentUserNameReturns("some-user", nil) 53 fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: "fake-org"}) 54 fakeConfig.TargetedSpaceReturns(configv3.Space{Name: "fake-space", GUID: "some-space-guid"}) 55 cmd.RequiredArgs = flag.LabelsArgs{ 56 ResourceType: "app", 57 ResourceName: "dora", 58 } 59 fakeLabelsActor.GetApplicationLabelsReturns( 60 map[string]types.NullString{ 61 "some-other-label": types.NewNullString("some-other-value"), 62 "some-label": types.NewNullString("some-value"), 63 }, 64 v7action.Warnings{}, 65 nil) 66 }) 67 68 It("doesn't error", func() { 69 Expect(executeErr).ToNot(HaveOccurred()) 70 }) 71 72 It("checks that the user is logged in and targeted to an org and space", func() { 73 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 74 checkOrg, checkSpace := fakeSharedActor.CheckTargetArgsForCall(0) 75 Expect(checkOrg).To(BeTrue()) 76 Expect(checkSpace).To(BeTrue()) 77 }) 78 79 It("displays a message that it is retrieving the labels", func() { 80 Expect(executeErr).ToNot(HaveOccurred()) 81 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 82 Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Getting labels for app dora in org fake-org / space fake-space as some-user...`))) 83 }) 84 85 It("retrieves the labels associated with the application", func() { 86 Expect(fakeLabelsActor.GetApplicationLabelsCallCount()).To(Equal(1)) 87 appName, spaceGUID := fakeLabelsActor.GetApplicationLabelsArgsForCall(0) 88 Expect(appName).To(Equal("dora")) 89 Expect(spaceGUID).To(Equal("some-space-guid")) 90 }) 91 92 It("displays the labels that are associated with the application, alphabetically", func() { 93 Expect(testUI.Out).To(Say(`key\s+value`)) 94 Expect(testUI.Out).To(Say(`some-label\s+some-value`)) 95 Expect(testUI.Out).To(Say(`some-other-label\s+some-other-value`)) 96 }) 97 98 When("CAPI returns warnings", func() { 99 BeforeEach(func() { 100 fakeLabelsActor.GetApplicationLabelsReturns( 101 map[string]types.NullString{ 102 "some-other-label": types.NewNullString("some-other-value"), 103 "some-label": types.NewNullString("some-value"), 104 }, 105 v7action.Warnings([]string{"some-warning-1", "some-warning-2"}), 106 nil) 107 }) 108 109 It("prints all warnings", func() { 110 Expect(testUI.Err).To(Say("some-warning-1")) 111 Expect(testUI.Err).To(Say("some-warning-2")) 112 }) 113 }) 114 115 When("there is an error retrieving the application", func() { 116 BeforeEach(func() { 117 fakeLabelsActor.GetApplicationLabelsReturns( 118 map[string]types.NullString{}, 119 v7action.Warnings([]string{"some-warning-1", "some-warning-2"}), 120 errors.New("boom")) 121 }) 122 123 It("returns the error", func() { 124 Expect(executeErr).To(MatchError("boom")) 125 }) 126 127 It("still prints all warnings", func() { 128 Expect(testUI.Err).To(Say("some-warning-1")) 129 Expect(testUI.Err).To(Say("some-warning-2")) 130 }) 131 132 It("doesn't say ok", func() { 133 Expect(testUI.Out).ToNot(Say("OK")) 134 }) 135 }) 136 137 When("checking targeted org and space fails", func() { 138 BeforeEach(func() { 139 fakeSharedActor.CheckTargetReturns(errors.New("nope")) 140 }) 141 142 It("returns an error", func() { 143 Expect(executeErr).To(MatchError("nope")) 144 }) 145 }) 146 147 When("fetching the current user's name fails", func() { 148 BeforeEach(func() { 149 fakeConfig.CurrentUserNameReturns("some-user", errors.New("boom")) 150 }) 151 152 It("returns an error", func() { 153 Expect(executeErr).To(MatchError("boom")) 154 }) 155 }) 156 }) 157 158 Describe("for orgs", func() { 159 BeforeEach(func() { 160 fakeConfig.CurrentUserNameReturns("some-user", nil) 161 cmd.RequiredArgs = flag.LabelsArgs{ 162 ResourceType: "org", 163 ResourceName: "fake-org", 164 } 165 fakeLabelsActor.GetOrganizationLabelsReturns( 166 map[string]types.NullString{ 167 "some-other-label": types.NewNullString("some-other-value"), 168 "some-label": types.NewNullString("some-value"), 169 }, 170 v7action.Warnings{}, 171 nil) 172 }) 173 174 It("doesn't error", func() { 175 Expect(executeErr).ToNot(HaveOccurred()) 176 }) 177 178 It("checks that the user is logged in", func() { 179 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 180 checkOrg, checkSpace := fakeSharedActor.CheckTargetArgsForCall(0) 181 Expect(checkOrg).To(BeFalse()) 182 Expect(checkSpace).To(BeFalse()) 183 }) 184 185 It("displays a message that it is retrieving the labels", func() { 186 Expect(executeErr).ToNot(HaveOccurred()) 187 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 188 Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Getting labels for org fake-org as some-user...`))) 189 }) 190 191 It("retrieves the labels associated with the organization", func() { 192 Expect(fakeLabelsActor.GetOrganizationLabelsCallCount()).To(Equal(1)) 193 }) 194 195 It("displays the labels that are associated with the organization, alphabetically", func() { 196 Expect(testUI.Out).To(Say(`key\s+value`)) 197 Expect(testUI.Out).To(Say(`some-label\s+some-value`)) 198 Expect(testUI.Out).To(Say(`some-other-label\s+some-other-value`)) 199 }) 200 201 When("CAPI returns warnings", func() { 202 BeforeEach(func() { 203 fakeLabelsActor.GetOrganizationLabelsReturns( 204 map[string]types.NullString{ 205 "some-other-label": types.NewNullString("some-other-value"), 206 "some-label": types.NewNullString("some-value"), 207 }, 208 v7action.Warnings([]string{"some-warning-1", "some-warning-2"}), 209 nil) 210 }) 211 212 It("prints all warnings", func() { 213 Expect(testUI.Err).To(Say("some-warning-1")) 214 Expect(testUI.Err).To(Say("some-warning-2")) 215 }) 216 }) 217 218 When("there is an error retrieving the organization", func() { 219 BeforeEach(func() { 220 fakeLabelsActor.GetOrganizationLabelsReturns( 221 map[string]types.NullString{}, 222 v7action.Warnings([]string{"some-warning-1", "some-warning-2"}), 223 errors.New("boom")) 224 }) 225 226 It("returns the error", func() { 227 Expect(executeErr).To(MatchError("boom")) 228 }) 229 230 It("still prints all warnings", func() { 231 Expect(testUI.Err).To(Say("some-warning-1")) 232 Expect(testUI.Err).To(Say("some-warning-2")) 233 }) 234 235 It("doesn't say ok", func() { 236 Expect(testUI.Out).ToNot(Say("OK")) 237 }) 238 }) 239 240 When("checking targeted org and space fails", func() { 241 BeforeEach(func() { 242 fakeSharedActor.CheckTargetReturns(errors.New("nope")) 243 }) 244 245 It("returns an error", func() { 246 Expect(executeErr).To(MatchError("nope")) 247 }) 248 }) 249 250 When("fetching the current user's name fails", func() { 251 BeforeEach(func() { 252 fakeConfig.CurrentUserNameReturns("some-user", errors.New("boom")) 253 }) 254 255 It("returns an error", func() { 256 Expect(executeErr).To(MatchError("boom")) 257 }) 258 }) 259 }) 260 261 Describe("for spaces", func() { 262 BeforeEach(func() { 263 fakeConfig.CurrentUserNameReturns("some-user", nil) 264 fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: "fake-org"}) 265 cmd.RequiredArgs = flag.LabelsArgs{ 266 ResourceType: "space", 267 ResourceName: "fake-space", 268 } 269 fakeLabelsActor.GetSpaceLabelsReturns( 270 map[string]types.NullString{ 271 "some-other-label": types.NewNullString("some-other-value"), 272 "some-label": types.NewNullString("some-value"), 273 }, 274 v7action.Warnings{}, 275 nil) 276 }) 277 278 It("doesn't error", func() { 279 Expect(executeErr).ToNot(HaveOccurred()) 280 }) 281 282 It("checks that the user is logged in and targetted to an org", func() { 283 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 284 checkOrg, checkSpace := fakeSharedActor.CheckTargetArgsForCall(0) 285 Expect(checkOrg).To(BeTrue()) 286 Expect(checkSpace).To(BeFalse()) 287 }) 288 289 It("displays a message that it is retrieving the labels", func() { 290 Expect(executeErr).ToNot(HaveOccurred()) 291 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 292 Expect(testUI.Out).To(Say(regexp.QuoteMeta(`Getting labels for space fake-space in org fake-org as some-user...`))) 293 }) 294 295 It("retrieves the labels associated with the space", func() { 296 Expect(fakeLabelsActor.GetSpaceLabelsCallCount()).To(Equal(1)) 297 }) 298 299 It("displays the labels that are associated with the space, alphabetically", func() { 300 Expect(testUI.Out).To(Say(`key\s+value`)) 301 Expect(testUI.Out).To(Say(`some-label\s+some-value`)) 302 Expect(testUI.Out).To(Say(`some-other-label\s+some-other-value`)) 303 }) 304 305 When("CAPI returns warnings", func() { 306 BeforeEach(func() { 307 fakeLabelsActor.GetSpaceLabelsReturns( 308 map[string]types.NullString{ 309 "some-other-label": types.NewNullString("some-other-value"), 310 "some-label": types.NewNullString("some-value"), 311 }, 312 v7action.Warnings([]string{"some-warning-1", "some-warning-2"}), 313 nil) 314 }) 315 316 It("prints all warnings", func() { 317 Expect(testUI.Err).To(Say("some-warning-1")) 318 Expect(testUI.Err).To(Say("some-warning-2")) 319 }) 320 }) 321 322 When("there is an error retrieving the space", func() { 323 BeforeEach(func() { 324 fakeLabelsActor.GetSpaceLabelsReturns( 325 map[string]types.NullString{}, 326 v7action.Warnings([]string{"some-warning-1", "some-warning-2"}), 327 errors.New("boom")) 328 }) 329 330 It("returns the error", func() { 331 Expect(executeErr).To(MatchError("boom")) 332 }) 333 334 It("still prints all warnings", func() { 335 Expect(testUI.Err).To(Say("some-warning-1")) 336 Expect(testUI.Err).To(Say("some-warning-2")) 337 }) 338 339 It("doesn't say ok", func() { 340 Expect(testUI.Out).ToNot(Say("OK")) 341 }) 342 }) 343 344 When("checking targeted org and space fails", func() { 345 BeforeEach(func() { 346 fakeSharedActor.CheckTargetReturns(errors.New("nope")) 347 }) 348 349 It("returns an error", func() { 350 Expect(executeErr).To(MatchError("nope")) 351 }) 352 }) 353 354 When("fetching the current user's name fails", func() { 355 BeforeEach(func() { 356 fakeConfig.CurrentUserNameReturns("some-user", errors.New("boom")) 357 }) 358 359 It("returns an error", func() { 360 Expect(executeErr).To(MatchError("boom")) 361 }) 362 }) 363 }) 364 }) 365 })