github.com/randomtask1155/cli@v6.41.1-0.20181227003417-a98eed78cbde+incompatible/actor/pushaction/resource_test.go (about) 1 package pushaction_test 2 3 import ( 4 "errors" 5 "io" 6 "io/ioutil" 7 "os" 8 "strings" 9 10 . "code.cloudfoundry.org/cli/actor/pushaction" 11 "code.cloudfoundry.org/cli/actor/pushaction/pushactionfakes" 12 "code.cloudfoundry.org/cli/actor/v2action" 13 14 . "github.com/onsi/ginkgo" 15 . "github.com/onsi/gomega" 16 ) 17 18 var _ = Describe("Resources", func() { 19 var ( 20 actor *Actor 21 fakeV2Actor *pushactionfakes.FakeV2Actor 22 fakeSharedActor *pushactionfakes.FakeSharedActor 23 ) 24 25 BeforeEach(func() { 26 actor, fakeV2Actor, _, fakeSharedActor = getTestPushActor() 27 }) 28 29 Describe("CreateArchive", func() { 30 var ( 31 config ApplicationConfig 32 33 archivePath string 34 executeErr error 35 36 resourcesToArchive []v2action.Resource 37 ) 38 39 BeforeEach(func() { 40 config = ApplicationConfig{ 41 Path: "some-path", 42 DesiredApplication: Application{ 43 Application: v2action.Application{ 44 GUID: "some-app-guid", 45 }}, 46 } 47 48 resourcesToArchive = []v2action.Resource{{Filename: "file1"}, {Filename: "file2"}} 49 config.UnmatchedResources = resourcesToArchive 50 }) 51 52 JustBeforeEach(func() { 53 archivePath, executeErr = actor.CreateArchive(config) 54 }) 55 56 When("the source is an archive", func() { 57 BeforeEach(func() { 58 config.Archive = true 59 }) 60 61 When("the zipping is successful", func() { 62 var fakeArchivePath string 63 64 BeforeEach(func() { 65 fakeArchivePath = "some-archive-path" 66 fakeSharedActor.ZipArchiveResourcesReturns(fakeArchivePath, nil) 67 }) 68 69 It("returns the path to the zip", func() { 70 Expect(executeErr).ToNot(HaveOccurred()) 71 Expect(archivePath).To(Equal(fakeArchivePath)) 72 73 Expect(fakeSharedActor.ZipArchiveResourcesCallCount()).To(Equal(1)) 74 sourceDir, passedResources := fakeSharedActor.ZipArchiveResourcesArgsForCall(0) 75 Expect(sourceDir).To(Equal("some-path")) 76 sharedResourcesToArchive := actor.ConvertV2ResourcesToSharedResources(resourcesToArchive) 77 Expect(passedResources).To(Equal(sharedResourcesToArchive)) 78 79 }) 80 }) 81 82 When("creating the archive errors", func() { 83 var expectedErr error 84 85 BeforeEach(func() { 86 expectedErr = errors.New("oh no") 87 fakeSharedActor.ZipArchiveResourcesReturns("", expectedErr) 88 }) 89 90 It("sends errors and returns true", func() { 91 Expect(executeErr).To(MatchError(expectedErr)) 92 }) 93 }) 94 }) 95 96 When("the source is a directory", func() { 97 When("the zipping is successful", func() { 98 var fakeArchivePath string 99 BeforeEach(func() { 100 fakeArchivePath = "some-archive-path" 101 fakeSharedActor.ZipDirectoryResourcesReturns(fakeArchivePath, nil) 102 }) 103 104 It("returns the path to the zip", func() { 105 Expect(executeErr).ToNot(HaveOccurred()) 106 Expect(archivePath).To(Equal(fakeArchivePath)) 107 108 Expect(fakeSharedActor.ZipDirectoryResourcesCallCount()).To(Equal(1)) 109 sourceDir, passedResources := fakeSharedActor.ZipDirectoryResourcesArgsForCall(0) 110 Expect(sourceDir).To(Equal("some-path")) 111 sharedResourcesToArchive := actor.ConvertV2ResourcesToSharedResources(resourcesToArchive) 112 Expect(passedResources).To(Equal(sharedResourcesToArchive)) 113 }) 114 }) 115 116 When("creating the archive errors", func() { 117 var expectedErr error 118 119 BeforeEach(func() { 120 expectedErr = errors.New("oh no") 121 fakeSharedActor.ZipDirectoryResourcesReturns("", expectedErr) 122 }) 123 124 It("sends errors and returns true", func() { 125 Expect(executeErr).To(MatchError(expectedErr)) 126 }) 127 }) 128 }) 129 }) 130 131 Describe("SetMatchedResources", func() { 132 var ( 133 inputConfig ApplicationConfig 134 outputConfig ApplicationConfig 135 warnings Warnings 136 ) 137 JustBeforeEach(func() { 138 outputConfig, warnings = actor.SetMatchedResources(inputConfig) 139 }) 140 141 BeforeEach(func() { 142 inputConfig.AllResources = []v2action.Resource{ 143 {Filename: "file-1"}, 144 {Filename: "file-2"}, 145 } 146 }) 147 148 When("the resource matching is successful", func() { 149 BeforeEach(func() { 150 fakeV2Actor.ResourceMatchReturns( 151 []v2action.Resource{{Filename: "file-1"}}, 152 []v2action.Resource{{Filename: "file-2"}}, 153 v2action.Warnings{"warning-1"}, 154 nil, 155 ) 156 }) 157 158 It("sets the matched and unmatched resources", func() { 159 Expect(outputConfig.MatchedResources).To(ConsistOf(v2action.Resource{Filename: "file-1"})) 160 Expect(outputConfig.UnmatchedResources).To(ConsistOf(v2action.Resource{Filename: "file-2"})) 161 162 Expect(warnings).To(ConsistOf("warning-1")) 163 }) 164 }) 165 166 When("resource matching returns an error", func() { 167 BeforeEach(func() { 168 fakeV2Actor.ResourceMatchReturns(nil, nil, v2action.Warnings{"warning-1"}, errors.New("some-error")) 169 }) 170 171 It("sets the unmatched resources to AllResources", func() { 172 Expect(outputConfig.UnmatchedResources).To(Equal(inputConfig.AllResources)) 173 174 Expect(warnings).To(ConsistOf("warning-1")) 175 }) 176 }) 177 }) 178 179 Describe("UploadPackage", func() { 180 var ( 181 config ApplicationConfig 182 183 warnings Warnings 184 executeErr error 185 186 resources []v2action.Resource 187 ) 188 189 BeforeEach(func() { 190 resources = []v2action.Resource{ 191 {Filename: "file-1"}, 192 {Filename: "file-2"}, 193 } 194 195 config = ApplicationConfig{ 196 DesiredApplication: Application{ 197 Application: v2action.Application{ 198 GUID: "some-app-guid", 199 }}, 200 MatchedResources: resources, 201 } 202 }) 203 204 JustBeforeEach(func() { 205 warnings, executeErr = actor.UploadPackage(config) 206 }) 207 208 When("the upload is successful", func() { 209 var uploadJob v2action.Job 210 211 BeforeEach(func() { 212 uploadJob.GUID = "some-job-guid" 213 fakeV2Actor.UploadApplicationPackageReturns(uploadJob, v2action.Warnings{"upload-warning-1", "upload-warning-2"}, nil) 214 }) 215 216 When("polling is successful", func() { 217 BeforeEach(func() { 218 fakeV2Actor.PollJobReturns(v2action.Warnings{"poll-warning-1", "poll-warning-2"}, nil) 219 }) 220 221 It("uploads the existing resources", func() { 222 Expect(executeErr).ToNot(HaveOccurred()) 223 Expect(warnings).To(ConsistOf("upload-warning-1", "upload-warning-2", "poll-warning-1", "poll-warning-2")) 224 225 Expect(fakeV2Actor.UploadApplicationPackageCallCount()).To(Equal(1)) 226 appGUID, existingResources, reader, newResourcesLength := fakeV2Actor.UploadApplicationPackageArgsForCall(0) 227 Expect(appGUID).To(Equal("some-app-guid")) 228 Expect(existingResources).To(Equal(resources)) 229 Expect(reader).To(BeNil()) 230 Expect(newResourcesLength).To(BeNumerically("==", 0)) 231 232 Expect(fakeV2Actor.PollJobCallCount()).To(Equal(1)) 233 Expect(fakeV2Actor.PollJobArgsForCall(0)).To(Equal(uploadJob)) 234 }) 235 }) 236 237 When("the polling fails", func() { 238 var expectedErr error 239 240 BeforeEach(func() { 241 expectedErr = errors.New("I can't let you do that starfox") 242 fakeV2Actor.PollJobReturns(v2action.Warnings{"poll-warning-1", "poll-warning-2"}, expectedErr) 243 }) 244 245 It("returns the warnings", func() { 246 Expect(executeErr).To(MatchError(expectedErr)) 247 Expect(warnings).To(ConsistOf("upload-warning-1", "upload-warning-2", "poll-warning-1", "poll-warning-2")) 248 }) 249 }) 250 }) 251 252 When("the upload errors", func() { 253 var expectedErr error 254 255 BeforeEach(func() { 256 expectedErr = errors.New("I can't let you do that starfox") 257 fakeV2Actor.UploadApplicationPackageReturns(v2action.Job{}, v2action.Warnings{"upload-warning-1", "upload-warning-2"}, expectedErr) 258 }) 259 260 It("returns the error and warnings", func() { 261 Expect(executeErr).To(MatchError(expectedErr)) 262 Expect(warnings).To(ConsistOf("upload-warning-1", "upload-warning-2")) 263 }) 264 }) 265 }) 266 267 Describe("UploadPackageWithArchive", func() { 268 var ( 269 config ApplicationConfig 270 archivePath string 271 fakeProgressBar *pushactionfakes.FakeProgressBar 272 eventStream chan Event 273 274 warnings Warnings 275 executeErr error 276 277 resources []v2action.Resource 278 ) 279 280 BeforeEach(func() { 281 resources = []v2action.Resource{ 282 {Filename: "file-1"}, 283 {Filename: "file-2"}, 284 } 285 286 config = ApplicationConfig{ 287 DesiredApplication: Application{ 288 Application: v2action.Application{ 289 GUID: "some-app-guid", 290 }}, 291 MatchedResources: resources, 292 } 293 fakeProgressBar = new(pushactionfakes.FakeProgressBar) 294 eventStream = make(chan Event) 295 }) 296 297 AfterEach(func() { 298 close(eventStream) 299 }) 300 301 JustBeforeEach(func() { 302 warnings, executeErr = actor.UploadPackageWithArchive(config, archivePath, fakeProgressBar, eventStream) 303 }) 304 305 When("the archive can be accessed properly", func() { 306 BeforeEach(func() { 307 tmpfile, err := ioutil.TempFile("", "fake-archive") 308 Expect(err).ToNot(HaveOccurred()) 309 _, err = tmpfile.Write([]byte("123456")) 310 Expect(err).ToNot(HaveOccurred()) 311 Expect(tmpfile.Close()).ToNot(HaveOccurred()) 312 313 archivePath = tmpfile.Name() 314 }) 315 316 AfterEach(func() { 317 Expect(os.Remove(archivePath)).ToNot(HaveOccurred()) 318 }) 319 320 When("the upload is successful", func() { 321 var ( 322 progressBarReader io.Reader 323 uploadJob v2action.Job 324 ) 325 326 BeforeEach(func() { 327 uploadJob.GUID = "some-job-guid" 328 fakeV2Actor.UploadApplicationPackageReturns(uploadJob, v2action.Warnings{"upload-warning-1", "upload-warning-2"}, nil) 329 330 progressBarReader = strings.NewReader("123456") 331 fakeProgressBar.NewProgressBarWrapperReturns(progressBarReader) 332 333 go func() { 334 defer GinkgoRecover() 335 336 Eventually(eventStream).Should(Receive(Equal(UploadingApplicationWithArchive))) 337 Eventually(eventStream).Should(Receive(Equal(UploadWithArchiveComplete))) 338 }() 339 }) 340 341 When("the polling is successful", func() { 342 BeforeEach(func() { 343 fakeV2Actor.PollJobReturns(v2action.Warnings{"poll-warning-1", "poll-warning-2"}, nil) 344 }) 345 346 It("returns the warnings", func() { 347 Expect(executeErr).ToNot(HaveOccurred()) 348 Expect(warnings).To(ConsistOf("upload-warning-1", "upload-warning-2", "poll-warning-1", "poll-warning-2")) 349 350 Expect(fakeV2Actor.UploadApplicationPackageCallCount()).To(Equal(1)) 351 appGUID, existingResources, _, newResourcesLength := fakeV2Actor.UploadApplicationPackageArgsForCall(0) 352 Expect(appGUID).To(Equal("some-app-guid")) 353 Expect(existingResources).To(Equal(resources)) 354 Expect(newResourcesLength).To(BeNumerically("==", 6)) 355 356 Expect(fakeV2Actor.PollJobCallCount()).To(Equal(1)) 357 Expect(fakeV2Actor.PollJobArgsForCall(0)).To(Equal(uploadJob)) 358 }) 359 360 It("passes the file reader to the progress bar", func() { 361 Expect(executeErr).ToNot(HaveOccurred()) 362 363 Expect(fakeProgressBar.NewProgressBarWrapperCallCount()).To(Equal(1)) 364 _, size := fakeProgressBar.NewProgressBarWrapperArgsForCall(0) 365 Expect(size).To(BeNumerically("==", 6)) 366 }) 367 }) 368 369 When("the polling fails", func() { 370 var expectedErr error 371 372 BeforeEach(func() { 373 expectedErr = errors.New("I can't let you do that starfox") 374 fakeV2Actor.PollJobReturns(v2action.Warnings{"poll-warning-1", "poll-warning-2"}, expectedErr) 375 }) 376 377 It("returns the warnings", func() { 378 Expect(executeErr).To(MatchError(expectedErr)) 379 Expect(warnings).To(ConsistOf("upload-warning-1", "upload-warning-2", "poll-warning-1", "poll-warning-2")) 380 }) 381 }) 382 }) 383 384 When("the upload errors", func() { 385 var ( 386 expectedErr error 387 done chan bool 388 ) 389 390 BeforeEach(func() { 391 expectedErr = errors.New("I can't let you do that starfox") 392 fakeV2Actor.UploadApplicationPackageReturns(v2action.Job{}, v2action.Warnings{"upload-warning-1", "upload-warning-2"}, expectedErr) 393 394 done = make(chan bool) 395 396 go func() { 397 defer GinkgoRecover() 398 399 Eventually(eventStream).Should(Receive(Equal(UploadingApplicationWithArchive))) 400 Consistently(eventStream).ShouldNot(Receive()) 401 done <- true 402 }() 403 }) 404 405 AfterEach(func() { 406 close(done) 407 }) 408 409 It("returns the error and warnings", func() { 410 Eventually(done).Should(Receive()) 411 Expect(executeErr).To(MatchError(expectedErr)) 412 Expect(warnings).To(ConsistOf("upload-warning-1", "upload-warning-2")) 413 }) 414 }) 415 }) 416 417 When("the archive returns any access errors", func() { 418 It("returns the error", func() { 419 _, ok := executeErr.(*os.PathError) 420 Expect(ok).To(BeTrue()) 421 }) 422 }) 423 }) 424 425 Describe("UploadDroplet", func() { 426 var ( 427 config ApplicationConfig 428 dropletPath string 429 fakeProgressBar *pushactionfakes.FakeProgressBar 430 eventStream chan Event 431 432 warnings Warnings 433 executeErr error 434 ) 435 436 BeforeEach(func() { 437 config = ApplicationConfig{ 438 DesiredApplication: Application{ 439 Application: v2action.Application{ 440 GUID: "some-app-guid", 441 }}, 442 } 443 fakeProgressBar = new(pushactionfakes.FakeProgressBar) 444 eventStream = make(chan Event) 445 }) 446 447 AfterEach(func() { 448 close(eventStream) 449 }) 450 451 JustBeforeEach(func() { 452 warnings, executeErr = actor.UploadDroplet(config, dropletPath, fakeProgressBar, eventStream) 453 }) 454 455 When("the droplet can be accessed properly", func() { 456 BeforeEach(func() { 457 tmpfile, err := ioutil.TempFile("", "fake-droplet") 458 Expect(err).ToNot(HaveOccurred()) 459 _, err = tmpfile.Write([]byte("123456")) 460 Expect(err).ToNot(HaveOccurred()) 461 Expect(tmpfile.Close()).ToNot(HaveOccurred()) 462 463 dropletPath = tmpfile.Name() 464 }) 465 466 AfterEach(func() { 467 Expect(os.RemoveAll(dropletPath)).ToNot(HaveOccurred()) 468 }) 469 470 When("the upload is successful", func() { 471 var ( 472 progressBarReader io.Reader 473 uploadJob v2action.Job 474 ) 475 476 BeforeEach(func() { 477 uploadJob.GUID = "some-job-guid" 478 fakeV2Actor.UploadDropletReturns(uploadJob, v2action.Warnings{"upload-warning-1", "upload-warning-2"}, nil) 479 480 progressBarReader = strings.NewReader("123456") 481 fakeProgressBar.NewProgressBarWrapperReturns(progressBarReader) 482 483 go func() { 484 defer GinkgoRecover() 485 486 Eventually(eventStream).Should(Receive(Equal(UploadDropletComplete))) 487 }() 488 }) 489 490 When("the polling is successful", func() { 491 BeforeEach(func() { 492 fakeV2Actor.PollJobReturns(v2action.Warnings{"poll-warning-1", "poll-warning-2"}, nil) 493 }) 494 495 It("returns the warnings", func() { 496 Expect(executeErr).ToNot(HaveOccurred()) 497 Expect(warnings).To(ConsistOf("upload-warning-1", "upload-warning-2", "poll-warning-1", "poll-warning-2")) 498 499 Expect(fakeV2Actor.UploadDropletCallCount()).To(Equal(1)) 500 appGUID, _, dropletLength := fakeV2Actor.UploadDropletArgsForCall(0) 501 Expect(appGUID).To(Equal("some-app-guid")) 502 Expect(dropletLength).To(BeNumerically("==", 6)) 503 504 Expect(fakeV2Actor.PollJobCallCount()).To(Equal(1)) 505 Expect(fakeV2Actor.PollJobArgsForCall(0)).To(Equal(uploadJob)) 506 }) 507 508 It("passes the file reader to the progress bar", func() { 509 Expect(executeErr).ToNot(HaveOccurred()) 510 511 Expect(fakeProgressBar.NewProgressBarWrapperCallCount()).To(Equal(1)) 512 _, size := fakeProgressBar.NewProgressBarWrapperArgsForCall(0) 513 Expect(size).To(BeNumerically("==", 6)) 514 }) 515 }) 516 517 When("the polling fails", func() { 518 var expectedErr error 519 520 BeforeEach(func() { 521 expectedErr = errors.New("I can't let you do that starfox") 522 fakeV2Actor.PollJobReturns(v2action.Warnings{"poll-warning-1", "poll-warning-2"}, expectedErr) 523 }) 524 525 It("returns the warnings", func() { 526 Expect(executeErr).To(MatchError(expectedErr)) 527 Expect(warnings).To(ConsistOf("upload-warning-1", "upload-warning-2", "poll-warning-1", "poll-warning-2")) 528 }) 529 }) 530 }) 531 532 When("the upload errors", func() { 533 var ( 534 expectedErr error 535 done chan bool 536 ) 537 538 BeforeEach(func() { 539 expectedErr = errors.New("I can't let you do that starfox") 540 fakeV2Actor.UploadDropletReturns(v2action.Job{}, v2action.Warnings{"upload-warning-1", "upload-warning-2"}, expectedErr) 541 542 done = make(chan bool) 543 544 go func() { 545 defer GinkgoRecover() 546 547 Consistently(eventStream).ShouldNot(Receive(Equal(UploadDropletComplete))) 548 done <- true 549 }() 550 }) 551 552 AfterEach(func() { 553 close(done) 554 }) 555 556 It("returns the error and warnings", func() { 557 Eventually(done).Should(Receive()) 558 Expect(executeErr).To(MatchError(expectedErr)) 559 Expect(warnings).To(ConsistOf("upload-warning-1", "upload-warning-2")) 560 }) 561 }) 562 }) 563 }) 564 })