-
Notifications
You must be signed in to change notification settings - Fork 295
/
net_fetcher_spec.rb
302 lines (244 loc) · 8.43 KB
/
net_fetcher_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
require "spec_helper"
module Omnibus
module RSpec
module OhaiHelpers
# Turn off the mandatory Ohai helper.
def stub_ohai(options = {}, &block); end
end
end
describe NetFetcher do
include_examples "a software", "zlib"
let(:source_url) { "http://chef-releng.s3.amazonaws.com/omnibus/integration_test/zlib-1.2.8.tar.gz" }
let(:source_md5) { "44d667c142d7cda120332623eab69f40" }
let(:source_sha1) { "a4d316c404ff54ca545ea71a27af7dbc29817088" }
let(:source_sha256) { "36658cb768a54c1d4dec43c3116c27ed893e88b02ecfcb44f2166f9c0b7f2a0d" }
let(:source_sha512) { "ece209d4c7ec0cb58ede791444dc754e0d10811cbbdebe3df61c0fd9f9f9867c1c3ccd5f1827f847c005e24eef34fb5bf87b5d3f894d75da04f1797538290e4a" }
let(:source) do
{ url: source_url, md5: source_md5 }
end
let(:downloaded_file) { subject.send(:downloaded_file) }
let(:extracted) { File.join(source_dir, "zlib-1.2.8") }
let(:fetch!) { capture_stdout { subject.fetch } }
let(:manifest_entry) do
double(ManifestEntry,
name: "software",
locked_version: "1.2.8",
described_version: "1.2.8",
locked_source: source)
end
subject { described_class.new(manifest_entry, project_dir, build_dir) }
describe "#fetch_required?" do
context "when the file is not downloaded" do
it "return true" do
expect(subject.fetch_required?).to be_truthy
end
end
context "when the file is downloaded" do
before { fetch! }
context "when the checksum is different" do
it "return true" do
allow(subject).to receive(:checksum).and_return("abcd1234")
expect(subject.fetch_required?).to be_truthy
end
end
context "when the checksum is the same" do
it "return false" do
expect(subject.fetch_required?).to be(false)
end
end
end
end
describe "#version_guid" do
context "source with md5" do
it "includes the md5 digest" do
expect(subject.version_guid).to eq("md5:#{source_md5}")
end
end
context "source with sha1" do
let(:source) do
{ url: source_url, sha1: source_sha1 }
end
it "includes the sha1 digest" do
expect(subject.version_guid).to eq("sha1:#{source_sha1}")
end
end
context "source with sha256" do
let(:source) do
{ url: source_url, sha256: source_sha256 }
end
it "includes the sha256 digest" do
expect(subject.version_guid).to eq("sha256:#{source_sha256}")
end
end
context "source with sha512" do
let(:source) do
{ url: source_url, sha512: source_sha512 }
end
it "includes the sha512 digest" do
expect(subject.version_guid).to eq("sha512:#{source_sha512}")
end
end
end
describe "#clean" do
before { fetch! }
context "when the project directory exists" do
before do
create_file("#{project_dir}/file_a")
end
it "extracts the asset" do
subject.clean
expect(extracted).to_not be_a_file
end
it "returns true" do
expect(subject.clean).to be_truthy
end
end
context "when the project directory does not exist" do
before do
remove_directory(project_dir)
end
it "returns false" do
expect(subject.clean).to be(false)
end
end
# we need to find a new test fixture because this one no longer exists
# context "when the source has read-only files"
context "when the source has broken symlinks" do
let(:source_url) { "http://www.openssl.org/source/openssl-1.0.1q.tar.gz" }
let(:source_md5) { "54538d0cdcb912f9bc2b36268388205e" }
let(:source) do
{ url: source_url, md5: source_md5, extract: :lax_tar }
end
it "extracts the asset without crashing" do
subject.clean
expect(extracted).to_not be_a_file
subject.clean
expect(extracted).to_not be_a_file
end
end
end
describe "#fetch" do
context "source with md5" do
it "downloads the file" do
fetch!
expect(downloaded_file).to be_a_file
end
context "when the checksum is invalid" do
let(:source_md5) { "bad01234checksum" }
it "raises an exception" do
expect { fetch! }.to raise_error(ChecksumMismatch)
end
end
end
context "source with no checksum" do
let(:source) do
{ url: source_url }
end
it "raises an exception" do
expect { fetch! }.to raise_error(ChecksumMissing)
end
end
context "source with sha1" do
let(:source) do
{ url: source_url, sha1: source_sha1 }
end
it "downloads the file" do
fetch!
expect(downloaded_file).to be_a_file
end
context "when the checksum is invalid" do
let(:source_sha1) { "bad01234checksum" }
it "raises an exception" do
expect { fetch! }.to raise_error(ChecksumMismatch)
end
end
end
context "source with sha256" do
let(:source) do
{ url: source_url, sha256: source_sha256 }
end
it "downloads the file" do
fetch!
expect(downloaded_file).to be_a_file
end
context "when the checksum is invalid" do
let(:source_sha256) { "bad01234checksum" }
it "raises an exception" do
expect { fetch! }.to raise_error(ChecksumMismatch)
end
end
end
context "source with sha512" do
let(:source) do
{ url: source_url, sha512: source_sha512 }
end
it "downloads the file" do
fetch!
expect(downloaded_file).to be_a_file
end
context "when the checksum is invalid" do
let(:source_sha512) { "bad01234checksum" }
it "raises an exception" do
expect { fetch! }.to raise_error(ChecksumMismatch)
end
end
end
it "when the download times out" do
# Mock the Timeout::Error for this particular test only
WebMock.disable_net_connect!
stub_request(:get, "http://chef-releng.s3.amazonaws.com/omnibus/integration_test/zlib-1.2.8.tar.gz").to_timeout
output = capture_logging do
expect { subject.send(:download) }.to raise_error(Timeout::Error)
end
expect(output).to include("Retrying failed download")
expect(output).to include("Download failed")
retry_count = output.scan("Retrying failed download").count
expect(retry_count).to eq(Omnibus::Config.fetcher_retries)
end
context "when the file is less than 10240 bytes" do
let(:source_url) { "https://downloads.chef.io/packages-chef-io-public.key" }
let(:source_md5) { "012a2c4e2a8edb86b2c072f02eea9f40" }
it "downloads the file" do
fetch!
expect(downloaded_file).to be_a_file
end
end
end
describe "#version_for_cache" do
before do
create_file("#{project_dir}/file_a")
create_file("#{project_dir}/file_b")
create_file("#{project_dir}/.file_c")
end
context "source with md5" do
it "includes the download_url and checksum" do
expect(subject.version_for_cache).to eq("download_url:#{source_url}|md5:#{source_md5}")
end
end
context "source with sha1" do
let(:source) do
{ url: source_url, sha1: source_sha1 }
end
it "includes the download_url and checksum" do
expect(subject.version_for_cache).to eq("download_url:#{source_url}|sha1:#{source_sha1}")
end
end
context "source with sha256" do
let(:source) do
{ url: source_url, sha256: source_sha256 }
end
it "includes the download_url and checksum" do
expect(subject.version_for_cache).to eq("download_url:#{source_url}|sha256:#{source_sha256}")
end
end
context "source with sha512" do
let(:source) do
{ url: source_url, sha512: source_sha512 }
end
it "includes the download_url and checksum" do
expect(subject.version_for_cache).to eq("download_url:#{source_url}|sha512:#{source_sha512}")
end
end
end
end
end