summaryrefslogtreecommitdiff
path: root/spec/unit/provider/service/freebsd_service_spec.rb
blob: cfdfbeb85a181ed154b747d3833deb63f249ed69 (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
#
# Author:: Bryan McLellan (btm@loftninjas.org)
# Copyright:: Copyright (c) 2009 Bryan McLellan
# 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::Freebsd do

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

  let(:new_resource) do
    new_resource = Chef::Resource::Service.new("apache22")
    new_resource.pattern("httpd")
    new_resource.supports({:status => false})
    new_resource
  end

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

  let(:provider) do
    events = Chef::EventDispatch::Dispatcher.new
    run_context = Chef::RunContext.new(node, {}, events)
    provider = Chef::Provider::Service::Freebsd.new(new_resource,run_context)
    provider.action = :start
    provider
  end

  # ununsed?
  #  let(:init_command) { "/usr/local/etc/rc.d/apache22" }

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

  describe "load_current_resource" do
    let(:stdout) do
      StringIO.new(<<-PS_SAMPLE)
413  ??  Ss     0:02.51 /usr/sbin/syslogd -s
539  ??  Is     0:00.14 /usr/sbin/sshd
545  ??  Ss     0:17.53 sendmail: accepting connections (sendmail)
PS_SAMPLE
    end

    let(:status) { double(:stdout => stdout, :exitstatus => 0) }

    before(:each) do
      allow(provider).to receive(:shell_out!).with(node[:command][:ps]).and_return(status)

      allow(::File).to receive(:exists?).and_return(false)
      allow(::File).to receive(:exists?).with("/usr/local/etc/rc.d/#{new_resource.service_name}").and_return(true)
      lines = double("lines")
      allow(lines).to receive(:each).and_yield("sshd_enable=\"YES\"").
                          and_yield("#{new_resource.name}_enable=\"YES\"")
      allow(::File).to receive(:open).and_return(lines)

      rc_with_name = StringIO.new(<<-RC_SAMPLE)
name="apache22"
rcvar=`set_rcvar`
RC_SAMPLE
      allow(::File).to receive(:open).with("/usr/local/etc/rc.d/#{new_resource.service_name}").and_return(rc_with_name)
      allow(provider).to receive(:service_enable_variable_name).and_return nil
    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 not raise an exception if the rcscript have a name variable" do
      provider.load_current_resource
      expect { provider.service_enable_variable_name }.not_to raise_error
    end

    describe "when the service supports status" do
      before do
        new_resource.supports({:status => true})
      end

      it "should run '/etc/init.d/service_name status'" do
        expect(provider).to receive(:shell_out).with("/usr/local/etc/rc.d/#{current_resource.service_name} status").and_return(status)
        provider.load_current_resource
      end

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

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

    describe "when a status command has been specified" do
      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.load_current_resource
      end

    end

    it "should raise error if the node has a nil ps attribute and no other means to get status" do
      node.automatic_attrs[:command] = {:ps => nil}
      provider.define_resource_requirements
      expect { provider.process_resource_requirements }.to raise_error(Chef::Exceptions::Service)
    end

    it "should raise error if the node has an empty ps attribute and no other means to get status" do
      node.automatic_attrs[:command] = {:ps => ""}
      provider.define_resource_requirements
      expect { provider.process_resource_requirements }.to raise_error(Chef::Exceptions::Service)
    end

    describe "when executing assertions" do
      it "should verify that /etc/rc.conf exists" do
        expect(::File).to receive(:exists?).with("/etc/rc.conf")
        allow(provider).to receive(:service_enable_variable_name).and_return("#{current_resource.service_name}_enable")
        provider.load_current_resource
      end

      context "and the init script is not found" do
        [ "start", "reload", "restart", "enable" ].each do |action|
          it "should raise an exception when the action is #{action}" do
            allow(::File).to receive(:exists?).and_return(false)
            provider.load_current_resource
            provider.define_resource_requirements
            expect(provider.instance_variable_get("@rcd_script_found")).to be_false
            provider.action = action
            expect { provider.process_resource_requirements }.to raise_error(Chef::Exceptions::Service)
          end
        end

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

      it "update state when current resource enabled state could not be determined" do
        expect(::File).to receive(:exists?).with("/etc/rc.conf").and_return false
        provider.load_current_resource
        expect(provider.instance_variable_get("@enabled_state_found")).to be_false
      end

      it "update state when current resource enabled state could be determined" do
        allow(::File).to receive(:exist?).with("/usr/local/etc/rc.d/#{new_resource.service_name}").and_return(true)
        expect(::File).to receive(:exists?).with("/etc/rc.conf").and_return  true
        provider.load_current_resource
        expect(provider.instance_variable_get("@enabled_state_found")).to be_false
        expect(provider.instance_variable_get("@rcd_script_found")).to be_true
        provider.define_resource_requirements
        expect { provider.process_resource_requirements }.to raise_error(Chef::Exceptions::Service,
          "Could not find the service name in /usr/local/etc/rc.d/#{current_resource.service_name} and rcvar")
      end

      it "should throw an exception if service line is missing from rc.d script" do
          pending "not implemented" do
            expect(false).to be_true
          end
      end

    end

    describe "when we have a 'ps' attribute" do
      before do
        node.automatic_attrs[:command] = {:ps => "ps -ax"}
      end

      it "should shell_out! the node's ps command" do
        expect(provider).to receive(:shell_out!).with(node[:command][:ps]).and_return(status)
        provider.load_current_resource
      end

      it "should read stdout of the ps command" do
        allow(provider).to receive(:shell_out!).and_return(status)
        expect(stdout).to receive(:each_line).and_return(true)
        provider.load_current_resource
      end

      it "should set running to true if the regex matches the output" do
        allow(stdout).to receive(:each_line).and_yield("555  ??  Ss     0:05.16 /usr/sbin/cron -s").
                                  and_yield(" 9881  ??  Ss     0:06.67 /usr/local/sbin/httpd -DNOHTTPACCEPT")
        provider.load_current_resource
        expect(current_resource.running).to be_true
      end

      it "should set running to false if the regex doesn't match" do
        allow(provider).to receive(:shell_out!).and_return(status)
        provider.load_current_resource
        expect(current_resource.running).to be_false
      end

      it "should raise an exception if ps fails" do
        allow(provider).to receive(:shell_out!).and_raise(Mixlib::ShellOut::ShellCommandFailed)
        provider.load_current_resource
        provider.define_resource_requirements
        expect { provider.process_resource_requirements }.to raise_error(Chef::Exceptions::Service)
      end
    end

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

    describe "when starting the 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")
        provider.load_current_resource
        provider.start_service()
      end

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

    describe Chef::Provider::Service::Init, "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")
        provider.load_current_resource
        provider.stop_service()
      end

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

    describe "when restarting a service" do
      it "should call 'restart' on the service_name if the resource supports it" do
        new_resource.supports({:restart => true})
        expect(provider).to receive(:shell_out!).with("/usr/local/etc/rc.d/#{new_resource.service_name} fastrestart")
        provider.load_current_resource
        provider.restart_service()
      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")
        provider.load_current_resource
        provider.restart_service()
      end
    end

    describe "when the rcscript does not have a name variable" do
      before do
        rc_with_noname = StringIO.new(<<-RC_SAMPLE)
