github.com/jghiloni/cli@v6.28.1-0.20170628223758-0ce05fe032a2+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 ) 23 24 BeforeEach(func() { 25 fakeV2Actor = new(pushactionfakes.FakeV2Actor) 26 actor = NewActor(fakeV2Actor) 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: v2action.Application{ 43 GUID: "some-app-guid", 44 }, 45 } 46 47 resourcesToArchive = []v2action.Resource{{Filename: "file1"}, {Filename: "file2"}} 48 config.AllResources = resourcesToArchive 49 }) 50 51 JustBeforeEach(func() { 52 archivePath, executeErr = actor.CreateArchive(config) 53 }) 54 55 Context("when the source is an archive", func() { 56 BeforeEach(func() { 57 config.Archive = true 58 }) 59 60 Context("when the zipping is successful", func() { 61 var fakeArchivePath string 62 63 BeforeEach(func() { 64 fakeArchivePath = "some-archive-path" 65 fakeV2Actor.ZipArchiveResourcesReturns(fakeArchivePath, nil) 66 }) 67 68 It("returns the path to the zip", func() { 69 Expect(executeErr).ToNot(HaveOccurred()) 70 Expect(archivePath).To(Equal(fakeArchivePath)) 71 72 Expect(fakeV2Actor.ZipArchiveResourcesCallCount()).To(Equal(1)) 73 sourceDir, passedResources := fakeV2Actor.ZipArchiveResourcesArgsForCall(0) 74 Expect(sourceDir).To(Equal("some-path")) 75 Expect(passedResources).To(Equal(resourcesToArchive)) 76 }) 77 }) 78 79 Context("when creating the archive errors", func() { 80 var expectedErr error 81 82 BeforeEach(func() { 83 expectedErr = errors.New("oh no") 84 fakeV2Actor.ZipArchiveResourcesReturns("", expectedErr) 85 }) 86 87 It("sends errors and returns true", func() { 88 Expect(executeErr).To(MatchError(expectedErr)) 89 }) 90 }) 91 }) 92 93 Context("when the source is a directory", func() { 94 Context("when the zipping is successful", func() { 95 var fakeArchivePath string 96 BeforeEach(func() { 97 fakeArchivePath = "some-archive-path" 98 fakeV2Actor.ZipDirectoryResourcesReturns(fakeArchivePath, nil) 99 }) 100 101 It("returns the path to the zip", func() { 102 Expect(executeErr).ToNot(HaveOccurred()) 103 Expect(archivePath).To(Equal(fakeArchivePath)) 104 105 Expect(fakeV2Actor.ZipDirectoryResourcesCallCount()).To(Equal(1)) 106 sourceDir, passedResources := fakeV2Actor.ZipDirectoryResourcesArgsForCall(0) 107 Expect(sourceDir).To(Equal("some-path")) 108 Expect(passedResources).To(Equal(resourcesToArchive)) 109 }) 110 }) 111 112 Context("when creating the archive errors", func() { 113 var expectedErr error 114 115 BeforeEach(func() { 116 expectedErr = errors.New("oh no") 117 fakeV2Actor.ZipDirectoryResourcesReturns("", expectedErr) 118 }) 119 120 It("sends errors and returns true", func() { 121 Expect(executeErr).To(MatchError(expectedErr)) 122 }) 123 }) 124 }) 125 }) 126 127 Describe("UploadPackage", func() { 128 var ( 129 config ApplicationConfig 130 archivePath string 131 fakeProgressBar *pushactionfakes.FakeProgressBar 132 eventStream chan Event 133 134 warnings Warnings 135 executeErr error 136 ) 137 138 BeforeEach(func() { 139 config = ApplicationConfig{ 140 DesiredApplication: v2action.Application{ 141 GUID: "some-app-guid", 142 }, 143 } 144 fakeProgressBar = new(pushactionfakes.FakeProgressBar) 145 eventStream = make(chan Event) 146 }) 147 148 AfterEach(func() { 149 close(eventStream) 150 }) 151 152 JustBeforeEach(func() { 153 warnings, executeErr = actor.UploadPackage(config, archivePath, fakeProgressBar, eventStream) 154 }) 155 156 Context("when the archive can be accessed properly", func() { 157 BeforeEach(func() { 158 tmpfile, err := ioutil.TempFile("", "fake-archive") 159 Expect(err).ToNot(HaveOccurred()) 160 _, err = tmpfile.Write([]byte("123456")) 161 Expect(err).ToNot(HaveOccurred()) 162 Expect(tmpfile.Close()).ToNot(HaveOccurred()) 163 164 archivePath = tmpfile.Name() 165 }) 166 167 AfterEach(func() { 168 Expect(os.Remove(archivePath)).ToNot(HaveOccurred()) 169 }) 170 171 Context("when the upload is successful", func() { 172 var ( 173 progressBarReader io.Reader 174 uploadJob v2action.Job 175 ) 176 177 BeforeEach(func() { 178 uploadJob.GUID = "some-job-guid" 179 fakeV2Actor.UploadApplicationPackageReturns(uploadJob, v2action.Warnings{"upload-warning-1", "upload-warning-2"}, nil) 180 181 progressBarReader = strings.NewReader("123456") 182 fakeProgressBar.NewProgressBarWrapperReturns(progressBarReader) 183 184 go func() { 185 defer GinkgoRecover() 186 187 Eventually(eventStream).Should(Receive(Equal(UploadingApplication))) 188 Eventually(eventStream).Should(Receive(Equal(UploadComplete))) 189 }() 190 }) 191 192 Context("when the polling is successful", func() { 193 BeforeEach(func() { 194 fakeV2Actor.PollJobReturns(v2action.Warnings{"poll-warning-1", "poll-warning-2"}, nil) 195 }) 196 197 It("returns the warnings", func() { 198 Expect(executeErr).ToNot(HaveOccurred()) 199 Expect(warnings).To(ConsistOf("upload-warning-1", "upload-warning-2", "poll-warning-1", "poll-warning-2")) 200 201 Expect(fakeV2Actor.UploadApplicationPackageCallCount()).To(Equal(1)) 202 appGUID, existingResources, _, newResourcesLength := fakeV2Actor.UploadApplicationPackageArgsForCall(0) 203 Expect(appGUID).To(Equal("some-app-guid")) 204 Expect(existingResources).To(BeEmpty()) 205 Expect(newResourcesLength).To(BeNumerically("==", 6)) 206 207 Expect(fakeV2Actor.PollJobCallCount()).To(Equal(1)) 208 Expect(fakeV2Actor.PollJobArgsForCall(0)).To(Equal(uploadJob)) 209 }) 210 211 It("passes the file reader to the progress bar", func() { 212 Expect(executeErr).ToNot(HaveOccurred()) 213 214 Expect(fakeProgressBar.NewProgressBarWrapperCallCount()).To(Equal(1)) 215 _, size := fakeProgressBar.NewProgressBarWrapperArgsForCall(0) 216 Expect(size).To(BeNumerically("==", 6)) 217 }) 218 }) 219 220 Context("when the polling fails", func() { 221 var expectedErr error 222 223 BeforeEach(func() { 224 expectedErr = errors.New("I can't let you do that starfox") 225 fakeV2Actor.PollJobReturns(v2action.Warnings{"poll-warning-1", "poll-warning-2"}, expectedErr) 226 }) 227 228 It("returns the warnings", func() { 229 Expect(executeErr).To(MatchError(expectedErr)) 230 Expect(warnings).To(ConsistOf("upload-warning-1", "upload-warning-2", "poll-warning-1", "poll-warning-2")) 231 }) 232 }) 233 }) 234 235 Context("when the upload errors", func() { 236 var ( 237 expectedErr error 238 done chan bool 239 ) 240 241 BeforeEach(func() { 242 expectedErr = errors.New("I can't let you do that starfox") 243 fakeV2Actor.UploadApplicationPackageReturns(v2action.Job{}, v2action.Warnings{"upload-warning-1", "upload-warning-2"}, expectedErr) 244 245 done = make(chan bool) 246 247 go func() { 248 defer GinkgoRecover() 249 250 Eventually(eventStream).Should(Receive(Equal(UploadingApplication))) 251 Consistently(eventStream).ShouldNot(Receive()) 252 done <- true 253 }() 254 }) 255 256 AfterEach(func() { 257 close(done) 258 }) 259 260 It("returns the error and warnings", func() { 261 Eventually(done).Should(Receive()) 262 Expect(executeErr).To(MatchError(expectedErr)) 263 Expect(warnings).To(ConsistOf("upload-warning-1", "upload-warning-2")) 264 }) 265 }) 266 }) 267 268 Context("when the archive returns any access errors", func() { 269 It("returns the error", func() { 270 _, ok := executeErr.(*os.PathError) 271 Expect(ok).To(BeTrue()) 272 }) 273 }) 274 }) 275 })