diff options
-rw-r--r-- | lib/puppet/provider.rb | 79 | ||||
-rwxr-xr-x | test/ral/providers/provider.rb | 23 |
2 files changed, 27 insertions, 75 deletions
diff --git a/lib/puppet/provider.rb b/lib/puppet/provider.rb index e73bb0cb6..e0de9d61d 100644 --- a/lib/puppet/provider.rb +++ b/lib/puppet/provider.rb @@ -5,6 +5,8 @@ class Puppet::Provider include Puppet::Util::Warnings extend Puppet::Util::Warnings + require 'puppet/provider/confiner' + Puppet::Util.logmethods(self, true) class << self @@ -55,13 +57,14 @@ class Puppet::Provider end def self.confine(hash) - hash.each do |p,v| - if v.is_a? Array - @confines[p] += v - else - @confines[p] << v - end + confiner.confine(hash) + end + + def self.confiner + unless defined?(@confiner) + @confiner = Puppet::Provider::Confiner.new end + @confiner end # Is the provided feature a declared feature? @@ -109,9 +112,6 @@ class Puppet::Provider @defaults = {} @commands = {} @origcommands = {} - @confines = Hash.new do |hash, key| - hash[key] = [] - end end # The method for returning a list of provider instances. Note that it returns providers, preferably with values already @@ -210,65 +210,8 @@ class Puppet::Provider # Check whether this implementation is suitable for our platform. def self.suitable?(short = true) - # A single false result is sufficient to turn the whole thing down. - # We don't return 'true' until the very end, though, so that every - # confine is tested. - missing = {} - @confines.each do |check, values| - case check - when :exists: - values.each do |value| - unless value and FileTest.exists? value - debug "Not suitable: missing %s" % value - return false if short - missing[:exists] ||= [] - missing[:exists] << value - end - end - when :true: - values.each do |v| - debug "Not suitable: false value" - unless v - return false if short - missing[:true] ||= 0 - missing[:true] += 1 - end - end - when :false: - values.each do |v| - debug "Not suitable: true value" - if v and short - return false if short - missing[:false] ||= 0 - missing[:false] += 1 - end - end - else # Just delegate everything else to facter - if result = Facter.value(check) - result = result.to_s.downcase.intern - - found = values.find do |v| - result == v.to_s.downcase.intern - end - unless found - debug "Not suitable: %s not in %s" % [check, values] - return false if short - missing[:facter] ||= {} - missing[:facter][check] = values - end - else - return false if short - missing[:facter] ||= {} - missing[:facter][check] = values - end - end - end - - if short - return true - else - return missing - end + return confiner.valid? if short + return confiner.result end # Does this provider support the specified parameter? diff --git a/test/ral/providers/provider.rb b/test/ral/providers/provider.rb index 349a56d5e..f284e9125 100755 --- a/test/ral/providers/provider.rb +++ b/test/ral/providers/provider.rb @@ -37,12 +37,13 @@ class TestProvider < Test::Unit::TestCase cleanup { Puppet::Type.rmtype(:provider_test) } end - def test_confine - provider = newprovider + def test_confine_defaults_to_suitable - assert(provider.suitable?, - "Marked unsuitable with no confines") + provider = newprovider + assert(provider.suitable?, "Marked unsuitable with no confines") + end + def test_confine_results { {:true => true} => true, {:true => false} => false, @@ -54,6 +55,8 @@ class TestProvider < Test::Unit::TestCase {:exists => echo} => true, {:exists => "/this/file/does/not/exist"} => false, }.each do |hash, result| + provider = newprovider + # First test :true hash.each do |test, val| assert_nothing_raised do @@ -61,19 +64,25 @@ class TestProvider < Test::Unit::TestCase end end - assert_equal(result, provider.suitable?, - "Failed for %s" % [hash.inspect]) + assert_equal(result, provider.suitable?, "Failed for %s" % [hash.inspect]) provider.initvars end + end + + def test_multiple_confines_do_not_override + provider = newprovider # Make sure multiple confines don't overwrite each other provider.confine :true => false assert(! provider.suitable?) provider.confine :true => true assert(! provider.suitable?) + end - provider.initvars + def test_one_failed_confine_is_sufficient + + provider = newprovider # Make sure we test multiple of them, and that a single false wins provider.confine :true => true, :false => false |