Adding resource convertion to the parser resources
Also uses Puppet::Resource's method for creating transportable resources. Signed-off-by: Luke Kanies <luke@madstop.com>
This commit is contained in:
Родитель
c306a17447
Коммит
e3b1590f57
|
@ -278,6 +278,42 @@ class Puppet::Parser::Resource
|
|||
return db_resource
|
||||
end
|
||||
|
||||
# Create a Puppet::Resource instance from this parser resource.
|
||||
# We plan, at some point, on not needing to do this conversion, but
|
||||
# it's sufficient for now.
|
||||
def to_resource
|
||||
result = Puppet::Resource.new(type, title)
|
||||
|
||||
to_hash.each do |p, v|
|
||||
if v.is_a?(Puppet::Parser::Resource::Reference)
|
||||
v = Puppet::Resource::Reference.new(v.type, v.title)
|
||||
elsif v.is_a?(Array)
|
||||
v = v.collect do |av|
|
||||
if av.is_a?(Puppet::Parser::Resource::Reference)
|
||||
av = Puppet::Resource::Reference.new(av.type, av.title)
|
||||
end
|
||||
av
|
||||
end
|
||||
end
|
||||
|
||||
# If the value is an array with only one value, then
|
||||
# convert it to a single value. This is largely so that
|
||||
# the database interaction doesn't have to worry about
|
||||
# whether it returns an array or a string.
|
||||
result[p] = if v.is_a?(Array) and v.length == 1
|
||||
v[0]
|
||||
else
|
||||
v
|
||||
end
|
||||
end
|
||||
|
||||
result.file = self.file
|
||||
result.line = self.line
|
||||
result.tag(*self.tags)
|
||||
|
||||
return result
|
||||
end
|
||||
|
||||
def to_s
|
||||
self.ref
|
||||
end
|
||||
|
@ -286,21 +322,7 @@ class Puppet::Parser::Resource
|
|||
def to_trans
|
||||
return nil if virtual?
|
||||
|
||||
if builtin?
|
||||
to_transobject
|
||||
else
|
||||
to_transbucket
|
||||
end
|
||||
end
|
||||
|
||||
def to_transbucket
|
||||
bucket = Puppet::TransBucket.new([])
|
||||
|
||||
bucket.type = self.type
|
||||
bucket.name = self.title
|
||||
|
||||
# TransBuckets don't support parameters, which is why they're being deprecated.
|
||||
return bucket
|
||||
return to_resource.to_trans
|
||||
end
|
||||
|
||||
# Convert this resource to a RAL resource. We hackishly go via the
|
||||
|
@ -308,40 +330,6 @@ class Puppet::Parser::Resource
|
|||
def to_type
|
||||
to_trans.to_type
|
||||
end
|
||||
|
||||
def to_transobject
|
||||
# Now convert to a transobject
|
||||
obj = Puppet::TransObject.new(@ref.title, @ref.type)
|
||||
to_hash.each do |p, v|
|
||||
if v.is_a?(Reference)
|
||||
v = v.to_ref
|
||||
elsif v.is_a?(Array)
|
||||
v = v.collect { |av|
|
||||
if av.is_a?(Reference)
|
||||
av = av.to_ref
|
||||
end
|
||||
av
|
||||
}
|
||||
end
|
||||
|
||||
# If the value is an array with only one value, then
|
||||
# convert it to a single value. This is largely so that
|
||||
# the database interaction doesn't have to worry about
|
||||
# whether it returns an array or a string.
|
||||
obj[p.to_s] = if v.is_a?(Array) and v.length == 1
|
||||
v[0]
|
||||
else
|
||||
v
|
||||
end
|
||||
end
|
||||
|
||||
obj.file = self.file
|
||||
obj.line = self.line
|
||||
|
||||
obj.tags = self.tags
|
||||
|
||||
return obj
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
|
|
|
@ -333,4 +333,84 @@ describe Puppet::Parser::Resource do
|
|||
|
||||
|
||||
end
|
||||
|
||||
it "should be able to be converted to a normal resource" do
|
||||
@source = stub 'scope', :name => "myscope"
|
||||
@resource = mkresource :source => @source
|
||||
@resource.should respond_to(:to_resource)
|
||||
end
|
||||
|
||||
it "should use its resource converter to convert to a transportable resource" do
|
||||
@source = stub 'scope', :name => "myscope"
|
||||
@resource = mkresource :source => @source
|
||||
|
||||
newresource = Puppet::Resource.new(:file, "/my")
|
||||
Puppet::Resource.expects(:new).returns(newresource)
|
||||
|
||||
newresource.expects(:to_trans).returns "mytrans"
|
||||
|
||||
@resource.to_trans.should == "mytrans"
|
||||
end
|
||||
|
||||
it "should return nil if converted to a transportable resource and it is virtual" do
|
||||
@source = stub 'scope', :name => "myscope"
|
||||
@resource = mkresource :source => @source
|
||||
|
||||
@resource.expects(:virtual?).returns true
|
||||
@resource.to_trans.should be_nil
|
||||
end
|
||||
|
||||
describe "when being converted to a resource" do
|
||||
before do
|
||||
@source = stub 'scope', :name => "myscope"
|
||||
@parser_resource = mkresource :source => @source, :params => {:foo => "bar", :fee => "fum"}
|
||||
end
|
||||
|
||||
it "should create an instance of Puppet::Resource" do
|
||||
@parser_resource.to_resource.should be_instance_of(Puppet::Resource)
|
||||
end
|
||||
|
||||
it "should set the type correctly on the Puppet::Resource" do
|
||||
@parser_resource.to_resource.type.should == @parser_resource.type
|
||||
end
|
||||
|
||||
it "should set the title correctly on the Puppet::Resource" do
|
||||
@parser_resource.to_resource.title.should == @parser_resource.title
|
||||
end
|
||||
|
||||
it "should copy over all of the parameters" do
|
||||
@parser_resource.to_resource.to_hash.should == {:foo => "bar", :fee => "fum"}
|
||||
end
|
||||
|
||||
it "should copy over the tags" do
|
||||
@parser_resource.tag "foo"
|
||||
@parser_resource.tag "bar"
|
||||
|
||||
@parser_resource.to_resource.tags.should == @parser_resource.tags
|
||||
end
|
||||
|
||||
it "should copy over the line" do
|
||||
@parser_resource.line = 40
|
||||
@parser_resource.to_resource.line.should == 40
|
||||
end
|
||||
|
||||
it "should copy over the file" do
|
||||
@parser_resource.file = "/my/file"
|
||||
@parser_resource.to_resource.file.should == "/my/file"
|
||||
end
|
||||
|
||||
it "should convert any parser resource references to Puppet::Resource::Reference instances" do
|
||||
ref = Puppet::Parser::Resource::Reference.new(:title => "/my/file", :type => "file")
|
||||
@parser_resource = mkresource :source => @source, :params => {:foo => "bar", :fee => ref}
|
||||
result = @parser_resource.to_resource
|
||||
result[:fee].should == Puppet::Resource::Reference.new(:file, "/my/file")
|
||||
end
|
||||
|
||||
it "should convert any parser resource references to Puppet::Resource::Reference instances even if they are in an array" do
|
||||
ref = Puppet::Parser::Resource::Reference.new(:title => "/my/file", :type => "file")
|
||||
@parser_resource = mkresource :source => @source, :params => {:foo => "bar", :fee => ["a", ref]}
|
||||
result = @parser_resource.to_resource
|
||||
result[:fee].should == ["a", Puppet::Resource::Reference.new(:file, "/my/file")]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -84,53 +84,6 @@ class TestResource < PuppetTest::TestCase
|
|||
res.send(:paramcheck, :other)
|
||||
end
|
||||
|
||||
def test_to_transobject
|
||||
# First try translating a builtin resource. Make sure we use some references
|
||||
# and arrays, to make sure they translate correctly.
|
||||
source = mock("source")
|
||||
scope = mkscope
|
||||
scope.stubs(:tags).returns([])
|
||||
refs = []
|
||||
4.times { |i| refs << Puppet::Parser::Resource::Reference.new(:title => "file%s" % i, :type => "file") }
|
||||
res = Parser::Resource.new :type => "file", :title => "/tmp",
|
||||
:source => source, :scope => scope,
|
||||
:params => paramify(source, :owner => "nobody", :group => %w{you me},
|
||||
:require => refs[0], :ignore => %w{svn},
|
||||
:subscribe => [refs[1], refs[2]], :notify => [refs[3]])
|
||||
|
||||
obj = nil
|
||||
assert_nothing_raised do
|
||||
obj = res.to_trans
|
||||
end
|
||||
|
||||
assert_instance_of(Puppet::TransObject, obj)
|
||||
|
||||
assert_equal(obj.type, res.type.downcase)
|
||||
assert_equal(obj.name, res.title)
|
||||
|
||||
# TransObjects use strings, resources use symbols
|
||||
assert_equal("nobody", obj["owner"], "Single-value string was not passed correctly")
|
||||
assert_equal(%w{you me}, obj["group"], "Array of strings was not passed correctly")
|
||||
assert_equal("svn", obj["ignore"], "Array with single string was not turned into single value")
|
||||
assert_equal(["file", refs[0].title], obj["require"], "Resource reference was not passed correctly")
|
||||
assert_equal([["file", refs[1].title], ["file", refs[2].title]], obj["subscribe"], "Array of resource references was not passed correctly")
|
||||
assert_equal(["file", refs[3].title], obj["notify"], "Array with single resource reference was not turned into single value")
|
||||
end
|
||||
|
||||
# FIXME This isn't a great test, but I need to move on.
|
||||
def test_to_transbucket
|
||||
bucket = mock("transbucket")
|
||||
source = mock("source")
|
||||
scope = mkscope
|
||||
res = Parser::Resource.new :type => "mydefine", :title => "yay",
|
||||
:source => source, :scope => scope
|
||||
|
||||
|
||||
result = res.to_trans
|
||||
assert_equal("yay", result.name, "did not set bucket name correctly")
|
||||
assert_equal("Mydefine", result.type, "did not set bucket type correctly")
|
||||
end
|
||||
|
||||
def test_evaluate
|
||||
# First try the most common case, we're not a builtin type.
|
||||
res = mkresource
|
||||
|
@ -154,43 +107,6 @@ class TestResource < PuppetTest::TestCase
|
|||
assert_equal(false, res.builtin?)
|
||||
end
|
||||
|
||||
def test_reference_conversion
|
||||
# First try it as a normal string
|
||||
ref = Parser::Resource::Reference.new(:type => "file", :title => "/tmp/ref1")
|
||||
|
||||
# Now create an obj that uses it
|
||||
res = mkresource :type => "file", :title => "/tmp/resource",
|
||||
:params => {:require => ref}
|
||||
res.scope = mkscope
|
||||
|
||||
trans = nil
|
||||
assert_nothing_raised do
|
||||
trans = res.to_trans
|
||||
end
|
||||
|
||||
assert_instance_of(Array, trans["require"])
|
||||
assert_equal(["file", "/tmp/ref1"], trans["require"])
|
||||
|
||||
# Now try it when using an array of references.
|
||||
two = Parser::Resource::Reference.new(:type => "file", :title => "/tmp/ref2")
|
||||
res = mkresource :type => "file", :title => "/tmp/resource2",
|
||||
:params => {:require => [ref, two]}
|
||||
res.scope = mkscope
|
||||
|
||||
trans = nil
|
||||
assert_nothing_raised do
|
||||
trans = res.to_trans
|
||||
end
|
||||
|
||||
assert_instance_of(Array, trans["require"][0])
|
||||
trans["require"].each do |val|
|
||||
assert_instance_of(Array, val)
|
||||
assert_equal("file", val[0])
|
||||
assert(val[1] =~ /\/tmp\/ref[0-9]/,
|
||||
"Was %s instead of the file name" % val[1])
|
||||
end
|
||||
end
|
||||
|
||||
# This is a bit of a weird one -- the user should not actually know
|
||||
# that components exist, so we want references to act like they're not
|
||||
# builtin
|
||||
|
|
Загрузка…
Ссылка в новой задаче