github.com/swisscom/cloudfoundry-cli@v7.1.0+incompatible/actor/v2action/job_test.go (about)

     1  package v2action_test
     2  
     3  import (
     4  	"errors"
     5  	"io"
     6  	"strings"
     7  
     8  	. "code.cloudfoundry.org/cli/actor/v2action"
     9  	"code.cloudfoundry.org/cli/actor/v2action/v2actionfakes"
    10  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2"
    11  	. "github.com/onsi/ginkgo"
    12  	. "github.com/onsi/gomega"
    13  )
    14  
    15  var _ = Describe("Job Actions", func() {
    16  	var (
    17  		actor                     *Actor
    18  		fakeCloudControllerClient *v2actionfakes.FakeCloudControllerClient
    19  	)
    20  
    21  	BeforeEach(func() {
    22  		fakeCloudControllerClient = new(v2actionfakes.FakeCloudControllerClient)
    23  		actor = NewActor(fakeCloudControllerClient, nil, nil)
    24  	})
    25  
    26  	Describe("UploadApplicationPackage", func() {
    27  		var (
    28  			appGUID           string
    29  			existingResources []Resource
    30  			reader            io.Reader
    31  			readerLength      int64
    32  
    33  			job        Job
    34  			warnings   Warnings
    35  			executeErr error
    36  		)
    37  
    38  		BeforeEach(func() {
    39  			appGUID = "some-app-guid"
    40  			existingResources = []Resource{{Filename: "some-resource"}, {Filename: "another-resource"}}
    41  			someString := "who reads these days"
    42  			reader = strings.NewReader(someString)
    43  			readerLength = int64(len([]byte(someString)))
    44  		})
    45  
    46  		JustBeforeEach(func() {
    47  			job, warnings, executeErr = actor.UploadApplicationPackage(appGUID, existingResources, reader, readerLength)
    48  		})
    49  
    50  		When("the upload is successful", func() {
    51  			BeforeEach(func() {
    52  				fakeCloudControllerClient.UploadApplicationPackageReturns(ccv2.Job{GUID: "some-job-guid"}, ccv2.Warnings{"upload-warning-1", "upload-warning-2"}, nil)
    53  			})
    54  
    55  			It("returns all warnings", func() {
    56  				Expect(executeErr).ToNot(HaveOccurred())
    57  				Expect(warnings).To(ConsistOf("upload-warning-1", "upload-warning-2"))
    58  				Expect(job).To(Equal(Job{GUID: "some-job-guid"}))
    59  
    60  				Expect(fakeCloudControllerClient.UploadApplicationPackageCallCount()).To(Equal(1))
    61  				passedAppGUID, passedExistingResources, passedReader, passedReaderLength := fakeCloudControllerClient.UploadApplicationPackageArgsForCall(0)
    62  				Expect(passedAppGUID).To(Equal(appGUID))
    63  				Expect(passedExistingResources).To(ConsistOf(ccv2.Resource{Filename: "some-resource"}, ccv2.Resource{Filename: "another-resource"}))
    64  				Expect(passedReader).To(Equal(reader))
    65  				Expect(passedReaderLength).To(Equal(readerLength))
    66  			})
    67  		})
    68  
    69  		When("the upload returns an error", func() {
    70  			var err error
    71  
    72  			BeforeEach(func() {
    73  				err = errors.New("some-error")
    74  				fakeCloudControllerClient.UploadApplicationPackageReturns(ccv2.Job{}, ccv2.Warnings{"upload-warning-1", "upload-warning-2"}, err)
    75  			})
    76  
    77  			It("returns the error", func() {
    78  				Expect(executeErr).To(MatchError(err))
    79  				Expect(warnings).To(ConsistOf("upload-warning-1", "upload-warning-2"))
    80  			})
    81  		})
    82  	})
    83  
    84  	Describe("UploadDroplet", func() {
    85  		var (
    86  			appGUID       string
    87  			droplet       io.Reader
    88  			dropletLength int64
    89  
    90  			job        Job
    91  			warnings   Warnings
    92  			executeErr error
    93  		)
    94  
    95  		BeforeEach(func() {
    96  			appGUID = "some-app-guid"
    97  			someString := "who reads these days"
    98  
    99  			droplet = strings.NewReader(someString)
   100  			dropletLength = int64(len([]byte(someString)))
   101  		})
   102  
   103  		JustBeforeEach(func() {
   104  			job, warnings, executeErr = actor.UploadDroplet(appGUID, droplet, dropletLength)
   105  		})
   106  
   107  		When("the upload is successful", func() {
   108  			BeforeEach(func() {
   109  				fakeCloudControllerClient.UploadDropletReturns(ccv2.Job{GUID: "some-job-guid"}, ccv2.Warnings{"upload-droplet-warning-1", "upload-droplet-warning-2"}, nil)
   110  			})
   111  
   112  			It("returns all warnings", func() {
   113  				Expect(executeErr).ToNot(HaveOccurred())
   114  				Expect(warnings).To(ConsistOf("upload-droplet-warning-1", "upload-droplet-warning-2"))
   115  				Expect(job).To(Equal(Job{GUID: "some-job-guid"}))
   116  
   117  				Expect(fakeCloudControllerClient.UploadDropletCallCount()).To(Equal(1))
   118  				passedAppGUID, passedDroplet, passedDropletLength := fakeCloudControllerClient.UploadDropletArgsForCall(0)
   119  				Expect(passedAppGUID).To(Equal(appGUID))
   120  				Expect(passedDroplet).To(Equal(droplet))
   121  				Expect(passedDropletLength).To(Equal(dropletLength))
   122  			})
   123  		})
   124  
   125  	})
   126  
   127  	Describe("PollJob", func() {
   128  		var (
   129  			job        Job
   130  			warnings   Warnings
   131  			executeErr error
   132  		)
   133  
   134  		JustBeforeEach(func() {
   135  			warnings, executeErr = actor.PollJob(job)
   136  		})
   137  
   138  		When("the job polling is successful", func() {
   139  			BeforeEach(func() {
   140  				fakeCloudControllerClient.PollJobReturns(ccv2.Warnings{"polling-warning"}, nil)
   141  			})
   142  
   143  			It("returns the warnings", func() {
   144  				Expect(executeErr).ToNot(HaveOccurred())
   145  				Expect(warnings).To(ConsistOf("polling-warning"))
   146  
   147  				Expect(fakeCloudControllerClient.PollJobCallCount()).To(Equal(1))
   148  				Expect(fakeCloudControllerClient.PollJobArgsForCall(0)).To(Equal(ccv2.Job(job)))
   149  			})
   150  		})
   151  
   152  		When("polling errors", func() {
   153  			var err error
   154  
   155  			BeforeEach(func() {
   156  				err = errors.New("some-error")
   157  				fakeCloudControllerClient.PollJobReturns(ccv2.Warnings{"polling-warning"}, err)
   158  			})
   159  
   160  			It("returns the error", func() {
   161  				Expect(executeErr).To(MatchError(err))
   162  				Expect(warnings).To(ConsistOf("polling-warning"))
   163  			})
   164  		})
   165  	})
   166  })