diff --git a/spec/unit/provider_spec.rb b/spec/unit/provider_spec.rb index eeda5dff9..2b0283d46 100755 --- a/spec/unit/provider_spec.rb +++ b/spec/unit/provider_spec.rb @@ -1,62 +1,104 @@ #!/usr/bin/env rspec require 'spec_helper' describe Puppet::Provider do it "should have a specifity class method" do Puppet::Provider.should respond_to(:specificity) end it "should consider two defaults to be higher specificity than one default" do one = Class.new(Puppet::Provider) one.initvars one.defaultfor :operatingsystem => "solaris" two = Class.new(Puppet::Provider) two.initvars two.defaultfor :operatingsystem => "solaris", :operatingsystemrelease => "5.10" two.specificity.should > one.specificity end it "should consider a subclass more specific than its parent class" do one = Class.new(Puppet::Provider) one.initvars two = Class.new(one) two.initvars two.specificity.should > one.specificity end it "should be Comparable" do res = Puppet::Type.type(:notify).new(:name => "res") # Normally I wouldn't like the stubs, but the only way to name a class # otherwise is to assign it to a constant, and that hurts more here in # testing world. --daniel 2012-01-29 a = Class.new(Puppet::Provider).new(res) a.class.stubs(:name).returns "Puppet::Provider::Notify::A" b = Class.new(Puppet::Provider).new(res) b.class.stubs(:name).returns "Puppet::Provider::Notify::B" c = Class.new(Puppet::Provider).new(res) c.class.stubs(:name).returns "Puppet::Provider::Notify::C" [[a, b, c], [a, c, b], [b, a, c], [b, c, a], [c, a, b], [c, b, a]].each do |this| this.sort.should == [a, b, c] end a.should be < b a.should be < c b.should be > a b.should be < c c.should be > a c.should be > b [a, b, c].each {|x| a.should be <= x } [a, b, c].each {|x| c.should be >= x } b.should be_between(a, c) end + + context "when confining" do + before :each do + Puppet::Type.newtype(:test) do + newparam(:name) { isnamevar } + end + end + + after :each do + Puppet::Type.rmtype(:test) + end + + let :type do Puppet::Type.type(:test) end + + it "should find the default provider" do + type.provide(:nondefault) {} + default = type.provide(:default) do + defaultfor :operatingsystem => Facter["operatingsystem"].value + end + + default.name.should == type.defaultprovider.name + end + + it "should raise for unknown commands" do + provider = type.provide(:unknown) {} + expect { provider.command(:something) }.to raise_error Puppet::DevError + end + + it "should handle command inheritance" do + parent = type.provide("parent") + child = type.provide("child", :parent => parent.name) + + command = Puppet::Util.which('sh') || Puppet::Util.which('cmd.exe') + parent.commands :sh => command + + FileTest.should be_exists parent.command(:sh) + parent.command(:sh).should =~ /#{command}$/ + + FileTest.should be_exists child.command(:sh) + child.command(:sh).should =~ /#{command}$/ + end + end end diff --git a/test/other/provider.rb b/test/other/provider.rb deleted file mode 100755 index c31c6bfb9..000000000 --- a/test/other/provider.rb +++ /dev/null @@ -1,90 +0,0 @@ -#!/usr/bin/env ruby - -require File.expand_path(File.dirname(__FILE__) + '/../lib/puppettest') - -require 'puppet' -require 'puppet/provider' -require 'puppettest' - -class TestImpl < Test::Unit::TestCase - include PuppetTest - - def setup - super - @type = newtype(@method_name.to_s + "type") - - # But create a new provider for every method. - @provider = newprovider(@method_name.to_s + "provider") - end - - def newtype(name) - # First create a fake type - return Puppet::Type.newtype(name) { - newparam(:name) { isnamevar } - } - end - - def newprovider(name, type = nil) - type ||= @type - provider = nil - assert_nothing_raised("Could not create provider") do - provider = type.provide(name) {} - end - provider - end - - def test_provider_default - nondef = nil - assert_nothing_raised { - nondef = newprovider(:nondefault) - } - - assert_nothing_raised do - @provider.defaultfor :operatingsystem => Facter["operatingsystem"].value - end - - assert_equal(@provider.name, @type.defaultprovider.name, "Did not get right provider") - - @type.suitableprovider - end - - def test_subclassconfines - parent = newprovider("parentprovider") - - # Now make a bad confine on the parent - parent.confine :exists => "/this/file/definitely/does/not/exist" - - child = nil - assert_nothing_raised { - child = @type.provide("child", :parent => parent.name) {} - } - - assert(child.suitable?, "Parent ruled out child") - end - - def test_commands - parent = newprovider("parentprovider") - - child = nil - assert_nothing_raised { - child = @type.provide("child", :parent => parent.name) {} - } - - assert_raise(Puppet::DevError) do - child.command(:nosuchcommand) - end - - # Now create a parent command - assert_nothing_raised { - parent.commands :sh => Puppet::Util.which('sh') - } - - assert(parent.command(:sh), "Did not find 'sh' command") - - assert(child.command(:sh), "Did not find parent's 'sh' command") - - assert(FileTest.exists?(child.command(:sh)), - "Somehow broke path to sh") - end -end -