forked from bluemixgaragelondon/cf-blue-green-deploy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
477 lines (405 loc) · 13.2 KB
/
main_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
package main_test
import (
"errors"
"fmt"
. "github.com/bluemixgaragelondon/cf-blue-green-deploy"
"code.cloudfoundry.org/cli/plugin"
"code.cloudfoundry.org/cli/plugin/pluginfakes"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("BGD Plugin", func() {
Describe("smoke test script", func() {
Context("when smoke test flag is not provided", func() {
It("returns empty string", func() {
args := []string{"blue-green-deploy", "appName"}
Expect(ExtractIntegrationTestScript(args)).To(Equal(""))
})
})
Context("when smoke test flag provided", func() {
It("returns flag value", func() {
args := []string{"blue-green-deploy", "appName", "--smoke-test=script/test"}
Expect(ExtractIntegrationTestScript(args)).To(Equal("script/test"))
})
})
})
Describe("blue green flow", func() {
Context("when there is a previous live app", func() {
It("calls methods in correct order", func() {
b := &BlueGreenDeployFake{liveApp: &Application{Name: "app-name-live"}}
p := CfPlugin{
Deployer: b,
}
p.Deploy("example.com", &FakeRepo{}, []string{"bgd", "app-name"})
Expect(b.flow).To(Equal([]string{
"delete old apps",
"get current live app",
"push app-name-new",
"unmap 1 routes from app-name-new",
"mapped 1 routes",
"rename app-name-live to app-name-old",
"rename app-name-new to app-name",
"unmap 0 routes from app-name-old",
}))
})
Context("with an existing live route", func() {
It("maps the live app routes to the new app", func() {
liveAppRoutes := []Route{
{Host: "host1", Domain: Domain{Name: "example.com"}},
{Host: "host2", Domain: Domain{Name: "example.com"}},
}
b := &BlueGreenDeployFake{
liveApp: &Application{Name: "app-name-live",
Routes: liveAppRoutes},
}
p := CfPlugin{
Deployer: b,
}
p.Deploy("example.com", &FakeRepo{}, []string{"bgd", "app-name"})
Expect(b.mappedRoutes).To(ConsistOf(liveAppRoutes))
})
})
Context("with an existing live route and manifest", func() {
It("maps both manifest & live app routes", func() {
liveAppRoutes := []Route{
{Host: "host1", Domain: Domain{Name: "example.com"}},
{Host: "host2", Domain: Domain{Name: "example.com"}},
}
b := &BlueGreenDeployFake{
liveApp: &Application{Name: "app-name-live",
Routes: liveAppRoutes},
}
p := CfPlugin{
Deployer: b,
}
repo := &FakeRepo{yaml: `---
name: app-name
hosts:
- man1
domains:
- example.com
`}
p.Deploy("example.com", repo, []string{"bgd", "app-name"})
expectedAppRoutes := append(liveAppRoutes, Route{Host: "man1", Domain: Domain{Name: "example.com"}})
Expect(b.mappedRoutes).To(ConsistOf(expectedAppRoutes))
})
It("maps unique routes", func() {
liveAppRoutes := []Route{
{Host: "host1", Domain: Domain{Name: "example.com"}},
{Host: "host2", Domain: Domain{Name: "example.com"}},
}
b := &BlueGreenDeployFake{
liveApp: &Application{Name: "app-name-live",
Routes: liveAppRoutes},
}
p := CfPlugin{
Deployer: b,
}
repo := &FakeRepo{yaml: `---
name: app-name
hosts:
- man1
- host1
- host2
domains:
- example.com
`}
p.Deploy("example.com", repo, []string{"bgd", "app-name"})
expectedAppRoutes := append(liveAppRoutes, Route{Host: "man1", Domain: Domain{Name: "example.com"}})
Expect(b.mappedRoutes).To(ConsistOf(expectedAppRoutes))
})
})
})
Context("when there is no previous live app", func() {
It("calls methods in correct order", func() {
b := &BlueGreenDeployFake{liveApp: nil}
p := CfPlugin{
Deployer: b,
}
p.Deploy("example.com", &FakeRepo{}, []string{"bgd", "app-name"})
Expect(b.flow).To(Equal([]string{
"delete old apps",
"get current live app",
"push app-name-new",
"unmap 1 routes from app-name-new",
"mapped 1 routes",
"rename app-name-new to app-name",
}))
})
})
Context("when app has manifest", func() {
It("maps manifest routes", func() {
b := &BlueGreenDeployFake{liveApp: nil}
p := CfPlugin{
Deployer: b,
}
repo := &FakeRepo{yaml: `---
name: app-name
hosts:
- host1
- host2
domains:
- example.com
- example.net
`}
p.Deploy("example.com", repo, []string{"bgd", "app-name"})
Expect(b.flow).To(Equal([]string{
"delete old apps",
"get current live app",
"push app-name-new",
"unmap 1 routes from app-name-new",
"mapped 4 routes",
"rename app-name-new to app-name",
}))
expectedRoutes := []Route{
{Host: "host1", Domain: Domain{Name: "example.com"}},
{Host: "host2", Domain: Domain{Name: "example.com"}},
{Host: "host1", Domain: Domain{Name: "example.net"}},
{Host: "host2", Domain: Domain{Name: "example.net"}},
}
Expect(b.mappedRoutes).To(ConsistOf(expectedRoutes))
})
Context("when no routes are specified in the manifest", func(){
It("maps the app name as the only route", func(){
b := &BlueGreenDeployFake{liveApp: nil}
p := CfPlugin{
Deployer: b,
}
repo := &FakeRepo{yaml: `---
name: app-name
hosts:
- host1
`}
p.Deploy("example.com", repo, []string{"bgd", "app-name"})
Expect(b.mappedRoutes).To(Equal([]Route{
{Host: "app-name", Domain: Domain{Name: "example.com"}},
}))
})
})
})
Context("when there is a smoke test defined", func() {
Context("when it succeeds", func() {
var (
b *BlueGreenDeployFake
p CfPlugin
)
BeforeEach(func() {
b = &BlueGreenDeployFake{liveApp: nil, passSmokeTest: true}
p = CfPlugin{
Deployer: b,
}
})
It("calls methods in correct order", func() {
p.Deploy("example.com", &FakeRepo{}, []string{"bgd", "app-name", "--smoke-test", "script/smoke-test"})
Expect(b.flow).To(Equal([]string{
"delete old apps",
"get current live app",
"push app-name-new",
"script/smoke-test app-name-new.example.com",
"unmap 1 routes from app-name-new",
"mapped 1 routes",
"rename app-name-new to app-name",
}))
})
It("returns true", func() {
result := p.Deploy("example.com", &FakeRepo{}, []string{"bgd", "app-name", "--smoke-test", "script/smoke-test"})
Expect(result).To(Equal(true))
})
})
Context("when it fails", func() {
var (
b *BlueGreenDeployFake
p CfPlugin
)
BeforeEach(func() {
b = &BlueGreenDeployFake{liveApp: nil, passSmokeTest: false}
p = CfPlugin{
Deployer: b,
}
})
It("calls methods in correct order", func() {
p.Deploy("example.com", &FakeRepo{}, []string{"bgd", "app-name", "--smoke-test", "script/smoke-test"})
Expect(b.flow).To(Equal([]string{
"delete old apps",
"get current live app",
"push app-name-new",
"script/smoke-test app-name-new.example.com",
"unmap 1 routes from app-name-new",
"rename app-name-new to app-name-failed",
}))
})
It("returns false", func() {
result := p.Deploy("example.com", &FakeRepo{}, []string{"bgd", "app-name", "--smoke-test", "script/smoke-test"})
Expect(result).To(Equal(false))
})
})
})
Describe("DefaultCfDomain", func() {
connection := &pluginfakes.FakeCliConnection{}
p := CfPlugin{Connection: connection}
Context("when CF command succeeds", func() {
It("returns CF default shared domain", func() {
connection.CliCommandWithoutTerminalOutputStub = func(args ...string) ([]string, error) {
return []string{`{
"total_results": 2,
"total_pages": 1,
"prev_url": null,
"next_url": null,
"resources": [
{
"metadata": {
"guid": "75049093-13e9-4520-80a6-2d6fea6542bc",
"url": "/v2/shared_domains/75049093-13e9-4520-80a6-2d6fea6542bc",
"created_at": "2014-10-20T09:21:39+00:00",
"updated_at": null
},
"entity": {
"name": "eu-gb.mybluemix.net"
}
}
]
}`}, nil
}
domain, _ := p.DefaultCfDomain()
Expect(domain).To(Equal("eu-gb.mybluemix.net"))
})
})
Context("when CF command fails", func() {
It("returns error", func() {
connection.CliCommandWithoutTerminalOutputStub = func(args ...string) ([]string, error) {
return nil, errors.New("cf curl failed")
}
_, err := p.DefaultCfDomain()
Expect(err).To(MatchError("cf curl failed"))
})
})
Context("when CF command returns invalid JSON", func() {
It("returns error", func() {
connection.CliCommandWithoutTerminalOutputStub = func(args ...string) ([]string, error) {
return []string{`{"resources": { "entity": "foo" }}`}, nil
}
_, err := p.DefaultCfDomain()
Expect(err).To(HaveOccurred())
})
})
})
})
Describe("Unique list of routes", func() {
p := CfPlugin{}
Context("when listA and ListB are empty", func() {
It("returns an empty list", func() {
listA := []Route{}
listB := []Route{}
Expect(p.UnionRouteLists(listA, listB)).To(BeEmpty())
})
})
Context("when listA is Empty", func() {
It("returns listB", func() {
listA := []Route{}
listB := []Route{{Host: "foo"}}
Expect(p.UnionRouteLists(listA, listB)).To(Equal(listB))
})
})
Context("when listB is Empty", func() {
It("returns listA", func() {
listA := []Route{{Host: "foo"}}
listB := []Route{}
Expect(p.UnionRouteLists(listA, listB)).To(ConsistOf(listA))
})
})
Context("when listB and listA contain the same routes", func() {
It("returns a list equal in contents to listB", func() {
listA := []Route{{Host: "foo"}}
listB := []Route{{Host: "foo"}}
Expect(p.UnionRouteLists(listA, listB)).To(ConsistOf(listA))
})
})
Context("when listB and listA contain different routes", func() {
It("returns a union of both routes", func() {
listA := []Route{{Host: "foo"}}
listB := []Route{{Host: "bar"}}
Expect(p.UnionRouteLists(listA, listB)).To(ConsistOf(append(listA, listB...)))
})
})
Context("when listA contains some routes not in listB", func() {
It("returns a union of both routes", func() {
listA := []Route{{Host: "foo"}, {Host: "bar"}}
listB := []Route{{Host: "foo"}}
Expect(p.UnionRouteLists(listA, listB)).To(ConsistOf(listA))
})
})
Context("when listB contains some routes not in listA", func() {
It("returns a union of both routes", func() {
listA := []Route{{Host: "foo"}}
listB := []Route{{Host: "foo"}, {Host: "bar"}}
Expect(p.UnionRouteLists(listA, listB)).To(ConsistOf(listB))
})
})
Context("when list A and List B contain both shared and non-shared routes", func() {
It("returns a union of both routes", func() {
listA := []Route{{Host: "shared"}, {Host: "listAOnly"}}
listB := []Route{{Host: "shared"}, {Host: "listBOnly"}}
expectedRoutes := []Route{{Host: "shared"}, {Host: "listAOnly"}, {Host: "listBOnly"}}
Expect(p.UnionRouteLists(listA, listB)).To(ConsistOf(expectedRoutes))
})
})
Context("when list A is nil", func() {
It("returns list B", func() {
listB := []Route{{Host: "foo"}}
Expect(p.UnionRouteLists(nil, listB)).To(ConsistOf(listB))
})
})
Context("when list B is nil", func() {
It("returns list A", func() {
listA := []Route{{Host: "foo"}}
Expect(p.UnionRouteLists(listA, nil)).To(ConsistOf(listA))
})
})
Context("when list A & list B are nil", func() {
It("returns an empty Array", func() {
Expect(p.UnionRouteLists(nil, nil)).To(BeEmpty())
})
})
})
})
type BlueGreenDeployFake struct {
flow []string
AppLister
liveApp *Application
passSmokeTest bool
mappedRoutes []Route
}
func (p *BlueGreenDeployFake) Setup(connection plugin.CliConnection) {
p.flow = append(p.flow, "setup")
}
func (p *BlueGreenDeployFake) PushNewApp(appName string, route Route) {
p.flow = append(p.flow, fmt.Sprintf("push %s", appName))
}
func (p *BlueGreenDeployFake) DeleteAllAppsExceptLiveApp(string) {
p.flow = append(p.flow, "delete old apps")
}
func (p *BlueGreenDeployFake) LiveApp(string) (string, []Route) {
p.flow = append(p.flow, "get current live app")
if p.liveApp == nil {
return "", nil
} else {
return p.liveApp.Name, p.liveApp.Routes
}
}
func (p *BlueGreenDeployFake) RunSmokeTests(script string, fqdn string) bool {
p.flow = append(p.flow, fmt.Sprintf("%s %s", script, fqdn))
return p.passSmokeTest
}
func (p *BlueGreenDeployFake) RemapRoutesFromLiveAppToNewApp(liveApp Application, newApp Application) {
p.flow = append(p.flow, fmt.Sprintf("remap routes from %s to %s", liveApp.Name, newApp.Name))
}
func (p *BlueGreenDeployFake) RenameApp(app string, newName string) {
p.flow = append(p.flow, fmt.Sprintf("rename %s to %s", app, newName))
}
func (p *BlueGreenDeployFake) MapRoutesToApp(appName string, routes ...Route) {
p.mappedRoutes = routes
p.flow = append(p.flow, fmt.Sprintf("mapped %d routes", len(routes)))
}
func (p *BlueGreenDeployFake) UnmapRoutesFromApp(oldAppName string, routes ...Route) {
p.flow = append(p.flow, fmt.Sprintf("unmap %d routes from %s", len(routes), oldAppName))
}