forked from bluemixgaragelondon/cf-blue-green-deploy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blue_green_deploy_test.go
517 lines (430 loc) · 14.3 KB
/
blue_green_deploy_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
package main_test
import (
"bytes"
"errors"
"strings"
"code.cloudfoundry.org/cli/plugin/models"
"code.cloudfoundry.org/cli/plugin/pluginfakes"
. "github.com/bluemixgaragelondon/cf-blue-green-deploy"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("BlueGreenDeploy", func() {
var (
bgdExitsWithErrors []error
bgdOut *bytes.Buffer
connection *pluginfakes.FakeCliConnection
p BlueGreenDeploy
testErrorFunc func(message string, err error)
)
BeforeEach(func() {
bgdExitsWithErrors = []error{}
testErrorFunc = func(message string, err error) {
bgdExitsWithErrors = append(bgdExitsWithErrors, err)
}
bgdOut = &bytes.Buffer{}
connection = &pluginfakes.FakeCliConnection{}
p = BlueGreenDeploy{Connection: connection, ErrorFunc: testErrorFunc, Out: bgdOut}
})
Describe("maps routes", func() {
var (
manifestApp plugin_models.GetAppModel
)
BeforeEach(func() {
manifestApp = plugin_models.GetAppModel{
Name: "new",
Routes: []plugin_models.GetApp_RouteSummary{
{Host: "host", Domain: plugin_models.GetApp_DomainFields{Name: "example.com"}},
{Host: "host", Domain: plugin_models.GetApp_DomainFields{Name: "example.net"}},
},
}
})
It("maps all", func() {
p.MapRoutesToApp(manifestApp.Name, manifestApp.Routes...)
cfCommands := getAllCfCommands(connection)
Expect(cfCommands).To(Equal([]string{
"map-route new example.com -n host",
"map-route new example.net -n host",
}))
})
})
Describe("remove routes from old app", func() {
var (
oldApp plugin_models.GetAppModel
)
BeforeEach(func() {
oldApp = plugin_models.GetAppModel{
Name: "old",
Routes: []plugin_models.GetApp_RouteSummary{
{Host: "live", Domain: plugin_models.GetApp_DomainFields{Name: "mybluemix.net"}},
{Host: "live", Domain: plugin_models.GetApp_DomainFields{Name: "example.com"}},
},
}
})
It("unmaps all routes from the old app", func() {
p.UnmapRoutesFromApp(oldApp.Name, oldApp.Routes...)
cfCommands := getAllCfCommands(connection)
Expect(cfCommands).To(Equal([]string{
"unmap-route old mybluemix.net -n live",
"unmap-route old example.com -n live",
}))
})
})
Describe("renaming an app", func() {
var app string
BeforeEach(func() {
app = "foo"
})
It("renames the app", func() {
p.RenameApp(app, "bar")
cfCommands := getAllCfCommands(connection)
Expect(cfCommands).To(ContainElement(
"rename foo bar",
))
})
Context("when renaming the app fails", func() {
It("calls the error callback", func() {
connection.CliCommandStub = func(args ...string) ([]string, error) {
return nil, errors.New("failed to rename app")
}
p.RenameApp(app, "bar")
Expect(bgdExitsWithErrors[0]).To(MatchError("failed to rename app"))
})
})
})
Describe("delete old apps", func() {
var (
apps []plugin_models.GetAppsModel
)
Context("with live and old apps", func() {
BeforeEach(func() {
apps = []plugin_models.GetAppsModel{
{Name: "app-name-old"},
{Name: "app-name"},
}
connection.GetAppsReturns(apps, nil)
})
It("only deletes the old apps", func() {
p.DeleteAllAppsExceptLiveApp("app-name")
cfCommands := getAllCfCommands(connection)
Expect(cfCommands).To(Equal([]string{
"delete app-name-old -f -r",
}))
})
Context("when the deletion of an app fails", func() {
BeforeEach(func() {
connection.CliCommandStub = func(args ...string) ([]string, error) {
return nil, errors.New("failed to delete app")
}
})
It("returns an error", func() {
p.DeleteAllAppsExceptLiveApp("app-name")
Expect(bgdExitsWithErrors[0]).To(HaveOccurred())
})
})
})
Context("with live and failed apps", func() {
BeforeEach(func() {
apps = []plugin_models.GetAppsModel{
{Name: "app-name-failed"},
{Name: "app-name"},
}
connection.GetAppsReturns(apps, nil)
})
It("only deletes the failed apps", func() {
p.DeleteAllAppsExceptLiveApp("app-name")
cfCommands := getAllCfCommands(connection)
Expect(cfCommands).To(Equal([]string{
"delete app-name-failed -f -r",
}))
})
})
Context("with live and new apps", func() {
BeforeEach(func() {
apps = []plugin_models.GetAppsModel{
{Name: "app-name-new"},
{Name: "app-name"},
}
connection.GetAppsReturns(apps, nil)
})
It("only deletes the new apps", func() {
p.DeleteAllAppsExceptLiveApp("app-name")
cfCommands := getAllCfCommands(connection)
Expect(cfCommands).To(Equal([]string{
"delete app-name-new -f -r",
}))
})
})
Context("when there is no old version deployed", func() {
BeforeEach(func() {
apps = []plugin_models.GetAppsModel{
{Name: "app-name"},
}
connection.GetAppsReturns(apps, nil)
})
It("succeeds", func() {
p.DeleteAllAppsExceptLiveApp("app-name")
Expect(bgdExitsWithErrors).To(HaveLen(0))
})
It("deletes nothing", func() {
p.DeleteAllAppsExceptLiveApp("app-name")
Expect(connection.CliCommandCallCount()).To(Equal(0))
})
})
})
Describe("deleting apps", func() {
Context("when there is an old version deployed", func() {
apps := []plugin_models.GetAppsModel{
{Name: "app-name-old"},
{Name: "app-name-old"},
}
It("deletes the apps", func() {
p.DeleteAppVersions(apps)
cfCommands := getAllCfCommands(connection)
Expect(cfCommands).To(Equal([]string{
"delete app-name-old -f -r",
"delete app-name-old -f -r",
}))
})
Context("when the deletion of an app fails", func() {
BeforeEach(func() {
connection.CliCommandStub = func(args ...string) ([]string, error) {
return nil, errors.New("failed to delete app")
}
})
It("returns an error", func() {
p.DeleteAppVersions(apps)
Expect(bgdExitsWithErrors[0]).To(HaveOccurred())
})
})
})
Context("when there is no old version deployed", func() {
apps := []plugin_models.GetAppsModel{}
It("succeeds", func() {
p.DeleteAppVersions(apps)
Expect(bgdExitsWithErrors).To(HaveLen(0))
})
It("deletes nothing", func() {
p.DeleteAppVersions(apps)
Expect(connection.CliCommandCallCount()).To(Equal(0))
})
})
})
Describe("getting the scale parameters", func() {
Context("for a running app", func() {
appName := "existing app"
var instanceCount int = 3
var memory int64 = 9001
var diskQuota int64 = 100
BeforeEach(func() {
appModel := plugin_models.GetAppModel{
InstanceCount: instanceCount,
Memory: memory,
DiskQuota: diskQuota,
}
connection.GetAppReturns(appModel, nil)
})
It("reads the app data and returns the scale parameters", func() {
scaleParameters, _ := p.GetScaleParameters(appName)
Expect(scaleParameters.InstanceCount).To(Equal(instanceCount))
Expect(scaleParameters.Memory).To(Equal(memory))
Expect(scaleParameters.DiskQuota).To(Equal(diskQuota))
})
})
Context("for an app that does not exist", func() {
appName := "invalid app"
BeforeEach(func() {
appModel := plugin_models.GetAppModel{}
connection.GetAppReturns(appModel, errors.New("App was not found"))
})
It("returns an empty struct and an error value", func() {
scaleParameters, error := p.GetScaleParameters(appName)
Expect(error).ToNot(Equal(nil))
Expect(scaleParameters.InstanceCount).To(Equal(0))
Expect(scaleParameters.Memory).To(Equal(int64(0)))
Expect(scaleParameters.DiskQuota).To(Equal(int64(0)))
})
})
})
Describe("pushing a new app", func() {
newApp := "app-name-new"
newRoute := plugin_models.GetApp_RouteSummary{Host: newApp, Domain: plugin_models.GetApp_DomainFields{Name: "example.com"}}
scaleParameters := ScaleParameters{}
It("pushes an app with new appended to its name", func() {
p.PushNewApp(newApp, newRoute, "", scaleParameters)
Expect(strings.Join(connection.CliCommandArgsForCall(0), " ")).
To(MatchRegexp(`^push app-name-new`))
})
It("uses the generated name for the route", func() {
p.PushNewApp(newApp, newRoute, "", scaleParameters)
Expect(strings.Join(connection.CliCommandArgsForCall(0), " ")).
To(MatchRegexp(`-n app-name-new`))
})
It("pushes with the default cf domain", func() {
p.PushNewApp(newApp, newRoute, "", scaleParameters)
Expect(strings.Join(connection.CliCommandArgsForCall(0), " ")).
To(MatchRegexp(`-d example.com`))
})
It("pushes with the specified manifest, if present", func() {
manifestPath := "./manifest-tst.yml"
p.PushNewApp(newApp, newRoute, manifestPath, scaleParameters)
Expect(strings.Join(connection.CliCommandArgsForCall(0), " ")).
To(MatchRegexp(`-f ./manifest-tst.yml`))
})
It("pushes without a manifest arg, if no manifest in deployer", func() {
p.PushNewApp(newApp, newRoute, "", scaleParameters)
Expect(strings.Join(connection.CliCommandArgsForCall(0), " ")).
To(Not(MatchRegexp(`-f `)))
})
It("pushes using the scale values of the old app", func() {
liveAppModel := plugin_models.GetAppModel{
Memory: int64(32),
DiskQuota: int64(700),
InstanceCount: 27,
}
connection.GetAppReturns(liveAppModel, nil)
p.PushNewApp(newApp, newRoute, "", ScaleParameters{})
commandString := strings.Join(connection.CliCommandArgsForCall(0), " ")
Expect(commandString).To(MatchRegexp(`-m 32M`))
Expect(commandString).To(MatchRegexp(`-k 700M`))
Expect(commandString).To(MatchRegexp(`-i 27`))
})
It("uses the manifest memory field if there is a live app running", func() {
liveAppModel := plugin_models.GetAppModel{
Memory: int64(16),
DiskQuota: int64(500),
InstanceCount: 6,
}
connection.GetAppReturns(liveAppModel, nil)
manifestScaleParameters := ScaleParameters{
Memory: int64(32),
}
p.PushNewApp(newApp, newRoute, "", manifestScaleParameters)
commandString := strings.Join(connection.CliCommandArgsForCall(0), " ")
Expect(commandString).To(MatchRegexp(`-m 32M`))
Expect(commandString).To(MatchRegexp(`-k 500M`))
Expect(commandString).To(MatchRegexp(`-i 6`))
})
Context("when some scale parameter values are zero", func() {
It("pushes using only the defined parameters", func() {
scaleParameters = ScaleParameters{
InstanceCount: 0,
Memory: 32,
DiskQuota: 0,
}
p.PushNewApp(newApp, newRoute, "", scaleParameters)
commandString := strings.Join(connection.CliCommandArgsForCall(0), " ")
Expect(commandString).To(MatchRegexp(`-m`))
Expect(commandString).ToNot(MatchRegexp(`-k`))
Expect(commandString).ToNot(MatchRegexp(`-i`))
})
})
Context("when the push fails", func() {
BeforeEach(func() {
connection.CliCommandStub = func(args ...string) ([]string, error) {
return nil, errors.New("failed to push app")
}
})
It("returns an error", func() {
p.PushNewApp(newApp, newRoute, "", scaleParameters)
Expect(bgdExitsWithErrors[0]).To(MatchError("failed to push app"))
})
})
})
Describe("live app", func() {
liveApp := plugin_models.GetAppModel{Name: "app-name"}
Context("with live and old apps", func() {
It("returns the live app", func() {
connection.GetAppReturns(liveApp, nil)
name, _ := p.LiveApp("app-name")
Expect(name).To(Equal(liveApp.Name))
})
})
Context("with no apps", func() {
It("returns an empty app name", func() {
connection.GetAppReturns(plugin_models.GetAppModel{}, errors.New("an error for no apps"))
name, _ := p.LiveApp("app-name")
Expect(name).To(BeEmpty())
})
})
})
Describe("app filter", func() {
Context("when there are 2 old versions and 1 non-old version", func() {
var (
appList []plugin_models.GetAppsModel
oldApps []plugin_models.GetAppsModel
)
BeforeEach(func() {
appList = []plugin_models.GetAppsModel{
{Name: "foo-old"},
{Name: "foo-old"},
{Name: "foo"},
{Name: "bar-foo-old"},
{Name: "foo-older"},
}
oldApps = p.GetOldApps("foo", appList)
})
Describe("old app list", func() {
It("returns all apps that have the same name, with a valid timestamp and -old suffix", func() {
Expect(oldApps).To(ContainElement(appList[0]))
Expect(oldApps).To(ContainElement(appList[1]))
})
It("doesn't return any apps that don't have a -old suffix", func() {
Expect(oldApps).ToNot(ContainElement(appList[2]))
})
It("doesn't return elements that have an additional prefix before the app name", func() {
Expect(oldApps).ToNot(ContainElement(appList[3]))
})
It("doesn't return elements that have an additional suffix after -old", func() {
Expect(oldApps).ToNot(ContainElement(appList[4]))
})
})
})
})
Describe("smoke test runner", func() {
It("returns stdout", func() {
_ = p.RunSmokeTests("test/support/smoke-test-script", "app.mybluemix.net")
Expect(bgdOut.String()).To(ContainSubstring("STDOUT"))
})
It("returns stderr", func() {
_ = p.RunSmokeTests("test/support/smoke-test-script", "app.mybluemix.net")
Expect(bgdOut.String()).To(ContainSubstring("STDERR"))
})
It("passes app FQDN as first argument", func() {
_ = p.RunSmokeTests("test/support/smoke-test-script", "app.mybluemix.net")
Expect(bgdOut.String()).To(ContainSubstring("App FQDN is: app.mybluemix.net"))
})
Context("when script doesn't exist", func() {
It("fails with useful error", func() {
_ = p.RunSmokeTests("inexistent-smoke-test-script", "app.mybluemix.net")
Expect(bgdExitsWithErrors[0].Error()).To(ContainSubstring("executable file not found"))
})
})
Context("when script isn't executable", func() {
It("fails with useful error", func() {
_ = p.RunSmokeTests("test/support/nonexec-smoke-test-script", "app.mybluemix.net")
Expect(bgdExitsWithErrors[0].Error()).To(ContainSubstring("permission denied"))
})
})
Context("when script fails", func() {
var passSmokeTest bool
BeforeEach(func() {
passSmokeTest = p.RunSmokeTests("test/support/smoke-test-script", "FORCE-SMOKE-TEST-FAILURE")
})
It("returns false", func() {
Expect(passSmokeTest).To(Equal(false))
})
It("doesn't fail", func() {
Expect(bgdExitsWithErrors).To(HaveLen(0))
})
})
})
})
func getAllCfCommands(connection *pluginfakes.FakeCliConnection) (commands []string) {
commands = []string{}
for i := 0; i < connection.CliCommandCallCount(); i++ {
args := connection.CliCommandArgsForCall(i)
commands = append(commands, strings.Join(args, " "))
}
return
}