github.com/jenspinney/cli@v6.42.1-0.20190207184520-7450c600020e+incompatible/actor/v7pushaction/push_state_test.go (about)

     1  package v7pushaction_test
     2  
     3  import (
     4  	"errors"
     5  	"io/ioutil"
     6  	"os"
     7  
     8  	"code.cloudfoundry.org/cli/actor/actionerror"
     9  	"code.cloudfoundry.org/cli/actor/sharedaction"
    10  	"code.cloudfoundry.org/cli/actor/v7action"
    11  	. "code.cloudfoundry.org/cli/actor/v7pushaction"
    12  	"code.cloudfoundry.org/cli/actor/v7pushaction/v7pushactionfakes"
    13  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
    14  	"code.cloudfoundry.org/cli/types"
    15  
    16  	. "github.com/onsi/ginkgo"
    17  	. "github.com/onsi/gomega"
    18  	. "github.com/onsi/gomega/gstruct"
    19  )
    20  
    21  var _ = Describe("Push State", func() {
    22  	var (
    23  		actor           *Actor
    24  		fakeV7Actor     *v7pushactionfakes.FakeV7Actor
    25  		fakeSharedActor *v7pushactionfakes.FakeSharedActor
    26  
    27  		pwd string
    28  	)
    29  
    30  	BeforeEach(func() {
    31  		actor, _, fakeV7Actor, fakeSharedActor = getTestPushActor()
    32  	})
    33  
    34  	Describe("Conceptualize", func() {
    35  		var (
    36  			appName       string
    37  			spaceGUID     string
    38  			orgGUID       string
    39  			currentDir    string
    40  			flagOverrides FlagOverrides
    41  			manifest      []byte
    42  
    43  			states     []PushState
    44  			warnings   Warnings
    45  			executeErr error
    46  		)
    47  
    48  		BeforeEach(func() {
    49  			var err error
    50  			pwd, err = os.Getwd()
    51  			Expect(err).ToNot(HaveOccurred())
    52  			appName = "some-app-name"
    53  			currentDir = pwd
    54  			flagOverrides = FlagOverrides{}
    55  			manifest = []byte("some yaml")
    56  
    57  			spaceGUID = "some-space-guid"
    58  			orgGUID = "some-org-guid"
    59  		})
    60  
    61  		JustBeforeEach(func() {
    62  			states, warnings, executeErr = actor.Conceptualize(appName, spaceGUID, orgGUID, currentDir, flagOverrides, manifest)
    63  		})
    64  
    65  		Describe("application", func() {
    66  			When("the application exists", func() {
    67  				var app v7action.Application
    68  
    69  				BeforeEach(func() {
    70  					app = v7action.Application{
    71  						GUID: "some-app-guid",
    72  						Name: "some-app-name",
    73  					}
    74  
    75  					fakeV7Actor.GetApplicationByNameAndSpaceReturns(app, v7action.Warnings{"some-app-warning"}, nil)
    76  				})
    77  
    78  				It("uses the found app in the application state", func() {
    79  					Expect(executeErr).ToNot(HaveOccurred())
    80  					Expect(warnings).To(ConsistOf("some-app-warning"))
    81  					Expect(states).To(HaveLen(1))
    82  
    83  					Expect(states[0]).To(MatchFields(IgnoreExtras,
    84  						Fields{
    85  							"Application": Equal(app),
    86  							"SpaceGUID":   Equal(spaceGUID),
    87  							"OrgGUID":     Equal(orgGUID),
    88  						}))
    89  
    90  					Expect(fakeV7Actor.GetApplicationByNameAndSpaceCallCount()).To(Equal(1))
    91  					passedName, passedSpaceGUID := fakeV7Actor.GetApplicationByNameAndSpaceArgsForCall(0)
    92  					Expect(passedName).To(Equal("some-app-name"))
    93  					Expect(passedSpaceGUID).To(Equal(spaceGUID))
    94  				})
    95  			})
    96  
    97  			When("the application does not exist", func() {
    98  				BeforeEach(func() {
    99  					fakeV7Actor.GetApplicationByNameAndSpaceReturns(v7action.Application{}, v7action.Warnings{"some-app-warning"}, actionerror.ApplicationNotFoundError{})
   100  				})
   101  
   102  				It("creates a new app in the application state", func() {
   103  					Expect(executeErr).ToNot(HaveOccurred())
   104  					Expect(warnings).To(ConsistOf("some-app-warning"))
   105  					Expect(states).To(HaveLen(1))
   106  
   107  					Expect(states[0]).To(MatchFields(IgnoreExtras,
   108  						Fields{
   109  							"Application": Equal(v7action.Application{
   110  								Name: "some-app-name",
   111  							}),
   112  							"SpaceGUID": Equal(spaceGUID),
   113  							"OrgGUID":   Equal(orgGUID),
   114  						}))
   115  				})
   116  			})
   117  
   118  			Context("regardless of application existance", func() {
   119  				When("buildpacks are provided via flagOverrides", func() {
   120  					BeforeEach(func() {
   121  						flagOverrides.Buildpacks = []string{"some-buildpack-1", "some-buildpack-2"}
   122  					})
   123  
   124  					It("sets the buildpacks on the app", func() {
   125  						Expect(executeErr).ToNot(HaveOccurred())
   126  						Expect(states[0].Application.LifecycleType).To(Equal(constant.AppLifecycleTypeBuildpack))
   127  						Expect(states[0].Application.LifecycleBuildpacks).To(ConsistOf("some-buildpack-1", "some-buildpack-2"))
   128  					})
   129  				})
   130  
   131  				When("docker image information is provided", func() {
   132  					BeforeEach(func() {
   133  						flagOverrides.DockerImage = "some-docker-image"
   134  						flagOverrides.DockerPassword = "some-docker-password"
   135  						flagOverrides.DockerUsername = "some-docker-username"
   136  					})
   137  
   138  					It("sets the buildpacks on the app", func() {
   139  						Expect(executeErr).ToNot(HaveOccurred())
   140  						Expect(states[0].Application.LifecycleType).To(Equal(constant.AppLifecycleTypeDocker))
   141  						Expect(states[0].Application.LifecycleBuildpacks).To(BeEmpty())
   142  					})
   143  				})
   144  			})
   145  
   146  			When("the application lookup errors", func() {
   147  				var expectedErr error
   148  
   149  				BeforeEach(func() {
   150  					expectedErr = errors.New("some-error")
   151  					fakeV7Actor.GetApplicationByNameAndSpaceReturns(v7action.Application{}, v7action.Warnings{"some-app-warning"}, expectedErr)
   152  				})
   153  
   154  				It("translates command line settings into a single push state", func() {
   155  					Expect(executeErr).To(MatchError(expectedErr))
   156  					Expect(warnings).To(ConsistOf("some-app-warning"))
   157  				})
   158  			})
   159  		})
   160  
   161  		Describe("bits path", func() {
   162  			When("no app path is provided in the command line settings", func() {
   163  				It("sets the bits path to the current directory in the settings", func() {
   164  					Expect(states[0].BitsPath).To(Equal(pwd))
   165  				})
   166  			})
   167  
   168  			When("an app path is provided in the command line settings", func() {
   169  				var providedPath string
   170  
   171  				BeforeEach(func() {
   172  					archive, err := ioutil.TempFile("", "push-state-provided-path")
   173  					Expect(err).ToNot(HaveOccurred())
   174  					defer archive.Close()
   175  
   176  					providedPath = archive.Name()
   177  					flagOverrides.ProvidedAppPath = providedPath
   178  				})
   179  
   180  				It("sets the bits path to the provided app path", func() {
   181  					Expect(executeErr).ToNot(HaveOccurred())
   182  					Expect(states[0].BitsPath).To(Equal(providedPath))
   183  				})
   184  			})
   185  		})
   186  
   187  		Describe("all resources/archive", func() {
   188  			When("the app resources are given as a directory", func() {
   189  				When("gathering the resources is successful", func() {
   190  					var resources []sharedaction.Resource
   191  
   192  					BeforeEach(func() {
   193  						resources = []sharedaction.Resource{
   194  							{
   195  								Filename: "fake-app-file",
   196  							},
   197  						}
   198  						fakeSharedActor.GatherDirectoryResourcesReturns(resources, nil)
   199  					})
   200  
   201  					It("adds the gathered resources to the push state", func() {
   202  						Expect(executeErr).ToNot(HaveOccurred())
   203  						Expect(fakeSharedActor.GatherDirectoryResourcesCallCount()).To(Equal(1))
   204  						Expect(fakeSharedActor.GatherDirectoryResourcesArgsForCall(0)).To(Equal(pwd))
   205  						Expect(states[0].AllResources).To(Equal(resources))
   206  					})
   207  
   208  					It("sets Archive to false", func() {
   209  						Expect(executeErr).ToNot(HaveOccurred())
   210  						Expect(states[0].Archive).To(BeFalse())
   211  					})
   212  				})
   213  
   214  				When("gathering the resources errors", func() {
   215  					BeforeEach(func() {
   216  						fakeSharedActor.GatherDirectoryResourcesReturns(nil, errors.New("kaboom"))
   217  					})
   218  
   219  					It("returns the error", func() {
   220  						Expect(executeErr).To(MatchError("kaboom"))
   221  					})
   222  				})
   223  			})
   224  
   225  			When("the app resources are given as an archive", func() {
   226  				var archivePath string
   227  
   228  				BeforeEach(func() {
   229  					archive, err := ioutil.TempFile("", "push-state-archive")
   230  					Expect(err).ToNot(HaveOccurred())
   231  					defer archive.Close()
   232  
   233  					archivePath = archive.Name()
   234  					flagOverrides.ProvidedAppPath = archivePath
   235  				})
   236  
   237  				AfterEach(func() {
   238  					Expect(os.RemoveAll(archivePath)).ToNot(HaveOccurred())
   239  				})
   240  
   241  				When("gathering the resources is successful", func() {
   242  					var resources []sharedaction.Resource
   243  
   244  					BeforeEach(func() {
   245  						resources = []sharedaction.Resource{
   246  							{
   247  								Filename: "fake-app-file",
   248  							},
   249  						}
   250  						fakeSharedActor.GatherArchiveResourcesReturns(resources, nil)
   251  					})
   252  
   253  					It("adds the gathered resources to the push state", func() {
   254  						Expect(executeErr).ToNot(HaveOccurred())
   255  						Expect(fakeSharedActor.GatherArchiveResourcesCallCount()).To(Equal(1))
   256  						Expect(fakeSharedActor.GatherArchiveResourcesArgsForCall(0)).To(Equal(archivePath))
   257  						Expect(states[0].AllResources).To(Equal(resources))
   258  					})
   259  
   260  					It("sets Archive to true", func() {
   261  						Expect(executeErr).ToNot(HaveOccurred())
   262  						Expect(states[0].Archive).To(BeTrue())
   263  					})
   264  				})
   265  
   266  				When("gathering the resources errors", func() {
   267  					BeforeEach(func() {
   268  						fakeSharedActor.GatherArchiveResourcesReturns(nil, errors.New("kaboom"))
   269  					})
   270  
   271  					It("returns the error", func() {
   272  						Expect(executeErr).To(MatchError("kaboom"))
   273  					})
   274  				})
   275  			})
   276  		})
   277  
   278  		Describe("manifest", func() {
   279  			It("attaches manifest to pushState", func() {
   280  				Expect(executeErr).ToNot(HaveOccurred())
   281  				Expect(states[0]).To(MatchFields(IgnoreExtras,
   282  					Fields{
   283  						"Manifest": Equal(manifest),
   284  					}))
   285  			})
   286  		})
   287  
   288  		When("flag overrides are passed", func() {
   289  			BeforeEach(func() {
   290  				flagOverrides.Memory = types.NullUint64{IsSet: true, Value: 123456}
   291  			})
   292  
   293  			It("sets the all the flag overrides on the state", func() {
   294  				Expect(states[0].Overrides).To(Equal(flagOverrides))
   295  			})
   296  		})
   297  	})
   298  })