-
Notifications
You must be signed in to change notification settings - Fork 22
/
grub2_test.rb
390 lines (288 loc) · 11.3 KB
/
grub2_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
# frozen_string_literal: true
require_relative "test_helper"
require "bootloader/grub2"
describe Bootloader::Grub2 do
subject do
sub = described_class.new
allow(sub).to receive(:cpu_mitigations).and_return(::Bootloader::CpuMitigations.new(:manual))
sub
end
before do
allow(::CFA::Grub2::Default).to receive(:new).and_return(double("GrubDefault").as_null_object)
allow(::CFA::Grub2::GrubCfg).to receive(:new).and_return(double("GrubCfg").as_null_object)
allow(Bootloader::Sections).to receive(:new).and_return(double("Sections").as_null_object)
allow(Yast::BootStorage).to receive(:available_swap_partitions).and_return([])
allow(Yast::BootStorage).to receive(:gpt_boot_disk?).and_return(false)
end
describe "#read" do
before do
allow(Bootloader::Stage1).to receive(:new).and_return(double.as_null_object)
allow(Bootloader::DeviceMap).to receive(:new).and_return(double.as_null_object)
end
it "reads device map on legacy intel" do
allow(Yast::Arch).to receive(:architecture).and_return("x86_64")
device_map = double(Bootloader::DeviceMap)
expect(device_map).to receive(:read)
allow(Bootloader::DeviceMap).to receive(:new).and_return(device_map)
subject.read
end
it "reads bootloader stage1 location" do
stage1 = double(Bootloader::Stage1)
expect(stage1).to receive(:read)
allow(Bootloader::Stage1).to receive(:new).and_return(stage1)
subject.read
end
end
describe "write" do
let(:stage1) { double(Bootloader::Stage1, devices: [], generic_mbr?: false, write: nil) }
before do
allow(Bootloader::Stage1).to receive(:new).and_return(stage1)
allow(Bootloader::MBRUpdate).to receive(:new).and_return(double(run: nil))
allow(Bootloader::GrubInstall).to receive(:new).and_return(double.as_null_object)
allow(Bootloader::DeviceMap).to receive(:new).and_return(double.as_null_object)
allow(Yast::BootStorage).to receive(:gpt_disks).and_return(["/dev/sdb"])
end
it "writes stage1 location" do
stage1 = double(Bootloader::Stage1, devices: [], generic_mbr?: false)
expect(stage1).to receive(:write)
allow(Bootloader::Stage1).to receive(:new).and_return(stage1)
subject.write
end
it "changes pmbr flag as specified in pmbr_action for all boot devices with gpt label" do
stage1 = double(Bootloader::Stage1, devices: ["/dev/sda", "/dev/sdb1"], generic_mbr?: false, write: nil)
allow(Bootloader::Stage1).to receive(:new).and_return(stage1)
allow(Yast::BootStorage).to receive(:gpt_boot_disk?).and_return(true)
devicegraph_stub("msdos_and_gpt.yaml")
expect(subject).to receive(:pmbr_setup).with("/dev/sdb")
subject.write
end
it "runs grub2-install for all configured stage1 locations" do
stage1 = double(Bootloader::Stage1, devices: ["/dev/sda", "/dev/sdb1"], generic_mbr?: false, write: nil)
allow(Bootloader::Stage1).to receive(:new).and_return(stage1)
grub2_install = double(Bootloader::GrubInstall)
expect(grub2_install).to receive(:execute)
.with(devices: ["/dev/sda", "/dev/sdb1"], secure_boot: nil, trusted_boot: false).and_return([])
expect(Bootloader::GrubInstall).to receive(:new).with(efi: false).and_return(grub2_install)
subject.trusted_boot = false
subject.write
end
context "on s390" do
before do
allow(Yast::Arch).to receive(:architecture).and_return("s390_64")
end
it "does not run mbr update for configured stage1 flags" do
expect(Bootloader::MBRUpdate).to_not receive(:new)
subject.write
end
end
context "on other architectures" do
before do
allow(Yast::Arch).to receive(:architecture).and_return("x86_64")
end
it "runs mbr update for configured stage1 flags" do
mbr_update = double(Bootloader::MBRUpdate)
expect(mbr_update).to receive(:run)
expect(Bootloader::MBRUpdate).to receive(:new).and_return(mbr_update)
subject.write
end
end
end
context "#propose" do
before do
stage1 = double.as_null_object
allow(Bootloader::Stage1).to receive(:new).and_return(stage1)
allow(Bootloader::DeviceMap).to receive(:new).and_return(double.as_null_object)
end
it "proposes stage1" do
stage1 = double
expect(stage1).to receive(:propose)
allow(Bootloader::Stage1).to receive(:new).and_return(stage1)
subject.propose
end
it "propose to add pmbr flag" do
subject.propose
expect(subject.pmbr_action).to eq :add
end
end
describe "#name" do
it "returns \"grub2\"" do
expect(subject.name).to eq "grub2"
end
end
describe "#packages" do
let(:initial_stage) { true }
let(:generic_mbr) { true }
let(:stage1) { double(generic_mbr?: generic_mbr) }
before do
allow(Yast::Stage).to receive(:initial).and_return(initial_stage)
allow(Bootloader::Stage1).to receive(:new).and_return(stage1)
end
it "contains grub2 package" do
expect(subject.packages).to include("grub2")
end
context "when is in initial stage" do
it "does not include syslinux" do
expect(subject.packages).to_not include("syslinux")
end
end
context "when is not in initial stage" do
let(:initial_stage) { false }
context "and generic_mbr is used" do
it "contains syslinux package" do
expect(subject.packages).to include("syslinux")
end
end
context "and generic_mbr is not used" do
let(:generic_mbr) { false }
it "contains syslinux package" do
expect(subject.packages).to_not include("syslinux")
end
end
end
context "when trusted boot is required" do
before do
allow(subject).to receive(:trusted_boot).and_return(true)
end
context "and is x86_64 architecture" do
before do
allow(Yast::Arch).to receive(:x86_64).and_return(true)
end
it "contains trustedgrub2 packages" do
expect(subject.packages).to include("trustedgrub2")
expect(subject.packages).to include("trustedgrub2-i386-pc")
end
end
context "and is i386 architecture" do
before do
allow(Yast::Arch).to receive(:x86_64).and_return(true)
end
it "contains trustedgrub2 packages" do
expect(subject.packages).to include("trustedgrub2")
expect(subject.packages).to include("trustedgrub2-i386-pc")
end
end
end
context "when trusted boot is not required" do
before do
allow(subject).to receive(:trusted_boot).and_return(false)
end
it "does not contain the trusged grub packages" do
expect(subject.packages).to_not include("trustedgrub2")
expect(subject.packages).to_not include("trustedgrub2-i386-pc")
end
end
end
describe "#summary" do
before do
stage1 = double(can_use_boot?: true, extended_partition?: false).as_null_object
allow(Bootloader::Stage1).to receive(:new).and_return(stage1)
end
it "contains line saying that bootloader type is GRUB2" do
expect(subject.summary).to include("Boot Loader Type: GRUB2")
end
context "when arch is not s390" do
before do
allow(Yast::Arch).to receive(:architecture).and_return("x86_64")
end
it "includes order of hard disks if there are more than 1" do
allow(subject.device_map).to receive(:size).and_return(2)
allow(subject.device_map).to receive(:disks_order).and_return(["/dev/sda", "/dev/sdb"])
expect(subject.summary).to include(%r{Order of Hard Disks: /dev/sda, /dev/sdb})
end
it "does not include order of hard disk if there is only 1" do
allow(subject.device_map).to receive(:size).and_return(1)
expect(subject.summary).to_not include(/Order of Hard Disks/)
end
end
context "when arch is s390" do
before do
allow(Yast::Arch).to receive(:architecture).and_return("s390_64")
end
it "does not includes order of hard disks" do
expect(subject.summary).to_not include(/Order of Hard Disks/)
end
end
end
describe "#merge" do
let(:other) { described_class.new }
it "replaces device map if merged one is not empty" do
other.device_map.add_mapping("hd0", "/dev/sda")
subject.merge(other)
expect(subject.device_map.system_device_for("hd0")).to eq "/dev/sda"
end
context "stage1 does not contain any device" do
before do
allow(Bootloader::Stage1).to receive(:new).and_call_original
allow(other.stage1).to receive(:devices).and_return([])
end
it "sets activate flag if subject or merged one contain it" do
subject.stage1.activate = true
other.stage1.activate = false
subject.merge(other)
expect(subject.stage1.activate?).to eq true
subject.stage1.activate = false
other.stage1.activate = false
subject.merge(other)
expect(subject.stage1.activate?).to eq false
subject.stage1.activate = false
other.stage1.activate = true
subject.merge(other)
expect(subject.stage1.activate?).to eq true
end
it "sets generic_mbr flag if subject or merged one contain it" do
subject.stage1.generic_mbr = true
other.stage1.generic_mbr = false
subject.merge(other)
expect(subject.stage1.generic_mbr?).to eq true
subject.stage1.generic_mbr = false
other.stage1.generic_mbr = false
subject.merge(other)
expect(subject.stage1.generic_mbr?).to eq false
subject.stage1.generic_mbr = false
other.stage1.generic_mbr = true
subject.merge(other)
expect(subject.stage1.generic_mbr?).to eq true
end
end
context "stage1 contains devices" do
before do
allow(Bootloader::Stage1).to receive(:new).and_call_original
allow(other.stage1).to receive(:devices).and_return(["/dev/sda"])
end
it "replaces activate flag with merged one" do
subject.stage1.activate = true
other.stage1.activate = false
subject.merge(other)
expect(subject.stage1.activate?).to eq false
subject.stage1.activate = false
other.stage1.activate = false
subject.merge(other)
expect(subject.stage1.activate?).to eq false
subject.stage1.activate = false
other.stage1.activate = true
subject.merge(other)
expect(subject.stage1.activate?).to eq true
end
it "replaces generic_mbr flag with merged one" do
subject.stage1.generic_mbr = true
other.stage1.generic_mbr = false
subject.merge(other)
expect(subject.stage1.generic_mbr?).to eq false
subject.stage1.generic_mbr = false
other.stage1.generic_mbr = false
subject.merge(other)
expect(subject.stage1.generic_mbr?).to eq false
subject.stage1.generic_mbr = false
other.stage1.generic_mbr = true
subject.merge(other)
expect(subject.stage1.generic_mbr?).to eq true
end
it "replaces all stage1 devices with merged ones" do
subject.stage1.clear_devices
subject.stage1.add_udev_device("/dev/sda2")
subject.merge(other)
expect(subject.stage1.devices).to eq ["/dev/sda"]
end
end
end
end