/
system_service_test.rb
executable file
·379 lines (305 loc) · 10.3 KB
/
system_service_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
#!/usr/bin/env rspec
# encoding: utf-8
# Copyright (c) [2018] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.
require_relative "../test_helper"
require "yast2/system_service"
describe Yast2::SystemService do
subject(:system_service) { described_class.new(service) }
let(:service) do
double("service", enabled?: true, name: "cups", active?: active?, enable: nil)
end
let(:socket) { double("socket", enabled?: true) }
let(:active?) { true }
before do
allow(service).to receive(:socket).and_return(socket)
end
describe ".find" do
let(:systemd_service) { instance_double(Yast::SystemdServiceClass::Service) }
before do
allow(Yast::SystemdService).to receive(:find).with("cups").and_return(systemd_service)
end
it "finds a systemd service" do
system_service = described_class.find("cups")
expect(system_service).to be_a(described_class)
expect(system_service.service).to eq(systemd_service)
end
end
describe ".find_many" do
let(:apparmor) { instance_double(Yast::SystemdServiceClass::Service) }
let(:cups) { instance_double(Yast::SystemdServiceClass::Service) }
before do
allow(Yast::SystemdService).to receive(:find_many).with(["apparmor", "cups"])
.and_return([apparmor, cups])
end
it "finds a set of systemd services" do
system_services = described_class.find_many(["apparmor", "cups"])
expect(system_services).to be_all(Yast2::SystemService)
expect(system_services.map(&:service)).to eq([apparmor, cups])
end
context "when some service is not found" do
before do
allow(Yast::SystemdService).to receive(:find_many).with(["apparmor", "cups"])
.and_return([nil, cups])
end
it "ignores the not found service" do
system_services = described_class.find_many(["apparmor", "cups"])
expect(system_services.map(&:service)).to eq([cups])
end
end
end
describe "#start_mode" do
context "when the service is enabled" do
it "returns :on_boot" do
expect(system_service.start_mode).to eq(:on_boot)
end
end
context "when the service is disabled" do
let(:service) { double("service", enabled?: false) }
context "but the associated socket is enabled" do
it "returns :on_demand" do
expect(system_service.start_mode).to eq(:on_demand)
end
end
context "and the socket is disabled" do
let(:socket) { double("socket", enabled?: false) }
it "returns :manual" do
expect(system_service.start_mode).to eq(:manual)
end
end
context "and there is no socket" do
let(:socket) { nil }
it "returns :manual" do
expect(system_service.start_mode).to eq(:manual)
end
end
end
end
describe "#start_mode=" do
before do
allow(service).to receive(:enable)
end
it "sets the wanted start_mode" do
expect { system_service.start_mode = :on_demand }.to change { system_service.start_mode }
.from(:on_boot).to(:on_demand)
end
context "when an invalid value is given" do
it "raises an error" do
expect { system_service.start_mode = :other }.to raise_error(ArgumentError)
end
end
context "when the wanted start_mode is the same than the current one" do
it "ignores the change" do
system_service.start_mode = :on_boot
expect(system_service.changed?).to eq(false)
end
end
end
describe "#start_modes" do
before do
allow(service).to receive(:socket).and_return(socket)
end
context "when an associated socket exists" do
let(:socket) { double("socket", disable: true) }
it "returns :on_boot, :on_demand and :manual" do
expect(system_service.start_modes).to eq([:on_boot, :manual, :on_demand])
end
end
context "when no associated socket exists" do
let(:socket) { nil }
it "returns :on_boot and :manual" do
expect(system_service.start_modes).to eq([:on_boot, :manual])
end
end
end
describe "#active=" do
context "when set to true" do
let(:active?) { false }
it "sets #active to true" do
expect { system_service.active = true }.to change { system_service.active }
.from(false).to(true)
end
context "and the service is already active" do
let(:active?) { true }
it "ignores the change" do
system_service.active = true
expect(system_service.changed?).to eq(false)
end
end
end
context "when set to false" do
let(:active?) { true }
it "sets #active to false" do
expect { system_service.active = false }.to change { system_service.active }
.from(true).to(false)
end
context "and the service is already inactive" do
let(:active?) { false }
it "ignores the change" do
system_service.active = false
expect(system_service.changed?).to eq(false)
end
end
end
end
describe "#active" do
context "when service is active" do
let(:active?) { true }
it "returns true" do
expect(system_service.active).to eq(true)
end
end
context "when service is inactive" do
let(:active?) { false }
it "returns false" do
expect(system_service.active).to eq(false)
end
end
context "when an active value was given" do
before do
system_service.active = false
end
it "returns the given value" do
expect(system_service.active).to eq(false)
end
end
end
describe "#save=" do
let(:socket) { double("socket", disable: true) }
let(:start_mode) { :on_boot }
before do
system_service.start_mode = start_mode
end
context "when start_mode was changed to :on_boot" do
let(:service) { double("service", enabled?: false, name: "cups") }
let(:socket) { double("socket", enabled?: true) }
let(:start_mode) { :on_boot }
it "enables the service to start on boot" do
expect(service).to receive(:enable)
expect(socket).to receive(:disable)
system_service.save
end
end
context "when start_mode was changed to :on_demand" do
let(:start_mode) { :on_demand }
it "enables the socket" do
expect(service).to receive(:disable)
expect(socket).to receive(:enable)
system_service.save
end
end
context "when start_mode was changed to :manual" do
let(:start_mode) { :manual }
it "disables the service and the socket" do
expect(service).to receive(:disable)
expect(socket).to receive(:disable)
system_service.save
end
end
context "when active is set to true" do
before { system_service.active = true }
context "and the service is already active" do
let(:active?) { true }
it "does not try to activate the service again" do
expect(service).to_not receive(:start)
system_service.save
end
end
context "and the service is inactive" do
let(:active?) { false }
it "tries to activate the service" do
expect(service).to receive(:start)
system_service.save
end
it "does not active the service if the status must be ignored" do
expect(service).to_not receive(:start)
system_service.save(ignore_status: true)
end
end
end
context "when active is set to false" do
before { system_service.active = false }
context "and the service is active" do
let(:active?) { true }
it "tries to stop the service" do
expect(service).to receive(:stop)
system_service.save
end
it "does not stop the service if the status must be ignored" do
expect(service).to_not receive(:start)
system_service.save(ignore_status: true)
end
end
context "and the service is inactive" do
let(:active?) { false }
it "does not try to stop the service again" do
expect(service).to_not receive(:stop)
system_service.save
end
end
end
end
describe "#changed?" do
context "when some change was made" do
before do
system_service.active = false
end
it "returns true" do
expect(system_service.changed?).to eq(true)
end
end
context "when no changes were made" do
it "returns false" do
expect(system_service.changed?).to eq(false)
end
end
end
describe "#search_terms" do
before do
allow(service).to receive(:id).and_return("cups.service")
end
context "when the service does not have an associated socket" do
let(:socket) { nil }
it "returns only the service full name" do
expect(system_service.search_terms).to contain_exactly("cups.service")
end
end
context "when the service has an associated socket" do
let(:socket) { instance_double(Yast::SystemdSocketClass::Socket, id: "cups.socket") }
it "returns the service and socket full names" do
expect(system_service.search_terms).to contain_exactly("cups.service", "cups.socket")
end
end
end
describe "#changed_value?" do
context "when no value has been changed" do
it "returns true" do
expect(system_service.changed_value?(:active)).to eq(false)
end
end
context "when some value has been changed" do
before do
system_service.active = !system_service.active
end
it "returns true" do
expect(system_service.changed_value?(:active)).to eq(true)
end
end
end
end