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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
|
#! /usr/bin/env ruby
require 'spec_helper'
describe Puppet::Type.type(:service).provider(:upstart) do
let(:manual) { "\nmanual" }
let(:start_on_default_runlevels) { "\nstart on runlevel [2,3,4,5]" }
let(:provider_class) { Puppet::Type.type(:service).provider(:upstart) }
def given_contents_of(file, content)
File.open(file, 'w') do |file|
file.write(content)
end
end
def then_contents_of(file)
File.open(file).read
end
def lists_processes_as(output)
Puppet::Util::Execution.stubs(:execpipe).with("/sbin/initctl list").yields(output)
provider_class.stubs(:which).with("/sbin/initctl").returns("/sbin/initctl")
end
it "should be the default provider on Ubuntu" do
Facter.expects(:value).with(:operatingsystem).returns("Ubuntu")
described_class.default?.should be_true
end
describe "excluding services" do
it "ignores tty and serial on Redhat systems" do
Facter.stubs(:value).with(:osfamily).returns('RedHat')
expect(described_class.excludes).to include 'serial'
expect(described_class.excludes).to include 'tty'
end
end
describe "#instances" do
it "should be able to find all instances" do
lists_processes_as("rc stop/waiting\nssh start/running, process 712")
provider_class.instances.map {|provider| provider.name}.should =~ ["rc","ssh"]
end
it "should attach the interface name for network interfaces" do
lists_processes_as("network-interface (eth0)")
provider_class.instances.first.name.should == "network-interface INTERFACE=eth0"
end
it "should attach the job name for network interface security" do
processes = "network-interface-security (network-interface/eth0)"
provider_class.stubs(:execpipe).yields(processes)
provider_class.instances.first.name.should == "network-interface-security JOB=network-interface/eth0"
end
it "should not find excluded services" do
processes = "wait-for-state stop/waiting"
processes += "\nportmap-wait start/running"
processes += "\nidmapd-mounting stop/waiting"
processes += "\nstartpar-bridge start/running"
processes += "\ncryptdisks-udev stop/waiting"
processes += "\nstatd-mounting stop/waiting"
processes += "\ngssd-mounting stop/waiting"
provider_class.stubs(:execpipe).yields(processes)
provider_class.instances.should be_empty
end
end
describe "#search" do
it "searches through paths to find a matching conf file" do
File.stubs(:directory?).returns(true)
Puppet::FileSystem.stubs(:exist?).returns(false)
Puppet::FileSystem.expects(:exist?).with("/etc/init/foo-bar.conf").returns(true)
resource = Puppet::Type.type(:service).new(:name => "foo-bar", :provider => :upstart)
provider = provider_class.new(resource)
provider.initscript.should == "/etc/init/foo-bar.conf"
end
it "searches for just the name of a compound named service" do
File.stubs(:directory?).returns(true)
Puppet::FileSystem.stubs(:exist?).returns(false)
Puppet::FileSystem.expects(:exist?).with("/etc/init/network-interface.conf").returns(true)
resource = Puppet::Type.type(:service).new(:name => "network-interface INTERFACE=lo", :provider => :upstart)
provider = provider_class.new(resource)
provider.initscript.should == "/etc/init/network-interface.conf"
end
end
describe "#status" do
it "should use the default status command if none is specified" do
resource = Puppet::Type.type(:service).new(:name => "foo", :provider => :upstart)
provider = provider_class.new(resource)
provider.stubs(:is_upstart?).returns(true)
provider.expects(:status_exec).with(["foo"]).returns("foo start/running, process 1000")
Process::Status.any_instance.stubs(:exitstatus).returns(0)
provider.status.should == :running
end
it "should properly handle services with 'start' in their name" do
resource = Puppet::Type.type(:service).new(:name => "foostartbar", :provider => :upstart)
provider = provider_class.new(resource)
provider.stubs(:is_upstart?).returns(true)
provider.expects(:status_exec).with(["foostartbar"]).returns("foostartbar stop/waiting")
Process::Status.any_instance.stubs(:exitstatus).returns(0)
provider.status.should == :stopped
end
end
describe "inheritance" do
let :resource do
resource = Puppet::Type.type(:service).new(:name => "foo", :provider => :upstart)
end
let :provider do
provider = provider_class.new(resource)
end
describe "when upstart job" do
before(:each) do
provider.stubs(:is_upstart?).returns(true)
end
["start", "stop"].each do |command|
it "should return the #{command}cmd of its parent provider" do
provider.send("#{command}cmd".to_sym).should == [provider.command(command.to_sym), resource.name]
end
end
it "should return nil for the statuscmd" do
provider.statuscmd.should be_nil
end
end
end
describe "should be enableable" do
let :resource do
Puppet::Type.type(:service).new(:name => "foo", :provider => :upstart)
end
let :provider do
provider_class.new(resource)
end
let :init_script do
PuppetSpec::Files.tmpfile("foo.conf")
end
let :over_script do
PuppetSpec::Files.tmpfile("foo.override")
end
let :disabled_content do
"\t # \t start on\nother file stuff"
end
let :multiline_disabled do
"# \t start on other file stuff (\n" +
"# more stuff ( # )))))inline comment\n" +
"# finishing up )\n" +
"# and done )\n" +
"this line shouldn't be touched\n"
end
let :multiline_disabled_bad do
"# \t start on other file stuff (\n" +
"# more stuff ( # )))))inline comment\n" +
"# finishing up )\n" +
"# and done )\n" +
"# this is a comment i want to be a comment\n" +
"this line shouldn't be touched\n"
end
let :multiline_enabled_bad do
" \t start on other file stuff (\n" +
" more stuff ( # )))))inline comment\n" +
" finishing up )\n" +
" and done )\n" +
"# this is a comment i want to be a comment\n" +
"this line shouldn't be touched\n"
end
let :multiline_enabled do
" \t start on other file stuff (\n" +
" more stuff ( # )))))inline comment\n" +
" finishing up )\n" +
" and done )\n" +
"this line shouldn't be touched\n"
end
let :multiline_enabled_standalone do
" \t start on other file stuff (\n" +
" more stuff ( # )))))inline comment\n" +
" finishing up )\n" +
" and done )\n"
end
let :enabled_content do
"\t \t start on\nother file stuff"
end
let :content do
"just some text"
end
describe "Upstart version < 0.6.7" do
before(:each) do
provider.stubs(:is_upstart?).returns(true)
provider.stubs(:upstart_version).returns("0.6.5")
provider.stubs(:search).returns(init_script)
end
[:enabled?,:enable,:disable].each do |enableable|
it "should respond to #{enableable}" do
provider.should respond_to(enableable)
end
end
describe "when enabling" do
it "should open and uncomment the '#start on' line" do
given_contents_of(init_script, disabled_content)
provider.enable
then_contents_of(init_script).should == enabled_content
end
it "should add a 'start on' line if none exists" do
given_contents_of(init_script, "this is a file")
provider.enable
then_contents_of(init_script).should == "this is a file" + start_on_default_runlevels
end
it "should handle multiline 'start on' stanzas" do
given_contents_of(init_script, multiline_disabled)
provider.enable
then_contents_of(init_script).should == multiline_enabled
end
it "should leave not 'start on' comments alone" do
given_contents_of(init_script, multiline_disabled_bad)
provider.enable
then_contents_of(init_script).should == multiline_enabled_bad
end
end
describe "when disabling" do
it "should open and comment the 'start on' line" do
given_contents_of(init_script, enabled_content)
provider.disable
then_contents_of(init_script).should == "#" + enabled_content
end
it "should handle multiline 'start on' stanzas" do
given_contents_of(init_script, multiline_enabled)
provider.disable
then_contents_of(init_script).should == multiline_disabled
end
end
describe "when checking whether it is enabled" do
it "should consider 'start on ...' to be enabled" do
given_contents_of(init_script, enabled_content)
provider.enabled?.should == :true
end
it "should consider '#start on ...' to be disabled" do
given_contents_of(init_script, disabled_content)
provider.enabled?.should == :false
end
it "should consider no start on line to be disabled" do
given_contents_of(init_script, content)
provider.enabled?.should == :false
end
end
end
describe "Upstart version < 0.9.0" do
before(:each) do
provider.stubs(:is_upstart?).returns(true)
provider.stubs(:upstart_version).returns("0.7.0")
provider.stubs(:search).returns(init_script)
end
[:enabled?,:enable,:disable].each do |enableable|
it "should respond to #{enableable}" do
provider.should respond_to(enableable)
end
end
describe "when enabling" do
it "should open and uncomment the '#start on' line" do
given_contents_of(init_script, disabled_content)
provider.enable
then_contents_of(init_script).should == enabled_content
end
it "should add a 'start on' line if none exists" do
given_contents_of(init_script, "this is a file")
provider.enable
then_contents_of(init_script).should == "this is a file" + start_on_default_runlevels
end
it "should handle multiline 'start on' stanzas" do
given_contents_of(init_script, multiline_disabled)
provider.enable
then_contents_of(init_script).should == multiline_enabled
end
it "should remove manual stanzas" do
given_contents_of(init_script, multiline_enabled + manual)
provider.enable
then_contents_of(init_script).should == multiline_enabled
end
it "should leave not 'start on' comments alone" do
given_contents_of(init_script, multiline_disabled_bad)
provider.enable
then_contents_of(init_script).should == multiline_enabled_bad
end
end
describe "when disabling" do
it "should add a manual stanza" do
given_contents_of(init_script, enabled_content)
provider.disable
then_contents_of(init_script).should == enabled_content + manual
end
it "should remove manual stanzas before adding new ones" do
given_contents_of(init_script, multiline_enabled + manual + "\n" + multiline_enabled)
provider.disable
then_contents_of(init_script).should == multiline_enabled + "\n" + multiline_enabled + manual
end
it "should handle multiline 'start on' stanzas" do
given_contents_of(init_script, multiline_enabled)
provider.disable
then_contents_of(init_script).should == multiline_enabled + manual
end
end
describe "when checking whether it is enabled" do
describe "with no manual stanza" do
it "should consider 'start on ...' to be enabled" do
given_contents_of(init_script, enabled_content)
provider.enabled?.should == :true
end
it "should consider '#start on ...' to be disabled" do
given_contents_of(init_script, disabled_content)
provider.enabled?.should == :false
end
it "should consider no start on line to be disabled" do
given_contents_of(init_script, content)
provider.enabled?.should == :false
end
end
describe "with manual stanza" do
it "should consider 'start on ...' to be disabled if there is a trailing manual stanza" do
given_contents_of(init_script, enabled_content + manual + "\nother stuff")
provider.enabled?.should == :false
end
it "should consider two start on lines with a manual in the middle to be enabled" do
given_contents_of(init_script, enabled_content + manual + "\n" + enabled_content)
provider.enabled?.should == :true
end
end
end
end
describe "Upstart version > 0.9.0" do
before(:each) do
provider.stubs(:is_upstart?).returns(true)
provider.stubs(:upstart_version).returns("0.9.5")
provider.stubs(:search).returns(init_script)
provider.stubs(:overscript).returns(over_script)
end
[:enabled?,:enable,:disable].each do |enableable|
it "should respond to #{enableable}" do
provider.should respond_to(enableable)
end
end
describe "when enabling" do
it "should add a 'start on' line if none exists" do
given_contents_of(init_script, "this is a file")
provider.enable
then_contents_of(init_script).should == "this is a file"
then_contents_of(over_script).should == start_on_default_runlevels
end
it "should handle multiline 'start on' stanzas" do
given_contents_of(init_script, multiline_disabled)
provider.enable
then_contents_of(init_script).should == multiline_disabled
then_contents_of(over_script).should == start_on_default_runlevels
end
it "should remove any manual stanzas from the override file" do
given_contents_of(over_script, manual)
given_contents_of(init_script, enabled_content)
provider.enable
then_contents_of(init_script).should == enabled_content
then_contents_of(over_script).should == ""
end
it "should copy existing start on from conf file if conf file is disabled" do
given_contents_of(init_script, multiline_enabled_standalone + manual)
provider.enable
then_contents_of(init_script).should == multiline_enabled_standalone + manual
then_contents_of(over_script).should == multiline_enabled_standalone
end
it "should leave not 'start on' comments alone" do
given_contents_of(init_script, multiline_disabled_bad)
given_contents_of(over_script, "")
provider.enable
then_contents_of(init_script).should == multiline_disabled_bad
then_contents_of(over_script).should == start_on_default_runlevels
end
end
describe "when disabling" do
it "should add a manual stanza to the override file" do
given_contents_of(init_script, enabled_content)
provider.disable
then_contents_of(init_script).should == enabled_content
then_contents_of(over_script).should == manual
end
it "should handle multiline 'start on' stanzas" do
given_contents_of(init_script, multiline_enabled)
provider.disable
then_contents_of(init_script).should == multiline_enabled
then_contents_of(over_script).should == manual
end
end
describe "when checking whether it is enabled" do
describe "with no override file" do
it "should consider 'start on ...' to be enabled" do
given_contents_of(init_script, enabled_content)
provider.enabled?.should == :true
end
it "should consider '#start on ...' to be disabled" do
given_contents_of(init_script, disabled_content)
provider.enabled?.should == :false
end
it "should consider no start on line to be disabled" do
given_contents_of(init_script, content)
provider.enabled?.should == :false
end
end
describe "with override file" do
it "should consider 'start on ...' to be disabled if there is manual in override file" do
given_contents_of(init_script, enabled_content)
given_contents_of(over_script, manual + "\nother stuff")
provider.enabled?.should == :false
end
it "should consider '#start on ...' to be enabled if there is a start on in the override file" do
given_contents_of(init_script, disabled_content)
given_contents_of(over_script, "start on stuff")
provider.enabled?.should == :true
end
end
end
end
end
end
|