summaryrefslogtreecommitdiff
path: root/spec/unit/provider/service/upstart_service_spec.rb
blob: 2fc49c7aa2df71dc4654658ae8e260e2aef1684c (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
#
# Author:: Bryan McLellan (btm@loftninjas.org)
# Copyright:: Copyright (c) 2010 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::Upstart do
  before(:each) do
    @node =Chef::Node.new
    @node.name('upstarter')
    @node.automatic_attrs[:platform] = 'ubuntu'
    @node.automatic_attrs[:platform_version] = '9.10'

    @events = Chef::EventDispatch::Dispatcher.new
    @run_context = Chef::RunContext.new(@node, {}, @events)

    @new_resource = Chef::Resource::Service.new("rsyslog")
    @provider = Chef::Provider::Service::Upstart.new(@new_resource, @run_context)
  end

  describe "when first created" do
    before do
      @platform = nil
    end

    it "should return /etc/event.d as the upstart job directory when running on Ubuntu 9.04" do
      @node.automatic_attrs[:platform_version] = '9.04'
      #Chef::Platform.stub!(:find_platform_and_version).and_return([ "ubuntu", "9.04" ])
      @provider = Chef::Provider::Service::Upstart.new(@new_resource, @run_context)
      @provider.instance_variable_get(:@upstart_job_dir).should == "/etc/event.d"
      @provider.instance_variable_get(:@upstart_conf_suffix).should == ""
    end

    it "should return /etc/init as the upstart job directory when running on Ubuntu 9.10" do
      @node.automatic_attrs[:platform_version] = '9.10'
      @provider = Chef::Provider::Service::Upstart.new(@new_resource, @run_context)
      @provider.instance_variable_get(:@upstart_job_dir).should == "/etc/init"
      @provider.instance_variable_get(:@upstart_conf_suffix).should == ".conf"
    end

    it "should return /etc/init as the upstart job directory by default" do
      @node.automatic_attrs[:platform_version] = '9000'
      @provider = Chef::Provider::Service::Upstart.new(@new_resource, @run_context)
      @provider.instance_variable_get(:@upstart_job_dir).should == "/etc/init"
      @provider.instance_variable_get(:@upstart_conf_suffix).should == ".conf"
    end
  end

  describe "load_current_resource" do
    before(:each) do
      @node.automatic_attrs[:command] = {:ps => "ps -ax"}

      @current_resource = Chef::Resource::Service.new("rsyslog")
      Chef::Resource::Service.stub!(:new).and_return(@current_resource)

      @status = mock("Status", :exitstatus => 0)
      @provider.stub!(:popen4).and_return(@status)
      @stdin = StringIO.new
      @stdout = StringIO.new
      @stderr = StringIO.new
      @pid = mock("PID")
      
      ::File.stub!(:exists?).and_return(true)
      ::File.stub!(:open).and_return(true)
    end

    it "should create a current resource with the name of the new resource" do
      Chef::Resource::Service.should_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
      @current_resource.should_receive(:service_name).with(@new_resource.service_name)
      @provider.load_current_resource
    end

    it "should run '/sbin/status rsyslog'" do
      @provider.should_receive(:popen4).with("/sbin/status rsyslog").and_return(@status)
      @provider.load_current_resource
    end

    describe "when the status command uses the new format" do
      before do
      end

      it "should set running to true if the the status command returns 0" do
        @stdout = StringIO.new("rsyslog start/running")
        @provider.stub!(:popen4).and_yield(@pid, @stdin, @stdout, @stderr).and_return(@status)
        @provider.load_current_resource
        @current_resource.running.should be_true
      end

      it "should set running to false if the status command returns anything except 0" do
        @stdout = StringIO.new("rsyslog stop/waiting")
        @provider.stub!(:popen4).and_yield(@pid, @stdin, @stdout, @stderr).and_return(@status)
        @provider.load_current_resource
        @current_resource.running.should be_false
      end
    end

    describe "when the status command uses the old format" do
      it "should set running to true if the the status command returns 0" do
        @stdout = StringIO.new("rsyslog (start) running, process 32225")
        @provider.stub!(:popen4).and_yield(@pid, @stdin, @stdout, @stderr).and_return(@status)
        @provider.load_current_resource
        @current_resource.running.should be_true
      end

      it "should set running to false if the status command returns anything except 0" do
        @stdout = StringIO.new("rsyslog (stop) waiting")
        @provider.stub!(:popen4).and_yield(@pid, @stdin, @stdout, @stderr).and_return(@status)
        @provider.load_current_resource
        @current_resource.running.should be_false
      end
    end

    it "should set running to false if it catches a Chef::Exceptions::Exec" do
      @provider.stub!(:popen4).and_yield(@pid, @stdin, @stdout, @stderr).and_raise(Chef::Exceptions::Exec)
      @current_resource.should_receive(:running).with(false)
      @provider.load_current_resource
    end

    it "should set enabled to true when it finds 'starts on'" do
      @lines = mock("start on filesystem", :gets => "start on filesystem")
      ::File.stub!(:open).and_yield(@lines)
      @current_resource.should_receive(:running).with(false)
      @provider.load_current_resource
    end

    it "should set enabled to false when it finds '#starts on'" do
      @lines = mock("start on filesystem", :gets => "#start on filesystem")
      ::File.stub!(:open).and_yield(@lines)
      @current_resource.should_receive(:running).with(false)
      @provider.load_current_resource
    end

    it "should assume disable when no job configuration file is found" do
      ::File.stub!(:exists?).and_return(false)
      @current_resource.should_receive(:running).with(false)
      @provider.load_current_resource
    end


    it "should track state when the upstart configuration file fails to load" do
      File.should_receive(:exists?).and_return false
      @provider.load_current_resource
      @provider.instance_variable_get("@config_file_found").should == false
    end

    describe "when a status command has been specified" do
      before do
        @new_resource.stub!(:status_command).and_return("/bin/chefhasmonkeypants status")
      end

      it "should run the services status command if one has been specified" do
        @provider.stub!(:run_command_with_systems_locale).with({:command => "/bin/chefhasmonkeypants status"}).and_return(0)
        @current_resource.should_receive(:running).with(true)
        @provider.load_current_resource
      end

      it "should track state when the user-provided status command fails" do 
        @provider.stub!(:popen4).and_yield(@pid, @stdin, @stdout, @stderr).and_raise(Chef::Exceptions::Exec)
        @provider.load_current_resource
        @provider.instance_variable_get("@command_success").should == false
      end

      it "should set running to false if it catches a Chef::Exceptions::Exec when using a status command" do
        @provider.stub!(:popen4).and_yield(@pid, @stdin, @stdout, @stderr).and_raise(Chef::Exceptions::Exec)
        @current_resource.should_receive(:running).with(false)
        @provider.load_current_resource
      end
    end

    it "should track state when we fail to obtain service status via upstart_state" do
      @provider.should_receive(:upstart_state).and_raise Chef::Exceptions::Exec
      @provider.load_current_resource
      @provider.instance_variable_get("@command_success").should == false
    end
    
    it "should return the current resource" do
      @provider.load_current_resource.should eql(@current_resource)
    end


  end

  describe "enable and disable service" do
    before(:each) do
      @current_resource = Chef::Resource::Service.new('rsyslog')
      Chef::Resource::Service.stub!(:new).and_return(@current_resource)
      @provider.current_resource = @current_resource
      Chef::Util::FileEdit.stub!(:new)
    end

    it "should enable the service if it is not enabled" do
      @file = Object.new
      Chef::Util::FileEdit.stub!(:new).and_return(@file)
      @current_resource.stub!(:enabled).and_return(false)
      @file.should_receive(:search_file_replace)
      @file.should_receive(:write_file)
      @provider.enable_service()
    end

    it "should disable the service if it is enabled" do
      @file = Object.new
      Chef::Util::FileEdit.stub!(:new).and_return(@file)
      @current_resource.stub!(:enabled).and_return(true)
      @file.should_receive(:search_file_replace)
      @file.should_receive(:write_file)
      @provider.disable_service()
    end

  end

  describe "start and stop service" do
    before(:each) do
      @current_resource = Chef::Resource::Service.new('rsyslog')

      Chef::Resource::Service.stub!(:new).and_return(@current_resource)
      @provider.current_resource = @current_resource
    end

    it "should call the start command if one is specified" do
      @new_resource.stub!(:start_command).and_return("/sbin/rsyslog startyousillysally")
      @provider.should_receive(:shell_out!).with("/sbin/rsyslog startyousillysally")
      @provider.start_service()
    end

    it "should call '/sbin/start service_name' if no start command is specified" do
      @provider.should_receive(:run_command_with_systems_locale).with({:command => "/sbin/start #{@new_resource.service_name}"}).and_return(0)
      @provider.start_service()
    end

    it "should not call '/sbin/start service_name' if it is already running" do
      @current_resource.stub!(:running).and_return(true)
      @provider.should_not_receive(:run_command_with_systems_locale).with({:command => "/sbin/start #{@new_resource.service_name}"}).and_return(0)
      @provider.start_service()
    end

    it "should pass parameters to the start command if they are provided" do
      @new_resource = Chef::Resource::Service.new("rsyslog")
      @new_resource.parameters({ "OSD_ID" => "2" })
      @provider = Chef::Provider::Service::Upstart.new(@new_resource, @run_context)
      @provider.current_resource = @current_resource
      @provider.should_receive(:run_command_with_systems_locale).with({:command => "/sbin/start rsyslog OSD_ID=2"}).and_return(0)
      @provider.start_service()
    end

    it "should call the restart command if one is specified" do
      @current_resource.stub!(:running).and_return(true)
      @new_resource.stub!(:restart_command).and_return("/sbin/rsyslog restartyousillysally")
      @provider.should_receive(:shell_out!).with("/sbin/rsyslog restartyousillysally")
      @provider.restart_service()
    end

    it "should call '/sbin/restart service_name' if no restart command is specified" do
      @current_resource.stub!(:running).and_return(true)
      @provider.should_receive(:run_command_with_systems_locale).with({:command => "/sbin/restart #{@new_resource.service_name}"}).and_return(0)
      @provider.restart_service()
    end

    it "should call '/sbin/start service_name' if restart_service is called for a stopped service" do
      @current_resource.stub!(:running).and_return(false)
      @provider.should_receive(:run_command_with_systems_locale).with({:command => "/sbin/start #{@new_resource.service_name}"}).and_return(0)
      @provider.restart_service()
    end

    it "should call the reload command if one is specified" do
      @current_resource.stub!(:running).and_return(true)
      @new_resource.stub!(:reload_command).and_return("/sbin/rsyslog reloadyousillysally")
      @provider.should_receive(:shell_out!).with("/sbin/rsyslog reloadyousillysally")
      @provider.reload_service()
    end

    it "should call '/sbin/reload service_name' if no reload command is specified" do
      @current_resource.stub!(:running).and_return(true)
      @provider.should_receive(:run_command_with_systems_locale).with({:command => "/sbin/reload #{@new_resource.service_name}"}).and_return(0)
      @provider.reload_service()
    end

    it "should call the stop command if one is specified" do
      @current_resource.stub!(:running).and_return(true)
      @new_resource.stub!(:stop_command).and_return("/sbin/rsyslog stopyousillysally")
      @provider.should_receive(:shell_out!).with("/sbin/rsyslog stopyousillysally")
      @provider.stop_service()
    end

    it "should call '/sbin/stop service_name' if no stop command is specified" do
      @current_resource.stub!(:running).and_return(true)
      @provider.should_receive(:run_command_with_systems_locale).with({:command => "/sbin/stop #{@new_resource.service_name}"}).and_return(0)
      @provider.stop_service()
    end

    it "should not call '/sbin/stop service_name' if it is already stopped" do
      @current_resource.stub!(:running).and_return(false)
      @provider.should_not_receive(:run_command_with_systems_locale).with({:command => "/sbin/stop #{@new_resource.service_name}"}).and_return(0)
      @provider.stop_service()
    end
  end
end