github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/actor/v2action/manifest_test.go (about)

     1  package v2action_test
     2  
     3  import (
     4  	"errors"
     5  	"io/ioutil"
     6  	"os"
     7  
     8  	. "github.com/liamawhite/cli-with-i18n/actor/v2action"
     9  	"github.com/liamawhite/cli-with-i18n/actor/v2action/v2actionfakes"
    10  	"github.com/liamawhite/cli-with-i18n/api/cloudcontroller/ccv2"
    11  	"github.com/liamawhite/cli-with-i18n/types"
    12  	. "github.com/onsi/ginkgo"
    13  	. "github.com/onsi/gomega"
    14  )
    15  
    16  var _ = Describe("Manifest Actions", func() {
    17  	var (
    18  		actor                     *Actor
    19  		fakeCloudControllerClient *v2actionfakes.FakeCloudControllerClient
    20  		manifestFilePath          string
    21  	)
    22  
    23  	BeforeEach(func() {
    24  		fakeCloudControllerClient = new(v2actionfakes.FakeCloudControllerClient)
    25  		actor = NewActor(fakeCloudControllerClient, nil, nil)
    26  	})
    27  
    28  	Describe("CreateApplicationManifestByNameAndSpace", func() {
    29  		var (
    30  			createWarnings Warnings
    31  			createErr      error
    32  		)
    33  
    34  		JustBeforeEach(func() {
    35  			createWarnings, createErr = actor.CreateApplicationManifestByNameAndSpace("some-app", "some-space-guid", manifestFilePath)
    36  		})
    37  
    38  		Context("when getting the application summary errors", func() {
    39  			BeforeEach(func() {
    40  				fakeCloudControllerClient.GetApplicationsReturns([]ccv2.Application{}, ccv2.Warnings{"some-app-warning"}, errors.New("some-app-error"))
    41  			})
    42  
    43  			It("returns the error and all warnings", func() {
    44  				Expect(createErr).To(MatchError("some-app-error"))
    45  				Expect(createWarnings).To(ConsistOf("some-app-warning"))
    46  			})
    47  		})
    48  
    49  		Context("when getting the application summary succeeds", func() {
    50  			var app ccv2.Application
    51  
    52  			BeforeEach(func() {
    53  				app = ccv2.Application{
    54  					GUID: "some-app-guid",
    55  					Name: "some-app",
    56  					Buildpack: types.FilteredString{
    57  						IsSet: true,
    58  						Value: "some-buildpack",
    59  					},
    60  					DetectedBuildpack: types.FilteredString{
    61  						IsSet: true,
    62  						Value: "some-detected-buildpack",
    63  					},
    64  					DockerImage: "some-docker-image",
    65  					DockerCredentials: ccv2.DockerCredentials{
    66  						Username: "some-docker-username",
    67  						Password: "some-docker-password", // CC currently always returns an empty string
    68  					},
    69  					Command: types.FilteredString{
    70  						IsSet: true,
    71  						Value: "some-command",
    72  					},
    73  					DetectedStartCommand: types.FilteredString{
    74  						IsSet: true,
    75  						Value: "some-detected-command",
    76  					},
    77  					EnvironmentVariables: map[string]string{
    78  						"env_1": "foo",
    79  						"env_2": "182837403930483038",
    80  						"env_3": "true",
    81  						"env_4": "1.00001",
    82  					},
    83  					HealthCheckTimeout:      120,
    84  					HealthCheckHTTPEndpoint: "\\some-endpoint",
    85  					HealthCheckType:         "http",
    86  					Instances: types.NullInt{
    87  						Value: 10,
    88  						IsSet: true,
    89  					},
    90  					DiskQuota: 1024,
    91  					Memory:    200,
    92  					StackGUID: "some-stack-guid",
    93  				}
    94  
    95  				fakeCloudControllerClient.GetApplicationsReturns(
    96  					[]ccv2.Application{app},
    97  					ccv2.Warnings{"some-app-warning"},
    98  					nil)
    99  
   100  				fakeCloudControllerClient.GetApplicationRoutesReturns(
   101  					[]ccv2.Route{
   102  						{
   103  							GUID:       "some-route-1-guid",
   104  							Host:       "host-1",
   105  							DomainGUID: "some-domain-guid",
   106  						},
   107  						{
   108  							GUID:       "some-route-2-guid",
   109  							Host:       "host-2",
   110  							DomainGUID: "some-domain-guid",
   111  						},
   112  					},
   113  					ccv2.Warnings{"some-routes-warning"},
   114  					nil)
   115  
   116  				fakeCloudControllerClient.GetSharedDomainReturns(
   117  					ccv2.Domain{GUID: "some-domain-guid", Name: "some-domain"},
   118  					ccv2.Warnings{"some-domain-warning"},
   119  					nil)
   120  
   121  				fakeCloudControllerClient.GetStackReturns(
   122  					ccv2.Stack{Name: "some-stack"},
   123  					ccv2.Warnings{"some-stack-warning"},
   124  					nil)
   125  			})
   126  
   127  			Context("when getting services fails", func() {
   128  				BeforeEach(func() {
   129  					fakeCloudControllerClient.GetServiceBindingsReturns(
   130  						[]ccv2.ServiceBinding{},
   131  						ccv2.Warnings{"some-service-warning"},
   132  						errors.New("some-service-error"),
   133  					)
   134  				})
   135  
   136  				It("returns the error and all warnings", func() {
   137  					Expect(createErr).To(MatchError("some-service-error"))
   138  					Expect(createWarnings).To(ConsistOf("some-app-warning", "some-routes-warning", "some-domain-warning", "some-stack-warning", "some-service-warning"))
   139  				})
   140  			})
   141  
   142  			Context("when getting services succeeds", func() {
   143  				BeforeEach(func() {
   144  					fakeCloudControllerClient.GetServiceBindingsReturns(
   145  						[]ccv2.ServiceBinding{
   146  							{ServiceInstanceGUID: "service-1-guid"},
   147  							{ServiceInstanceGUID: "service-2-guid"},
   148  						},
   149  						ccv2.Warnings{"some-service-warning"},
   150  						nil,
   151  					)
   152  					fakeCloudControllerClient.GetServiceInstanceStub = func(serviceInstanceGUID string) (ccv2.ServiceInstance, ccv2.Warnings, error) {
   153  						switch serviceInstanceGUID {
   154  						case "service-1-guid":
   155  							return ccv2.ServiceInstance{Name: "service-1"}, ccv2.Warnings{"some-service-1-warning"}, nil
   156  						case "service-2-guid":
   157  							return ccv2.ServiceInstance{Name: "service-2"}, ccv2.Warnings{"some-service-2-warning"}, nil
   158  						default:
   159  							panic("unknown service instance")
   160  						}
   161  					}
   162  				})
   163  
   164  				Context("when writing manifest succeeds", func() {
   165  					BeforeEach(func() {
   166  						manifestFile, err := ioutil.TempFile("", "manifest-test-")
   167  						Expect(err).NotTo(HaveOccurred())
   168  						Expect(manifestFile.Close()).To(Succeed())
   169  						manifestFilePath = manifestFile.Name()
   170  					})
   171  
   172  					AfterEach(func() {
   173  						Expect(os.Remove(manifestFilePath)).To(Succeed())
   174  					})
   175  
   176  					It("writes the manifest to the specified path", func() {
   177  						manifestBytes, err := ioutil.ReadFile(manifestFilePath)
   178  						Expect(err).NotTo(HaveOccurred())
   179  						Expect(createWarnings).To(ConsistOf("some-app-warning", "some-routes-warning", "some-domain-warning", "some-stack-warning", "some-service-warning", "some-service-1-warning", "some-service-2-warning"))
   180  						Expect(string(manifestBytes)).To(Equal(`applications:
   181  - name: some-app
   182    buildpack: some-buildpack
   183    command: some-command
   184    disk_quota: 1G
   185    docker:
   186      image: some-docker-image
   187      username: some-docker-username
   188    env:
   189      env_1: foo
   190      env_2: "182837403930483038"
   191      env_3: "true"
   192      env_4: "1.00001"
   193    health-check-http-endpoint: \some-endpoint
   194    health-check-type: http
   195    instances: 10
   196    memory: 200M
   197    routes:
   198    - route: host-1.some-domain
   199    - route: host-2.some-domain
   200    services:
   201    - service-1
   202    - service-2
   203    stack: some-stack
   204    timeout: 120
   205  `))
   206  					})
   207  
   208  					Context("when there are no routes", func() {
   209  						BeforeEach(func() {
   210  							fakeCloudControllerClient.GetApplicationRoutesReturns(nil, nil, nil)
   211  						})
   212  
   213  						It("writes the manifest with no-route set to true", func() {
   214  							manifestBytes, err := ioutil.ReadFile(manifestFilePath)
   215  							Expect(err).NotTo(HaveOccurred())
   216  							Expect(createWarnings).To(ConsistOf("some-app-warning", "some-stack-warning", "some-service-warning", "some-service-1-warning", "some-service-2-warning"))
   217  							Expect(string(manifestBytes)).To(Equal(`applications:
   218  - name: some-app
   219    buildpack: some-buildpack
   220    command: some-command
   221    disk_quota: 1G
   222    docker:
   223      image: some-docker-image
   224      username: some-docker-username
   225    env:
   226      env_1: foo
   227      env_2: "182837403930483038"
   228      env_3: "true"
   229      env_4: "1.00001"
   230    health-check-http-endpoint: \some-endpoint
   231    health-check-type: http
   232    instances: 10
   233    memory: 200M
   234    no-route: true
   235    services:
   236    - service-1
   237    - service-2
   238    stack: some-stack
   239    timeout: 120
   240  `))
   241  						})
   242  					})
   243  
   244  					Context("when docker image and username are not provided", func() {
   245  						BeforeEach(func() {
   246  							app.DockerImage = ""
   247  							app.DockerCredentials = ccv2.DockerCredentials{}
   248  							fakeCloudControllerClient.GetApplicationsReturns(
   249  								[]ccv2.Application{app},
   250  								ccv2.Warnings{"some-app-warning"},
   251  								nil)
   252  						})
   253  
   254  						It("does not include it in manifest", func() {
   255  							manifestBytes, err := ioutil.ReadFile(manifestFilePath)
   256  							Expect(err).NotTo(HaveOccurred())
   257  							Expect(string(manifestBytes)).To(Equal(`applications:
   258  - name: some-app
   259    buildpack: some-buildpack
   260    command: some-command
   261    disk_quota: 1G
   262    env:
   263      env_1: foo
   264      env_2: "182837403930483038"
   265      env_3: "true"
   266      env_4: "1.00001"
   267    health-check-http-endpoint: \some-endpoint
   268    health-check-type: http
   269    instances: 10
   270    memory: 200M
   271    routes:
   272    - route: host-1.some-domain
   273    - route: host-2.some-domain
   274    services:
   275    - service-1
   276    - service-2
   277    stack: some-stack
   278    timeout: 120
   279  `))
   280  						})
   281  					})
   282  
   283  					Describe("default CC values", func() {
   284  						// We ommitting default CC values from manifest
   285  						// so that it won't get too big
   286  
   287  						Context("when the health check type is port", func() {
   288  							BeforeEach(func() {
   289  								app.HealthCheckType = ccv2.ApplicationHealthCheckPort
   290  								fakeCloudControllerClient.GetApplicationsReturns(
   291  									[]ccv2.Application{app},
   292  									ccv2.Warnings{"some-app-warning"},
   293  									nil)
   294  							})
   295  
   296  							It("does not include health check type and endpoint", func() {
   297  								manifestBytes, err := ioutil.ReadFile(manifestFilePath)
   298  								Expect(err).NotTo(HaveOccurred())
   299  								Expect(string(manifestBytes)).To(Equal(`applications:
   300  - name: some-app
   301    buildpack: some-buildpack
   302    command: some-command
   303    disk_quota: 1G
   304    docker:
   305      image: some-docker-image
   306      username: some-docker-username
   307    env:
   308      env_1: foo
   309      env_2: "182837403930483038"
   310      env_3: "true"
   311      env_4: "1.00001"
   312    instances: 10
   313    memory: 200M
   314    routes:
   315    - route: host-1.some-domain
   316    - route: host-2.some-domain
   317    services:
   318    - service-1
   319    - service-2
   320    stack: some-stack
   321    timeout: 120
   322  `))
   323  							})
   324  						})
   325  
   326  						Context("when the health check type is http", func() {
   327  							Context("when the health check endpoint path is '/'", func() {
   328  								BeforeEach(func() {
   329  									app.HealthCheckType = ccv2.ApplicationHealthCheckHTTP
   330  									app.HealthCheckHTTPEndpoint = "/"
   331  									fakeCloudControllerClient.GetApplicationsReturns(
   332  										[]ccv2.Application{app},
   333  										ccv2.Warnings{"some-app-warning"},
   334  										nil)
   335  								})
   336  
   337  								It("does not include health check endpoint in manifest", func() {
   338  									manifestBytes, err := ioutil.ReadFile(manifestFilePath)
   339  									Expect(err).NotTo(HaveOccurred())
   340  									Expect(string(manifestBytes)).To(Equal(`applications:
   341  - name: some-app
   342    buildpack: some-buildpack
   343    command: some-command
   344    disk_quota: 1G
   345    docker:
   346      image: some-docker-image
   347      username: some-docker-username
   348    env:
   349      env_1: foo
   350      env_2: "182837403930483038"
   351      env_3: "true"
   352      env_4: "1.00001"
   353    health-check-type: http
   354    instances: 10
   355    memory: 200M
   356    routes:
   357    - route: host-1.some-domain
   358    - route: host-2.some-domain
   359    services:
   360    - service-1
   361    - service-2
   362    stack: some-stack
   363    timeout: 120
   364  `))
   365  								})
   366  							})
   367  
   368  							Context("when the health check type is process", func() {
   369  								BeforeEach(func() {
   370  									app.HealthCheckType = ccv2.ApplicationHealthCheckProcess
   371  									fakeCloudControllerClient.GetApplicationsReturns(
   372  										[]ccv2.Application{app},
   373  										ccv2.Warnings{"some-app-warning"},
   374  										nil)
   375  								})
   376  
   377  								It("does not include health check endpoint in manifest", func() {
   378  									manifestBytes, err := ioutil.ReadFile(manifestFilePath)
   379  									Expect(err).NotTo(HaveOccurred())
   380  									Expect(string(manifestBytes)).To(Equal(`applications:
   381  - name: some-app
   382    buildpack: some-buildpack
   383    command: some-command
   384    disk_quota: 1G
   385    docker:
   386      image: some-docker-image
   387      username: some-docker-username
   388    env:
   389      env_1: foo
   390      env_2: "182837403930483038"
   391      env_3: "true"
   392      env_4: "1.00001"
   393    health-check-type: process
   394    instances: 10
   395    memory: 200M
   396    routes:
   397    - route: host-1.some-domain
   398    - route: host-2.some-domain
   399    services:
   400    - service-1
   401    - service-2
   402    stack: some-stack
   403    timeout: 120
   404  `))
   405  								})
   406  							})
   407  						})
   408  					})
   409  				})
   410  
   411  				Context("when writing the manifest fails", func() {
   412  					BeforeEach(func() {
   413  						var err error
   414  						manifestFilePath, err = ioutil.TempDir("", "manifest-test-")
   415  						Expect(err).NotTo(HaveOccurred())
   416  					})
   417  
   418  					AfterEach(func() {
   419  						Expect(os.RemoveAll(manifestFilePath)).To(Succeed())
   420  					})
   421  
   422  					It("returns an ManifestCreationError", func() {
   423  						Expect(createErr).To(HaveOccurred())
   424  						Expect(createErr.Error()).To(ContainSubstring("Error creating manifest file:"))
   425  						Expect(createWarnings).To(ConsistOf("some-app-warning", "some-routes-warning", "some-domain-warning", "some-stack-warning", "some-service-warning", "some-service-1-warning", "some-service-2-warning"))
   426  					})
   427  				})
   428  			})
   429  		})
   430  	})
   431  })