summaryrefslogtreecommitdiffstats
path: root/spec
diff options
context:
space:
mode:
authorLuke Kanies <luke@madstop.com>2007-11-28 18:38:48 -0600
committerLuke Kanies <luke@madstop.com>2007-11-28 18:38:48 -0600
commitdedc56a6ae583daca304c053b1be8a52bcdbd13a (patch)
tree79421acea6e98035a2d09b984f1796331af61a51 /spec
parent600d093df55e012e2e00a5b525daefb77c2eded1 (diff)
downloadpuppet-dedc56a6ae583daca304c053b1be8a52bcdbd13a.tar.gz
puppet-dedc56a6ae583daca304c053b1be8a52bcdbd13a.tar.xz
puppet-dedc56a6ae583daca304c053b1be8a52bcdbd13a.zip
Fixing #527 (rewrote service tests), #766 (services only restart when they
are running), and #918 (service tests fail when hddtemp is not installed). Mostly, I just rewrote the service tests, but I cleaned up the cruft from the Service class, too.
Diffstat (limited to 'spec')
-rwxr-xr-xspec/unit/ral/types/package.rb2
-rwxr-xr-xspec/unit/ral/types/service.rb249
2 files changed, 249 insertions, 2 deletions
diff --git a/spec/unit/ral/types/package.rb b/spec/unit/ral/types/package.rb
index f7d93da6e..fd200c56f 100755
--- a/spec/unit/ral/types/package.rb
+++ b/spec/unit/ral/types/package.rb
@@ -4,8 +4,6 @@ require File.dirname(__FILE__) + '/../../../spec_helper'
require 'puppet/type/package'
-$platform = Facter["operatingsystem"].value
-
describe Puppet::Type::Package do
it "should have an :installable feature that requires the :install method" do
Puppet::Type::Package.provider_feature(:installable).methods.should == [:install]
diff --git a/spec/unit/ral/types/service.rb b/spec/unit/ral/types/service.rb
new file mode 100755
index 000000000..ee3d747a8
--- /dev/null
+++ b/spec/unit/ral/types/service.rb
@@ -0,0 +1,249 @@
+#!/usr/bin/env ruby
+
+require File.dirname(__FILE__) + '/../../../spec_helper'
+
+require 'puppet/type/service'
+
+describe Puppet::Type::Service do
+ it "should have an :enableable feature that requires the :enable, :disable, and :enabled? methods" do
+ Puppet::Type::Service.provider_feature(:enableable).methods.should == [:disable, :enable, :enabled?]
+ end
+
+ it "should have a :refreshable feature that requires the :restart method" do
+ Puppet::Type::Service.provider_feature(:refreshable).methods.should == [:restart]
+ end
+end
+
+describe Puppet::Type::Service, "when validating attributes" do
+ [:name, :binary, :hasstatus, :path, :pattern, :start, :restart, :stop, :status, :hasrestart].each do |param|
+ it "should have a #{param} parameter" do
+ Puppet::Type::Service.attrtype(param).should == :param
+ end
+ end
+
+ [:ensure, :enable].each do |param|
+ it "should have an #{param} property" do
+ Puppet::Type::Service.attrtype(param).should == :property
+ end
+ end
+end
+
+describe Puppet::Type::Service, "when validating attribute values" do
+ before do
+ @provider = stub 'provider', :class => Puppet::Type::Service.defaultprovider, :clear => nil
+ Puppet::Type::Service.defaultprovider.stubs(:new).returns(@provider)
+ end
+
+ it "should support :running as a value to :ensure" do
+ Puppet::Type::Service.create(:name => "yay", :ensure => :running)
+ end
+
+ it "should support :stopped as a value to :ensure" do
+ Puppet::Type::Service.create(:name => "yay", :ensure => :stopped)
+ end
+
+ it "should alias the value :true to :running in :ensure" do
+ svc = Puppet::Type::Service.create(:name => "yay", :ensure => true)
+ svc.should(:ensure).should == :running
+ end
+
+ it "should alias the value :false to :stopped in :ensure" do
+ svc = Puppet::Type::Service.create(:name => "yay", :ensure => false)
+ svc.should(:ensure).should == :stopped
+ end
+
+ it "should support :true as a value to :enable" do
+ Puppet::Type::Service.create(:name => "yay", :enable => :true)
+ end
+
+ it "should support :false as a value to :enable" do
+ Puppet::Type::Service.create(:name => "yay", :enable => :false)
+ end
+
+ it "should support :true as a value to :hasstatus" do
+ Puppet::Type::Service.create(:name => "yay", :hasstatus => :true)
+ end
+
+ it "should support :false as a value to :hasstatus" do
+ Puppet::Type::Service.create(:name => "yay", :hasstatus => :false)
+ end
+
+ it "should support :true as a value to :hasrestart" do
+ Puppet::Type::Service.create(:name => "yay", :hasrestart => :true)
+ end
+
+ it "should support :false as a value to :hasrestart" do
+ Puppet::Type::Service.create(:name => "yay", :hasrestart => :false)
+ end
+
+ it "should allow setting the :enable parameter if the provider has the :enableable feature" do
+ Puppet::Type::Service.defaultprovider.stubs(:supports_parameter?).returns(true)
+ Puppet::Type::Service.defaultprovider.expects(:supports_parameter?).with(Puppet::Type::Service.attrclass(:enable)).returns(true)
+ svc = Puppet::Type::Service.create(:name => "yay", :enable => true)
+ svc.should(:enable).should == :true
+ end
+
+ it "should not allow setting the :enable parameter if the provider is missing the :enableable feature" do
+ Puppet::Type::Service.defaultprovider.stubs(:supports_parameter?).returns(true)
+ Puppet::Type::Service.defaultprovider.expects(:supports_parameter?).with(Puppet::Type::Service.attrclass(:enable)).returns(false)
+ svc = Puppet::Type::Service.create(:name => "yay", :enable => true)
+ svc.should(:enable).should be_nil
+ end
+
+ it "should discard paths that do not exist" do
+ FileTest.stubs(:exist?).returns(false)
+ FileTest.stubs(:directory?).returns(false)
+ svc = Puppet::Type::Service.create(:name => "yay", :path => "/one/two")
+ svc[:path].should be_empty
+ end
+
+ it "should discard paths that are not directories" do
+ FileTest.stubs(:exist?).returns(true)
+ FileTest.stubs(:directory?).returns(false)
+ svc = Puppet::Type::Service.create(:name => "yay", :path => "/one/two")
+ svc[:path].should be_empty
+ end
+
+ it "should split paths on ':'" do
+ FileTest.stubs(:exist?).returns(true)
+ FileTest.stubs(:directory?).returns(true)
+ svc = Puppet::Type::Service.create(:name => "yay", :path => "/one/two:/three/four")
+ svc[:path].should == %w{/one/two /three/four}
+ end
+
+ it "should accept arrays of paths joined by ':'" do
+ FileTest.stubs(:exist?).returns(true)
+ FileTest.stubs(:directory?).returns(true)
+ svc = Puppet::Type::Service.create(:name => "yay", :path => ["/one:/two", "/three:/four"])
+ svc[:path].should == %w{/one /two /three /four}
+ end
+
+ after { Puppet::Type::Service.clear }
+end
+
+describe Puppet::Type::Service, "when setting default attribute values" do
+ it "should default to the provider's default path if one is available" do
+ FileTest.stubs(:directory?).returns(true)
+ FileTest.stubs(:exist?).returns(true)
+
+ Puppet::Type::Service.defaultprovider.stubs(:respond_to?).returns(true)
+ Puppet::Type::Service.defaultprovider.stubs(:defpath).returns("testing")
+ svc = Puppet::Type::Service.create(:name => "other")
+ svc[:path].should == ["testing"]
+ end
+
+ it "should default to the binary for the pattern if one is provided" do
+ svc = Puppet::Type::Service.create(:name => "other", :binary => "/some/binary")
+ svc[:pattern].should == "/some/binary"
+ end
+
+ it "should default to the name for the pattern if no pattern is provided" do
+ svc = Puppet::Type::Service.create(:name => "other")
+ svc[:pattern].should == "other"
+ end
+
+ after { Puppet::Type::Service.clear }
+end
+
+describe Puppet::Type::Service, "when retrieving the host's current state" do
+ before do
+ @service = Puppet::Type::Service.create(:name => "yay")
+ end
+
+ it "should use the provider's status to determine whether the service is running" do
+ @service.provider.expects(:status).returns(:yepper)
+ @service[:ensure] = :running
+ @service.property(:ensure).retrieve.should == :yepper
+ end
+
+ it "should ask the provider whether it is enabled" do
+ @service.provider.class.stubs(:supports_parameter?).returns(true)
+ @service.provider.expects(:enabled?).returns(:yepper)
+ @service[:enable] = true
+ @service.property(:enable).retrieve.should == :yepper
+ end
+
+ after { Puppet::Type::Service.clear }
+end
+
+describe Puppet::Type::Service, "when changing the host" do
+ before do
+ @service = Puppet::Type::Service.create(:name => "yay")
+ end
+
+ it "should start the service if it is supposed to be running" do
+ @service[:ensure] = :running
+ @service.provider.expects(:start)
+ @service.property(:ensure).sync
+ end
+
+ it "should stop the service if it is supposed to be stopped" do
+ @service[:ensure] = :stopped
+ @service.provider.expects(:stop)
+ @service.property(:ensure).sync
+ end
+
+ it "should enable the service if it is supposed to be enabled" do
+ @service.provider.class.stubs(:supports_parameter?).returns(true)
+ @service[:enable] = true
+ @service.provider.expects(:enable)
+ @service.property(:enable).sync
+ end
+
+ it "should disable the service if it is supposed to be disabled" do
+ @service.provider.class.stubs(:supports_parameter?).returns(true)
+ @service[:enable] = false
+ @service.provider.expects(:disable)
+ @service.property(:enable).sync
+ end
+
+ it "should sync the service's enable state when changing the state of :ensure if :enable is being managed" do
+ @service.provider.class.stubs(:supports_parameter?).returns(true)
+ @service[:enable] = false
+ @service[:ensure] = :stopped
+
+ @service.property(:enable).expects(:retrieve).returns("whatever")
+ @service.property(:enable).expects(:insync?).returns(false)
+ @service.property(:enable).expects(:sync)
+
+ @provider.stubs(:stop)
+
+ @service.property(:ensure).sync
+ end
+
+ after { Puppet::Type::Service.clear }
+end
+
+describe Puppet::Type::Service, "when refreshing the service" do
+ before do
+ @service = Puppet::Type::Service.create(:name => "yay")
+ end
+
+ it "should restart the service if it is running" do
+ @service[:ensure] = :running
+ @service.provider.expects(:status).returns(:running)
+ @service.provider.expects(:restart)
+ @service.refresh
+ end
+
+ it "should restart the service if it is running, even if it is supposed to stopped" do
+ @service[:ensure] = :stopped
+ @service.provider.expects(:status).returns(:running)
+ @service.provider.expects(:restart)
+ @service.refresh
+ end
+
+ it "should not restart the service if it is not running" do
+ @service[:ensure] = :running
+ @service.provider.expects(:status).returns(:stopped)
+ @service.refresh
+ end
+
+ it "should add :ensure as a property if it is not being managed" do
+ @service.provider.expects(:status).returns(:running)
+ @service.provider.expects(:restart)
+ @service.refresh
+ end
+
+ after { Puppet::Type::Service.clear }
+end