This repository has been archived by the owner on Aug 22, 2018. It is now read-only.
/
out_spec.rb
358 lines (301 loc) · 10.7 KB
/
out_spec.rb
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
require "spec_helper"
require "digest"
require "fileutils"
require "json"
require "open3"
require "tmpdir"
require "stringio"
describe "Out Command" do
let(:manifest) { instance_double(BoshDeploymentResource::BoshManifest, fallback_director_uuid: nil, use_stemcell: nil, use_release: nil, name: "bosh-deployment", validate_stemcells: nil, shasum: "1234") }
let(:bosh) { instance_double(BoshDeploymentResource::Bosh, upload_stemcell: nil, upload_release: nil, deploy: nil, director_uuid: "some-director-uuid", target: "bosh-target") }
let(:response) { StringIO.new }
let(:command) { BoshDeploymentResource::OutCommand.new(bosh, manifest, response) }
let(:written_manifest) do
file = Tempfile.new("bosh_manifest")
file.write("hello world")
file.close
file
end
def touch(*paths)
path = File.join(paths)
FileUtils.mkdir_p(File.dirname(path))
FileUtils.touch(path)
end
def cp(src, *paths)
path = File.join(paths)
FileUtils.mkdir_p(File.dirname(path))
FileUtils.cp(src, path)
end
def in_dir
Dir.mktmpdir do |working_dir|
yield working_dir
end
end
def add_default_artefacts(working_dir)
cp "spec/fixtures/stemcell.tgz", working_dir, "stemcells", "stemcell.tgz"
cp "spec/fixtures/stemcell.tgz", working_dir, "stemcells", "other-stemcell.tgz"
touch working_dir, "stemcells", "not-stemcell.txt"
cp "spec/fixtures/release.tgz", working_dir, "releases", "release.tgz"
cp "spec/fixtures/release.tgz", working_dir, "releases", "other-release.tgz"
touch working_dir, "releases", "not-release.txt"
end
before do
allow(manifest).to receive(:write!).and_return(written_manifest)
end
let(:request) {
{
"source" => {
"target" => "http://bosh.example.com",
"username" => "bosh-username",
"password" => "bosh-password",
"deployment" => "bosh-deployment",
},
"params" => {
"manifest" => "manifest/deployment.yml",
"stemcells" => [
"stemcells/*.tgz"
],
"releases" => [
"releases/*.tgz"
]
}
}
}
context "with valid inputs" do
it "uploads matching stemcells to the director" do
in_dir do |working_dir|
add_default_artefacts working_dir
expect(bosh).to receive(:upload_stemcell).
with(File.join(working_dir, "stemcells", "stemcell.tgz"))
expect(bosh).to receive(:upload_stemcell).
with(File.join(working_dir, "stemcells", "other-stemcell.tgz"))
command.run(working_dir, request)
end
end
it "emits the version as the manifest_sha1 and target" do
in_dir do |working_dir|
add_default_artefacts working_dir
command.run(working_dir, request)
expect(JSON.parse(response.string)["version"]).to eq({
"manifest_sha1" => manifest.shasum,
"target" => "bosh-target",
})
end
end
it "emits the release/stemcell versions in the metadata" do
in_dir do |working_dir|
add_default_artefacts working_dir
command.run(working_dir, request)
expect(JSON.parse(response.string)["metadata"]).to eq([
{"name" => "stemcell", "value" => "bosh-aws-xen-hvm-ubuntu-trusty-go_agent v2905"},
{"name" => "stemcell", "value" => "bosh-aws-xen-hvm-ubuntu-trusty-go_agent v2905"},
{"name" => "release", "value" => "concourse v0.43.0"},
{"name" => "release", "value" => "concourse v0.43.0"}
])
end
end
it "handles overlapping stemcell globs by removing duplication" do
request.fetch("params").store("stemcells", [
"stemcells/*.tgz",
"stemcells/*.tgz"
])
in_dir do |working_dir|
add_default_artefacts working_dir
expect(bosh).to receive(:upload_stemcell).exactly(2).times
command.run(working_dir, request)
end
end
it "uploads matching releases to the director" do
in_dir do |working_dir|
add_default_artefacts working_dir
expect(bosh).to receive(:upload_release).
with(File.join(working_dir, "releases", "release.tgz"))
expect(bosh).to receive(:upload_release).
with(File.join(working_dir, "releases", "other-release.tgz"))
command.run(working_dir, request)
end
end
it "handles overlapping release globs by removing duplication" do
request.fetch("params").store("releases", [
"releases/*.tgz",
"releases/*.tgz"
])
in_dir do |working_dir|
add_default_artefacts working_dir
expect(bosh).to receive(:upload_release).exactly(2).times
command.run(working_dir, request)
end
end
it "generates a new manifest (with locked down versions and a defaulted director uuid) and deploys it" do
in_dir do |working_dir|
add_default_artefacts working_dir
expect(bosh).to receive(:director_uuid).and_return("abcdef")
expect(manifest).to receive(:use_release)
expect(manifest).to receive(:use_stemcell)
expect(manifest).to receive(:fallback_director_uuid).with("abcdef")
expect(bosh).to receive(:deploy).with(written_manifest.path,false)
command.run(working_dir, request)
end
end
it "does NOT run a bosh cleanup when the cleanup parameter is NOT passed" do
in_dir do |working_dir|
add_default_artefacts working_dir
expect(bosh).not_to receive(:cleanup)
command.run(working_dir, request)
end
end
it "runs a bosh cleanup when the cleanup parameter is set to true" do
request.fetch("params").store("cleanup", true)
in_dir do |working_dir|
add_default_artefacts working_dir
expect(bosh).to receive(:cleanup)
command.run(working_dir, request)
end
end
it "runs a bosh no-redact when the no_redact parameter is set to true" do
request.fetch("params").store("no_redact", true)
in_dir do |working_dir|
add_default_artefacts working_dir
expect(bosh).to receive(:deploy).with(anything,true)
command.run(working_dir, request)
end
end
end
context "with invalid inputs" do
it "errors if the given deployment name and the name in the manifest do not match" do
allow(manifest).to receive(:name).and_return("other-name")
in_dir do |working_dir|
expect do
command.run(working_dir, {
"source" => {
"target" => "http://bosh.example.com",
"username" => "bosh-user",
"password" => "bosh-password",
"deployment" => "bosh-deployment",
},
"params" => {
"manifest" => "deployment.yml",
"stemcells" => [],
"releases" => []
}
})
end.to raise_error /given deployment name 'bosh-deployment' does not match manifest name 'other-name'/
end
end
it "errors when provided stemcells cannot be validated against the manifest" do
allow(manifest).to receive(:validate_stemcells).and_raise("invalid")
in_dir do |working_dir|
add_default_artefacts working_dir
expect(bosh).to_not receive(:upload_stemcell)
expect do
command.run(working_dir, request)
end.to raise_error "invalid"
end
end
it "requires a deployment" do
in_dir do |working_dir|
expect do
command.run(working_dir, {
"source" => {
"target" => "http://bosh.example.com",
"username" => "bosh-username",
"password" => "bosh-password",
},
"params" => {
"manifest" => "deployment.yml",
"stemcells" => [],
"releases" => []
}
})
end.to raise_error /source must include 'deployment'/
end
end
it "requires a manifest" do
in_dir do |working_dir|
expect do
command.run(working_dir, {
"source" => {
"target" => "http://bosh.example.com",
"username" => "bosh-username",
"password" => "bosh-password",
"deployment" => "bosh-deployment",
},
"params" => {
"stemcells" => [],
"releases" => []
}
})
end.to raise_error /params must include 'manifest'/
end
end
it "errors if the cleanup paramater is NOT a boolean value" do
in_dir do |working_dir|
expect do
command.run(working_dir, {
"source" => {
"target" => "http://bosh.example.com",
"username" => "bosh-username",
"password" => "bosh-password",
"deployment" => "bosh-deployment",
},
"params" => {
"manifest" => "deployment.yml",
"stemcells" => [],
"releases" => [],
"cleanup" => 1
}
})
end.to raise_error /given cleanup value must be a boolean/
end
end
describe "stemcell and release globs" do
it "errors if the stemcells are not an array" do
in_dir do |working_dir|
expect do
command.run(working_dir, {
"source" => {
"target" => "http://bosh.example.com",
"username" => "bosh-username",
"password" => "bosh-password",
"deployment" => "bosh-deployment",
},
"params" => {
"manifest" => "deployment.yml",
"stemcells" => "stemcell.tgz",
"releases" => []
}
})
end.to raise_error /stemcells must be an array of globs/
end
end
it "errors if the stemcells are not an array" do
in_dir do |working_dir|
expect do
command.run(working_dir, {
"source" => {
"target" => "http://bosh.example.com",
"username" => "bosh-username",
"password" => "bosh-password",
"deployment" => "bosh-deployment",
},
"params" => {
"manifest" => "deployment.yml",
"stemcells" => [],
"releases" => "release.tgz"
}
})
end.to raise_error /releases must be an array of globs/
end
end
it "errors if a glob resolves to an empty list of files" do
in_dir do |working_dir|
touch working_dir, "stemcells", "stemcell.tgz"
touch working_dir, "releases", "release.rtf"
expect do
command.run(working_dir, request)
end.to raise_error "glob 'releases/*.tgz' matched no files"
end
end
end
end
end