github.com/DaAlbrecht/cf-cli@v0.0.0-20231128151943-1fe19bb400b9/actor/v7action/organization_test.go (about) 1 package v7action_test 2 3 import ( 4 "errors" 5 6 "code.cloudfoundry.org/cli/actor/actionerror" 7 . "code.cloudfoundry.org/cli/actor/v7action" 8 "code.cloudfoundry.org/cli/actor/v7action/v7actionfakes" 9 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3" 10 "code.cloudfoundry.org/cli/resources" 11 12 . "github.com/onsi/ginkgo" 13 . "github.com/onsi/gomega" 14 ) 15 16 var _ = Describe("Organization Actions", func() { 17 var ( 18 actor *Actor 19 fakeCloudControllerClient *v7actionfakes.FakeCloudControllerClient 20 ) 21 22 BeforeEach(func() { 23 fakeCloudControllerClient = new(v7actionfakes.FakeCloudControllerClient) 24 actor = NewActor(fakeCloudControllerClient, nil, nil, nil, nil, nil) 25 }) 26 27 Describe("GetOrganizations", func() { 28 var ( 29 returnOrganizations []resources.Organization 30 organizations []resources.Organization 31 32 organization1Name string 33 organization1GUID string 34 35 organization2Name string 36 organization2GUID string 37 38 organization3Name string 39 organization3GUID string 40 41 warnings Warnings 42 executeErr error 43 ) 44 45 BeforeEach(func() { 46 returnOrganizations = []resources.Organization{ 47 {Name: organization1Name, GUID: organization1GUID}, 48 {Name: organization2Name, GUID: organization2GUID}, 49 {Name: organization3Name, GUID: organization3GUID}, 50 } 51 }) 52 53 When("the API layer call is successful", func() { 54 55 BeforeEach(func() { 56 fakeCloudControllerClient.GetOrganizationsReturns( 57 returnOrganizations, 58 ccv3.Warnings{"some-organizations-warning"}, 59 nil, 60 ) 61 }) 62 63 JustBeforeEach(func() { 64 organizations, warnings, executeErr = actor.GetOrganizations("") 65 }) 66 67 It("does not error", func() { 68 Expect(executeErr).ToNot(HaveOccurred()) 69 }) 70 71 It("does not pass through the label selector", func() { 72 Expect(fakeCloudControllerClient.GetOrganizationsCallCount()).To(Equal(1)) 73 expectedQuery := []ccv3.Query{ 74 {Key: ccv3.OrderBy, Values: []string{ccv3.NameOrder}}, 75 } 76 actualQuery := fakeCloudControllerClient.GetOrganizationsArgsForCall(0) 77 Expect(actualQuery).To(Equal(expectedQuery)) 78 }) 79 80 It("returns back the organizations and warnings", func() { 81 Expect(fakeCloudControllerClient.GetOrganizationsCallCount()).To(Equal(1)) 82 83 Expect(organizations).To(ConsistOf( 84 resources.Organization{Name: organization1Name, GUID: organization1GUID}, 85 resources.Organization{Name: organization2Name, GUID: organization2GUID}, 86 resources.Organization{Name: organization3Name, GUID: organization3GUID}, 87 )) 88 Expect(warnings).To(ConsistOf("some-organizations-warning")) 89 }) 90 }) 91 92 When("a label selector is provided", func() { 93 JustBeforeEach(func() { 94 organizations, warnings, executeErr = actor.GetOrganizations("some-label-selector") 95 }) 96 97 It("passes the label selector through", func() { 98 Expect(executeErr).ToNot(HaveOccurred()) 99 100 Expect(fakeCloudControllerClient.GetOrganizationsCallCount()).To(Equal(1)) 101 102 expectedQuery := []ccv3.Query{ 103 {Key: ccv3.OrderBy, Values: []string{ccv3.NameOrder}}, 104 {Key: ccv3.LabelSelectorFilter, Values: []string{"some-label-selector"}}, 105 } 106 actualQuery := fakeCloudControllerClient.GetOrganizationsArgsForCall(0) 107 Expect(actualQuery).To(Equal(expectedQuery)) 108 }) 109 110 }) 111 112 When("when the API layer call returns an error", func() { 113 BeforeEach(func() { 114 fakeCloudControllerClient.GetOrganizationsReturns( 115 []resources.Organization{}, 116 ccv3.Warnings{"some-organizations-warning"}, 117 errors.New("some-organizations-error"), 118 ) 119 }) 120 121 JustBeforeEach(func() { 122 organizations, warnings, executeErr = actor.GetOrganizations("") 123 }) 124 125 It("returns the error and prints warnings", func() { 126 Expect(executeErr).To(MatchError("some-organizations-error")) 127 Expect(warnings).To(ConsistOf("some-organizations-warning")) 128 Expect(organizations).To(ConsistOf([]resources.Organization{})) 129 130 Expect(fakeCloudControllerClient.GetOrganizationsCallCount()).To(Equal(1)) 131 }) 132 133 }) 134 }) 135 136 Describe("GetOrganizationByGUID", func() { 137 When("the org exists", func() { 138 BeforeEach(func() { 139 fakeCloudControllerClient.GetOrganizationReturns( 140 resources.Organization{ 141 Name: "some-org-name", 142 GUID: "some-org-guid", 143 }, 144 ccv3.Warnings{"some-warning"}, 145 nil, 146 ) 147 }) 148 149 It("returns the organization and warnings", func() { 150 org, warnings, err := actor.GetOrganizationByGUID("some-org-guid") 151 Expect(err).ToNot(HaveOccurred()) 152 Expect(org).To(Equal(resources.Organization{ 153 Name: "some-org-name", 154 GUID: "some-org-guid", 155 })) 156 Expect(warnings).To(ConsistOf("some-warning")) 157 158 Expect(fakeCloudControllerClient.GetOrganizationCallCount()).To(Equal(1)) 159 Expect(fakeCloudControllerClient.GetOrganizationArgsForCall(0)).To(Equal( 160 "some-org-guid", 161 )) 162 }) 163 }) 164 165 When("the cloud controller client returns an error", func() { 166 var expectedError error 167 168 BeforeEach(func() { 169 expectedError = errors.New("I am a CloudControllerClient Error") 170 fakeCloudControllerClient.GetOrganizationReturns( 171 resources.Organization{}, 172 ccv3.Warnings{"some-warning"}, 173 expectedError) 174 }) 175 176 It("returns the warnings and the error", func() { 177 _, warnings, err := actor.GetOrganizationByGUID("some-org-guid") 178 Expect(warnings).To(ConsistOf("some-warning")) 179 Expect(err).To(MatchError(expectedError)) 180 }) 181 }) 182 }) 183 184 Describe("GetOrganizationByName", func() { 185 When("the org exists", func() { 186 BeforeEach(func() { 187 fakeCloudControllerClient.GetOrganizationsReturns( 188 []resources.Organization{ 189 { 190 Name: "some-org-name", 191 GUID: "some-org-guid", 192 }, 193 }, 194 ccv3.Warnings{"some-warning"}, 195 nil, 196 ) 197 }) 198 199 It("returns the organization and warnings", func() { 200 org, warnings, err := actor.GetOrganizationByName("some-org-name") 201 Expect(err).ToNot(HaveOccurred()) 202 Expect(org).To(Equal(resources.Organization{ 203 Name: "some-org-name", 204 GUID: "some-org-guid", 205 })) 206 Expect(warnings).To(ConsistOf("some-warning")) 207 208 Expect(fakeCloudControllerClient.GetOrganizationsCallCount()).To(Equal(1)) 209 Expect(fakeCloudControllerClient.GetOrganizationsArgsForCall(0)).To(ConsistOf( 210 ccv3.Query{Key: ccv3.NameFilter, Values: []string{"some-org-name"}}, 211 )) 212 }) 213 }) 214 215 When("the cloud controller client returns an error", func() { 216 var expectedError error 217 218 BeforeEach(func() { 219 expectedError = errors.New("I am a CloudControllerClient Error") 220 fakeCloudControllerClient.GetOrganizationsReturns( 221 []resources.Organization{}, 222 ccv3.Warnings{"some-warning"}, 223 expectedError) 224 }) 225 226 It("returns the warnings and the error", func() { 227 _, warnings, err := actor.GetOrganizationByName("some-org-name") 228 Expect(warnings).To(ConsistOf("some-warning")) 229 Expect(err).To(MatchError(expectedError)) 230 }) 231 }) 232 }) 233 234 Describe("GetDefaultDomain", func() { 235 var ( 236 domain resources.Domain 237 warnings Warnings 238 executeErr error 239 240 orgGUID = "org-guid" 241 ) 242 243 JustBeforeEach(func() { 244 domain, warnings, executeErr = actor.GetDefaultDomain(orgGUID) 245 }) 246 247 When("the api call is successful", func() { 248 BeforeEach(func() { 249 fakeCloudControllerClient.GetDefaultDomainReturns( 250 resources.Domain{ 251 Name: "some-domain-name", 252 GUID: "some-domain-guid", 253 }, 254 ccv3.Warnings{"some-warning"}, 255 nil, 256 ) 257 }) 258 259 It("returns the domain and warnings", func() { 260 Expect(executeErr).ToNot(HaveOccurred()) 261 Expect(domain).To(Equal(resources.Domain{ 262 Name: "some-domain-name", 263 GUID: "some-domain-guid", 264 })) 265 Expect(warnings).To(ConsistOf("some-warning")) 266 267 Expect(fakeCloudControllerClient.GetDefaultDomainCallCount()).To(Equal(1)) 268 Expect(fakeCloudControllerClient.GetDefaultDomainArgsForCall(0)).To(Equal(orgGUID)) 269 }) 270 }) 271 272 When("the cloud controller client returns an error", func() { 273 var expectedError error 274 275 BeforeEach(func() { 276 expectedError = errors.New("I am a CloudControllerClient Error") 277 fakeCloudControllerClient.GetDefaultDomainReturns( 278 resources.Domain{}, 279 ccv3.Warnings{"some-warning"}, 280 expectedError) 281 }) 282 283 It("returns the warnings and the error", func() { 284 Expect(warnings).To(ConsistOf("some-warning")) 285 Expect(executeErr).To(MatchError(expectedError)) 286 }) 287 }) 288 }) 289 290 When("the org does not exist", func() { 291 BeforeEach(func() { 292 fakeCloudControllerClient.GetOrganizationsReturns( 293 []resources.Organization{}, 294 ccv3.Warnings{"some-warning"}, 295 nil, 296 ) 297 }) 298 299 It("returns an OrganizationNotFoundError and the warnings", func() { 300 _, warnings, err := actor.GetOrganizationByName("some-org-name") 301 Expect(warnings).To(ConsistOf("some-warning")) 302 Expect(err).To(MatchError(actionerror.OrganizationNotFoundError{Name: "some-org-name"})) 303 }) 304 }) 305 306 Describe("CreateOrganization", func() { 307 var ( 308 org resources.Organization 309 warnings Warnings 310 err error 311 ) 312 313 JustBeforeEach(func() { 314 org, warnings, err = actor.CreateOrganization("some-org-name") 315 }) 316 317 When("the org is created successfully", func() { 318 BeforeEach(func() { 319 fakeCloudControllerClient.CreateOrganizationReturns( 320 resources.Organization{Name: "some-org-name", GUID: "some-org-guid"}, 321 ccv3.Warnings{"warning-1", "warning-2"}, 322 nil, 323 ) 324 }) 325 326 It("returns the org resource", func() { 327 Expect(err).NotTo(HaveOccurred()) 328 Expect(warnings).To(ConsistOf("warning-1", "warning-2")) 329 Expect(org).To(Equal(resources.Organization{Name: "some-org-name", GUID: "some-org-guid"})) 330 }) 331 }) 332 333 When("the request fails", func() { 334 BeforeEach(func() { 335 fakeCloudControllerClient.CreateOrganizationReturns( 336 resources.Organization{}, 337 ccv3.Warnings{"warning-1", "warning-2"}, 338 errors.New("create-org-failed"), 339 ) 340 }) 341 342 It("returns warnings and the client error", func() { 343 Expect(warnings).To(ConsistOf("warning-1", "warning-2")) 344 Expect(err).To(MatchError("create-org-failed")) 345 }) 346 }) 347 }) 348 349 Describe("DeleteOrganization", func() { 350 var ( 351 warnings Warnings 352 err error 353 ) 354 355 JustBeforeEach(func() { 356 warnings, err = actor.DeleteOrganization("some-org") 357 }) 358 359 When("the org is not found", func() { 360 BeforeEach(func() { 361 fakeCloudControllerClient.GetOrganizationsReturns( 362 []resources.Organization{}, 363 ccv3.Warnings{ 364 "warning-1", 365 "warning-2", 366 }, 367 nil, 368 ) 369 }) 370 371 It("returns an OrganizationNotFoundError", func() { 372 Expect(err).To(MatchError(actionerror.OrganizationNotFoundError{Name: "some-org"})) 373 Expect(warnings).To(ConsistOf("warning-1", "warning-2")) 374 }) 375 }) 376 377 When("the org is found", func() { 378 BeforeEach(func() { 379 fakeCloudControllerClient.GetOrganizationsReturns( 380 []resources.Organization{{Name: "some-org", GUID: "some-org-guid"}}, 381 ccv3.Warnings{"warning-1", "warning-2"}, 382 nil, 383 ) 384 }) 385 386 When("the delete returns an error", func() { 387 var expectedErr error 388 389 BeforeEach(func() { 390 expectedErr = errors.New("some delete space error") 391 fakeCloudControllerClient.DeleteOrganizationReturns( 392 ccv3.JobURL(""), 393 ccv3.Warnings{"warning-5", "warning-6"}, 394 expectedErr, 395 ) 396 }) 397 398 It("returns the error", func() { 399 Expect(err).To(Equal(expectedErr)) 400 Expect(warnings).To(ConsistOf("warning-1", "warning-2", "warning-5", "warning-6")) 401 }) 402 }) 403 404 When("the delete returns a job", func() { 405 BeforeEach(func() { 406 fakeCloudControllerClient.DeleteOrganizationReturns( 407 ccv3.JobURL("some-url"), 408 ccv3.Warnings{"warning-5", "warning-6"}, 409 nil, 410 ) 411 }) 412 413 When("polling errors", func() { 414 var expectedErr error 415 416 BeforeEach(func() { 417 expectedErr = errors.New("Never expected, by anyone") 418 fakeCloudControllerClient.PollJobReturns(ccv3.Warnings{"warning-7", "warning-8"}, expectedErr) 419 }) 420 421 It("returns the error", func() { 422 Expect(err).To(Equal(expectedErr)) 423 Expect(warnings).To(ConsistOf("warning-1", "warning-2", "warning-5", "warning-6", "warning-7", "warning-8")) 424 }) 425 }) 426 427 When("the job is successful", func() { 428 BeforeEach(func() { 429 fakeCloudControllerClient.PollJobReturns(ccv3.Warnings{"warning-7", "warning-8"}, nil) 430 }) 431 432 It("returns warnings and no error", func() { 433 Expect(err).ToNot(HaveOccurred()) 434 Expect(warnings).To(ConsistOf("warning-1", "warning-2", "warning-5", "warning-6", "warning-7", "warning-8")) 435 436 Expect(fakeCloudControllerClient.GetOrganizationsCallCount()).To(Equal(1)) 437 Expect(fakeCloudControllerClient.GetOrganizationsArgsForCall(0)).To(Equal([]ccv3.Query{{ 438 Key: ccv3.NameFilter, 439 Values: []string{"some-org"}, 440 }})) 441 442 Expect(fakeCloudControllerClient.DeleteOrganizationCallCount()).To(Equal(1)) 443 Expect(fakeCloudControllerClient.DeleteOrganizationArgsForCall(0)).To(Equal("some-org-guid")) 444 445 Expect(fakeCloudControllerClient.PollJobCallCount()).To(Equal(1)) 446 Expect(fakeCloudControllerClient.PollJobArgsForCall(0)).To(Equal(ccv3.JobURL("some-url"))) 447 }) 448 }) 449 }) 450 }) 451 }) 452 453 Describe("RenameOrganization", func() { 454 var ( 455 oldOrgName = "old-and-stale-org-name" 456 newOrgName = "fresh-and-new-org-name" 457 458 org resources.Organization 459 warnings Warnings 460 executeErr error 461 ) 462 463 JustBeforeEach(func() { 464 org, warnings, executeErr = actor.RenameOrganization( 465 oldOrgName, 466 newOrgName, 467 ) 468 }) 469 470 It("delegate to the actor to get the org", func() { 471 // assert on the underlying client call because we dont have a fake actor 472 Expect(fakeCloudControllerClient.GetOrganizationsCallCount()).To(Equal(1)) 473 }) 474 475 When("getting the org fails", func() { 476 BeforeEach(func() { 477 fakeCloudControllerClient.GetOrganizationsReturns( 478 nil, 479 ccv3.Warnings{"get-org-warning"}, 480 errors.New("get-org-error"), 481 ) 482 }) 483 484 It("returns the error and warnings", func() { 485 Expect(executeErr).To(MatchError("get-org-error")) 486 Expect(warnings).To(ConsistOf("get-org-warning")) 487 }) 488 }) 489 490 When("getting the org succeeds", func() { 491 BeforeEach(func() { 492 fakeCloudControllerClient.GetOrganizationsReturns( 493 []resources.Organization{{Name: oldOrgName, GUID: "org-guid"}}, 494 ccv3.Warnings{"get-org-warning"}, 495 nil, 496 ) 497 }) 498 499 It("delegates to the client to update the org", func() { 500 Expect(fakeCloudControllerClient.GetOrganizationsCallCount()).To(Equal(1)) 501 Expect(fakeCloudControllerClient.UpdateOrganizationCallCount()).To(Equal(1)) 502 Expect(fakeCloudControllerClient.UpdateOrganizationArgsForCall(0)).To(Equal(resources.Organization{ 503 GUID: "org-guid", 504 Name: newOrgName, 505 })) 506 }) 507 508 When("updating the org fails", func() { 509 BeforeEach(func() { 510 fakeCloudControllerClient.UpdateOrganizationReturns( 511 resources.Organization{}, 512 ccv3.Warnings{"update-org-warning"}, 513 errors.New("update-org-error"), 514 ) 515 }) 516 517 It("returns an error and all warnings", func() { 518 Expect(executeErr).To(MatchError("update-org-error")) 519 Expect(warnings).To(ConsistOf("get-org-warning", "update-org-warning")) 520 }) 521 522 }) 523 524 When("updating the org succeeds", func() { 525 BeforeEach(func() { 526 fakeCloudControllerClient.UpdateOrganizationReturns( 527 resources.Organization{Name: newOrgName, GUID: "org-guid"}, 528 ccv3.Warnings{"update-org-warning"}, 529 nil, 530 ) 531 }) 532 533 It("returns warnings and no error", func() { 534 Expect(executeErr).ToNot(HaveOccurred()) 535 Expect(warnings).To(ConsistOf("get-org-warning", "update-org-warning")) 536 Expect(org).To(Equal(resources.Organization{Name: newOrgName, GUID: "org-guid"})) 537 }) 538 }) 539 }) 540 }) 541 })