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
|
#
# Author:: Stephen Haynes (<sh@nomitor.com>)
# Author:: Davide Cavalca (<dcavalca@fb.com>)
# Copyright:: Copyright 2011-2018, Chef Software Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
require "spec_helper"
describe Chef::Provider::Service::Systemd do
let(:node) { Chef::Node.new }
let(:events) { Chef::EventDispatch::Dispatcher.new }
let(:run_context) { Chef::RunContext.new(node, {}, events) }
let(:service_name) { "rsyslog\\x2d.service" }
let(:service_name_escaped) { "rsyslog\\\\x2d.service" }
let(:new_resource) { Chef::Resource::Service.new(service_name) }
let(:provider) { Chef::Provider::Service::Systemd.new(new_resource, run_context) }
let(:shell_out_success) do
double("shell_out", exitstatus: 0, error?: false)
end
let(:shell_out_failure) do
double("shell_out", exitstatus: 1, error?: true)
end
let(:current_resource) { Chef::Resource::Service.new(service_name) }
before(:each) do
allow(Chef::Resource::Service).to receive(:new).with(service_name).and_return(current_resource)
allow(Etc).to receive(:getpwnam).and_return(OpenStruct.new(uid: 10000))
end
describe "load_current_resource" do
before(:each) do
allow(provider).to receive(:is_active?).and_return(false)
allow(provider).to receive(:is_enabled?).and_return(false)
allow(provider).to receive(:is_masked?).and_return(false)
end
it "should create a current resource with the name of the new resource" do
expect(Chef::Resource::Service).to receive(:new).with(new_resource.name).and_return(current_resource)
provider.load_current_resource
end
it "should set the current resources service name to the new resources service name" do
provider.load_current_resource
expect(current_resource.service_name).to eql(service_name)
end
it "should check if the service is running" do
expect(provider).to receive(:is_active?)
provider.load_current_resource
end
it "should set running to true if the service is running" do
allow(provider).to receive(:is_active?).and_return(true)
provider.load_current_resource
expect(current_resource.running).to be true
end
it "should set running to false if the service is not running" do
allow(provider).to receive(:is_active?).and_return(false)
provider.load_current_resource
expect(current_resource.running).to be false
end
describe "when a status command has been specified" do
before do
allow(new_resource).to receive(:status_command).and_return("/bin/chefhasmonkeypants status")
end
it "should run the services status command if one has been specified" do
allow(provider).to receive(:shell_out).and_return(shell_out_success)
provider.load_current_resource
expect(current_resource.running).to be true
end
it "should run the services status command if one has been specified and properly set status check state" do
allow(provider).to receive(:shell_out).with("/bin/chefhasmonkeypants status").and_return(shell_out_success)
provider.load_current_resource
expect(provider.status_check_success).to be true
end
it "should set running to false if a status command fails" do
allow(provider).to receive(:shell_out).and_return(shell_out_failure)
provider.load_current_resource
expect(current_resource.running).to be false
end
it "should update state to indicate status check failed when a status command fails" do
allow(provider).to receive(:shell_out).and_return(shell_out_failure)
provider.load_current_resource
expect(provider.status_check_success).to be false
end
end
it "should check if the service is enabled" do
expect(provider).to receive(:is_enabled?)
provider.load_current_resource
end
it "should set enabled to true if the service is enabled" do
allow(provider).to receive(:is_enabled?).and_return(true)
provider.load_current_resource
expect(current_resource.enabled).to be true
end
it "should set enabled to false if the service is not enabled" do
allow(provider).to receive(:is_enabled?).and_return(false)
provider.load_current_resource
expect(current_resource.enabled).to be false
end
it "should check if the service is masked" do
expect(provider).to receive(:is_masked?)
provider.load_current_resource
end
it "should set masked to true if the service is masked" do
allow(provider).to receive(:is_masked?).and_return(true)
provider.load_current_resource
expect(current_resource.masked).to be true
end
it "should set masked to false if the service is not masked" do
allow(provider).to receive(:is_masked?).and_return(false)
provider.load_current_resource
expect(current_resource.masked).to be false
end
it "should return the current resource" do
expect(provider.load_current_resource).to eql(current_resource)
end
end
def setup_current_resource
provider.current_resource = current_resource
current_resource.service_name(service_name)
end
%w{/usr/bin/systemctl /bin/systemctl}.each do |systemctl_path|
describe "when systemctl path is #{systemctl_path}" do
before(:each) do
setup_current_resource
allow(provider).to receive(:which).with("systemctl").and_return(systemctl_path)
end
describe "start and stop service" do
it "should call the start command if one is specified" do
allow(new_resource).to receive(:start_command).and_return("/sbin/rsyslog startyousillysally")
expect(provider).to receive(:shell_out!).with("/sbin/rsyslog startyousillysally", default_env: false)
provider.start_service
end
context "when a user is not specified" do
it "should call '#{systemctl_path} --system start service_name' if no start command is specified" do
expect(provider).to receive(:shell_out!).with("#{systemctl_path} --system start #{service_name_escaped}", default_env: false).and_return(shell_out_success)
provider.start_service
end
it "should not call '#{systemctl_path} --system start service_name' if it is already running" do
current_resource.running(true)
expect(provider).not_to receive(:shell_out!).with("#{systemctl_path} --system start #{service_name_escaped}", {})
provider.start_service
end
end
context "when a user is specified" do
it "should call '#{systemctl_path} --user start service_name' if no start command is specified" do
current_resource.user("joe")
expect(provider).to receive(:shell_out!).with("#{systemctl_path} --user start #{service_name_escaped}", { environment: { "DBUS_SESSION_BUS_ADDRESS" => "unix:path=/run/user/10000/bus" }, user: "joe", default_env: false }).and_return(shell_out_success)
provider.start_service
end
it "should not call '#{systemctl_path} --user start service_name' if it is already running" do
current_resource.running(true)
current_resource.user("joe")
expect(provider).not_to receive(:shell_out!).with("#{systemctl_path} --user start #{service_name_escaped}", { environment: { "DBUS_SESSION_BUS_ADDRESS" => "unix:path=/run/user/10000/bus" }, user: "joe" })
provider.start_service
end
end
it "should call the restart command if one is specified" do
current_resource.running(true)
allow(new_resource).to receive(:restart_command).and_return("/sbin/rsyslog restartyousillysally")
expect(provider).to receive(:shell_out!).with("/sbin/rsyslog restartyousillysally", default_env: false)
provider.restart_service
end
it "should call '#{systemctl_path} --system restart service_name' if no restart command is specified" do
current_resource.running(true)
expect(provider).to receive(:shell_out!).with("#{systemctl_path} --system restart #{service_name_escaped}", default_env: false).and_return(shell_out_success)
provider.restart_service
end
describe "reload service" do
context "when a reload command is specified" do
it "should call the reload command" do
current_resource.running(true)
allow(new_resource).to receive(:reload_command).and_return("/sbin/rsyslog reloadyousillysally")
expect(provider).to receive(:shell_out!).with("/sbin/rsyslog reloadyousillysally", default_env: false)
provider.reload_service
end
end
context "when a reload command is not specified" do
it "should call '#{systemctl_path} --system reload service_name' if the service is running" do
current_resource.running(true)
expect(provider).to receive(:shell_out!).with("#{systemctl_path} --system reload #{service_name_escaped}", default_env: false).and_return(shell_out_success)
provider.reload_service
end
it "should start the service if the service is not running" do
current_resource.running(false)
expect(provider).to receive(:start_service).and_return(true)
provider.reload_service
end
end
end
it "should call the stop command if one is specified" do
current_resource.running(true)
allow(new_resource).to receive(:stop_command).and_return("/sbin/rsyslog stopyousillysally")
expect(provider).to receive(:shell_out!).with("/sbin/rsyslog stopyousillysally", default_env: false)
provider.stop_service
end
it "should call '#{systemctl_path} --system stop service_name' if no stop command is specified" do
current_resource.running(true)
expect(provider).to receive(:shell_out!).with("#{systemctl_path} --system stop #{service_name_escaped}", default_env: false).and_return(shell_out_success)
provider.stop_service
end
it "should not call '#{systemctl_path} --system stop service_name' if it is already stopped" do
current_resource.running(false)
expect(provider).not_to receive(:shell_out!).with("#{systemctl_path} --system stop #{service_name_escaped}", {})
provider.stop_service
end
end
describe "enable and disable service" do
before(:each) do
provider.current_resource = current_resource
current_resource.service_name(service_name)
allow(provider).to receive(:which).with("systemctl").and_return("#{systemctl_path}")
end
it "should call '#{systemctl_path} --system enable service_name' to enable the service" do
expect(provider).to receive(:shell_out!).with("#{systemctl_path} --system enable #{service_name_escaped}", {}).and_return(shell_out_success)
provider.enable_service
end
it "should call '#{systemctl_path} --system disable service_name' to disable the service" do
expect(provider).to receive(:shell_out!).with("#{systemctl_path} --system disable #{service_name_escaped}", {}).and_return(shell_out_success)
provider.disable_service
end
end
describe "mask and unmask service" do
before(:each) do
provider.current_resource = current_resource
current_resource.service_name(service_name)
allow(provider).to receive(:which).with("systemctl").and_return("#{systemctl_path}")
end
it "should call '#{systemctl_path} --system mask service_name' to mask the service" do
expect(provider).to receive(:shell_out!).with("#{systemctl_path} --system mask #{service_name_escaped}", {}).and_return(shell_out_success)
provider.mask_service
end
it "should call '#{systemctl_path} --system unmask service_name' to unmask the service" do
expect(provider).to receive(:shell_out!).with("#{systemctl_path} --system unmask #{service_name_escaped}", {}).and_return(shell_out_success)
provider.unmask_service
end
end
describe "is_active?" do
before(:each) do
provider.current_resource = current_resource
current_resource.service_name(service_name)
allow(provider).to receive(:which).with("systemctl").and_return("#{systemctl_path}")
end
it "should return true if '#{systemctl_path} --system is-active service_name' returns 0" do
expect(provider).to receive(:shell_out).with("#{systemctl_path} --system is-active #{service_name_escaped} --quiet", {}).and_return(shell_out_success)
expect(provider.is_active?).to be true
end
it "should return false if '#{systemctl_path} --system is-active service_name' returns anything except 0" do
expect(provider).to receive(:shell_out).with("#{systemctl_path} --system is-active #{service_name_escaped} --quiet", {}).and_return(shell_out_failure)
expect(provider.is_active?).to be false
end
end
describe "is_enabled?" do
before(:each) do
provider.current_resource = current_resource
current_resource.service_name(service_name)
allow(provider).to receive(:which).with("systemctl").and_return("#{systemctl_path}")
end
it "should return true if '#{systemctl_path} --system is-enabled service_name' returns 0" do
expect(provider).to receive(:shell_out).with("#{systemctl_path} --system is-enabled #{service_name_escaped} --quiet", {}).and_return(shell_out_success)
expect(provider.is_enabled?).to be true
end
it "should return false if '#{systemctl_path} --system is-enabled service_name' returns anything except 0" do
expect(provider).to receive(:shell_out).with("#{systemctl_path} --system is-enabled #{service_name_escaped} --quiet", {}).and_return(shell_out_failure)
expect(provider.is_enabled?).to be false
end
end
describe "is_masked?" do
before(:each) do
provider.current_resource = current_resource
current_resource.service_name(service_name)
allow(provider).to receive(:which).with("systemctl").and_return("#{systemctl_path}")
end
it "should return true if '#{systemctl_path} --system is-enabled service_name' returns 'masked' and returns anything except 0" do
expect(provider).to receive(:shell_out).with("#{systemctl_path} --system is-enabled #{service_name_escaped}", {}).and_return(double(stdout: "masked", exitstatus: shell_out_failure))
expect(provider.is_masked?).to be true
end
it "should return true if '#{systemctl_path} --system is-enabled service_name' outputs 'masked-runtime' and returns anything except 0" do
expect(provider).to receive(:shell_out).with("#{systemctl_path} --system is-enabled #{service_name_escaped}", {}).and_return(double(stdout: "masked-runtime", exitstatus: shell_out_failure))
expect(provider.is_masked?).to be true
end
it "should return false if '#{systemctl_path} --system is-enabled service_name' returns 0" do
expect(provider).to receive(:shell_out).with("#{systemctl_path} --system is-enabled #{service_name_escaped}", {}).and_return(double(stdout: "enabled", exitstatus: shell_out_success))
expect(provider.is_masked?).to be false
end
it "should return false if '#{systemctl_path} --system is-enabled service_name' returns anything except 0 and outputs an error'" do
expect(provider).to receive(:shell_out).with("#{systemctl_path} --system is-enabled #{service_name_escaped}", {}).and_return(double(stdout: "Failed to get unit file state for #{service_name}: No such file or directory", exitstatus: shell_out_failure))
expect(provider.is_masked?).to be false
end
end
end
end
end
|