github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/commands/create_app_manifest_test.go (about)

     1  package commands_test
     2  
     3  import (
     4  	"time"
     5  
     6  	testAppInstanaces "github.com/cloudfoundry/cli/cf/api/app_instances/fakes"
     7  	testapi "github.com/cloudfoundry/cli/cf/api/fakes"
     8  	"github.com/cloudfoundry/cli/cf/command_registry"
     9  	"github.com/cloudfoundry/cli/cf/configuration/core_config"
    10  	"github.com/cloudfoundry/cli/cf/formatters"
    11  	testManifest "github.com/cloudfoundry/cli/cf/manifest/fakes"
    12  	"github.com/cloudfoundry/cli/cf/models"
    13  	testcmd "github.com/cloudfoundry/cli/testhelpers/commands"
    14  	testconfig "github.com/cloudfoundry/cli/testhelpers/configuration"
    15  	testreq "github.com/cloudfoundry/cli/testhelpers/requirements"
    16  	testterm "github.com/cloudfoundry/cli/testhelpers/terminal"
    17  	testtime "github.com/cloudfoundry/cli/testhelpers/time"
    18  
    19  	. "github.com/cloudfoundry/cli/testhelpers/matchers"
    20  	. "github.com/onsi/ginkgo"
    21  	. "github.com/onsi/gomega"
    22  )
    23  
    24  var _ = Describe("create-app-manifest Command", func() {
    25  	var (
    26  		ui                  *testterm.FakeUI
    27  		configRepo          core_config.Repository
    28  		appSummaryRepo      *testapi.FakeAppSummaryRepo
    29  		appInstancesRepo    *testAppInstanaces.FakeAppInstancesRepository
    30  		requirementsFactory *testreq.FakeReqFactory
    31  		fakeManifest        *testManifest.FakeAppManifest
    32  		deps                command_registry.Dependency
    33  	)
    34  
    35  	updateCommandDependency := func(pluginCall bool) {
    36  		deps.Ui = ui
    37  		deps.RepoLocator = deps.RepoLocator.SetAppSummaryRepository(appSummaryRepo)
    38  		deps.RepoLocator = deps.RepoLocator.SetAppInstancesRepository(appInstancesRepo)
    39  		deps.Config = configRepo
    40  		deps.AppManifest = fakeManifest
    41  		command_registry.Commands.SetCommand(command_registry.Commands.FindCommand("create-app-manifest").SetDependency(deps, pluginCall))
    42  	}
    43  
    44  	BeforeEach(func() {
    45  		fakeManifest = &testManifest.FakeAppManifest{}
    46  		ui = &testterm.FakeUI{}
    47  		appSummaryRepo = &testapi.FakeAppSummaryRepo{}
    48  		appInstancesRepo = &testAppInstanaces.FakeAppInstancesRepository{}
    49  		configRepo = testconfig.NewRepositoryWithDefaults()
    50  		requirementsFactory = &testreq.FakeReqFactory{
    51  			LoginSuccess:         true,
    52  			TargetedSpaceSuccess: true,
    53  		}
    54  	})
    55  
    56  	runCommand := func(args ...string) bool {
    57  		return testcmd.RunCliCommand("create-app-manifest", args, requirementsFactory, updateCommandDependency, false)
    58  	}
    59  
    60  	Describe("requirements", func() {
    61  		It("fails if not logged in", func() {
    62  			requirementsFactory.LoginSuccess = false
    63  			Expect(runCommand("cf-plays-dwarf-fortress")).To(BeFalse())
    64  		})
    65  
    66  		It("fails if a space is not targeted", func() {
    67  			requirementsFactory.TargetedSpaceSuccess = false
    68  			Expect(runCommand("cf-plays-dwarf-fortress")).To(BeFalse())
    69  		})
    70  
    71  		It("fails with usage when not provided exactly one arg", func() {
    72  			passed := runCommand()
    73  			Expect(ui.Outputs).To(ContainSubstrings(
    74  				[]string{"create-app-manifest", "APP_NAME"},
    75  				[]string{"Incorrect Usage", "Requires", "argument"},
    76  			))
    77  			Expect(passed).To(BeFalse())
    78  		})
    79  
    80  	})
    81  
    82  	Describe("creating app manifest", func() {
    83  		var (
    84  			appInstance  models.AppInstanceFields
    85  			appInstance2 models.AppInstanceFields
    86  			instances    []models.AppInstanceFields
    87  		)
    88  
    89  		BeforeEach(func() {
    90  			appInstance = models.AppInstanceFields{
    91  				State:     models.InstanceRunning,
    92  				Since:     testtime.MustParse("Mon Jan 2 15:04:05 -0700 MST 2006", "Mon Jan 2 15:04:05 -0700 MST 2012"),
    93  				CpuUsage:  1.0,
    94  				DiskQuota: 1 * formatters.GIGABYTE,
    95  				DiskUsage: 32 * formatters.MEGABYTE,
    96  				MemQuota:  64 * formatters.MEGABYTE,
    97  				MemUsage:  13 * formatters.BYTE,
    98  			}
    99  
   100  			appInstance2 = models.AppInstanceFields{
   101  				State: models.InstanceDown,
   102  				Since: testtime.MustParse("Mon Jan 2 15:04:05 -0700 MST 2006", "Mon Apr 1 15:04:05 -0700 MST 2012"),
   103  			}
   104  
   105  			instances = []models.AppInstanceFields{appInstance, appInstance2}
   106  			appInstancesRepo.GetInstancesReturns(instances, nil)
   107  		})
   108  
   109  		Context("app with Services, Routes, Environment Vars", func() {
   110  			BeforeEach(func() {
   111  				app := makeAppWithOptions("my-app")
   112  				appSummaryRepo.GetSummarySummary = app
   113  				requirementsFactory.Application = app
   114  			})
   115  
   116  			It("creates a manifest with services, routes and environment vars", func() {
   117  				runCommand("my-app")
   118  				Ω(fakeManifest.MemoryCallCount()).To(Equal(1))
   119  				Ω(fakeManifest.EnvironmentVarsCallCount()).To(Equal(1))
   120  				Ω(fakeManifest.HealthCheckTimeoutCallCount()).To(Equal(1))
   121  				Ω(fakeManifest.InstancesCallCount()).To(Equal(1))
   122  				Ω(fakeManifest.DomainCallCount()).To(Equal(2))
   123  				Ω(fakeManifest.ServiceCallCount()).To(Equal(1))
   124  				Ω(fakeManifest.StartCommandCallCount()).To(Equal(1))
   125  			})
   126  		})
   127  
   128  		Context("app with buildpack", func() {
   129  			BeforeEach(func() {
   130  				app := makeAppWithOptions("my-app")
   131  				appSummaryRepo.GetSummarySummary = app
   132  				requirementsFactory.Application = app
   133  			})
   134  
   135  			It("creates a manifest with a buildpack", func() {
   136  				runCommand("my-app")
   137  				Ω(fakeManifest.BuildpackUrlCallCount()).To(Equal(1))
   138  			})
   139  		})
   140  
   141  		Context("Env Vars will be written in aplhabetical order", func() {
   142  			BeforeEach(func() {
   143  				app := makeAppWithMultipleEnvVars("my-app")
   144  				appSummaryRepo.GetSummarySummary = app
   145  				requirementsFactory.Application = app
   146  			})
   147  
   148  			It("calls manifest EnvironmentVars() aphlhabetically", func() {
   149  				runCommand("my-app")
   150  				Ω(fakeManifest.EnvironmentVarsCallCount()).To(Equal(4))
   151  				_, k, _ := fakeManifest.EnvironmentVarsArgsForCall(0)
   152  				Ω(k).To(Equal("abc"))
   153  				_, k, _ = fakeManifest.EnvironmentVarsArgsForCall(1)
   154  				Ω(k).To(Equal("bar"))
   155  				_, k, _ = fakeManifest.EnvironmentVarsArgsForCall(2)
   156  				Ω(k).To(Equal("foo"))
   157  				_, k, _ = fakeManifest.EnvironmentVarsArgsForCall(3)
   158  				Ω(k).To(Equal("xyz"))
   159  			})
   160  		})
   161  
   162  		Context("Env Vars can be in different types (string, float64, bool)", func() {
   163  			BeforeEach(func() {
   164  				app := makeAppWithMultipleEnvVars("my-app")
   165  				appSummaryRepo.GetSummarySummary = app
   166  				requirementsFactory.Application = app
   167  			})
   168  
   169  			It("calls manifest EnvironmentVars() aphlhabetically", func() {
   170  				runCommand("my-app")
   171  				Ω(fakeManifest.EnvironmentVarsCallCount()).To(Equal(4))
   172  				_, _, v := fakeManifest.EnvironmentVarsArgsForCall(0)
   173  				Ω(v).To(Equal("\"abc\""))
   174  				_, _, v = fakeManifest.EnvironmentVarsArgsForCall(1)
   175  				Ω(v).To(Equal("10"))
   176  				_, _, v = fakeManifest.EnvironmentVarsArgsForCall(2)
   177  				Ω(v).To(Equal("true"))
   178  				_, _, v = fakeManifest.EnvironmentVarsArgsForCall(3)
   179  				Ω(v).To(Equal("false"))
   180  			})
   181  		})
   182  
   183  		Context("app without Services, Routes, Environment Vars", func() {
   184  			BeforeEach(func() {
   185  				app := makeAppWithoutOptions("my-app")
   186  				appSummaryRepo.GetSummarySummary = app
   187  				requirementsFactory.Application = app
   188  			})
   189  
   190  			It("creates a manifest with services, routes and environment vars", func() {
   191  				runCommand("my-app")
   192  				Ω(fakeManifest.MemoryCallCount()).To(Equal(1))
   193  				Ω(fakeManifest.EnvironmentVarsCallCount()).To(Equal(0))
   194  				Ω(fakeManifest.HealthCheckTimeoutCallCount()).To(Equal(0))
   195  				Ω(fakeManifest.InstancesCallCount()).To(Equal(1))
   196  				Ω(fakeManifest.DomainCallCount()).To(Equal(0))
   197  				Ω(fakeManifest.ServiceCallCount()).To(Equal(0))
   198  			})
   199  		})
   200  
   201  		Context("when the flag -p is supplied", func() {
   202  			BeforeEach(func() {
   203  				app := makeAppWithoutOptions("my-app")
   204  				appSummaryRepo.GetSummarySummary = app
   205  				requirementsFactory.Application = app
   206  			})
   207  
   208  			It("creates a manifest with services, routes and environment vars", func() {
   209  				filePath := "another/location/manifest.yml"
   210  				runCommand("-p", filePath, "my-app")
   211  				Ω(fakeManifest.FileSavePathArgsForCall(0)).To(Equal(filePath))
   212  			})
   213  		})
   214  
   215  		Context("when no -p flag is supplied", func() {
   216  			BeforeEach(func() {
   217  				app := makeAppWithoutOptions("my-app2")
   218  				appSummaryRepo.GetSummarySummary = app
   219  				requirementsFactory.Application = app
   220  			})
   221  
   222  			It("creates a manifest named <app-name>_manifest.yml", func() {
   223  				runCommand("my-app2")
   224  				Ω(fakeManifest.FileSavePathArgsForCall(0)).To(Equal("./my-app2_manifest.yml"))
   225  			})
   226  		})
   227  
   228  	})
   229  })
   230  
   231  func makeAppWithOptions(appName string) models.Application {
   232  	application := models.Application{}
   233  	application.Name = appName
   234  	application.Guid = "app-guid"
   235  	application.Command = "run main.go"
   236  	application.BuildpackUrl = "go-buildpack"
   237  
   238  	domain := models.DomainFields{}
   239  	domain.Name = "example.com"
   240  
   241  	route := models.RouteSummary{Host: "foo", Domain: domain}
   242  	secondRoute := models.RouteSummary{Host: appName, Domain: domain}
   243  	packgeUpdatedAt, _ := time.Parse("2006-01-02T15:04:05Z07:00", "2012-10-24T19:54:00Z")
   244  
   245  	application.State = "started"
   246  	application.InstanceCount = 2
   247  	application.RunningInstances = 2
   248  	application.Memory = 256
   249  	application.HealthCheckTimeout = 100
   250  	application.Routes = []models.RouteSummary{route, secondRoute}
   251  	application.PackageUpdatedAt = &packgeUpdatedAt
   252  
   253  	envMap := make(map[string]interface{})
   254  	envMap["foo"] = "bar"
   255  	application.EnvironmentVars = envMap
   256  
   257  	application.Services = append(application.Services, models.ServicePlanSummary{
   258  		Guid: "",
   259  		Name: "server1",
   260  	})
   261  
   262  	return application
   263  }
   264  
   265  func makeAppWithoutOptions(appName string) models.Application {
   266  	application := models.Application{}
   267  	application.Name = appName
   268  	application.Guid = "app-guid"
   269  	packgeUpdatedAt, _ := time.Parse("2006-01-02T15:04:05Z07:00", "2012-10-24T19:54:00Z")
   270  
   271  	application.State = "started"
   272  	application.InstanceCount = 2
   273  	application.RunningInstances = 2
   274  	application.Memory = 256
   275  	application.PackageUpdatedAt = &packgeUpdatedAt
   276  
   277  	return application
   278  }
   279  
   280  func makeAppWithMultipleEnvVars(appName string) models.Application {
   281  	application := models.Application{}
   282  	application.Name = appName
   283  	application.Guid = "app-guid"
   284  	packgeUpdatedAt, _ := time.Parse("2006-01-02T15:04:05Z07:00", "2012-10-24T19:54:00Z")
   285  
   286  	application.State = "started"
   287  	application.InstanceCount = 2
   288  	application.RunningInstances = 2
   289  	application.Memory = 256
   290  	application.PackageUpdatedAt = &packgeUpdatedAt
   291  
   292  	envMap := make(map[string]interface{})
   293  	envMap["foo"] = bool(true)
   294  	envMap["abc"] = "abc"
   295  	envMap["xyz"] = bool(false)
   296  	envMap["bar"] = float64(10)
   297  	application.EnvironmentVars = envMap
   298  
   299  	return application
   300  }