github.com/Thanhphan1147/cloudfoundry-cli@v7.1.0+incompatible/actor/v7pushaction/setup_all_resources_for_push_plan_test.go (about)

     1  package v7pushaction_test
     2  
     3  import (
     4  	"errors"
     5  	"io/ioutil"
     6  	"os"
     7  
     8  	"code.cloudfoundry.org/cli/actor/sharedaction"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
    10  
    11  	. "code.cloudfoundry.org/cli/actor/v7pushaction"
    12  	"code.cloudfoundry.org/cli/actor/v7pushaction/v7pushactionfakes"
    13  
    14  	. "github.com/onsi/ginkgo"
    15  	. "github.com/onsi/gomega"
    16  )
    17  
    18  var _ = Describe("SetupAllResourcesForPushPlan", func() {
    19  	var (
    20  		actor           *Actor
    21  		fakeSharedActor *v7pushactionfakes.FakeSharedActor
    22  
    23  		pushPlan  PushPlan
    24  		overrides FlagOverrides
    25  
    26  		expectedPushPlan PushPlan
    27  		executeErr       error
    28  	)
    29  
    30  	BeforeEach(func() {
    31  		actor, _, fakeSharedActor = getTestPushActor()
    32  
    33  		pushPlan = PushPlan{}
    34  		overrides = FlagOverrides{}
    35  	})
    36  
    37  	JustBeforeEach(func() {
    38  		expectedPushPlan, executeErr = actor.SetupAllResourcesForPushPlan(pushPlan, overrides)
    39  	})
    40  
    41  	When("the plan has a droplet path", func() {
    42  		BeforeEach(func() {
    43  			pushPlan.DropletPath = "some-droplet.tgz"
    44  		})
    45  
    46  		It("skips settings the resources", func() {
    47  			Expect(executeErr).ToNot(HaveOccurred())
    48  			Expect(pushPlan.AllResources).To(BeEmpty())
    49  
    50  			Expect(fakeSharedActor.GatherArchiveResourcesCallCount()).To(Equal(0))
    51  			Expect(fakeSharedActor.GatherDirectoryResourcesCallCount()).To(Equal(0))
    52  		})
    53  	})
    54  
    55  	When("the application is a docker app", func() {
    56  		BeforeEach(func() {
    57  			pushPlan.Application.LifecycleType = constant.AppLifecycleTypeDocker
    58  		})
    59  
    60  		It("skips settings the resources", func() {
    61  			Expect(executeErr).ToNot(HaveOccurred())
    62  			Expect(pushPlan.AllResources).To(BeEmpty())
    63  
    64  			Expect(fakeSharedActor.GatherArchiveResourcesCallCount()).To(Equal(0))
    65  			Expect(fakeSharedActor.GatherDirectoryResourcesCallCount()).To(Equal(0))
    66  		})
    67  	})
    68  
    69  	When("the application is a buildpack app", func() {
    70  		When("push plan's bits path is not set", func() {
    71  			It("returns an error", func() {
    72  				Expect(executeErr).To(MatchError("developer error: Bits Path needs to be set prior to generating app resources"))
    73  
    74  				Expect(fakeSharedActor.GatherArchiveResourcesCallCount()).To(Equal(0))
    75  				Expect(fakeSharedActor.GatherDirectoryResourcesCallCount()).To(Equal(0))
    76  			})
    77  		})
    78  
    79  		When("the app resources are given as a directory", func() {
    80  			var pwd string
    81  
    82  			BeforeEach(func() {
    83  				var err error
    84  				pwd, err = os.Getwd()
    85  				Expect(err).To(Not(HaveOccurred()))
    86  				pushPlan.BitsPath = pwd
    87  			})
    88  
    89  			When("gathering the resources is successful", func() {
    90  				var resources []sharedaction.Resource
    91  
    92  				BeforeEach(func() {
    93  					resources = []sharedaction.Resource{
    94  						{
    95  							Filename: "fake-app-file",
    96  						},
    97  					}
    98  					fakeSharedActor.GatherDirectoryResourcesReturns(resources, nil)
    99  				})
   100  
   101  				It("adds the gathered resources to the push plan", func() {
   102  					Expect(executeErr).ToNot(HaveOccurred())
   103  					Expect(fakeSharedActor.GatherDirectoryResourcesCallCount()).To(Equal(1))
   104  					Expect(fakeSharedActor.GatherDirectoryResourcesArgsForCall(0)).To(Equal(pwd))
   105  					Expect(expectedPushPlan.AllResources[0]).To(Equal(resources[0].ToV3Resource()))
   106  				})
   107  
   108  				It("sets Archive to false", func() {
   109  					Expect(executeErr).ToNot(HaveOccurred())
   110  					Expect(expectedPushPlan.Archive).To(BeFalse())
   111  				})
   112  			})
   113  
   114  			When("gathering the resources errors", func() {
   115  				BeforeEach(func() {
   116  					fakeSharedActor.GatherDirectoryResourcesReturns(nil, errors.New("kaboom"))
   117  				})
   118  
   119  				It("returns the error", func() {
   120  					Expect(executeErr).To(MatchError("kaboom"))
   121  				})
   122  			})
   123  		})
   124  
   125  		When("the app resources are given as an archive", func() {
   126  			var archivePath string
   127  
   128  			BeforeEach(func() {
   129  				archive, err := ioutil.TempFile("", "push-plan-archive")
   130  				Expect(err).ToNot(HaveOccurred())
   131  				defer archive.Close()
   132  
   133  				archivePath = archive.Name()
   134  				pushPlan.BitsPath = archivePath
   135  			})
   136  
   137  			AfterEach(func() {
   138  				Expect(os.RemoveAll(archivePath)).ToNot(HaveOccurred())
   139  			})
   140  
   141  			When("gathering the resources is successful", func() {
   142  				var resources []sharedaction.Resource
   143  
   144  				BeforeEach(func() {
   145  					resources = []sharedaction.Resource{
   146  						{
   147  							Filename: "fake-app-file",
   148  						},
   149  					}
   150  					fakeSharedActor.GatherArchiveResourcesReturns(resources, nil)
   151  				})
   152  
   153  				It("adds the gathered resources to the push plan", func() {
   154  					Expect(executeErr).ToNot(HaveOccurred())
   155  					Expect(fakeSharedActor.GatherArchiveResourcesCallCount()).To(Equal(1))
   156  					Expect(fakeSharedActor.GatherArchiveResourcesArgsForCall(0)).To(Equal(archivePath))
   157  					Expect(expectedPushPlan.AllResources[0]).To(Equal(resources[0].ToV3Resource()))
   158  				})
   159  
   160  				It("sets Archive to true", func() {
   161  					Expect(executeErr).ToNot(HaveOccurred())
   162  					Expect(expectedPushPlan.Archive).To(BeTrue())
   163  				})
   164  			})
   165  
   166  			When("gathering the resources errors", func() {
   167  				BeforeEach(func() {
   168  					fakeSharedActor.GatherArchiveResourcesReturns(nil, errors.New("kaboom"))
   169  				})
   170  
   171  				It("returns the error", func() {
   172  					Expect(executeErr).To(MatchError("kaboom"))
   173  				})
   174  			})
   175  		})
   176  	})
   177  })