-
Notifications
You must be signed in to change notification settings - Fork 171
Expand file tree
/
Copy pathdeploy_test.go
More file actions
464 lines (365 loc) · 12.8 KB
/
deploy_test.go
File metadata and controls
464 lines (365 loc) · 12.8 KB
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
package cmd_test
import (
"errors"
"time"
"github.com/cppforlife/go-patch/patch"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/cloudfoundry/bosh-cli/v7/cmd"
fakecmd "github.com/cloudfoundry/bosh-cli/v7/cmd/cmdfakes"
"github.com/cloudfoundry/bosh-cli/v7/cmd/opts"
boshdir "github.com/cloudfoundry/bosh-cli/v7/director"
fakedir "github.com/cloudfoundry/bosh-cli/v7/director/directorfakes"
boshtpl "github.com/cloudfoundry/bosh-cli/v7/director/template"
fakeui "github.com/cloudfoundry/bosh-cli/v7/ui/fakes"
)
var _ = Describe("DeployCmd", func() {
var (
ui *fakeui.FakeUI
deployment *fakedir.FakeDeployment
releaseUploader *fakecmd.FakeReleaseUploader
director *fakedir.FakeDirector
command cmd.DeployCmd
)
BeforeEach(func() {
ui = &fakeui.FakeUI{}
deployment = &fakedir.FakeDeployment{
NameStub: func() string { return "dep" },
}
releaseUploader = &fakecmd.FakeReleaseUploader{
UploadReleasesStub: func(bytes []byte) ([]byte, error) { return bytes, nil },
}
director = &fakedir.FakeDirector{}
command = cmd.NewDeployCmd(ui, deployment, releaseUploader, director)
})
Describe("Run", func() {
var (
deployOpts opts.DeployOpts
)
BeforeEach(func() {
deployOpts = opts.DeployOpts{
Args: opts.DeployArgs{
Manifest: opts.FileBytesArg{Bytes: []byte("name: dep")},
},
}
})
act := func() error { return command.Run(deployOpts) }
It("deploys manifest", func() {
err := act()
Expect(err).ToNot(HaveOccurred())
Expect(deployment.UpdateCallCount()).To(Equal(1))
bytes, updateOpts := deployment.UpdateArgsForCall(0)
Expect(bytes).To(Equal([]byte("name: dep\n")))
Expect(updateOpts).To(Equal(boshdir.UpdateOpts{}))
})
It("deploys manifest allowing to recreate, recreate persistent disks, fix, and skip drain", func() {
deployOpts.RecreatePersistentDisks = true
deployOpts.Recreate = true
deployOpts.Fix = true
deployOpts.SkipDrain = boshdir.SkipDrains{boshdir.SkipDrain{All: true}}
err := act()
Expect(err).ToNot(HaveOccurred())
Expect(deployment.UpdateCallCount()).To(Equal(1))
bytes, updateOpts := deployment.UpdateArgsForCall(0)
Expect(bytes).To(Equal([]byte("name: dep\n")))
Expect(updateOpts).To(Equal(boshdir.UpdateOpts{
RecreatePersistentDisks: true,
Recreate: true,
Fix: true,
SkipDrain: boshdir.SkipDrains{boshdir.SkipDrain{All: true}},
}))
})
It("deploys manifest allowing to recreate VMs created before a timestamp", func() {
deployOpts.Recreate = true
deployOpts.RecreateVMsCreatedBefore = opts.TimeArg{Time: time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)}
err := act()
Expect(err).ToNot(HaveOccurred())
Expect(deployment.UpdateCallCount()).To(Equal(1))
bytes, updateOpts := deployment.UpdateArgsForCall(0)
Expect(bytes).To(Equal([]byte("name: dep\n")))
Expect(updateOpts).To(Equal(boshdir.UpdateOpts{
Recreate: true,
RecreateVMsCreatedBefore: time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC),
}))
})
It("deploys manifest allowing to dry_run", func() {
deployOpts.DryRun = true
err := act()
Expect(err).ToNot(HaveOccurred())
Expect(deployment.UpdateCallCount()).To(Equal(1))
bytes, updateOpts := deployment.UpdateArgsForCall(0)
Expect(bytes).To(Equal([]byte("name: dep\n")))
Expect(updateOpts).To(Equal(boshdir.UpdateOpts{
DryRun: true,
}))
})
It("deploys manifest allowing to force latest variables", func() {
deployOpts.ForceLatestVariables = true
err := act()
Expect(err).ToNot(HaveOccurred())
Expect(deployment.UpdateCallCount()).To(Equal(1))
bytes, updateOpts := deployment.UpdateArgsForCall(0)
Expect(bytes).To(Equal([]byte("name: dep\n")))
Expect(updateOpts).To(Equal(boshdir.UpdateOpts{
ForceLatestVariables: true,
}))
})
It("deploys templated manifest", func() {
deployOpts.Args.Manifest = opts.FileBytesArg{
Bytes: []byte("name: dep\nname1: ((name1))\nname2: ((name2))\n"),
}
deployOpts.VarKVs = []boshtpl.VarKV{
{Name: "name1", Value: "val1-from-kv"},
}
deployOpts.VarsFiles = []boshtpl.VarsFileArg{
{Vars: boshtpl.StaticVariables(map[string]interface{}{"name1": "val1-from-file"})},
{Vars: boshtpl.StaticVariables(map[string]interface{}{"name2": "val2-from-file"})},
}
deployOpts.OpsFiles = []opts.OpsFileArg{
{
Ops: patch.Ops([]patch.Op{
patch.ReplaceOp{Path: patch.MustNewPointerFromString("/xyz?"), Value: "val"},
}),
},
}
err := act()
Expect(err).ToNot(HaveOccurred())
Expect(deployment.UpdateCallCount()).To(Equal(1))
bytes, _ := deployment.UpdateArgsForCall(0)
Expect(bytes).To(Equal([]byte("name: dep\nname1: val1-from-kv\nname2: val2-from-file\nxyz: val\n")))
})
It("does not deploy if name specified in the manifest does not match deployment's name", func() {
deployOpts.Args.Manifest = opts.FileBytesArg{
Bytes: []byte("name: other-name"),
}
err := act()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(Equal(
"Expected manifest to specify deployment name 'dep' but was 'other-name'"))
Expect(deployment.UpdateCallCount()).To(Equal(0))
})
It("uploads releases provided in the manifest after manifest has been interpolated", func() {
deployOpts.Args.Manifest = opts.FileBytesArg{
Bytes: []byte("name: dep\nbefore-upload-manifest: ((key))"),
}
deployOpts.VarKVs = []boshtpl.VarKV{
{Name: "key", Value: "key-val"},
}
releaseUploader.UploadReleasesReturns([]byte("after-upload-manifest"), nil)
err := act()
Expect(err).ToNot(HaveOccurred())
bytes := releaseUploader.UploadReleasesArgsForCall(0)
Expect(bytes).To(Equal([]byte("before-upload-manifest: key-val\nname: dep\n")))
Expect(deployment.UpdateCallCount()).To(Equal(1))
bytes, _ = deployment.UpdateArgsForCall(0)
Expect(bytes).To(Equal([]byte("after-upload-manifest")))
})
It("uploads releases provided in the manifest with fix after manifest has been interpolated", func() {
deployOpts.Args.Manifest = opts.FileBytesArg{
Bytes: []byte("name: dep\nbefore-upload-manifest-with-fix: ((key))"),
}
deployOpts.VarKVs = []boshtpl.VarKV{
{Name: "key", Value: "key-val"},
}
deployOpts.FixReleases = true
releaseUploader.UploadReleasesWithFixReturns([]byte("after-upload-manifest-with-fix"), nil)
err := act()
Expect(err).ToNot(HaveOccurred())
bytes := releaseUploader.UploadReleasesWithFixArgsForCall(0)
Expect(bytes).To(Equal([]byte("before-upload-manifest-with-fix: key-val\nname: dep\n")))
Expect(deployment.UpdateCallCount()).To(Equal(1))
bytes, _ = deployment.UpdateArgsForCall(0)
Expect(bytes).To(Equal([]byte("after-upload-manifest-with-fix")))
})
It("skips the upload of all releases in the corresponding deployment", func() {
deployOpts.SkipUploadReleases = true
err := act()
Expect(err).ToNot(HaveOccurred())
Expect(releaseUploader.UploadReleasesWithFixCallCount()).To(Equal(0))
Expect(releaseUploader.UploadReleasesCallCount()).To(Equal(0))
Expect(ui.Said).To(ContainElement("Release-Check skipped."))
})
It("returns error and does not deploy if uploading releases fails", func() {
deployOpts.Args.Manifest = opts.FileBytesArg{
Bytes: []byte(`
name: dep
releases:
- name: capi
sha1: capi-sha1
url: https://capi-url
version: 1+capi
`),
}
releaseUploader.UploadReleasesReturns(nil, errors.New("fake-err"))
err := act()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("fake-err"))
Expect(deployment.UpdateCallCount()).To(Equal(0))
})
It("uploads releases but does not deploy if confirmation is rejected", func() {
deployOpts.Args.Manifest = opts.FileBytesArg{
Bytes: []byte(`
name: dep
releases:
- name: capi
sha1: capi-sha1
url: /capi-url
version: create
`),
}
ui.AskedConfirmationErr = errors.New("stop")
err := act()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("stop"))
Expect(releaseUploader.UploadReleasesCallCount()).To(Equal(1))
Expect(deployment.UpdateCallCount()).To(Equal(0))
})
It("returns an error if diffing failed", func() {
deployment.DiffReturns(boshdir.DeploymentDiff{}, errors.New("Fetching diff result"))
err := act()
Expect(err).To(HaveOccurred())
})
It("gets the diff from the deployment", func() {
diff := [][]interface{}{
[]interface{}{"some line that stayed", nil},
[]interface{}{"some line that was added", "added"},
[]interface{}{"some line that was removed", "removed"},
}
expectedDiff := boshdir.NewDeploymentDiff(diff, nil)
deployment.DiffReturns(expectedDiff, nil)
err := act()
Expect(err).ToNot(HaveOccurred())
Expect(deployment.DiffCallCount()).To(Equal(1))
Expect(ui.Said).To(ContainElement(" some line that stayed\n"))
Expect(ui.Said).To(ContainElement("+ some line that was added\n"))
Expect(ui.Said).To(ContainElement("- some line that was removed\n"))
})
It("deploys manifest with diff context", func() {
context := map[string]interface{}{
"cloud_config_id": 2,
"runtime_config_id": 3,
}
expectedDiff := boshdir.NewDeploymentDiff(nil, context)
deployment.DiffReturns(expectedDiff, nil)
err := act()
Expect(err).ToNot(HaveOccurred())
Expect(deployment.DiffCallCount()).To(Equal(1))
_, updateOptions := deployment.UpdateArgsForCall(0)
Expect(updateOptions.Diff).To(Equal(expectedDiff))
})
It("returns error if deploying failed", func() {
deployment.UpdateReturns(errors.New("fake-err"))
err := act()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("fake-err"))
})
It("overwrites the deployOpts with the flags from configs of type deploy", func() {
configs := []boshdir.Config{
{
ID: "1",
Name: "default", Type: "deploy",
CreatedAt: "0000",
Team: "",
Content: "flags:\n - fix",
Current: true,
},
}
director.ListConfigsReturns(configs, nil)
err := act()
Expect(err).ToNot(HaveOccurred())
Expect(deployment.UpdateCallCount()).To(Equal(1))
_, updateOpts := deployment.UpdateArgsForCall(0)
Expect(updateOpts).To(Equal(boshdir.UpdateOpts{
Fix: true,
}))
})
It("overwrites the deployOpts with the flags from configs of type deploy if the deployment is included", func() {
configs := []boshdir.Config{
{
ID: "1",
Name: "default",
Type: "deploy",
CreatedAt: "0000",
Team: "",
Content: "flags:\n - fix\ninclude:\n - dep",
Current: true,
},
}
director.ListConfigsReturns(configs, nil)
deployment.NameReturns("dep")
err := act()
Expect(err).ToNot(HaveOccurred())
Expect(deployment.UpdateCallCount()).To(Equal(1))
_, updateOpts := deployment.UpdateArgsForCall(0)
Expect(updateOpts).To(Equal(boshdir.UpdateOpts{
Fix: true,
}))
})
It("does not overwrite the deployOpts with the flags from configs of type deploy if the deployment is not included", func() {
configs := []boshdir.Config{
{
ID: "1",
Name: "default",
Type: "deploy",
CreatedAt: "0000",
Team: "",
Content: "flags:\n - fix\ninclude:\n - foo",
Current: true,
},
}
director.ListConfigsReturns(configs, nil)
deployment.NameReturns("dep")
err := act()
Expect(err).ToNot(HaveOccurred())
Expect(deployment.UpdateCallCount()).To(Equal(1))
_, updateOpts := deployment.UpdateArgsForCall(0)
Expect(updateOpts).To(Equal(boshdir.UpdateOpts{
Fix: false,
}))
})
It("does not overwrite the deployOpts with the flags from configs of type deploy if the deployment is excluded", func() {
configs := []boshdir.Config{
{
ID: "1",
Name: "default",
Type: "deploy",
CreatedAt: "0000",
Team: "",
Content: "flags:\n - fix\nexclude:\n - dep",
Current: true,
},
}
director.ListConfigsReturns(configs, nil)
deployment.NameReturns("dep")
err := act()
Expect(err).ToNot(HaveOccurred())
Expect(deployment.UpdateCallCount()).To(Equal(1))
_, updateOpts := deployment.UpdateArgsForCall(0)
Expect(updateOpts).To(Equal(boshdir.UpdateOpts{
Fix: false,
}))
})
It("overwrites the deployOpts with the flags from configs of type deploy if the deployment is not excluded", func() {
configs := []boshdir.Config{
{
ID: "1",
Name: "default",
Type: "deploy",
CreatedAt: "0000",
Team: "",
Content: "flags:\n - fix\nexclude:\n - foo",
Current: true,
},
}
director.ListConfigsReturns(configs, nil)
deployment.NameReturns("dep")
err := act()
Expect(err).ToNot(HaveOccurred())
Expect(deployment.UpdateCallCount()).To(Equal(1))
_, updateOpts := deployment.UpdateArgsForCall(0)
Expect(updateOpts).To(Equal(boshdir.UpdateOpts{
Fix: true,
}))
})
})
})