diff options
| author | Luke Kanies <luke@madstop.com> | 2008-12-09 17:02:29 -0600 |
|---|---|---|
| committer | Luke Kanies <luke@madstop.com> | 2008-12-18 11:10:21 -0600 |
| commit | e3b1590f57a18b89c5f97ca0aa8e8d2bd9187b58 (patch) | |
| tree | 12a504efed000f16bbe1d45e9e6f71471714ae44 | |
| parent | c306a1744793420337421f6fdf3630a9121861bd (diff) | |
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>
| -rw-r--r-- | lib/puppet/parser/resource.rb | 76 | ||||
| -rwxr-xr-x | spec/unit/parser/resource.rb | 80 | ||||
| -rwxr-xr-x | test/language/resource.rb | 84 |
3 files changed, 112 insertions, 128 deletions
diff --git a/lib/puppet/parser/resource.rb b/lib/puppet/parser/resource.rb index 747338b3b..7dec02b1f 100644 --- a/lib/puppet/parser/resource.rb +++ b/lib/puppet/parser/resource.rb @@ -278,69 +278,57 @@ class Puppet::Parser::Resource return db_resource end - def to_s - self.ref - end - - # Translate our object to a transportable object. - def to_trans - return nil if virtual? + # 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) - 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 - end - - # Convert this resource to a RAL resource. We hackishly go via the - # transportable stuff. - 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 + 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 { |av| - if av.is_a?(Reference) - av = av.to_ref + 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. - obj[p.to_s] = if v.is_a?(Array) and v.length == 1 + result[p] = if v.is_a?(Array) and v.length == 1 v[0] else v end end - obj.file = self.file - obj.line = self.line + result.file = self.file + result.line = self.line + result.tag(*self.tags) + + return result + end + + def to_s + self.ref + end + + # Translate our object to a transportable object. + def to_trans + return nil if virtual? - obj.tags = self.tags + return to_resource.to_trans + end - return obj + # Convert this resource to a RAL resource. We hackishly go via the + # transportable stuff. + def to_type + to_trans.to_type end private diff --git a/spec/unit/parser/resource.rb b/spec/unit/parser/resource.rb index 63cfbc2ed..37fd44387 100755 --- a/spec/unit/parser/resource.rb +++ b/spec/unit/parser/resource.rb @@ -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 diff --git a/test/language/resource.rb b/test/language/resource.rb index b3eaf0390..fa04117f1 100755 --- a/test/language/resource.rb +++ b/test/language/resource.rb @@ -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 |
