github.com/jghiloni/cli@v6.28.1-0.20170628223758-0ce05fe032a2+incompatible/actor/pushaction/application_config_test.go (about)

     1  package pushaction_test
     2  
     3  import (
     4  	"errors"
     5  	"io/ioutil"
     6  	"os"
     7  
     8  	. "code.cloudfoundry.org/cli/actor/pushaction"
     9  	"code.cloudfoundry.org/cli/actor/pushaction/manifest"
    10  	"code.cloudfoundry.org/cli/actor/pushaction/pushactionfakes"
    11  	"code.cloudfoundry.org/cli/actor/v2action"
    12  
    13  	. "github.com/onsi/ginkgo"
    14  	. "github.com/onsi/gomega"
    15  )
    16  
    17  var _ = Describe("Application Config", func() {
    18  	var (
    19  		actor       *Actor
    20  		fakeV2Actor *pushactionfakes.FakeV2Actor
    21  	)
    22  
    23  	BeforeEach(func() {
    24  		fakeV2Actor = new(pushactionfakes.FakeV2Actor)
    25  		actor = NewActor(fakeV2Actor)
    26  	})
    27  
    28  	Describe("ApplicationConfig", func() {
    29  		Describe("CreatingApplication", func() {
    30  			Context("when the app did not exist", func() {
    31  				It("returns true", func() {
    32  					config := ApplicationConfig{}
    33  					Expect(config.CreatingApplication()).To(BeTrue())
    34  				})
    35  			})
    36  
    37  			Context("when the app exists", func() {
    38  				It("returns false", func() {
    39  					config := ApplicationConfig{CurrentApplication: v2action.Application{GUID: "some-app-guid"}}
    40  					Expect(config.CreatingApplication()).To(BeFalse())
    41  				})
    42  			})
    43  		})
    44  
    45  		Describe("UpdatedApplication", func() {
    46  			Context("when the app did not exist", func() {
    47  				It("returns false", func() {
    48  					config := ApplicationConfig{}
    49  					Expect(config.UpdatingApplication()).To(BeFalse())
    50  				})
    51  			})
    52  
    53  			Context("when the app exists", func() {
    54  				It("returns true", func() {
    55  					config := ApplicationConfig{CurrentApplication: v2action.Application{GUID: "some-app-guid"}}
    56  					Expect(config.UpdatingApplication()).To(BeTrue())
    57  				})
    58  			})
    59  		})
    60  	})
    61  
    62  	Describe("ConvertToApplicationConfigs", func() {
    63  		var (
    64  			appName      string
    65  			orgGUID      string
    66  			spaceGUID    string
    67  			domain       v2action.Domain
    68  			manifestApps []manifest.Application
    69  			filesPath    string
    70  
    71  			configs    []ApplicationConfig
    72  			warnings   Warnings
    73  			executeErr error
    74  
    75  			firstConfig ApplicationConfig
    76  		)
    77  
    78  		BeforeEach(func() {
    79  			appName = "some-app"
    80  			orgGUID = "some-org-guid"
    81  			spaceGUID = "some-space-guid"
    82  
    83  			var err error
    84  			filesPath, err = ioutil.TempDir("", "convert-to-application-configs")
    85  			Expect(err).ToNot(HaveOccurred())
    86  
    87  			manifestApps = []manifest.Application{{
    88  				Name: appName,
    89  				Path: filesPath,
    90  			}}
    91  
    92  			domain = v2action.Domain{
    93  				Name: "private-domain.com",
    94  				GUID: "some-private-domain-guid",
    95  			}
    96  			// Prevents NoDomainsFoundError
    97  			fakeV2Actor.GetOrganizationDomainsReturns(
    98  				[]v2action.Domain{domain},
    99  				v2action.Warnings{"private-domain-warnings", "shared-domain-warnings"},
   100  				nil,
   101  			)
   102  		})
   103  
   104  		JustBeforeEach(func() {
   105  			configs, warnings, executeErr = actor.ConvertToApplicationConfigs(orgGUID, spaceGUID, manifestApps)
   106  			if len(configs) > 0 {
   107  				firstConfig = configs[0]
   108  			}
   109  		})
   110  
   111  		AfterEach(func() {
   112  			Expect(os.RemoveAll(filesPath)).ToNot(HaveOccurred())
   113  		})
   114  
   115  		Context("when the application exists", func() {
   116  			var app v2action.Application
   117  			var route v2action.Route
   118  
   119  			BeforeEach(func() {
   120  				app = v2action.Application{
   121  					Name:      appName,
   122  					GUID:      "some-app-guid",
   123  					SpaceGUID: spaceGUID,
   124  				}
   125  
   126  				route = v2action.Route{
   127  					Domain: v2action.Domain{
   128  						Name: "some-domain.com",
   129  						GUID: "some-domain-guid",
   130  					},
   131  					Host:      app.Name,
   132  					GUID:      "route-guid",
   133  					SpaceGUID: spaceGUID,
   134  				}
   135  
   136  				fakeV2Actor.GetApplicationByNameAndSpaceReturns(app, v2action.Warnings{"some-app-warning-1", "some-app-warning-2"}, nil)
   137  			})
   138  
   139  			Context("when retrieving the application's routes is successful", func() {
   140  				BeforeEach(func() {
   141  					fakeV2Actor.GetApplicationRoutesReturns([]v2action.Route{route}, v2action.Warnings{"app-route-warnings"}, nil)
   142  				})
   143  
   144  				It("sets the current application to the existing application", func() {
   145  					Expect(executeErr).ToNot(HaveOccurred())
   146  					Expect(warnings).To(ConsistOf("some-app-warning-1", "some-app-warning-2", "app-route-warnings", "private-domain-warnings", "shared-domain-warnings"))
   147  					Expect(firstConfig.CurrentApplication).To(Equal(app))
   148  					Expect(firstConfig.TargetedSpaceGUID).To(Equal(spaceGUID))
   149  
   150  					Expect(fakeV2Actor.GetApplicationByNameAndSpaceCallCount()).To(Equal(1))
   151  					appName, passedSpaceGUID := fakeV2Actor.GetApplicationByNameAndSpaceArgsForCall(0)
   152  					Expect(appName).To(Equal(app.Name))
   153  					Expect(passedSpaceGUID).To(Equal(spaceGUID))
   154  				})
   155  
   156  				It("sets the current routes", func() {
   157  					Expect(executeErr).ToNot(HaveOccurred())
   158  					Expect(warnings).To(ConsistOf("some-app-warning-1", "some-app-warning-2", "app-route-warnings", "private-domain-warnings", "shared-domain-warnings"))
   159  					Expect(firstConfig.CurrentRoutes).To(ConsistOf(route))
   160  				})
   161  			})
   162  
   163  			Context("when retrieving the application's routes errors", func() {
   164  				var expectedErr error
   165  
   166  				BeforeEach(func() {
   167  					expectedErr = errors.New("dios mio")
   168  					fakeV2Actor.GetApplicationRoutesReturns(nil, v2action.Warnings{"app-route-warnings"}, expectedErr)
   169  				})
   170  
   171  				It("sets the current and desired application to the current", func() {
   172  					Expect(executeErr).To(MatchError(expectedErr))
   173  					Expect(warnings).To(ConsistOf("some-app-warning-1", "some-app-warning-2", "app-route-warnings"))
   174  
   175  					Expect(fakeV2Actor.GetApplicationRoutesCallCount()).To(Equal(1))
   176  					Expect(fakeV2Actor.GetApplicationRoutesArgsForCall(0)).To(Equal(app.GUID))
   177  				})
   178  			})
   179  		})
   180  
   181  		Context("when the application does not exist", func() {
   182  			BeforeEach(func() {
   183  				fakeV2Actor.GetApplicationByNameAndSpaceReturns(v2action.Application{}, v2action.Warnings{"some-app-warning-1", "some-app-warning-2"}, v2action.ApplicationNotFoundError{})
   184  			})
   185  
   186  			It("creates a new application and sets it to the desired application", func() {
   187  				Expect(executeErr).ToNot(HaveOccurred())
   188  				Expect(warnings).To(ConsistOf("some-app-warning-1", "some-app-warning-2", "private-domain-warnings", "shared-domain-warnings"))
   189  				Expect(firstConfig.CurrentApplication).To(Equal(v2action.Application{}))
   190  				Expect(firstConfig.DesiredApplication).To(Equal(v2action.Application{
   191  					Name:      "some-app",
   192  					SpaceGUID: spaceGUID,
   193  				}))
   194  				Expect(firstConfig.TargetedSpaceGUID).To(Equal(spaceGUID))
   195  			})
   196  		})
   197  
   198  		Context("when retrieving the application errors", func() {
   199  			var expectedErr error
   200  
   201  			BeforeEach(func() {
   202  				expectedErr = errors.New("dios mio")
   203  				fakeV2Actor.GetApplicationByNameAndSpaceReturns(v2action.Application{}, v2action.Warnings{"some-app-warning-1", "some-app-warning-2"}, expectedErr)
   204  			})
   205  
   206  			It("returns the error and warnings", func() {
   207  				Expect(executeErr).To(MatchError(expectedErr))
   208  				Expect(warnings).To(ConsistOf("some-app-warning-1", "some-app-warning-2"))
   209  			})
   210  		})
   211  
   212  		Context("when retrieving the default route is successful", func() {
   213  			BeforeEach(func() {
   214  				// Assumes new route
   215  				fakeV2Actor.FindRouteBoundToSpaceWithSettingsReturns(v2action.Route{}, v2action.Warnings{"get-route-warnings"}, v2action.RouteNotFoundError{})
   216  			})
   217  
   218  			It("adds the route to desired routes", func() {
   219  				Expect(executeErr).ToNot(HaveOccurred())
   220  				Expect(warnings).To(ConsistOf("private-domain-warnings", "shared-domain-warnings", "get-route-warnings"))
   221  				Expect(firstConfig.DesiredRoutes).To(ConsistOf(v2action.Route{
   222  					Domain:    domain,
   223  					Host:      appName,
   224  					SpaceGUID: spaceGUID,
   225  				}))
   226  			})
   227  		})
   228  
   229  		Context("when retrieving the default route errors", func() {
   230  			var expectedErr error
   231  
   232  			BeforeEach(func() {
   233  				expectedErr = errors.New("dios mio")
   234  				fakeV2Actor.FindRouteBoundToSpaceWithSettingsReturns(v2action.Route{}, v2action.Warnings{"get-route-warnings"}, expectedErr)
   235  			})
   236  
   237  			It("returns the error and warnings", func() {
   238  				Expect(executeErr).To(MatchError(expectedErr))
   239  				Expect(warnings).To(ConsistOf("private-domain-warnings", "shared-domain-warnings", "get-route-warnings"))
   240  			})
   241  		})
   242  
   243  		Context("when scanning for files", func() {
   244  			Context("given a directory", func() {
   245  				Context("when scanning is successful", func() {
   246  					var resources []v2action.Resource
   247  
   248  					BeforeEach(func() {
   249  						resources = []v2action.Resource{
   250  							{Filename: "I am a file!"},
   251  							{Filename: "I am not a file"},
   252  						}
   253  						fakeV2Actor.GatherDirectoryResourcesReturns(resources, nil)
   254  					})
   255  
   256  					It("sets the full resource list on the config", func() {
   257  						Expect(executeErr).ToNot(HaveOccurred())
   258  						Expect(warnings).To(ConsistOf("private-domain-warnings", "shared-domain-warnings"))
   259  						Expect(firstConfig.AllResources).To(Equal(resources))
   260  						Expect(firstConfig.Path).To(Equal(filesPath))
   261  						Expect(firstConfig.Archive).To(BeFalse())
   262  
   263  						Expect(fakeV2Actor.GatherDirectoryResourcesCallCount()).To(Equal(1))
   264  						Expect(fakeV2Actor.GatherDirectoryResourcesArgsForCall(0)).To(Equal(filesPath))
   265  					})
   266  				})
   267  
   268  				Context("when scanning errors", func() {
   269  					var expectedErr error
   270  
   271  					BeforeEach(func() {
   272  						expectedErr = errors.New("dios mio")
   273  						fakeV2Actor.GatherDirectoryResourcesReturns(nil, expectedErr)
   274  					})
   275  
   276  					It("returns the error and warnings", func() {
   277  						Expect(executeErr).To(MatchError(expectedErr))
   278  						Expect(warnings).To(ConsistOf("private-domain-warnings", "shared-domain-warnings"))
   279  					})
   280  				})
   281  			})
   282  
   283  			Context("given archive", func() {
   284  				var archive string
   285  
   286  				BeforeEach(func() {
   287  					f, err := ioutil.TempFile("", "convert-to-application-configs-archive")
   288  					Expect(err).ToNot(HaveOccurred())
   289  					archive = f.Name()
   290  					Expect(f.Close()).ToNot(HaveOccurred())
   291  
   292  					manifestApps[0].Path = archive
   293  				})
   294  
   295  				AfterEach(func() {
   296  					Expect(os.RemoveAll(archive)).ToNot(HaveOccurred())
   297  				})
   298  
   299  				Context("when scanning is successful", func() {
   300  					var resources []v2action.Resource
   301  
   302  					BeforeEach(func() {
   303  						resources = []v2action.Resource{
   304  							{Filename: "I am a file!"},
   305  							{Filename: "I am not a file"},
   306  						}
   307  						fakeV2Actor.GatherArchiveResourcesReturns(resources, nil)
   308  					})
   309  
   310  					It("sets the full resource list on the config", func() {
   311  						Expect(executeErr).ToNot(HaveOccurred())
   312  						Expect(warnings).To(ConsistOf("private-domain-warnings", "shared-domain-warnings"))
   313  						Expect(firstConfig.AllResources).To(Equal(resources))
   314  						Expect(firstConfig.Path).To(Equal(archive))
   315  						Expect(firstConfig.Archive).To(BeTrue())
   316  
   317  						Expect(fakeV2Actor.GatherArchiveResourcesCallCount()).To(Equal(1))
   318  						Expect(fakeV2Actor.GatherArchiveResourcesArgsForCall(0)).To(Equal(archive))
   319  					})
   320  				})
   321  
   322  				Context("when scanning errors", func() {
   323  					var expectedErr error
   324  
   325  					BeforeEach(func() {
   326  						expectedErr = errors.New("dios mio")
   327  						fakeV2Actor.GatherArchiveResourcesReturns(nil, expectedErr)
   328  					})
   329  
   330  					It("returns the error and warnings", func() {
   331  						Expect(executeErr).To(MatchError(expectedErr))
   332  						Expect(warnings).To(ConsistOf("private-domain-warnings", "shared-domain-warnings"))
   333  					})
   334  				})
   335  			})
   336  
   337  			Context("given a path that does not exist", func() {
   338  				BeforeEach(func() {
   339  					manifestApps[0].Path = "/i/will/fight/you/if/this/exists"
   340  				})
   341  
   342  				It("returns errors and warnings", func() {
   343  					Expect(os.IsNotExist(executeErr)).To(BeTrue())
   344  					Expect(warnings).To(ConsistOf("private-domain-warnings", "shared-domain-warnings"))
   345  
   346  					Expect(fakeV2Actor.GatherDirectoryResourcesCallCount()).To(Equal(0))
   347  					Expect(fakeV2Actor.GatherArchiveResourcesCallCount()).To(Equal(0))
   348  				})
   349  			})
   350  		})
   351  
   352  		Context("when a docker image is configured", func() {
   353  			BeforeEach(func() {
   354  				manifestApps[0].DockerImage = "some-docker-image-path"
   355  			})
   356  
   357  			It("sets the docker image on DesiredApplication and does not gather resources", func() {
   358  				Expect(executeErr).ToNot(HaveOccurred())
   359  				Expect(firstConfig.DesiredApplication.DockerImage).To(Equal("some-docker-image-path"))
   360  
   361  				Expect(fakeV2Actor.GatherDirectoryResourcesCallCount()).To(Equal(0))
   362  			})
   363  		})
   364  	})
   365  })