github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+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  		fakeV2Actor = new(pushactionfakes.FakeV2Actor)
    27  		fakeSharedActor = new(pushactionfakes.FakeSharedActor)
    28  		actor = NewActor(fakeV2Actor, fakeSharedActor)
    29  	})
    30  
    31  	Describe("CreateArchive", func() {
    32  		var (
    33  			config ApplicationConfig
    34  
    35  			archivePath string
    36  			executeErr  error
    37  
    38  			resourcesToArchive []v2action.Resource
    39  		)
    40  
    41  		BeforeEach(func() {
    42  			config = ApplicationConfig{
    43  				Path: "some-path",
    44  				DesiredApplication: Application{
    45  					Application: v2action.Application{
    46  						GUID: "some-app-guid",
    47  					}},
    48  			}
    49  
    50  			resourcesToArchive = []v2action.Resource{{Filename: "file1"}, {Filename: "file2"}}
    51  			config.UnmatchedResources = resourcesToArchive
    52  		})
    53  
    54  		JustBeforeEach(func() {
    55  			archivePath, executeErr = actor.CreateArchive(config)
    56  		})
    57  
    58  		Context("when the source is an archive", func() {
    59  			BeforeEach(func() {
    60  				config.Archive = true
    61  			})
    62  
    63  			Context("when the zipping is successful", func() {
    64  				var fakeArchivePath string
    65  
    66  				BeforeEach(func() {
    67  					fakeArchivePath = "some-archive-path"
    68  					fakeSharedActor.ZipArchiveResourcesReturns(fakeArchivePath, nil)
    69  				})
    70  
    71  				It("returns the path to the zip", func() {
    72  					Expect(executeErr).ToNot(HaveOccurred())
    73  					Expect(archivePath).To(Equal(fakeArchivePath))
    74  
    75  					Expect(fakeSharedActor.ZipArchiveResourcesCallCount()).To(Equal(1))
    76  					sourceDir, passedResources := fakeSharedActor.ZipArchiveResourcesArgsForCall(0)
    77  					Expect(sourceDir).To(Equal("some-path"))
    78  					sharedResourcesToArchive := actor.ConvertV2ResourcesToSharedResources(resourcesToArchive)
    79  					Expect(passedResources).To(Equal(sharedResourcesToArchive))
    80  
    81  				})
    82  			})
    83  
    84  			Context("when creating the archive errors", func() {
    85  				var expectedErr error
    86  
    87  				BeforeEach(func() {
    88  					expectedErr = errors.New("oh no")
    89  					fakeSharedActor.ZipArchiveResourcesReturns("", expectedErr)
    90  				})
    91  
    92  				It("sends errors and returns true", func() {
    93  					Expect(executeErr).To(MatchError(expectedErr))
    94  				})
    95  			})
    96  		})
    97  
    98  		Context("when the source is a directory", func() {
    99  			Context("when the zipping is successful", func() {
   100  				var fakeArchivePath string
   101  				BeforeEach(func() {
   102  					fakeArchivePath = "some-archive-path"
   103  					fakeSharedActor.ZipDirectoryResourcesReturns(fakeArchivePath, nil)
   104  				})
   105  
   106  				It("returns the path to the zip", func() {
   107  					Expect(executeErr).ToNot(HaveOccurred())
   108  					Expect(archivePath).To(Equal(fakeArchivePath))
   109  
   110  					Expect(fakeSharedActor.ZipDirectoryResourcesCallCount()).To(Equal(1))
   111  					sourceDir, passedResources := fakeSharedActor.ZipDirectoryResourcesArgsForCall(0)
   112  					Expect(sourceDir).To(Equal("some-path"))
   113  					sharedResourcesToArchive := actor.ConvertV2ResourcesToSharedResources(resourcesToArchive)
   114  					Expect(passedResources).To(Equal(sharedResourcesToArchive))
   115  				})
   116  			})
   117  
   118  			Context("when creating the archive errors", func() {
   119  				var expectedErr error
   120  
   121  				BeforeEach(func() {
   122  					expectedErr = errors.New("oh no")
   123  					fakeSharedActor.ZipDirectoryResourcesReturns("", expectedErr)
   124  				})
   125  
   126  				It("sends errors and returns true", func() {
   127  					Expect(executeErr).To(MatchError(expectedErr))
   128  				})
   129  			})
   130  		})
   131  	})
   132  
   133  	Describe("SetMatchedResources", func() {
   134  		var (
   135  			inputConfig  ApplicationConfig
   136  			outputConfig ApplicationConfig
   137  			warnings     Warnings
   138  		)
   139  		JustBeforeEach(func() {
   140  			outputConfig, warnings = actor.SetMatchedResources(inputConfig)
   141  		})
   142  
   143  		BeforeEach(func() {
   144  			inputConfig.AllResources = []v2action.Resource{
   145  				{Filename: "file-1"},
   146  				{Filename: "file-2"},
   147  			}
   148  		})
   149  
   150  		Context("when the resource matching is successful", func() {
   151  			BeforeEach(func() {
   152  				fakeV2Actor.ResourceMatchReturns(
   153  					[]v2action.Resource{{Filename: "file-1"}},
   154  					[]v2action.Resource{{Filename: "file-2"}},
   155  					v2action.Warnings{"warning-1"},
   156  					nil,
   157  				)
   158  			})
   159  
   160  			It("sets the matched and unmatched resources", func() {
   161  				Expect(outputConfig.MatchedResources).To(ConsistOf(v2action.Resource{Filename: "file-1"}))
   162  				Expect(outputConfig.UnmatchedResources).To(ConsistOf(v2action.Resource{Filename: "file-2"}))
   163  
   164  				Expect(warnings).To(ConsistOf("warning-1"))
   165  			})
   166  		})
   167  
   168  		Context("when resource matching returns an error", func() {
   169  			BeforeEach(func() {
   170  				fakeV2Actor.ResourceMatchReturns(nil, nil, v2action.Warnings{"warning-1"}, errors.New("some-error"))
   171  			})
   172  
   173  			It("sets the unmatched resources to AllResources", func() {
   174  				Expect(outputConfig.UnmatchedResources).To(Equal(inputConfig.AllResources))
   175  
   176  				Expect(warnings).To(ConsistOf("warning-1"))
   177  			})
   178  		})
   179  	})
   180  
   181  	Describe("UploadPackage", func() {
   182  		var (
   183  			config          ApplicationConfig
   184  			archivePath     string
   185  			fakeProgressBar *pushactionfakes.FakeProgressBar
   186  			eventStream     chan Event
   187  
   188  			warnings   Warnings
   189  			executeErr error
   190  
   191  			resources []v2action.Resource
   192  		)
   193  
   194  		BeforeEach(func() {
   195  			resources = []v2action.Resource{
   196  				{Filename: "file-1"},
   197  				{Filename: "file-2"},
   198  			}
   199  
   200  			config = ApplicationConfig{
   201  				DesiredApplication: Application{
   202  					Application: v2action.Application{
   203  						GUID: "some-app-guid",
   204  					}},
   205  				MatchedResources: resources,
   206  			}
   207  			fakeProgressBar = new(pushactionfakes.FakeProgressBar)
   208  			eventStream = make(chan Event)
   209  		})
   210  
   211  		AfterEach(func() {
   212  			close(eventStream)
   213  		})
   214  
   215  		JustBeforeEach(func() {
   216  			warnings, executeErr = actor.UploadPackage(config, archivePath, fakeProgressBar, eventStream)
   217  		})
   218  
   219  		Context("when the archive can be accessed properly", func() {
   220  			BeforeEach(func() {
   221  				tmpfile, err := ioutil.TempFile("", "fake-archive")
   222  				Expect(err).ToNot(HaveOccurred())
   223  				_, err = tmpfile.Write([]byte("123456"))
   224  				Expect(err).ToNot(HaveOccurred())
   225  				Expect(tmpfile.Close()).ToNot(HaveOccurred())
   226  
   227  				archivePath = tmpfile.Name()
   228  			})
   229  
   230  			AfterEach(func() {
   231  				Expect(os.Remove(archivePath)).ToNot(HaveOccurred())
   232  			})
   233  
   234  			Context("when the upload is successful", func() {
   235  				var (
   236  					progressBarReader io.Reader
   237  					uploadJob         v2action.Job
   238  				)
   239  
   240  				BeforeEach(func() {
   241  					uploadJob.GUID = "some-job-guid"
   242  					fakeV2Actor.UploadApplicationPackageReturns(uploadJob, v2action.Warnings{"upload-warning-1", "upload-warning-2"}, nil)
   243  
   244  					progressBarReader = strings.NewReader("123456")
   245  					fakeProgressBar.NewProgressBarWrapperReturns(progressBarReader)
   246  
   247  					go func() {
   248  						defer GinkgoRecover()
   249  
   250  						Eventually(eventStream).Should(Receive(Equal(UploadingApplication)))
   251  						Eventually(eventStream).Should(Receive(Equal(UploadComplete)))
   252  					}()
   253  				})
   254  
   255  				Context("when the polling is successful", func() {
   256  					BeforeEach(func() {
   257  						fakeV2Actor.PollJobReturns(v2action.Warnings{"poll-warning-1", "poll-warning-2"}, nil)
   258  					})
   259  
   260  					It("returns the warnings", func() {
   261  						Expect(executeErr).ToNot(HaveOccurred())
   262  						Expect(warnings).To(ConsistOf("upload-warning-1", "upload-warning-2", "poll-warning-1", "poll-warning-2"))
   263  
   264  						Expect(fakeV2Actor.UploadApplicationPackageCallCount()).To(Equal(1))
   265  						appGUID, existingResources, _, newResourcesLength := fakeV2Actor.UploadApplicationPackageArgsForCall(0)
   266  						Expect(appGUID).To(Equal("some-app-guid"))
   267  						Expect(existingResources).To(Equal(resources))
   268  						Expect(newResourcesLength).To(BeNumerically("==", 6))
   269  
   270  						Expect(fakeV2Actor.PollJobCallCount()).To(Equal(1))
   271  						Expect(fakeV2Actor.PollJobArgsForCall(0)).To(Equal(uploadJob))
   272  					})
   273  
   274  					It("passes the file reader to the progress bar", func() {
   275  						Expect(executeErr).ToNot(HaveOccurred())
   276  
   277  						Expect(fakeProgressBar.NewProgressBarWrapperCallCount()).To(Equal(1))
   278  						_, size := fakeProgressBar.NewProgressBarWrapperArgsForCall(0)
   279  						Expect(size).To(BeNumerically("==", 6))
   280  					})
   281  				})
   282  
   283  				Context("when the polling fails", func() {
   284  					var expectedErr error
   285  
   286  					BeforeEach(func() {
   287  						expectedErr = errors.New("I can't let you do that starfox")
   288  						fakeV2Actor.PollJobReturns(v2action.Warnings{"poll-warning-1", "poll-warning-2"}, expectedErr)
   289  					})
   290  
   291  					It("returns the warnings", func() {
   292  						Expect(executeErr).To(MatchError(expectedErr))
   293  						Expect(warnings).To(ConsistOf("upload-warning-1", "upload-warning-2", "poll-warning-1", "poll-warning-2"))
   294  					})
   295  				})
   296  			})
   297  
   298  			Context("when the upload errors", func() {
   299  				var (
   300  					expectedErr error
   301  					done        chan bool
   302  				)
   303  
   304  				BeforeEach(func() {
   305  					expectedErr = errors.New("I can't let you do that starfox")
   306  					fakeV2Actor.UploadApplicationPackageReturns(v2action.Job{}, v2action.Warnings{"upload-warning-1", "upload-warning-2"}, expectedErr)
   307  
   308  					done = make(chan bool)
   309  
   310  					go func() {
   311  						defer GinkgoRecover()
   312  
   313  						Eventually(eventStream).Should(Receive(Equal(UploadingApplication)))
   314  						Consistently(eventStream).ShouldNot(Receive())
   315  						done <- true
   316  					}()
   317  				})
   318  
   319  				AfterEach(func() {
   320  					close(done)
   321  				})
   322  
   323  				It("returns the error and warnings", func() {
   324  					Eventually(done).Should(Receive())
   325  					Expect(executeErr).To(MatchError(expectedErr))
   326  					Expect(warnings).To(ConsistOf("upload-warning-1", "upload-warning-2"))
   327  				})
   328  			})
   329  		})
   330  
   331  		Context("when the archive returns any access errors", func() {
   332  			It("returns the error", func() {
   333  				_, ok := executeErr.(*os.PathError)
   334  				Expect(ok).To(BeTrue())
   335  			})
   336  		})
   337  	})
   338  })