/
kernel_test.rb
executable file
·289 lines (238 loc) · 10.1 KB
/
kernel_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
#!/usr/bin/env rspec
require_relative "test_helper"
require "tmpdir"
Yast.import "Kernel"
Yast.import "FileUtils"
describe Yast::Kernel do
let(:stubbed_modules_dir) { File.join(File.dirname(__FILE__), "data", "modules.d") }
before do
Yast.y2milestone "--- test ---"
# reset variables
subject.main
stub_const("Yast::KernelClass::MODULES_DIR", stubbed_modules_dir)
@default_modules = {
Yast::KernelClass::MODULES_CONF_FILE => [],
"MODULES_LOADED_ON_BOOT.conf" => ["module-a", "module-b"],
"user-added-1.conf" => ["user-module-1", "user-module-2", "user-module-3"],
"user-added-2.conf" => ["user-module-4"]
}
Yast::Kernel.reset_modules_to_load
allow(Yast::FileUtils).to receive(:Exists).and_return(true)
end
describe "#modules_to_load" do
describe "when modules.d directory exists" do
it "returns hash of modules to load" do
expect(Yast::Kernel.modules_to_load).to eq(@default_modules)
end
end
describe "when modules.d directory is missing" do
it "returns empty list of modules for modules.d directory" do
expect(Yast::FileUtils).to receive(:Exists).with(Yast::KernelClass::MODULES_DIR).and_return(false)
expect(Yast::Kernel.modules_to_load).to eq(Yast::KernelClass::MODULES_CONF_FILE => [])
end
end
end
describe "#GetPackages" do
it "returns kernel packages for i386 and no cpu info flags" do
allow(Yast::Arch).to receive(:architecture).and_return("i386")
allow(Yast::Arch).to receive(:is_uml).and_return(false)
allow(Yast::Arch).to receive(:is_xen).and_return(false)
expect(Yast::SCR).to receive(:Read).with(path(".probe.is_xen")).and_return(false)
expect(Yast::SCR).to receive(:Read).with(path(".proc.cpuinfo.value.\"0\".\"flags\"")).and_return(nil)
expect(Yast::SCR).to receive(:Read).with(path(".probe.memory")).and_return(10)
expect(Yast::Kernel.GetPackages).to eq(["kernel-default"])
end
end
describe "#module_to_be_loaded?" do
it "tests whether module is loaded on boot" do
["module-a", "module-b", "user-module-1", "user-module-2", "user-module-3", "user-module-4"].each do |kernel_module|
expect(Yast::Kernel.module_to_be_loaded?(kernel_module)).to eq(true)
end
["module-c", "user-module-5"].each do |kernel_module|
expect(Yast::Kernel.module_to_be_loaded?(kernel_module)).to eq(false)
end
end
end
describe "#AddModuleToLoad" do
it "adds new module to be loaded on boot" do
new_module = "new-kernel-module"
expect(Yast::Kernel.module_to_be_loaded?(new_module)).to eq(false)
Yast::Kernel.AddModuleToLoad new_module
expect(Yast::Kernel.module_to_be_loaded?(new_module)).to eq(true)
end
it "adds module only once" do
new_module = "new-kernel-module"
Yast::Kernel.AddModuleToLoad new_module
Yast::Kernel.AddModuleToLoad new_module
expect(Yast::Kernel.modules_to_load.values.flatten.select { |m| m == new_module }.size).to eq(1)
end
end
describe "#RemoveModuleToLoad" do
it "removes module from list of modules to be loaded on boot" do
module_to_remove = "user-module-2"
expect(Yast::Kernel.module_to_be_loaded?(module_to_remove)).to eq(true)
Yast::Kernel.RemoveModuleToLoad module_to_remove
expect(Yast::Kernel.module_to_be_loaded?(module_to_remove)).to eq(false)
end
it "does not remove module which is not in list" do
module_to_remove = "not-in-list"
expect(Yast::Kernel.module_to_be_loaded?(module_to_remove)).to eq(false)
Yast::Kernel.RemoveModuleToLoad module_to_remove
expect(Yast::Kernel.module_to_be_loaded?(module_to_remove)).to eq(false)
end
end
describe "#SaveModulesToLoad" do
describe "when modules.d directory does not exist" do
it "tries to create the missing directory and returns false if it fails" do
expect(Yast::FileUtils).to receive(:Exists).twice.and_return(false)
expect(Yast::SCR).to receive(:Execute).with(
path(".target.mkdir"),
anything
).and_return(false)
expect(Yast::Kernel.SaveModulesToLoad).to eq(false)
end
end
describe "when modules.d directory exists" do
it "stores all modules to be loaded to configuration files and returns true" do
Dir.mktmpdir do |tmpdir|
FileUtils.cp_r(stubbed_modules_dir + "/.", tmpdir)
stub_const("Yast::KernelClass::MODULES_DIR", tmpdir)
Yast::Kernel.reset_modules_to_load
# Tests on the default data
["module-a", "module-b", "user-module-1", "user-module-2", "user-module-3", "user-module-4"].each do |kernel_module|
expect(Yast::Kernel.module_to_be_loaded?(kernel_module)).to eq(true)
end
new_module = "new-kernel-module"
remove_module = "user-module-2"
# Modifying data
Yast::Kernel.AddModuleToLoad new_module
Yast::Kernel.RemoveModuleToLoad remove_module
expect(Yast::Kernel.SaveModulesToLoad).to eq(true)
# Tests on the stored modified data
Yast::Kernel.reset_modules_to_load
["module-a", "module-b", "user-module-1", "user-module-3", "user-module-4", new_module].each do |kernel_module|
expect(Yast::Kernel.module_to_be_loaded?(kernel_module)).to eq(true)
end
expect(Yast::Kernel.module_to_be_loaded?(remove_module)).to eq(false)
# Tests directly on the system
number_of_nkm = `grep --count --no-filename #{new_module} #{tmpdir}/*`
expect(number_of_nkm.split.map(&:to_i).inject(:+)).to eq(1)
number_of_rkm = `grep --count --no-filename #{remove_module} #{tmpdir}/*`
expect(number_of_rkm.split.map(&:to_i).inject(:+)).to eq(0)
end
end
end
end
describe ".ParseInstallationKernelCmdline" do
before do
allow(Yast::SCR).to receive(:Read).with(path(".etc.install_inf.Cmdline")).and_return(cmdline)
allow(Yast::Stage).to receive(:initial).and_return(true)
allow(Yast::Arch).to receive(:architecture).and_return("x86_64")
end
context "for common options" do
let(:cmdline) { "splash=verbose silent" }
it "adds options to CmdLine" do
subject.ParseInstallationKernelCmdline
expect(subject.GetCmdLine).to eq " splash=verbose silent"
end
end
context "when command line contain newline" do
let(:cmdline) { "splash=verbose silent\n" }
it "deletes this newline from CmdLine" do
subject.ParseInstallationKernelCmdline
expect(subject.GetCmdLine).to eq " splash=verbose silent"
end
end
context "when command line contain vga= key" do
let(:cmdline) { "vga=ask splash=verbose silent\n" }
it "deletes vga parameter from CmdLine" do
subject.ParseInstallationKernelCmdline
expect(subject.GetCmdLine).to eq " splash=verbose silent"
end
it "sets vgaType" do
subject.ParseInstallationKernelCmdline
expect(subject.GetVgaType).to eq "ask"
end
end
context "when command line value including quotes" do
let(:cmdline) { " splash=\"verbose, silent\" text=abc\n" }
it "it adds it to CmdLine correctly" do
subject.ParseInstallationKernelCmdline
expect(subject.GetCmdLine).to eq " splash=\"verbose, silent\" text=abc"
end
end
# FIXME: we do backslash escapes but in fact
# the kernel does not care about any backslashes, only about double quotes
context "when command line value including escaped quotes" do
let(:cmdline) { " splash=\"verbose\\\", silent\" text=\\\"abc\n" }
it "it adds it to CmdLine correctly" do
subject.ParseInstallationKernelCmdline
expect(subject.GetCmdLine).to eq " splash=\"verbose\\\", silent\" text=\\\"abc"
end
end
context "when value of parameter contain '='" do
let(:cmdline) { " splash=verbose silent pci=hpiosize=0,hpmemsize=0,nobar\n" }
it "it adds it to CmdLine correctly" do
subject.ParseInstallationKernelCmdline
expect(subject.GetCmdLine).to eq " splash=verbose silent pci=hpiosize=0,hpmemsize=0,nobar"
end
end
context "when there is special characters for filter on s390" do
let(:cmdline) { " splash=verbose silent User=root init=5 ramdisk_size=5G\n" }
before do
allow(Yast::Arch).to receive(:architecture).and_return("s390_64")
end
it "it filters it out of CmdLine" do
subject.ParseInstallationKernelCmdline
expect(subject.GetCmdLine).to eq " splash=verbose silent"
end
end
end
describe ".propose_hibernation?" do
context "on non-intel architectures" do
before do
allow(Yast::Arch).to receive(:architecture).and_return("s390_64")
end
it "returns false" do
expect(subject.propose_hibernation?).to eq false
end
end
context "on intel architecture" do
before do
allow(Yast::Arch).to receive(:architecture).and_return("x86_64")
end
context "on virtual machine" do
before do
allow(Yast::Arch).to receive(:is_kvm).and_return(true)
end
it "returns false" do
expect(subject.propose_hibernation?).to eq false
end
end
context "on real hardware" do
before do
allow(Yast::Arch).to receive(:is_kvm).and_return(false)
allow(Yast::Arch).to receive(:is_xenU).and_return(false)
end
context "when product does not want hibernation proposal" do
before do
allow(Yast::ProductFeatures).to receive(:GetBooleanFeature)
.with("general", "propose_hibernation").and_return(false)
end
it "returns false" do
expect(subject.propose_hibernation?).to eq false
end
end
context "when product wants hibernation proposal" do
before do
allow(Yast::ProductFeatures).to receive(:GetBooleanFeature)
.with("general", "propose_hibernation").and_return(true)
end
it "returns true" do
expect(subject.propose_hibernation?).to eq true
end
end
end
end
end
end