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