rcvar=`set_rcvar`
RC_SAMPLE
        allow(::File).to receive(:open).with("/usr/local/etc/rc.d/#{current_resource.service_name}").and_return(rc_with_noname)
        provider.current_resource = current_resource
      end

      describe "when rcvar returns foobar_enable" do
        let(:rcvar_stdout) do
          rcvar_stdout = <<RCVAR_SAMPLE
# apache22
#
# #{current_resource.service_name}_enable="YES"
#   (default: "")
RCVAR_SAMPLE
        end

        before do
          status = double(:stdout => rcvar_stdout, :exitstatus => 0)
          allow(provider).to receive(:shell_out!).with("/usr/local/etc/rc.d/#{current_resource.service_name} rcvar").and_return(status)
        end

        it "should get the service name from rcvar if the rcscript does not have a name variable" do
          provider.load_current_resource
          allow(provider).to receive(:service_enable_variable_name).and_call_original
          expect(provider.service_enable_variable_name).to eq("#{current_resource.service_name}_enable")
        end

        it "should not raise an exception if the rcscript does not have a name variable" do
          provider.load_current_resource
          expect { provider.service_enable_variable_name }.not_to raise_error
        end
      end

      describe "when rcvar does not return foobar_enable" do
        let(:rcvar_stdout) do
          rcvar_stdout = <<RCVAR_SAMPLE
# service_with_noname
#
RCVAR_SAMPLE
        end

        before do
          status = double(:stdout => rcvar_stdout, :exitstatus => 0)
          allow(provider).to receive(:shell_out!).with("/usr/local/etc/rc.d/#{current_resource.service_name} rcvar").and_return(status)
        end

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

        [ "stop", "disable" ].each do |action|
          it "should not raise an error when the action is #{action}" do
            allow(::File).to receive(:exist?).with("/usr/local/etc/rc.d/#{new_resource.service_name}").and_return(true)
            provider.action = action
            provider.load_current_resource
            provider.define_resource_requirements
            expect { provider.process_resource_requirements }.not_to raise_error
          end
        end
      end
    end
  end

  describe Chef::Provider::Service::Freebsd, "enable_service" do
    before do
      provider.current_resource = current_resource
      allow(provider).to receive(:service_enable_variable_name).and_return("#{current_resource.service_name}_enable")
    end

    it "should enable the service if it is not enabled" do
      allow(current_resource).to receive(:enabled).and_return(false)
      expect(provider).to receive(:read_rc_conf).and_return([ "foo", "#{current_resource.service_name}_enable=\"NO\"", "bar" ])
      expect(provider).to receive(:write_rc_conf).with(["foo", "bar", "#{current_resource.service_name}_enable=\"YES\""])
      provider.enable_service()
    end

    it "should enable the service if it is not enabled and not already specified in the rc.conf file" do
      allow(current_resource).to receive(:enabled).and_return(false)
      expect(provider).to receive(:read_rc_conf).and_return([ "foo", "bar" ])
      expect(provider).to receive(:write_rc_conf).with(["foo", "bar", "#{current_resource.service_name}_enable=\"YES\""])
      provider.enable_service()
    end

    it "should not enable the service if it is already enabled" do
      allow(current_resource).to receive(:enabled).and_return(true)
      expect(provider).not_to receive(:write_rc_conf)
      provider.enable_service
    end
  end

  describe Chef::Provider::Service::Freebsd, "disable_service" do
    before do
      provider.current_resource = current_resource
      allow(provider).to receive(:service_enable_variable_name).and_return("#{current_resource.service_name}_enable")
    end

    it "should should disable the service if it is not disabled" do
      allow(current_resource).to receive(:enabled).and_return(true)
      expect(provider).to receive(:read_rc_conf).and_return([ "foo", "#{current_resource.service_name}_enable=\"YES\"", "bar" ])
      expect(provider).to receive(:write_rc_conf).with(["foo", "bar", "#{current_resource.service_name}_enable=\"NO\""])
      provider.disable_service()
    end

    it "should not disable the service if it is already disabled" do
      allow(current_resource).to receive(:enabled).and_return(false)
      expect(provider).not_to receive(:write_rc_conf)
      provider.disable_service()
    end
  end
end