Adding a "supports_parameter?" method to test whether a given provider supports the features required by a given parameter. This is used during attribute instance creation, but its creation was necessicated by test code.
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@2444 980ebf18-57e1-0310-9a29-db15c13687c0
This commit is contained in:
Родитель
c9de332e8a
Коммит
64382700d8
|
@ -204,7 +204,7 @@ class Puppet::Type
|
|||
|
||||
# Grr.
|
||||
if options[:required_features]
|
||||
s.required_features = options[:required_features]
|
||||
param.required_features = options[:required_features]
|
||||
end
|
||||
|
||||
handle_param_options(name, options)
|
||||
|
@ -250,7 +250,7 @@ class Puppet::Type
|
|||
|
||||
# Grr.
|
||||
if options[:required_features]
|
||||
s.required_features = options[:required_features]
|
||||
param.required_features = options[:required_features]
|
||||
end
|
||||
|
||||
# These might be enabled later.
|
||||
|
@ -299,7 +299,7 @@ class Puppet::Type
|
|||
# We have to create our own, new block here because we want to define
|
||||
# an initial :retrieve method, if told to, and then eval the passed
|
||||
# block if available.
|
||||
s = genclass(name,
|
||||
prop = genclass(name,
|
||||
:parent => options[:parent] || Puppet::Property,
|
||||
:hash => @validproperties
|
||||
) do
|
||||
|
@ -320,18 +320,18 @@ class Puppet::Type
|
|||
|
||||
# If it's the 'ensure' property, always put it first.
|
||||
if name == :ensure
|
||||
@properties.unshift s
|
||||
@properties.unshift prop
|
||||
else
|
||||
@properties << s
|
||||
@properties << prop
|
||||
end
|
||||
|
||||
if options[:event]
|
||||
s.event = options[:event]
|
||||
prop.event = options[:event]
|
||||
end
|
||||
|
||||
# Grr.
|
||||
if options[:required_features]
|
||||
s.required_features = options[:required_features]
|
||||
prop.required_features = options[:required_features]
|
||||
end
|
||||
|
||||
# define_method(name) do
|
||||
|
@ -342,7 +342,7 @@ class Puppet::Type
|
|||
# newproperty(name, :should => value)
|
||||
# end
|
||||
|
||||
return s
|
||||
return prop
|
||||
end
|
||||
|
||||
def self.paramdoc(param)
|
||||
|
@ -584,7 +584,7 @@ class Puppet::Type
|
|||
end
|
||||
|
||||
unless klass = self.class.attrclass(name)
|
||||
raise Puppet::Error, "Invalid parameter %s" % name
|
||||
raise Puppet::Error, "Resource type %s does not support parameter %s" % [self.class.name, name]
|
||||
end
|
||||
|
||||
if @parameters.include?(name)
|
||||
|
@ -592,12 +592,10 @@ class Puppet::Type
|
|||
[name, self.ref]
|
||||
end
|
||||
|
||||
if provider and features = klass.required_features
|
||||
unless provider.class.satisfies?(features)
|
||||
missing = features.find_all { |f| ! provider.class.feature?(f) }
|
||||
info "Provider %s does not support features %s; not managing attribute %s" % [provider.class.name, missing.join(", "), name]
|
||||
return nil
|
||||
end
|
||||
if provider and ! provider.class.supports_parameter?(klass)
|
||||
missing = klass.required_features.find_all { |f| ! provider.class.feature?(f) }
|
||||
info "Provider %s does not support features %s; not managing attribute %s" % [provider.class.name, missing.join(", "), name]
|
||||
return nil
|
||||
end
|
||||
|
||||
# Add parent information at creation time, so it's available
|
||||
|
|
|
@ -222,6 +222,24 @@ class Puppet::Provider
|
|||
return true
|
||||
end
|
||||
|
||||
# Does this provider support the specified parameter?
|
||||
def self.supports_parameter?(param)
|
||||
if param.is_a?(Class)
|
||||
klass = param
|
||||
else
|
||||
unless klass = @model.attrclass(param)
|
||||
raise Puppet::DevError, "'%s' is not a valid parameter for %s" % [param, @model.name]
|
||||
end
|
||||
end
|
||||
return true unless features = klass.required_features
|
||||
|
||||
if satisfies?(*features)
|
||||
return true
|
||||
else
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
def self.to_s
|
||||
unless defined? @str
|
||||
if self.model
|
||||
|
|
|
@ -114,6 +114,10 @@ module PuppetTest
|
|||
end
|
||||
end
|
||||
|
||||
def self.supports_parameter?(param)
|
||||
true
|
||||
end
|
||||
|
||||
def self.suitable?
|
||||
true
|
||||
end
|
||||
|
|
|
@ -383,6 +383,41 @@ class TestProviderFeatures < Test::Unit::TestCase
|
|||
"Got incorrect feature list for %s" % name)
|
||||
end
|
||||
end
|
||||
|
||||
def test_supports_parameter?
|
||||
# Make some parameters for each setting
|
||||
@type.newparam(:neither) {}
|
||||
@type.newparam(:some, :required_features => :alpha)
|
||||
@type.newparam(:both, :required_features => [:alpha, :numeric])
|
||||
|
||||
# and appropriate providers
|
||||
nope = @type.provide(:nope) {}
|
||||
maybe = @type.provide(:maybe) { has_features(:alpha) }
|
||||
yep = @type.provide(:yep) { has_features(:alpha, :numeric) }
|
||||
|
||||
# Now make sure our providers answer correctly.
|
||||
[nope, maybe, yep].each do |prov|
|
||||
assert(prov.respond_to?(:supports_parameter?), "%s does not respond to :supports_parameter?" % prov.name)
|
||||
case prov.name
|
||||
when :nope:
|
||||
supported = [:neither]
|
||||
un = [:some, :both]
|
||||
when :maybe:
|
||||
supported = [:neither, :some]
|
||||
un = [:both]
|
||||
when :yep:
|
||||
supported = [:neither, :some, :both]
|
||||
un = []
|
||||
end
|
||||
|
||||
supported.each do |param|
|
||||
assert(prov.supports_parameter?(param), "%s was not supported by %s" % [param, prov.name])
|
||||
end
|
||||
un.each do |param|
|
||||
assert(! prov.supports_parameter?(param), "%s was incorrectly supported by %s" % [param, prov.name])
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# $Id$
|
||||
|
|
|
@ -185,6 +185,8 @@ class TestUserProvider < Test::Unit::TestCase
|
|||
|
||||
Puppet::Type.type(:user).validproperties.each do |property|
|
||||
next if property == :ensure
|
||||
# This is mostly in place for the 'password' stuff.
|
||||
next unless user.class.supports_parameter?(property)
|
||||
val = nil
|
||||
assert_nothing_raised {
|
||||
val = user.send(property)
|
||||
|
|
|
@ -178,23 +178,9 @@ class UserAddProviderTest < PuppetTest::TestCase
|
|||
end
|
||||
|
||||
def test_manages_password
|
||||
if Facter.value(:kernel) != "Linux"
|
||||
assert(! @provider.feature?(:manages_passwords),
|
||||
"Defaulted to managing passwords on %s" %
|
||||
Facter.value(:kernel))
|
||||
|
||||
# Now just make sure it's not allowed, and return
|
||||
setup_user
|
||||
assert_raise(Puppet::Error, "allowed passwd mgmt on failing host") do
|
||||
@user[:password] = "yayness"
|
||||
end
|
||||
unless @provider.feature?(:manages_passwords)
|
||||
return
|
||||
end
|
||||
|
||||
# Now, test that it works correctly.
|
||||
assert(@provider.manages_passwords?,
|
||||
"Defaulted to not managing passwords on %s" %
|
||||
Facter.value(:kernel))
|
||||
@vals[:password] = "somethingorother"
|
||||
setup_user
|
||||
|
||||
|
@ -244,6 +230,7 @@ end
|
|||
|
||||
class UserRootAddProviderTest < PuppetTest::TestCase
|
||||
confine "useradd user provider missing" => Puppet::Type.type(:user).provider(:useradd).suitable?
|
||||
confine "useradd does not manage passwords" => Puppet::Type.type(:user).provider(:useradd).manages_passwords?
|
||||
confine "not running as root" => (Process.uid == 0)
|
||||
|
||||
def test_password
|
||||
|
|
Загрузка…
Ссылка в новой задаче