/
systemd_unit_test.rb
executable file
·323 lines (274 loc) · 10.5 KB
/
systemd_unit_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
#!/usr/bin/env rspec
require_relative "test_helper"
module Yast
describe SystemdUnit do
include SystemdSocketStubs
include SystemdServiceStubs
def trigger_reloading_properties(command)
unit = SystemdUnit.new("new.socket")
properties = unit.properties
unit.send(command)
expect(unit.properties).not_to equal(properties)
end
before do
stub_sockets
stub_services
end
context "Installation system without full support of systemd" do
before do
allow(Yast::Stage).to receive(:initial).and_return(true)
end
describe "#properties" do
context "Unit found" do
it "returns struct with restricted installation properties" do
allow_any_instance_of(SystemdUnit).to receive(:command)
.with("is-enabled sshd.service").and_return(
OpenStruct.new("stderr" => "", "stdout" => "enabled", "exit" => 0)
)
unit = SystemdUnit.new("sshd.service")
expect(unit.properties).to be_a(SystemdUnit::InstallationProperties)
expect(unit.properties.not_found?).to eq(false)
end
describe "#enabled?" do
it "returns true if service is enabled" do
allow_any_instance_of(SystemdUnit).to receive(:command)
.with("is-enabled sshd.service").and_return(
OpenStruct.new("stderr" => "", "stdout" => "enabled", "exit" => 0)
)
unit = SystemdUnit.new("sshd.service")
expect(unit.enabled?).to eq(true)
end
it "returns false if service is disabled" do
stub_unit_command(success: false)
allow_any_instance_of(SystemdUnit).to receive(:command)
.with("is-enabled sshd.service").and_return(
OpenStruct.new("stderr" => "", "stdout" => "disabled", "exit" => 1)
)
unit = SystemdUnit.new("sshd.service")
expect(unit.enabled?).to eq(false)
end
end
end
context "Unit not found" do
it "returns struct with restricted installation properties" do
stub_services(service: "unknown")
stub_unit_command(success: false)
unit = SystemdUnit.new("unknown.service")
expect(unit.properties).to be_a(SystemdUnit::InstallationProperties)
expect(unit.properties.not_found?).to eq(true)
end
end
end
end
describe "#properties" do
it "always returns struct including default properties" do
unit = SystemdUnit.new("iscsi.socket")
expect(unit.properties.to_h.keys).to include(*SystemdUnit::DEFAULT_PROPMAP.keys)
end
it "provides status properties methods" do
unit = SystemdUnit.new("iscsid.socket")
expect(unit.properties[:enabled?]).not_to be_nil
expect(unit.properties[:active?]).not_to be_nil
expect(unit.properties[:loaded?]).not_to be_nil
expect(unit.properties[:supported?]).not_to be_nil
expect(unit.properties[:not_found?]).not_to be_nil
expect(unit.properties[:static?]).not_to be_nil
expect(unit.properties[:path]).not_to be_nil
expect(unit.properties[:error]).not_to be_nil
expect(unit.properties[:raw]).not_to be_nil
expect(unit.properties[:can_reload?]).not_to be_nil
end
it "delegates the status properties onto the unit object" do
unit = SystemdUnit.new("iscsid.socket")
expect(unit).to respond_to(:enabled?)
expect(unit).to respond_to(:active?)
expect(unit).to respond_to(:loaded?)
expect(unit).to respond_to(:path)
expect(unit).to respond_to(:can_reload?)
end
end
describe ".new" do
it "creates a new SystemdUnit instance with unit name and type parsed from first parameter" do
instance = nil
expect { instance = SystemdUnit.new("random.socket") }.not_to raise_error
expect(instance.unit_name).to eq("random")
expect(instance.unit_type).to eq("socket")
end
it "correctly parses a name with many dots" do
instance = nil
expect { instance = SystemdUnit.new("dbus-org.freedesktop.hostname1.service") }.not_to raise_error
expect(instance.unit_name).to eq("dbus-org.freedesktop.hostname1")
expect(instance.unit_type).to eq("service")
end
it "raises an exception if an incomplete unit name is passed" do
expect { SystemdUnit.new("sshd") }.to raise_error(RuntimeError)
end
it "allows to create supported units" do
expect { SystemdUnit.new("my.socket") }.not_to raise_error
expect { SystemdUnit.new("default.target") }.not_to raise_error
expect { SystemdUnit.new("sshd.service") }.not_to raise_error
end
it "accepts parameters to extend the default properties" do
unit = SystemdUnit.new("iscsid.socket", requires: "Requires", wants: "Wants")
expect(unit.properties.wants).not_to be_nil
expect(unit.properties.requires).not_to be_nil
end
end
describe "#stop" do
it "returns true if unit has been stopped" do
stub_unit_command
unit = SystemdUnit.new("my.socket")
expect(unit.stop).to eq(true)
end
it "returns false if failed" do
stub_unit_command(success: false)
unit = SystemdUnit.new("my.socket")
expect(unit.stop).to eq(false)
expect(unit.error).not_to be_empty
end
it "triggers reloading of unit properties" do
trigger_reloading_properties(:stop)
end
end
describe "#start" do
it "returns true if starts (activates) the unit" do
stub_unit_command(success: true)
unit = SystemdUnit.new("my.socket")
expect(unit.start).to eq(true)
end
it "returns false if failed to start the unit" do
stub_unit_command(success: false)
unit = SystemdUnit.new("my.socket")
expect(unit.start).to eq(false)
expect(unit.error).not_to be_empty
end
it "triggers reloading of unit properties" do
trigger_reloading_properties(:start)
end
end
describe "#enable" do
it "returns true if the unit has been enabled successfully" do
stub_unit_command(success: true)
unit = SystemdUnit.new("your.socket")
expect(unit.enable).to eq(true)
end
it "returns false if unit fails" do
stub_unit_command(success: false)
unit = SystemdUnit.new("your.socket")
expect(unit.enable).to eq(false)
end
it "triggers reloading of unit properties" do
trigger_reloading_properties(:enable)
end
end
describe "#disable" do
it "returns true if the unit has been disabled successfully" do
stub_unit_command(success: true)
unit = SystemdUnit.new("your.socket")
expect(unit.disable).to eq(true)
end
it "returns false if unit disabling fails" do
stub_unit_command(success: false)
unit = SystemdUnit.new("your.socket")
expect(unit.disable).to eq(false)
end
it "triggers reloading of unit properties" do
trigger_reloading_properties(:disable)
end
end
describe "#show" do
it "always returns new unit properties object" do
unit = SystemdUnit.new("startrek.socket")
expect(unit.show).not_to equal(unit.show)
end
end
describe "#refresh!" do
it "rewrites and returns the properties instance variable" do
unit = SystemdUnit.new("your.socket")
properties = unit.properties
expect(unit.refresh!).not_to equal(properties)
end
end
describe "#restart" do
it "returns true if unit has been restarted" do
unit = SystemdUnit.new("sshd.service")
expect(unit.restart).to eq(true)
end
it "returns false if the restart fails" do
stub_unit_command(success: false)
unit = SystemdUnit.new("sshd.service")
expect(unit.restart).to eq(false)
end
end
describe "#try_restart" do
it "returns true if the unit has been restarted" do
unit = SystemdUnit.new("sshd.service")
expect(unit.try_restart).to eq(true)
end
it "returns false if the try_restart fails" do
stub_unit_command(success: false)
unit = SystemdUnit.new("sshd.service")
expect(unit.try_restart).to eq(false)
end
end
describe "#reload" do
it "returns true if the unit has been reloaded" do
unit = SystemdUnit.new("sshd.service")
expect(unit.reload).to eq(true)
end
it "returns false if the reload fails" do
stub_unit_command(success: false)
unit = SystemdUnit.new("sshd.service")
expect(unit.reload).to eq(false)
end
end
describe "#reload_or_restart" do
it "returns true if the unit has been reloaded or restarted" do
unit = SystemdUnit.new("sshd.service")
expect(unit.reload_or_restart).to eq(true)
end
it "returns false if the reload_or_restart action fails" do
stub_unit_command(success: false)
unit = SystemdUnit.new("sshd.service")
expect(unit.reload_or_restart).to eq(false)
end
end
describe "#reload_or_try_restart" do
it "returns true if the unit has been reload_or_try_restarted" do
unit = SystemdUnit.new("sshd.service")
expect(unit.reload_or_try_restart).to eq(true)
end
it "returns false if the reload_or_try_restart action fails" do
stub_unit_command(success: false)
unit = SystemdUnit.new("sshd.service")
expect(unit.reload_or_try_restart).to eq(false)
end
end
describe "#error" do
it "returns empty string if the unit commands succeed" do
stub_unit_command(success: true)
unit = SystemdUnit.new("your.socket")
unit.start
expect(unit.error).to be_empty
unit.stop
expect(unit.error).to be_empty
unit.enable
expect(unit.error).to be_empty
unit.disable
expect(unit.error).to be_empty
end
it "returns error string if the unit commands fail" do
stub_unit_command(success: false)
unit = SystemdUnit.new("your.socket")
unit.start
expect(unit.error).not_to be_empty
unit.stop
expect(unit.error).not_to be_empty
unit.enable
expect(unit.error).not_to be_empty
unit.disable
expect(unit.error).not_to be_empty
end
end
end
end