pairing with luke, nagios_command provider skeleton
[vinpup.git] / spec / unit / ral / types / service.rb
blob981d38a158877c788aaf23ce9b7c7ad2127d7b06
1 #!/usr/bin/env ruby
3 require File.dirname(__FILE__) + '/../../../spec_helper'
5 require 'puppet/type/service'
7 describe Puppet::Type::Service do
8     it "should have an :enableable feature that requires the :enable, :disable, and :enabled? methods" do
9         Puppet::Type::Service.provider_feature(:enableable).methods.should == [:disable, :enable, :enabled?]
10     end
12     it "should have a :refreshable feature that requires the :restart method" do
13         Puppet::Type::Service.provider_feature(:refreshable).methods.should == [:restart]
14     end
15 end
17 describe Puppet::Type::Service, "when validating attributes" do
18     [:name, :binary, :hasstatus, :path, :pattern, :start, :restart, :stop, :status, :hasrestart].each do |param|
19         it "should have a #{param} parameter" do
20             Puppet::Type::Service.attrtype(param).should == :param
21         end
22     end
24     [:ensure, :enable].each do |param|
25         it "should have an #{param} property" do
26             Puppet::Type::Service.attrtype(param).should == :property
27         end
28     end
29 end
31 describe Puppet::Type::Service, "when validating attribute values" do
32     before do
33         @provider = stub 'provider', :class => Puppet::Type::Service.defaultprovider, :clear => nil
34         Puppet::Type::Service.defaultprovider.stubs(:new).returns(@provider)
35     end
37     it "should support :running as a value to :ensure" do
38         Puppet::Type::Service.create(:name => "yay", :ensure => :running)
39     end
41     it "should support :stopped as a value to :ensure" do
42         Puppet::Type::Service.create(:name => "yay", :ensure => :stopped)
43     end
45     it "should alias the value :true to :running in :ensure" do
46         svc = Puppet::Type::Service.create(:name => "yay", :ensure => true)
47         svc.should(:ensure).should == :running
48     end
50     it "should alias the value :false to :stopped in :ensure" do
51         svc = Puppet::Type::Service.create(:name => "yay", :ensure => false)
52         svc.should(:ensure).should == :stopped
53     end
55     it "should support :true as a value to :enable" do
56         Puppet::Type::Service.create(:name => "yay", :enable => :true)
57     end
59     it "should support :false as a value to :enable" do
60         Puppet::Type::Service.create(:name => "yay", :enable => :false)
61     end
63     it "should support :true as a value to :hasstatus" do
64         Puppet::Type::Service.create(:name => "yay", :hasstatus => :true)
65     end
67     it "should support :false as a value to :hasstatus" do
68         Puppet::Type::Service.create(:name => "yay", :hasstatus => :false)
69     end
71     it "should support :true as a value to :hasrestart" do
72         Puppet::Type::Service.create(:name => "yay", :hasrestart => :true)
73     end
75     it "should support :false as a value to :hasrestart" do
76         Puppet::Type::Service.create(:name => "yay", :hasrestart => :false)
77     end
79     it "should allow setting the :enable parameter if the provider has the :enableable feature" do
80         Puppet::Type::Service.defaultprovider.stubs(:supports_parameter?).returns(true)
81         Puppet::Type::Service.defaultprovider.expects(:supports_parameter?).with(Puppet::Type::Service.attrclass(:enable)).returns(true)
82         svc = Puppet::Type::Service.create(:name => "yay", :enable => true)
83         svc.should(:enable).should == :true
84     end
86     it "should not allow setting the :enable parameter if the provider is missing the :enableable feature" do
87         Puppet::Type::Service.defaultprovider.stubs(:supports_parameter?).returns(true)
88         Puppet::Type::Service.defaultprovider.expects(:supports_parameter?).with(Puppet::Type::Service.attrclass(:enable)).returns(false)
89         svc = Puppet::Type::Service.create(:name => "yay", :enable => true)
90         svc.should(:enable).should be_nil
91     end
93     it "should discard paths that do not exist" do
94         FileTest.stubs(:exist?).returns(false)
95         FileTest.stubs(:directory?).returns(false)
96         svc = Puppet::Type::Service.create(:name => "yay", :path => "/one/two")
97         svc[:path].should be_empty
98     end
100     it "should discard paths that are not directories" do
101         FileTest.stubs(:exist?).returns(true)
102         FileTest.stubs(:directory?).returns(false)
103         svc = Puppet::Type::Service.create(:name => "yay", :path => "/one/two")
104         svc[:path].should be_empty
105     end
107     it "should split paths on ':'" do
108         FileTest.stubs(:exist?).returns(true)
109         FileTest.stubs(:directory?).returns(true)
110         svc = Puppet::Type::Service.create(:name => "yay", :path => "/one/two:/three/four")
111         svc[:path].should == %w{/one/two /three/four}
112     end
114     it "should accept arrays of paths joined by ':'" do
115         FileTest.stubs(:exist?).returns(true)
116         FileTest.stubs(:directory?).returns(true)
117         svc = Puppet::Type::Service.create(:name => "yay", :path => ["/one:/two", "/three:/four"])
118         svc[:path].should == %w{/one /two /three /four}
119     end
121     after { Puppet::Type::Service.clear }
124 describe Puppet::Type::Service, "when setting default attribute values" do
125     it "should default to the provider's default path if one is available" do
126         FileTest.stubs(:directory?).returns(true)
127         FileTest.stubs(:exist?).returns(true)
129         Puppet::Type::Service.defaultprovider.stubs(:respond_to?).returns(true)
130         Puppet::Type::Service.defaultprovider.stubs(:defpath).returns("testing")
131         svc = Puppet::Type::Service.create(:name => "other")
132         svc[:path].should == ["testing"]
133     end
135     it "should default to the binary for the pattern if one is provided" do
136         svc = Puppet::Type::Service.create(:name => "other", :binary => "/some/binary")
137         svc[:pattern].should == "/some/binary"
138     end
140     it "should default to the name for the pattern if no pattern is provided" do
141         svc = Puppet::Type::Service.create(:name => "other")
142         svc[:pattern].should == "other"
143     end
145     after { Puppet::Type::Service.clear }
148 describe Puppet::Type::Service, "when retrieving the host's current state" do
149     before do
150         @service = Puppet::Type::Service.create(:name => "yay")
151     end
153     it "should use the provider's status to determine whether the service is running" do
154         @service.provider.expects(:status).returns(:yepper)
155         @service[:ensure] = :running
156         @service.property(:ensure).retrieve.should == :yepper
157     end
159     it "should ask the provider whether it is enabled" do
160         @service.provider.class.stubs(:supports_parameter?).returns(true)
161         @service.provider.expects(:enabled?).returns(:yepper)
162         @service[:enable] = true
163         @service.property(:enable).retrieve.should == :yepper
164     end
166     after { Puppet::Type::Service.clear }
169 describe Puppet::Type::Service, "when changing the host" do
170     before do
171         @service = Puppet::Type::Service.create(:name => "yay")
172     end
174     it "should start the service if it is supposed to be running" do
175         @service[:ensure] = :running
176         @service.provider.expects(:start)
177         @service.property(:ensure).sync
178     end
180     it "should stop the service if it is supposed to be stopped" do
181         @service[:ensure] = :stopped
182         @service.provider.expects(:stop)
183         @service.property(:ensure).sync
184     end
186     it "should enable the service if it is supposed to be enabled" do
187         @service.provider.class.stubs(:supports_parameter?).returns(true)
188         @service[:enable] = true
189         @service.provider.expects(:enable)
190         @service.property(:enable).sync
191     end
193     it "should disable the service if it is supposed to be disabled" do
194         @service.provider.class.stubs(:supports_parameter?).returns(true)
195         @service[:enable] = false
196         @service.provider.expects(:disable)
197         @service.property(:enable).sync
198     end
200     it "should sync the service's enable state when changing the state of :ensure if :enable is being managed" do
201         @service.provider.class.stubs(:supports_parameter?).returns(true)
202         @service[:enable] = false
203         @service[:ensure] = :stopped
205         @service.property(:enable).expects(:retrieve).returns("whatever")
206         @service.property(:enable).expects(:insync?).returns(false)
207         @service.property(:enable).expects(:sync)
209         @service.provider.stubs(:stop)
211         @service.property(:ensure).sync
212     end
214     after { Puppet::Type::Service.clear }
217 describe Puppet::Type::Service, "when refreshing the service" do
218     before do
219         @service = Puppet::Type::Service.create(:name => "yay")
220     end
222     it "should restart the service if it is running" do
223         @service[:ensure] = :running
224         @service.provider.expects(:status).returns(:running)
225         @service.provider.expects(:restart)
226         @service.refresh
227     end
229     it "should restart the service if it is running, even if it is supposed to stopped" do
230         @service[:ensure] = :stopped
231         @service.provider.expects(:status).returns(:running)
232         @service.provider.expects(:restart)
233         @service.refresh
234     end
236     it "should not restart the service if it is not running" do
237         @service[:ensure] = :running
238         @service.provider.expects(:status).returns(:stopped)
239         @service.refresh
240     end
242     it "should add :ensure as a property if it is not being managed" do
243         @service.provider.expects(:status).returns(:running)
244         @service.provider.expects(:restart)
245         @service.refresh
246     end
248     after { Puppet::Type::Service.clear }