summaryrefslogtreecommitdiff
path: root/spec/unit/provider/service/openbsd_service_spec.rb
blob: b990532aafe56c437615c1a8455fd69ca1694a60 (plain)
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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
#
# Author:: Bryan McLellan (btm@loftninjas.org)
# Author:: Scott Bonds (scott@ggr.com)
# Copyright:: Copyright 2009-2016, Bryan McLellan
# Copyright:: Copyright 2014-2016, Scott Bonds
# 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"

class Chef::Provider::Service::Openbsd
  public :builtin_service_enable_variable_name
  public :determine_enabled_status!
  public :determine_current_status!
  public :is_enabled?
  attr_accessor :rc_conf, :rc_conf_local
end

describe Chef::Provider::Service::Openbsd do
  let(:node) do
    node = Chef::Node.new
    node.automatic_attrs[:command] = { :ps => "ps -ax" }
    node
  end

  let(:supports) { { :status => false } }

  let(:new_resource) do
    new_resource = Chef::Resource::Service.new("sndiod")
    new_resource.pattern("sndiod")
    new_resource.supports(supports)
    new_resource
  end

  let(:current_resource) do
    current_resource = Chef::Resource::Service.new("sndiod")
    current_resource
  end

  let(:provider) do
    events = Chef::EventDispatch::Dispatcher.new
    run_context = Chef::RunContext.new(node, {}, events)
    allow(::File).to receive(:read).with("/etc/rc.conf").and_return("")
    allow(::File).to receive(:read).with("/etc/rc.conf.local").and_return("")
    provider = Chef::Provider::Service::Openbsd.new(new_resource, run_context)
    provider.action = :start
    provider
  end

  before do
    allow(Chef::Resource::Service).to receive(:new).and_return(current_resource)
  end

  def stub_etc_rcd_script
    allow(::File).to receive(:exist?).and_return(false)
    expect(::File).to receive(:exist?).with("/etc/rc.d/#{new_resource.service_name}").and_return(true)
  end

  def run_load_current_resource
    stub_etc_rcd_script
    provider.load_current_resource
  end

  describe Chef::Provider::Service::Openbsd, "initialize" do
    it "should find /etc/rc.d init scripts" do
      stub_etc_rcd_script
      expect(provider.init_command).to eql "/etc/rc.d/sndiod"
    end

    it "should set init_command to nil if it can't find anything" do
      expect(::File).to receive(:exist?).with("/etc/rc.d/sndiod").and_return(false)
      expect(provider.init_command).to be nil
    end
  end

  describe Chef::Provider::Service::Openbsd, "determine_current_status!" do
    before do
      stub_etc_rcd_script
      provider.current_resource = current_resource
      current_resource.service_name(new_resource.service_name)
    end

    context "when a status command has been specified" do
      let(:status) { double(:stdout => "", :exitstatus => 0) }

      before do
        new_resource.status_command("/bin/chefhasmonkeypants status")
      end

      it "should run the services status command if one has been specified" do
        expect(provider).to receive(:shell_out).with("/bin/chefhasmonkeypants status").and_return(status)
        provider.determine_current_status!
      end
    end

    context "when the service supports status" do
      let(:status) { double(:stdout => "", :exitstatus => 0) }

      let(:supports) { { :status => true } }

      it "should run '/etc/rc.d/service_name status'" do
        expect(provider).to receive(:shell_out).with("/etc/rc.d/#{new_resource.service_name} check").and_return(status)
        provider.determine_current_status!
      end

      it "should set running to true if the status command returns 0" do
        expect(provider).to receive(:shell_out).with("/etc/rc.d/#{new_resource.service_name} check").and_return(status)
        provider.determine_current_status!
        expect(current_resource.running).to be true
      end

      it "should set running to false if the status command returns anything except 0" do
        expect(provider).to receive(:shell_out).with("/etc/rc.d/#{new_resource.service_name} check").and_raise(Mixlib::ShellOut::ShellCommandFailed)
        provider.determine_current_status!
        expect(current_resource.running).to be false
      end
    end
  end

  describe Chef::Provider::Service::Openbsd, "determine_enabled_status!" do
    before do
      stub_etc_rcd_script
      provider.current_resource = current_resource
      current_resource.service_name(new_resource.service_name)

      allow(provider).to receive(:service_enable_variable_name).and_return("#{new_resource.service_name}_enable")
    end

    context "when the service is builtin" do
      before do
        expect(::File).to receive(:open).with("/etc/rc.d/#{new_resource.service_name}")
        provider.rc_conf = "#{provider.builtin_service_enable_variable_name}=NO"
        provider.rc_conf_local = lines.join("\n")
      end

      %w{YES Yes yes yEs YeS}.each do |setting|
        context "when the enable variable is set to #{setting}" do
          let(:lines) { [ %Q{#{provider.builtin_service_enable_variable_name}="#{setting}"} ] }
          it "sets enabled to true" do
            provider.determine_enabled_status!
            expect(current_resource.enabled).to be true
          end
        end
      end

      %w{No NO no nO None NONE none nOnE}.each do |setting|
        context "when the enable variable is set to #{setting}" do
          let(:lines) { [ %Q{#{provider.builtin_service_enable_variable_name}="#{setting}"} ] }
          it "sets enabled to false" do
            provider.determine_enabled_status!
            expect(current_resource.enabled).to be false
          end
        end
      end

      context "when the enable variable is garbage" do
        let(:lines) { [ %Q{#{provider.builtin_service_enable_variable_name}_enable="alskdjflasdkjflakdfj"} ] }
        it "sets enabled to false" do
          provider.determine_enabled_status!
          expect(current_resource.enabled).to be false
        end
      end

      context "when the enable variable partial matches (left) some other service and we are disabled" do
        let(:lines) do
          [
          %Q{thing_#{provider.builtin_service_enable_variable_name}="YES"},
          %Q{#{provider.builtin_service_enable_variable_name}="NO"},
        ] end
        it "sets enabled based on the exact match (false)" do
          provider.determine_enabled_status!
          expect(current_resource.enabled).to be false
        end
      end

      context "when the enable variable partial matches (right) some other service and we are disabled" do
        let(:lines) do
          [
          %Q{#{provider.builtin_service_enable_variable_name}_thing="YES"},
          %Q{#{provider.builtin_service_enable_variable_name}},
        ] end
        it "sets enabled based on the exact match (false)" do
          provider.determine_enabled_status!
          expect(current_resource.enabled).to be false
        end
      end

      context "when the enable variable partial matches (left) some other disabled service and we are enabled" do
        let(:lines) do
          [
          %Q{thing_#{provider.builtin_service_enable_variable_name}="NO"},
          %Q{#{provider.builtin_service_enable_variable_name}="YES"},
        ] end
        it "sets enabled based on the exact match (true)" do
          provider.determine_enabled_status!
          expect(current_resource.enabled).to be true
        end
      end

      context "when the enable variable partial matches (right) some other disabled service and we are enabled" do
        let(:lines) do
          [
          %Q{#{provider.builtin_service_enable_variable_name}_thing="NO"},
          %Q{#{provider.builtin_service_enable_variable_name}="YES"},
        ] end
        it "sets enabled based on the exact match (true)" do
          provider.determine_enabled_status!
          expect(current_resource.enabled).to be true
        end
      end

      context "when the enable variable only partial matches (left) some other enabled service" do
        let(:lines) { [ %Q{thing_#{provider.builtin_service_enable_variable_name}_enable="YES"} ] }
        it "sets enabled to false" do
          provider.determine_enabled_status!
          expect(current_resource.enabled).to be false
        end
      end

      context "when the enable variable only partial matches (right) some other enabled service" do
        let(:lines) { [ %Q{#{provider.builtin_service_enable_variable_name}_thing_enable="YES"} ] }
        it "sets enabled to false" do
          provider.determine_enabled_status!
          expect(current_resource.enabled).to be false
        end
      end

      context "when nothing matches" do
        let(:lines) { [] }
        it "sets enabled to true" do
          provider.determine_enabled_status!
          expect(current_resource.enabled).to be false
        end
      end
    end
  end

  describe Chef::Provider::Service::Openbsd, "load_current_resource" do
    before(:each) do
      stub_etc_rcd_script
      expect(provider).to receive(:determine_current_status!)
      current_resource.running(false)
      allow(provider).to receive(:service_enable_variable_name).and_return "#{new_resource.service_name}_enable"
      expect(::File).to receive(:open).with("/etc/rc.d/#{new_resource.service_name}")
    end

    it "should create a current resource with the name of the new resource" do
      expect(Chef::Resource::Service).to receive(:new).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 eq(new_resource.service_name)
    end

    it "should return the current resource" do
      expect(provider.load_current_resource).to eql(current_resource)
    end

  end

  context "when testing actions" do
    before(:each) do
      stub_etc_rcd_script
      expect(provider).to receive(:determine_current_status!)
      current_resource.running(false)
      expect(provider).to receive(:determine_enabled_status!)
      current_resource.enabled(false)
      provider.load_current_resource
    end

    describe Chef::Provider::Service::Openbsd, "start_service" do
      it "should call the start command if one is specified" do
        new_resource.start_command("/etc/rc.d/chef startyousillysally")
        expect(provider).to receive(:shell_out!).with("/etc/rc.d/chef startyousillysally", default_env: false)
        provider.start_service()
      end

      it "should call '/usr/local/etc/rc.d/service_name start' if no start command is specified" do
        expect(provider).to receive(:shell_out!).with("/etc/rc.d/#{new_resource.service_name} start", default_env: false)
        provider.start_service()
      end
    end

    describe Chef::Provider::Service::Openbsd, "stop_service" do
      it "should call the stop command if one is specified" do
        new_resource.stop_command("/etc/init.d/chef itoldyoutostop")
        expect(provider).to receive(:shell_out!).with("/etc/init.d/chef itoldyoutostop", default_env: false)
        provider.stop_service()
      end

      it "should call '/usr/local/etc/rc.d/service_name stop' if no stop command is specified" do
        expect(provider).to receive(:shell_out!).with("/etc/rc.d/#{new_resource.service_name} stop", default_env: false)
        provider.stop_service()
      end
    end

    describe Chef::Provider::Service::Openbsd, "restart_service" do
      context "when the new_resource supports restart" do
        let(:supports) { { restart: true } }
        it "should call 'restart' on the service_name if the resource supports it" do
          expect(provider).to receive(:shell_out!).with("/etc/rc.d/#{new_resource.service_name} restart", default_env: false)
          provider.restart_service()
        end
      end

      it "should call the restart_command if one has been specified" do
        new_resource.restart_command("/etc/init.d/chef restartinafire")
        expect(provider).to receive(:shell_out!).with("/etc/init.d/chef restartinafire", default_env: false)
        provider.restart_service()
      end

      it "otherwise it should call stop and start" do
        expect(provider).to receive(:stop_service)
        expect(provider).to receive(:start_service)
        provider.restart_service()
      end
    end
  end

  describe Chef::Provider::Service::Openbsd, "define_resource_requirements" do
    before do
      provider.current_resource = current_resource
    end

    context "when the init script is not found" do
      before do
        provider.init_command = nil
        allow(provider).to receive(:builtin_service_enable_variable_name).and_return("#{new_resource.service_name}_enable")
      end

      %w{start reload restart enable}.each do |action|
        it "should raise an exception when the action is #{action}" do
          provider.define_resource_requirements
          provider.action = action
          expect { provider.process_resource_requirements }.to raise_error(Chef::Exceptions::Service)
        end
      end

      %w{stop disable}.each do |action|
        it "should not raise an error when the action is #{action}" do
          provider.define_resource_requirements
          provider.action = action
          expect { provider.process_resource_requirements }.not_to raise_error
        end
      end
    end

    context "when the init script is found, but the service_enable_variable_name is nil" do
      before do
        allow(provider).to receive(:builtin_service_enable_variable_name).and_return(nil)
      end

      %w{start reload restart enable}.each do |action|
        it "should raise an exception when the action is #{action}" do
          provider.action = action
          provider.define_resource_requirements
          expect { provider.process_resource_requirements }.to raise_error(Chef::Exceptions::Service)
        end
      end

      %w{stop disable}.each do |action|
        it "should not raise an error when the action is #{action}" do
          provider.action = action
          provider.define_resource_requirements
          expect { provider.process_resource_requirements }.not_to raise_error
        end
      end
    end
  end

  describe Chef::Provider::Service::Openbsd, "enable_service" do
    before do
      provider.current_resource = current_resource
      allow(FileUtils).to receive(:touch).with("/etc/rc.conf.local")
    end
    context "is builtin and disabled by default" do
      before do
        provider.rc_conf = "#{provider.builtin_service_enable_variable_name}=NO"
      end
      context "is enabled" do
        before do
          provider.rc_conf_local = "#{provider.builtin_service_enable_variable_name}=\"\""
        end
        it "should not change rc.conf.local since it is already enabled" do
          expect(::File).not_to receive(:write)
          provider.enable_service
        end
      end
      context "is disabled" do
        before do
          provider.rc_conf_local = ""
        end
        it "should enable the service by adding a line to rc.conf.local" do
          expect(::File).to receive(:write).with("/etc/rc.conf.local", include("#{provider.builtin_service_enable_variable_name}=\"\""))
          expect(provider.is_enabled?).to be false
          provider.enable_service
          expect(provider.is_enabled?).to be true
        end
      end
    end
    context "is builtin and enabled by default" do
      before do
        provider.rc_conf = "#{provider.builtin_service_enable_variable_name}=\"\""
      end
      context "is enabled" do
        before do
          provider.rc_conf_local = ""
        end
        it "should not change rc.conf.local since it is already enabled" do
          expect(::File).not_to receive(:write)
          provider.enable_service
        end
      end
      context "is disabled" do
        before do
          provider.rc_conf_local = "#{provider.builtin_service_enable_variable_name}=NO"
        end
        it "should enable the service by removing a line from rc.conf.local" do
          expect(::File).to receive(:write).with("/etc/rc.conf.local", /^(?!#{provider.builtin_service_enable_variable_name})$/)
          expect(provider.is_enabled?).to be false
          provider.enable_service
          expect(provider.is_enabled?).to be true
        end
      end
    end
    context "is not builtin" do
      before do
        provider.rc_conf = ""
      end
      context "is enabled" do
        before do
          provider.rc_conf_local = "pkg_scripts=\"#{new_resource.service_name}\"\n"
        end
        it "should not change rc.conf.local since it is already enabled" do
          expect(::File).not_to receive(:write)
          provider.enable_service
        end
      end
      context "is disabled" do
        before do
          provider.rc_conf_local = ""
        end
        it "should enable the service by adding it to the pkg_scripts list" do
          expect(::File).to receive(:write).with("/etc/rc.conf.local", "\npkg_scripts=\"#{new_resource.service_name}\"\n")
          expect(provider.is_enabled?).to be false
          provider.enable_service
          expect(provider.is_enabled?).to be true
        end
      end
    end
  end

  describe Chef::Provider::Service::Openbsd, "disable_service" do
    before do
      provider.current_resource = current_resource
      allow(FileUtils).to receive(:touch).with("/etc/rc.conf.local")
    end
    context "is builtin and disabled by default" do
      before do
        provider.rc_conf = "#{provider.builtin_service_enable_variable_name}=NO"
      end
      context "is enabled" do
        before do
          provider.rc_conf_local = "#{provider.builtin_service_enable_variable_name}=\"\""
        end
        it "should disable the service by removing its line from rc.conf.local" do
          expect(::File).to receive(:write).with("/etc/rc.conf.local", /^(?!#{provider.builtin_service_enable_variable_name})$/)
          expect(provider.is_enabled?).to be true
          provider.disable_service
          expect(provider.is_enabled?).to be false
        end
      end
      context "is disabled" do
        before do
          provider.rc_conf_local = ""
        end
        it "should not change rc.conf.local since it is already disabled" do
          expect(::File).not_to receive(:write)
          provider.disable_service
        end
      end
    end
    context "is builtin and enabled by default" do
      before do
        provider.rc_conf = "#{provider.builtin_service_enable_variable_name}=\"\""
      end
      context "is enabled" do
        before do
          provider.rc_conf_local = ""
        end
        it "should disable the service by adding a line to rc.conf.local" do
          expect(::File).to receive(:write).with("/etc/rc.conf.local", include("#{provider.builtin_service_enable_variable_name}=\"NO\""))
          expect(provider.is_enabled?).to be true
          provider.disable_service
          expect(provider.is_enabled?).to be false
        end
      end
      context "is disabled" do
        before do
          provider.rc_conf_local = "#{provider.builtin_service_enable_variable_name}=NO"
        end
        it "should not change rc.conf.local since it is already disabled" do
          expect(::File).not_to receive(:write)
          provider.disable_service
        end
      end
    end
    context "is not builtin" do
      before do
        provider.rc_conf = ""
      end
      context "is enabled" do
        before do
          provider.rc_conf_local = "pkg_scripts=\"#{new_resource.service_name}\"\n"
        end
        it "should disable the service by removing it from the pkg_scripts list" do
          expect(::File).to receive(:write).with("/etc/rc.conf.local", /^(?!#{new_resource.service_name})$/)
          expect(provider.is_enabled?).to be true
          provider.disable_service
          expect(provider.is_enabled?).to be false
        end
      end
      context "is disabled" do
        before do
          provider.rc_conf_local = ""
        end
        it "should not change rc.conf.local since it is already disabled" do
          expect(::File).not_to receive(:write)
          provider.disable_service
        end
      end
    end
  end

end