forked from puppetlabs/puppet
/
systemd_spec.rb
executable file
·342 lines (300 loc) · 14.5 KB
/
systemd_spec.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
#! /usr/bin/env ruby
#
# Unit testing for the systemd service Provider
#
require 'spec_helper'
describe Puppet::Type.type(:service).provider(:systemd) do
before :each do
Puppet::Type.type(:service).stubs(:defaultprovider).returns described_class
described_class.stubs(:which).with('systemctl').returns '/bin/systemctl'
end
let :provider do
described_class.new(:name => 'sshd.service')
end
osfamily = [ 'archlinux' ]
osfamily.each do |osfamily|
it "should be the default provider on #{osfamily}" do
Facter.stubs(:value).with(:osfamily).returns(osfamily)
expect(described_class).to be_default
end
end
it "should be the default provider on rhel7" do
Facter.stubs(:value).with(:osfamily).returns(:redhat)
Facter.stubs(:value).with(:operatingsystemmajrelease).returns("7")
expect(described_class).to be_default
end
[ 4, 5, 6 ].each do |ver|
it "should not be the default provider on rhel#{ver}" do
# In Ruby 1.8.7, the order of hash elements differs from 1.9+ and
# caused short-circuiting of the logic used by default.all? in the
# provider. As a workaround we need to use stubs() instead of
# expects() here.
Facter.stubs(:value).with(:osfamily).returns(:redhat)
Facter.stubs(:value).with(:operatingsystem).returns(:redhat)
Facter.stubs(:value).with(:operatingsystemmajrelease).returns("#{ver}")
expect(described_class).not_to be_default
end
end
[ 17, 18, 19, 20, 21, 22, 23 ].each do |ver|
it "should be the default provider on fedora#{ver}" do
Facter.stubs(:value).with(:osfamily).returns(:redhat)
Facter.stubs(:value).with(:operatingsystem).returns(:fedora)
Facter.stubs(:value).with(:operatingsystemmajrelease).returns("#{ver}")
expect(described_class).to be_default
end
end
it "should be the default provider on sles12" do
Facter.stubs(:value).with(:osfamily).returns(:suse)
Facter.stubs(:value).with(:operatingsystemmajrelease).returns("12")
expect(described_class).to be_default
end
it "should be the default provider on opensuse13" do
Facter.stubs(:value).with(:osfamily).returns(:suse)
Facter.stubs(:value).with(:operatingsystemmajrelease).returns("13")
expect(described_class).to be_default
end
# tumbleweed is a rolling release with date-based major version numbers
it "should be the default provider on tumbleweed" do
Facter.stubs(:value).with(:osfamily).returns(:suse)
Facter.stubs(:value).with(:operatingsystemmajrelease).returns("20150829")
expect(described_class).to be_default
end
# leap is the next generation suse release
it "should be the default provider on leap" do
Facter.stubs(:value).with(:osfamily).returns(:suse)
Facter.stubs(:value).with(:operatingsystemmajrelease).returns("42")
expect(described_class).to be_default
end
it "should not be the default provider on debian7" do
Facter.stubs(:value).with(:osfamily).returns(:debian)
Facter.stubs(:value).with(:operatingsystem).returns(:debian)
Facter.stubs(:value).with(:operatingsystemmajrelease).returns("7")
expect(described_class).not_to be_default
end
it "should be the default provider on debian8" do
Facter.stubs(:value).with(:osfamily).returns(:debian)
Facter.stubs(:value).with(:operatingsystem).returns(:debian)
Facter.stubs(:value).with(:operatingsystemmajrelease).returns("8")
expect(described_class).to be_default
end
it "should not be the default provider on ubuntu14.04" do
Facter.stubs(:value).with(:osfamily).returns(:debian)
Facter.stubs(:value).with(:operatingsystem).returns(:ubuntu)
Facter.stubs(:value).with(:operatingsystemmajrelease).returns("14.04")
expect(described_class).not_to be_default
end
it "should be the default provider on ubuntu15.04" do
Facter.stubs(:value).with(:osfamily).returns(:debian)
Facter.stubs(:value).with(:operatingsystem).returns(:ubuntu)
Facter.stubs(:value).with(:operatingsystemmajrelease).returns("15.04")
expect(described_class).to be_default
end
it "should be the default provider on ubuntu15.10" do
Facter.stubs(:value).with(:osfamily).returns(:debian)
Facter.stubs(:value).with(:operatingsystem).returns(:ubuntu)
Facter.stubs(:value).with(:operatingsystemmajrelease).returns("15.10")
expect(described_class).to be_default
end
it "should be the default provider on ubuntu16.04" do
Facter.stubs(:value).with(:osfamily).returns(:debian)
Facter.stubs(:value).with(:operatingsystem).returns(:ubuntu)
Facter.stubs(:value).with(:operatingsystemmajrelease).returns("16.04")
expect(described_class).to be_default
end
[:enabled?, :enable, :disable, :start, :stop, :status, :restart].each do |method|
it "should have a #{method} method" do
expect(provider).to respond_to(method)
end
end
describe ".instances" do
it "should have an instances method" do
expect(described_class).to respond_to :instances
end
it "should return only services" do
described_class.expects(:systemctl).with('list-unit-files', '--type', 'service', '--full', '--all', '--no-pager').returns File.read(my_fixture('list_unit_files_services'))
expect(described_class.instances.map(&:name)).to match_array(%w{
arp-ethers.service
auditd.service
autovt@.service
avahi-daemon.service
blk-availability.service
})
end
end
describe "#start" do
it "should use the supplied start command if specified" do
provider = described_class.new(Puppet::Type.type(:service).new(:name => 'sshd.service', :start => '/bin/foo'))
provider.expects(:execute).with(['/bin/foo'], :failonfail => true, :override_locale => false, :squelch => false, :combine => true)
provider.start
end
it "should start the service with systemctl start otherwise" do
provider = described_class.new(Puppet::Type.type(:service).new(:name => 'sshd.service'))
provider.expects(:systemctl).with('unmask', 'sshd.service')
provider.expects(:execute).with(['/bin/systemctl','start','sshd.service'], :failonfail => true, :override_locale => false, :squelch => false, :combine => true)
provider.start
end
end
describe "#stop" do
it "should use the supplied stop command if specified" do
provider = described_class.new(Puppet::Type.type(:service).new(:name => 'sshd.service', :stop => '/bin/foo'))
provider.expects(:execute).with(['/bin/foo'], :failonfail => true, :override_locale => false, :squelch => false, :combine => true)
provider.stop
end
it "should stop the service with systemctl stop otherwise" do
provider = described_class.new(Puppet::Type.type(:service).new(:name => 'sshd.service'))
provider.expects(:execute).with(['/bin/systemctl','stop','sshd.service'], :failonfail => true, :override_locale => false, :squelch => false, :combine => true)
provider.stop
end
end
describe "#enabled?" do
it "should return :true if the service is enabled" do
provider = described_class.new(Puppet::Type.type(:service).new(:name => 'sshd.service'))
provider.expects(:systemctl).with(
'show',
'sshd.service',
'--property', 'LoadState',
'--property', 'UnitFileState',
'--no-pager'
).returns "LoadState=loaded\nUnitFileState=enabled\n"
expect(provider.enabled?).to eq(:true)
end
it "should return :false if the service is disabled" do
provider = described_class.new(Puppet::Type.type(:service).new(:name => 'sshd.service'))
provider.expects(:systemctl).with(
'show',
'sshd.service',
'--property', 'LoadState',
'--property', 'UnitFileState',
'--no-pager'
).returns "LoadState=loaded\nUnitFileState=disabled\n"
expect(provider.enabled?).to eq(:false)
end
it "should return :false if the service is masked and the resource is attempting to be disabled" do
provider = described_class.new(Puppet::Type.type(:service).new(:name => 'sshd.service', :enable => false))
provider.expects(:systemctl).with(
'show',
'sshd.service',
'--property', 'LoadState',
'--property', 'UnitFileState',
'--no-pager'
).returns "LoadState=masked\nUnitFileState=\n"
expect(provider.enabled?).to eq(:false)
end
it "should return :mask if the service is masked and the resource is attempting to be masked" do
provider = described_class.new(Puppet::Type.type(:service).new(:name => 'sshd.service', :enable => 'mask'))
provider.expects(:systemctl).with(
'show',
'sshd.service',
'--property', 'LoadState',
'--property', 'UnitFileState',
'--no-pager'
).returns "LoadState=masked\nUnitFileState=\n"
expect(provider.enabled?).to eq(:mask)
end
end
describe "#enable" do
it "should run systemctl enable to enable a service" do
provider = described_class.new(Puppet::Type.type(:service).new(:name => 'sshd.service'))
provider.expects(:systemctl).with('unmask', 'sshd.service')
provider.expects(:systemctl).with('enable', 'sshd.service')
provider.enable
end
end
describe "#disable" do
it "should run systemctl disable to disable a service" do
provider = described_class.new(Puppet::Type.type(:service).new(:name => 'sshd.service'))
provider.expects(:systemctl).with(:disable, 'sshd.service')
provider.disable
end
end
describe "#mask" do
it "should run systemctl to disable and mask a service" do
provider = described_class.new(Puppet::Type.type(:service).new(:name => 'sshd.service'))
# :disable is the only call in the provider that uses a symbol instead of
# a string.
# This should be made consistent in the future and all tests updated.
provider.expects(:systemctl).with(:disable, 'sshd.service')
provider.expects(:systemctl).with('mask', 'sshd.service')
provider.mask
end
end
# Note: systemd provider does not care about hasstatus or a custom status
# command. I just assume that it does not make sense for systemd.
describe "#status" do
it "should return running if if the command returns 0" do
provider = described_class.new(Puppet::Type.type(:service).new(:name => 'sshd.service'))
provider.expects(:execute).with(['/bin/systemctl','is-active','sshd.service'], :failonfail => false, :override_locale => false, :squelch => false, :combine => true).returns "active\n"
$CHILD_STATUS.stubs(:exitstatus).returns(0)
expect(provider.status).to eq(:running)
end
[-10,-1,3,10].each { |ec|
it "should return stopped if the command returns something non-0" do
provider = described_class.new(Puppet::Type.type(:service).new(:name => 'sshd.service'))
provider.expects(:execute).with(['/bin/systemctl','is-active','sshd.service'], :failonfail => false, :override_locale => false, :squelch => false, :combine => true).returns "inactive\n"
$CHILD_STATUS.stubs(:exitstatus).returns(ec)
expect(provider.status).to eq(:stopped)
end
}
it "should use the supplied status command if specified" do
provider = described_class.new(Puppet::Type.type(:service).new(:name => 'sshd.service', :status => '/bin/foo'))
provider.expects(:execute).with(['/bin/foo'], :failonfail => false, :override_locale => false, :squelch => false, :combine => true)
provider.status
end
end
# Note: systemd provider does not care about hasrestart. I just assume it
# does not make sense for systemd
describe "#restart" do
it "should use the supplied restart command if specified" do
provider = described_class.new(Puppet::Type.type(:service).new(:name => 'sshd', :restart => '/bin/foo'))
provider.expects(:execute).with(['/bin/systemctl','restart','sshd.service'], :failonfail => true, :override_locale => false, :squelch => false, :combine => true).never
provider.expects(:execute).with(['/bin/foo'], :failonfail => true, :override_locale => false, :squelch => false, :combine => true)
provider.restart
end
it "should restart the service with systemctl restart" do
provider = described_class.new(Puppet::Type.type(:service).new(:name => 'sshd.service'))
provider.expects(:execute).with(['/bin/systemctl','restart','sshd.service'], :failonfail => true, :override_locale => false, :squelch => false, :combine => true)
provider.restart
end
end
describe "#debian_enabled?" do
[104, 106].each do |status|
it "should return true when invoke-rc.d returns #{status}" do
provider = described_class.new(Puppet::Type.type(:service).new(:name => 'sshd.service'))
provider.stubs(:system)
$CHILD_STATUS.expects(:exitstatus).returns(status)
expect(provider.debian_enabled?({:LoadState => "loaded", :UnitFileState => "UnitFileState"})).to eq(:true)
end
end
[101, 105].each do |status|
it "should return true when status is #{status} and there are at least 4 start links" do
provider = described_class.new(Puppet::Type.type(:service).new(:name => 'sshd.service'))
provider.stubs(:system)
provider.expects(:get_start_link_count).returns(4)
$CHILD_STATUS.expects(:exitstatus).twice.returns(status)
expect(provider.debian_enabled?({:LoadState => "loaded", :UnitFileState => "UnitFileState"})).to eq(:true)
end
it "should return false when status is #{status} and there are less than 4 start links" do
provider = described_class.new(Puppet::Type.type(:service).new(:name => 'sshd.service'))
provider.stubs(:system)
provider.expects(:get_start_link_count).returns(1)
$CHILD_STATUS.expects(:exitstatus).twice.returns(status)
expect(provider.debian_enabled?({:LoadState => "loaded", :UnitFileState => "UnitFileState"})).to eq(:false)
end
end
end
describe "#get_start_link_count" do
it "should strip the '.service' from the search if present in the resource name" do
provider = described_class.new(Puppet::Type.type(:service).new(:name => 'sshd.service'))
Dir.expects(:glob).with("/etc/rc*.d/S??sshd").returns(['files'])
provider.get_start_link_count
end
it "should use the full service name if it does not include '.service'" do
provider = described_class.new(Puppet::Type.type(:service).new(:name => 'sshd'))
Dir.expects(:glob).with("/etc/rc*.d/S??sshd").returns(['files'])
provider.get_start_link_count
end
end
it "(#16451) has command systemctl without being fully qualified" do
expect(described_class.instance_variable_get(:@commands)).to include(:systemctl => 'systemctl')
end
end