-
Notifications
You must be signed in to change notification settings - Fork 44
/
file_from_url_test.rb
executable file
·421 lines (341 loc) · 14.2 KB
/
file_from_url_test.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
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
#!/usr/bin/env rspec
require_relative "test_helper"
require "transfer/file_from_url"
require "fileutils"
require "tmpdir"
describe Yast::Transfer::FileFromUrl do
Yast.import "Installation"
before do
stub_const("Yast::FTP", double(fake_method: nil))
stub_const("Yast::HTTP", double(Get: nil))
stub_const("Yast::TFTP", double(Get: nil))
end
class FileFromUrlTest
include Yast::I18n
include Yast::Transfer::FileFromUrl
# adaptor for existing tests
def Get(scheme, host, urlpath, localfile, destdir: "/destdir")
get_file_from_url(scheme: scheme, host: host, urlpath: urlpath,
localfile: localfile,
urltok: {}, destdir: destdir)
end
end
let(:destdir) { "/destdir" }
# avoid BuildRequiring a package that we stub entirely anyway
before do
allow(Yast).to receive(:import).and_call_original
end
subject { FileFromUrlTest.new }
describe "#Get" do
before do
expect(Yast::WFM).to receive(:Read).with(path(".local.tmpdir"), [])
.and_return(tmpdir)
expect(Yast::WFM).to receive(:SCRGetDefault)
.and_return(333)
expect(Yast::WFM).to receive(:SCRGetName).with(333)
.and_return("chroot=/mnt:scr")
allow(Yast::WFM).to receive(:Execute)
.with(path(".local.mkdir"), "#{destdir}/tmp_dir/tmp_mount")
# the local/target mess was last modified in
# https://github.com/yast/yast-autoinstallation/commit/69f1966dd1456301a69102c6d3bacfe7c9f9dc49
# for https://bugzilla.suse.com/show_bug.cgi?id=829265
allow(Yast::Execute).to receive(:new).and_return(execute_object)
end
let(:tmpdir) { "/tmp_dir" }
let(:execute_object) { Yast::Execute.new }
it "returns false for unknown scheme" do
expect(subject.Get("money_transfer_protocol",
"bank", "account", "pocket")).to eq(false)
end
context "when scheme is 'device'" do
let(:scheme) { "device" }
it "returns false for an empty path" do
expect(subject.Get(scheme, "sda", "", "/localfile")).to eq(false)
end
context "when host is empty" do
let(:host) { "" }
it "probes disks" do
probed_disks = [
{},
{ "dev_name" => "/dev/sda" },
{ "dev_name" => "/dev/sdb" }
]
expect(Yast::SCR).to receive(:Read)
.with(path(".probe.disk"))
.and_return(probed_disks)
lstat_mocks = {
"/dev/sda1" => true,
"/dev/sda2" => false,
"/dev/sda3" => false,
"/dev/sda4" => true,
"/dev/sda5" => true,
"/dev/sda6" => false,
"/dev/sdb1" => true,
"/dev/sdb2" => false,
"/dev/sdb3" => false,
"/dev/sdb4" => false,
"/dev/sdb5" => false
}
lstat_mocks.each do |device, exists|
expect(Yast::SCR).to receive(:Read)
.with(path(".target.lstat"), device).twice
.and_return(exists ? { "size" => 1 } : {})
end
allow(Yast::SCR).to receive(:Dir)
.with(path(".product.features.section")).and_return([])
# only up to sda5 because that is when we find the file
mount_points = {
# device => [prior mount point, temporary mount point]
"/dev/sda" => ["", "/tmp_dir/tmp_mount"],
"/dev/sda1" => ["/mnt_sda1", nil],
"/dev/sda4" => ["", "/mnt_sda1"],
"/dev/sda5" => ["", "/mnt_sda1"]
}
mount_points.each do |device, mp|
expect(execute_object).to receive(:stdout)
.with("/usr/bin/findmnt", "--first-only", "--noheadings", "--output=target", device)
.and_return(mp.first)
end
# only up to sda5 because that is when we find the file
mount_succeeded = {
"/dev/sda" => false,
"/dev/sda4" => true,
"/dev/sda5" => true
}
mount_succeeded.each do |device, result|
expect(Yast::SCR).to receive(:Execute)
.with(path(".target.mount"),
[device, mount_points[device].last],
"-o noatime")
.and_return(result)
end
expect(Yast::WFM).to receive(:Execute)
.with(path(".local.bash"),
"/bin/cp /mnt_sda1/mypath /localfile")
.exactly(3).times
.and_return(1, 1, 0) # sda1 fails, sda4 fails, sda5 succeeds
expect(Yast::WFM).to receive(:Execute)
.with(path(".local.bash"),
# "/bin/cp /destdir/tmp_dir/tmp_mount/mypath /localfile")
# Bug: it is wrong if destdir is used
"/bin/cp /tmp_dir/tmp_mount/mypath /localfile")
.exactly(0).times
# .and_return(1, 0) # sda4 fails, sda5 succeeds
# Bug: local is wrong. nfs and cifs correctly use .target.umount
expect(Yast::WFM).to receive(:Execute)
.with(path(".local.umount"), "/mnt_sda1")
.exactly(2).times
# DO IT, this is the call that needs all the above mocking
expect(subject.Get(scheme, host, "mypath", "/localfile"))
.to eq(true)
end
end
context "when host specifies a device"
context "when host+path specify a device"
end
context "when scheme is 'usb'" do
let(:scheme) { "usb" }
it "returns false for an empty path" do
expect(subject.Get(scheme, "sda", "", "/localfile")).to eq(false)
end
end
# not yet covered
context "when scheme is 'http' or 'https'"
context "when scheme is 'ftp'"
context "when scheme is 'file'" do
let(:scheme) { "file" }
let(:cd_device) { "/dev/sr0" }
let(:tmp_mount) { File.join(tmpdir, "tmp_mount") }
let(:destination) { File.join(dir, "dest") }
let(:source) { File.join(dir, "source") }
let(:dir) { Dir.mktmpdir }
before do
allow(Yast::Installation).to receive(:sourcedir).and_return(File.join(dir, "mnt"))
allow(Yast::Installation).to receive(:boot).and_return("cd")
allow(Yast::InstURL).to receive("installInf2Url").and_return("cd:/?devices=#{cd_device}")
allow(Yast::Builtins).to receive(:sleep).with(3000)
allow(Yast::SCR).to receive(:Execute)
.with(path(".target.bash"), /bin\/cp/) do |*args|
cmd = args.last
_, from, to = cmd.split
begin
FileUtils.cp(from, to)
rescue Errno::ENOENT
nil
end
end
allow(Yast::WFM).to receive(:Execute)
.with(path(".local.mount"), anything).and_return(false)
end
after do
FileUtils.remove_entry(dir) if File.exist?(dir)
end
context "when the source file exists in the installation sourcedir" do
before do
inst_source = File.join(Yast::Installation.sourcedir, source)
FileUtils.mkdir_p(File.dirname(inst_source))
File.write(inst_source, "sourcedir")
end
it "tries to copy the file from the installation sourcedir" do
expect(subject.Get(scheme, "", source, destination)).to eq(true)
expect(File.read(destination)).to eq("sourcedir")
end
end
context "when the source file exists" do
before { File.write(source, "testing") }
it "copies the file to the given destination and returns true" do
expect(subject.Get(scheme, "", source, destination)).to eq(true)
expect(File.read(destination)).to eq("testing")
end
end
context "when the source file does not exist" do
it "returns false" do
expect(subject.Get(scheme, "", source, destination)).to eq(false)
end
end
context "when the destination directory does not exist" do
let(:destination) { File.join(dir, "not", "a", "directory") }
before { File.write(source, "testing") }
it "returns false" do
expect(subject.Get(scheme, "", source, destination)).to eq(false)
end
end
context "when the file cannot be copied and the installation medium is a CD/DVD" do
let(:mounts) { "" }
let(:tmpdir) { File.join(dir, "tmp") }
let(:source_on_dvd) { File.join(tmp_mount, source) }
before do
allow(Yast::WFM).to receive(:Execute).with(path(".local.mkdir"), anything)
allow(Yast::WFM).to receive(:Execute)
.with(path(".local.mount"), anything).and_return(true)
allow(File).to receive(:read).and_call_original
allow(File).to receive(:read).with("/proc/mounts").and_return(mounts)
FileUtils.mkdir_p(File.dirname(source_on_dvd))
File.write(source_on_dvd, "testing")
end
it "tries to copy the file from the CD/DVD" do
expect(Yast::WFM).to receive(:Execute)
.with(Yast::Path.new(".local.mount"), [cd_device, tmp_mount,
Yast::Installation.mountlog])
.and_return(true)
expect(Yast::WFM).to receive(:Execute).with(path(".local.umount"), anything)
expect(subject.Get(scheme, "", source, destination))
expect(File.read(destination)).to eq("testing")
end
context "and the CD/DVD is already mounted" do
let(:mounts) do
"#{cd_device} /mounts/mp_0005 iso9660 ro,relatime 0 0\n" \
"#{cd_device} /mounts/mp_0006 iso9660 ro,relatime 0 0"
end
it "bind mounts the CD/DVD and tries to copy the file from it" do
expect(Yast::SCR).to receive(:Execute)
.with(path(".target.bash_output"),
"/bin/mount -v --bind /mounts/mp_0005 #{tmp_mount}")
.and_return("exit" => 0, "stdout" => "ok")
expect(Yast::WFM).to receive(:Execute).with(path(".local.umount"), anything)
expect(subject.Get(scheme, "", source, destination)).to eq(true)
expect(File.read(destination)).to eq("testing")
end
end
end
end
context "when scheme is 'nfs'" do
let(:scheme) { "nfs" }
let(:host) { "myhost" }
let(:tmp_mount) { File.join(tmpdir, "tmp_mount") }
let(:localfile) { File.join(dir, "dest") }
let(:source) { File.join(dir, "source") }
let(:dir) { Dir.mktmpdir }
let(:destdir) { Dir.mktmpdir("chroot") }
before do
FileUtils.mkdir_p(File.join(destdir, tmp_mount))
FileUtils.touch(File.join(destdir, tmp_mount, "source"))
allow(Yast::SCR).to receive(:Execute).with(
Yast::Path.new(".target.mount"), any_args
).and_return(true)
allow(Yast::WFM).to receive(:Execute).with(
Yast::Path.new(".local.bash"),
"/bin/cp #{destdir}#{tmp_mount}/source #{localfile}"
).and_call_original
allow(Yast::SCR).to receive(:Execute).with(
Yast::Path.new(".target.umount"), tmp_mount
).and_return(true)
end
it "mounts the file system and copies the file" do
expect(Yast::SCR).to receive(:Execute).with(
Yast::Path.new(".target.mount"),
["#{host}:#{dir}/", tmp_mount], "-o noatime,nolock"
).and_return(true)
expect(Yast::WFM).to receive(:Execute).with(
Yast::Path.new(".local.bash"),
"/bin/cp #{destdir}#{tmp_mount}/source #{localfile}"
).and_call_original
expect(Yast::SCR).to receive(:Execute).with(
Yast::Path.new(".target.umount"), tmp_mount
).and_return(true)
expect(subject.Get(scheme, host, source, localfile, destdir: destdir)).to eq(true)
expect(File).to exist(localfile)
end
context "when an IPv4 address is given" do
let(:host) { "192.168.1.1" }
it "passes the address to the mount command" do
expect(Yast::SCR).to receive(:Execute).with(
Yast::Path.new(".target.mount"),
["#{host}:#{dir}/", tmp_mount], "-o noatime,nolock"
).and_return(true)
expect(subject.Get(scheme, host, source, localfile, destdir: destdir)).to eq(true)
end
end
context "when an IPv6 address is given" do
let(:host) { "fd12:3456:789a:1::1" }
it "passes the address between square brackets" do
expect(Yast::SCR).to receive(:Execute).with(
Yast::Path.new(".target.mount"),
["[#{host}]:#{dir}/", tmp_mount], "-o noatime,nolock"
).and_return(true)
expect(subject.Get(scheme, host, source, localfile, destdir: destdir)).to eq(true)
end
end
end
context "when scheme is cifs" do
let(:scheme) { "cifs" }
let(:host) { "myhost" }
let(:tmp_mount) { File.join(tmpdir, "tmp_mount") }
let(:localfile) { File.join(dir, "dest") }
let(:source) { File.join(dir, "source") }
let(:dir) { Dir.mktmpdir }
let(:destdir) { Dir.mktmpdir("chroot") }
before do
FileUtils.mkdir_p(File.join(destdir, tmp_mount))
FileUtils.touch(File.join(destdir, tmp_mount, "source"))
allow(Yast::SCR).to receive(:Execute).with(
Yast::Path.new(".target.mount"), any_args
).and_return(true)
allow(Yast::WFM).to receive(:Execute).with(
Yast::Path.new(".local.bash"),
"/bin/cp #{destdir}#{tmp_mount}/source #{localfile}"
).and_call_original
allow(Yast::SCR).to receive(:Execute).with(
Yast::Path.new(".target.umount"), tmp_mount
).and_return(true)
end
it "mounts the file system and copies the file" do
expect(Yast::SCR).to receive(:Execute).with(
Yast::Path.new(".target.mount"),
["//#{host}#{dir}/", tmp_mount], "-t cifs -o guest,ro,noatime"
).and_return(true)
expect(Yast::WFM).to receive(:Execute).with(
Yast::Path.new(".local.bash"),
"/bin/cp #{destdir}#{tmp_mount}/source #{localfile}"
).and_call_original
expect(Yast::SCR).to receive(:Execute).with(
Yast::Path.new(".target.umount"), tmp_mount
).and_return(true)
expect(subject.Get(scheme, host, source, localfile, destdir: destdir)).to eq(true)
expect(File).to exist(localfile)
end
end
context "when scheme is 'floppy'"
context "when scheme is 'tftp'"
end
end