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?]
12 it "should have a :refreshable feature that requires the :restart method" do
13 Puppet::Type::Service.provider_feature(:refreshable).methods.should == [:restart]
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
24 [:ensure, :enable].each do |param|
25 it "should have an #{param} property" do
26 Puppet::Type::Service.attrtype(param).should == :property
31 describe Puppet::Type::Service, "when validating attribute values" do
33 @provider = stub 'provider', :class => Puppet::Type::Service.defaultprovider, :clear => nil
34 Puppet::Type::Service.defaultprovider.stubs(:new).returns(@provider)
37 it "should support :running as a value to :ensure" do
38 Puppet::Type::Service.create(:name => "yay", :ensure => :running)
41 it "should support :stopped as a value to :ensure" do
42 Puppet::Type::Service.create(:name => "yay", :ensure => :stopped)
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
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
55 it "should support :true as a value to :enable" do
56 Puppet::Type::Service.create(:name => "yay", :enable => :true)
59 it "should support :false as a value to :enable" do
60 Puppet::Type::Service.create(:name => "yay", :enable => :false)
63 it "should support :true as a value to :hasstatus" do
64 Puppet::Type::Service.create(:name => "yay", :hasstatus => :true)
67 it "should support :false as a value to :hasstatus" do
68 Puppet::Type::Service.create(:name => "yay", :hasstatus => :false)
71 it "should support :true as a value to :hasrestart" do
72 Puppet::Type::Service.create(:name => "yay", :hasrestart => :true)
75 it "should support :false as a value to :hasrestart" do
76 Puppet::Type::Service.create(:name => "yay", :hasrestart => :false)
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
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
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
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
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}
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}
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"]
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"
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"
145 after { Puppet::Type::Service.clear }
148 describe Puppet::Type::Service, "when retrieving the host's current state" do
150 @service = Puppet::Type::Service.create(:name => "yay")
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
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
166 after { Puppet::Type::Service.clear }
169 describe Puppet::Type::Service, "when changing the host" do
171 @service = Puppet::Type::Service.create(:name => "yay")
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
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
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
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
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
214 after { Puppet::Type::Service.clear }
217 describe Puppet::Type::Service, "when refreshing the service" do
219 @service = Puppet::Type::Service.create(:name => "yay")
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)
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)
236 it "should not restart the service if it is not running" do
237 @service[:ensure] = :running
238 @service.provider.expects(:status).returns(:stopped)
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)
248 after { Puppet::Type::Service.clear }