diff options
author | luke <luke@980ebf18-57e1-0310-9a29-db15c13687c0> | 2007-06-17 21:41:50 +0000 |
---|---|---|
committer | luke <luke@980ebf18-57e1-0310-9a29-db15c13687c0> | 2007-06-17 21:41:50 +0000 |
commit | 46252b5bb858a1f2b87cc8646f3a59f935c58061 (patch) | |
tree | 47d016a74967ae7fac18b711dcf7c8a998730d9d | |
parent | 6084e1a0efa2165e5cb10fff9ef0b06c1560f9c0 (diff) | |
download | puppet-46252b5bb858a1f2b87cc8646f3a59f935c58061.tar.gz puppet-46252b5bb858a1f2b87cc8646f3a59f935c58061.tar.xz puppet-46252b5bb858a1f2b87cc8646f3a59f935c58061.zip |
All rails and language tests now pass again. All of the rails tests should now be in the rails/ directory, and I have modified resource translation so that it always converts single-member arrays to singe values, which means the rails collection does not need to worry about it.
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@2597 980ebf18-57e1-0310-9a29-db15c13687c0
-rw-r--r-- | lib/puppet/parser/resource.rb | 11 | ||||
-rw-r--r-- | lib/puppet/parser/resource/param.rb | 2 | ||||
-rw-r--r-- | lib/puppet/rails/resource.rb | 15 | ||||
-rwxr-xr-x | test/language/ast.rb | 150 | ||||
-rwxr-xr-x | test/language/collector.rb | 3 | ||||
-rwxr-xr-x | test/language/interpreter.rb | 84 | ||||
-rwxr-xr-x | test/language/resource.rb | 19 | ||||
-rw-r--r-- | test/lib/puppettest/support/collection.rb | 30 | ||||
-rwxr-xr-x | test/rails/ast.rb | 74 | ||||
-rwxr-xr-x | test/rails/interpreter.rb | 91 | ||||
-rwxr-xr-x | test/rails/railsresource.rb | 28 |
11 files changed, 251 insertions, 256 deletions
diff --git a/lib/puppet/parser/resource.rb b/lib/puppet/parser/resource.rb index 25c9ab707..0722f388c 100644 --- a/lib/puppet/parser/resource.rb +++ b/lib/puppet/parser/resource.rb @@ -356,7 +356,16 @@ class Puppet::Parser::Resource av } end - obj[p.to_s] = v + + # 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 diff --git a/lib/puppet/parser/resource/param.rb b/lib/puppet/parser/resource/param.rb index c719a7fd8..4d95db46a 100644 --- a/lib/puppet/parser/resource/param.rb +++ b/lib/puppet/parser/resource/param.rb @@ -22,7 +22,7 @@ class Puppet::Parser::Resource::Param # Store a new parameter in a Rails db. def to_rails(db_resource) values = value.is_a?(Array) ? value : [value] - values.map! { |v| v.to_s } + values = values.map { |v| v.to_s } param_name = Puppet::Rails::ParamName.find_or_create_by_name(self.name.to_s) line_number = line_to_i() diff --git a/lib/puppet/rails/resource.rb b/lib/puppet/rails/resource.rb index 2f58681ab..0163394b1 100644 --- a/lib/puppet/rails/resource.rb +++ b/lib/puppet/rails/resource.rb @@ -35,7 +35,7 @@ class Puppet::Rails::Resource < ActiveRecord::Base # returns a hash of param_names.name => [param_values] def get_params_hash(values = nil) values ||= param_values.find(:all, :include => :param_name) - return values.inject({}) do | hash, value | + values.inject({}) do | hash, value | hash[value.param_name.name] ||= [] hash[value.param_name.name] << value hash @@ -69,12 +69,15 @@ class Puppet::Rails::Resource < ActiveRecord::Base end def parameters - return self.param_values.find(:all, - :include => :param_name).inject({}) do |hash, pvalue| - hash[pvalue.param_name.name] ||= [] - hash[pvalue.param_name.name] << pvalue.value - hash + result = get_params_hash + result.each do |param, value| + if value.is_a?(Array) + result[param] = value.collect { |v| v.value } + else + result[param] = value.value + end end + result end def ref diff --git a/test/language/ast.rb b/test/language/ast.rb index 29b8004dc..9e00c610d 100755 --- a/test/language/ast.rb +++ b/test/language/ast.rb @@ -2,80 +2,17 @@ $:.unshift("../lib").unshift("../../lib") if __FILE__ =~ /\.rb$/ -require 'puppet' -require 'puppet/rails' +require 'puppettest' require 'puppet/parser/interpreter' require 'puppet/parser/parser' -require 'puppet/network/client' -require 'puppettest' require 'puppettest/resourcetesting' require 'puppettest/parsertesting' -require 'puppettest/railstesting' +require 'puppettest/support/collection' class TestAST < Test::Unit::TestCase - include PuppetTest::RailsTesting include PuppetTest::ParserTesting include PuppetTest::ResourceTesting - - if defined? ActiveRecord - # Verify that our collection stuff works. - def test_collection - collectable = [] - non = [] - # First put some objects into the database. - bucket = mk_transtree do |object, depth, width| - # and mark some of them collectable - if width % 2 == 1 - object.collectable = true - collectable << object - else - non << object - end - end - - # Now collect our facts - facts = {} - Facter.each do |fact, value| facts[fact] = value end - - assert_nothing_raised { - Puppet::Rails.init - } - - # Now try storing our crap - assert_nothing_raised { - host = Puppet::Rails::Host.store( - :objects => bucket, - :facts => facts, - :host => facts["hostname"] - ) - } - - # Now create an ast tree that collects that. They should all be files. - coll = nil - assert_nothing_raised { - coll = AST::Collection.new( - :type => nameobj("file") - ) - } - - top = nil - assert_nothing_raised("Could not create top object") { - top = AST::ASTArray.new( - :children => [coll] - ) - } - - objects = nil - assert_nothing_raised("Could not evaluate") { - scope = mkscope - objects = scope.evaluate(:ast => top).flatten - } - - assert(objects.length > 0, "Did not receive any collected objects") - end - else - $stderr.puts "No ActiveRecord -- skipping collection tests" - end + include PuppetTest::Support::Collection def test_if astif = nil @@ -251,87 +188,6 @@ class TestAST < Test::Unit::TestCase end end end - - if defined? ActiveRecord::Base - def test_exported_collexp - railsinit - Puppet[:storeconfigs] = true - @interp, @scope, @source = mkclassframing - - # make a rails resource - railsresource "file", "/tmp/testing", :owner => "root", :group => "bin", - :mode => "644" - - run_collection_queries(:exported) do |string, result, query| - code = nil - str = nil - - # We don't support anything but the title in rails right now - retval = nil - bad = false - # Figure out if the search is for anything rails will ignore - string.scan(/(\w+) [!=]= \w+/) do |s| - unless s[0] == "title" - bad = true - break - end - end - - # And if it is, make sure we throw an error. - if bad - assert_raise(Puppet::ParseError, "Evaluated '#{string}'") do - str, code = query.evaluate :scope => @scope - end - next - else - assert_nothing_raised("Could not evaluate '#{string}'") do - str, code = query.evaluate :scope => @scope - end - end - assert_nothing_raised("Could not find resource") do - retval = Puppet::Rails::Resource.find(:all, - :include => :param_values, - :conditions => str) - end - - if result - assert_equal(1, retval.length, "Did not find resource with '#{string}'") - res = retval.shift - - assert_equal("file", res.restype) - assert_equal("/tmp/testing", res.title) - else - assert_equal(0, retval.length, "found a resource with '#{string}'") - end - end - end - end - - def run_collection_queries(form) - {true => [%{title == "/tmp/testing"}, %{(title == "/tmp/testing")}, - %{title == "/tmp/testing" and group == bin}, %{title == bin or group == bin}, - %{title == "/tmp/testing" or title == bin}, %{title == "/tmp/testing"}, - %{(title == "/tmp/testing" or title == bin) and group == bin}], - false => [%{title == bin}, %{title == bin or (title == bin and group == bin)}, - %{title != "/tmp/testing"}, %{title != "/tmp/testing" and group != bin}] - }.each do |res, ary| - ary.each do |str| - if form == :virtual - code = "File <| #{str} |>" - else - code = "File <<| #{str} |>>" - end - parser = mkparser - query = nil - - assert_nothing_raised("Could not parse '#{str}'") do - query = parser.parse(code)[0].query - end - - yield str, res, query - end - end - end end # $Id$ diff --git a/test/language/collector.rb b/test/language/collector.rb index e3a2db98c..bdcaf4aec 100755 --- a/test/language/collector.rb +++ b/test/language/collector.rb @@ -2,17 +2,14 @@ $:.unshift("../lib").unshift("../../lib") if __FILE__ =~ /\.rb$/ -require 'puppet/rails' require 'puppettest' require 'puppettest/parsertesting' require 'puppettest/resourcetesting' -require 'puppettest/railstesting' class TestCollector < Test::Unit::TestCase include PuppetTest include PuppetTest::ParserTesting include PuppetTest::ResourceTesting - include PuppetTest::RailsTesting Parser = Puppet::Parser AST = Parser::AST diff --git a/test/language/interpreter.rb b/test/language/interpreter.rb index cf2cba854..aababc1d0 100755 --- a/test/language/interpreter.rb +++ b/test/language/interpreter.rb @@ -8,12 +8,10 @@ require 'puppet' require 'puppet/parser/interpreter' require 'puppet/parser/parser' require 'puppet/network/client' -require 'puppet/rails' require 'puppettest' require 'puppettest/resourcetesting' require 'puppettest/parsertesting' require 'puppettest/servertest' -require 'puppettest/railstesting' require 'timeout' class TestInterpreter < PuppetTest::TestCase @@ -21,7 +19,6 @@ class TestInterpreter < PuppetTest::TestCase include PuppetTest::ServerTest include PuppetTest::ParserTesting include PuppetTest::ResourceTesting - include PuppetTest::RailsTesting AST = Puppet::Parser::AST NodeDef = Puppet::Parser::Interpreter::NodeDef @@ -920,7 +917,6 @@ class LdapNodeTest < PuppetTest::TestCase include PuppetTest::ServerTest include PuppetTest::ParserTesting include PuppetTest::ResourceTesting - include PuppetTest::RailsTesting AST = Puppet::Parser::AST NodeDef = Puppet::Parser::Interpreter::NodeDef confine "LDAP is not available" => Puppet.features.ldap? @@ -1004,7 +1000,6 @@ class LdapReconnectTests < PuppetTest::TestCase include PuppetTest::ServerTest include PuppetTest::ParserTesting include PuppetTest::ResourceTesting - include PuppetTest::RailsTesting AST = Puppet::Parser::AST NodeDef = Puppet::Parser::Interpreter::NodeDef confine "Not running on culain as root" => (Puppet::Util::SUIDManager.uid == 0 and Facter.value("hostname") == "culain") @@ -1048,83 +1043,4 @@ class LdapReconnectTests < PuppetTest::TestCase end end -class InterpreterRailsTests < PuppetTest::TestCase - include PuppetTest - include PuppetTest::ServerTest - include PuppetTest::ParserTesting - include PuppetTest::ResourceTesting - include PuppetTest::RailsTesting - AST = Puppet::Parser::AST - NodeDef = Puppet::Parser::Interpreter::NodeDef - confine "No rails support" => Puppet.features.rails? - - # We need to make sure finished objects are stored in the db. - def test_finish_before_store - railsinit - interp = mkinterp - - node = interp.newnode ["myhost"], :code => AST::ASTArray.new(:children => [ - resourcedef("file", "/tmp/yay", :group => "root"), - defaultobj("file", :owner => "root") - ]) - - interp.newclass "myclass", :code => AST::ASTArray.new(:children => [ - ]) - - interp.newclass "sub", :parent => "myclass", - :code => AST::ASTArray.new(:children => [ - resourceoverride("file", "/tmp/yay", :owner => "root") - ] - ) - - # Now do the rails crap - Puppet[:storeconfigs] = true - - interp.evaluate("myhost", {}) - - # And then retrieve the object from rails - res = Puppet::Rails::Resource.find_by_restype_and_title("file", "/tmp/yay") - - assert(res, "Did not get resource from rails") - - param = res.param_names.find_by_name("owner", :include => :param_values) - - assert(param, "Did not find owner param") - - pvalue = param.param_values.find_by_value("root") - assert_equal("root", pvalue[:value]) - end - - def test_hoststorage - assert_nothing_raised { - Puppet[:storeconfigs] = true - } - - file = tempfile() - File.open(file, "w") { |f| - f.puts "file { \"/etc\": owner => root }" - } - - interp = nil - assert_nothing_raised { - interp = Puppet::Parser::Interpreter.new( - :Manifest => file, - :UseNodes => false, - :ForkSave => false - ) - } - - facts = {} - Facter.each { |fact, val| facts[fact] = val } - - objects = nil - assert_nothing_raised { - objects = interp.run(facts["hostname"], facts) - } - - obj = Puppet::Rails::Host.find_by_name(facts["hostname"]) - assert(obj, "Could not find host object") - end -end - # $Id$ diff --git a/test/language/resource.rb b/test/language/resource.rb index 47978eda9..d2fdd0967 100755 --- a/test/language/resource.rb +++ b/test/language/resource.rb @@ -4,13 +4,11 @@ $:.unshift("../lib").unshift("../../lib") if __FILE__ =~ /\.rb$/ require 'puppettest' require 'puppettest/resourcetesting' -require 'puppettest/railstesting' class TestResource < PuppetTest::TestCase include PuppetTest include PuppetTest::ParserTesting include PuppetTest::ResourceTesting - include PuppetTest::RailsTesting Parser = Puppet::Parser AST = Parser::AST Reference = Puppet::Parser::Resource::Reference @@ -134,10 +132,15 @@ class TestResource < PuppetTest::TestCase end def test_to_trans - # First try translating a builtin resource + # First try translating a builtin resource. Make sure we use some references + # and arrays, to make sure they translate correctly. + 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", :mode => "644") + :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 @@ -150,8 +153,12 @@ class TestResource < PuppetTest::TestCase assert_equal(obj.name, res.title) # TransObjects use strings, resources use symbols - hash = obj.to_hash.inject({}) { |h,a| h[a[0].intern] = a[1]; h } - assert_equal(hash, res.to_hash) + 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 def test_adddefaults diff --git a/test/lib/puppettest/support/collection.rb b/test/lib/puppettest/support/collection.rb new file mode 100644 index 000000000..5dbb7a223 --- /dev/null +++ b/test/lib/puppettest/support/collection.rb @@ -0,0 +1,30 @@ + +module PuppetTest::Support::Collection + def run_collection_queries(form) + {true => [%{title == "/tmp/testing"}, %{(title == "/tmp/testing")}, + %{title == "/tmp/testing" and group == bin}, %{title == bin or group == bin}, + %{title == "/tmp/testing" or title == bin}, %{title == "/tmp/testing"}, + %{(title == "/tmp/testing" or title == bin) and group == bin}], + false => [%{title == bin}, %{title == bin or (title == bin and group == bin)}, + %{title != "/tmp/testing"}, %{title != "/tmp/testing" and group != bin}] + }.each do |res, ary| + ary.each do |str| + if form == :virtual + code = "File <| #{str} |>" + else + code = "File <<| #{str} |>>" + end + parser = mkparser + query = nil + + assert_nothing_raised("Could not parse '#{str}'") do + query = parser.parse(code)[0].query + end + + yield str, res, query + end + end + end +end + +# $Id$ diff --git a/test/rails/ast.rb b/test/rails/ast.rb new file mode 100755 index 000000000..0493237f5 --- /dev/null +++ b/test/rails/ast.rb @@ -0,0 +1,74 @@ +#!/usr/bin/env ruby + +$:.unshift("../lib").unshift("../../lib") if __FILE__ =~ /\.rb$/ + +require 'puppettest' +require 'puppet/rails' +require 'puppet/parser/parser' +require 'puppettest/resourcetesting' +require 'puppettest/parsertesting' +require 'puppettest/railstesting' +require 'puppettest/support/collection' + +class TestRailsAST < PuppetTest::TestCase + confine "Missing rails" => Puppet.features.rails? + include PuppetTest::RailsTesting + include PuppetTest::ParserTesting + include PuppetTest::ResourceTesting + include PuppetTest::Support::Collection + + def test_exported_collexp + railsinit + Puppet[:storeconfigs] = true + @interp, @scope, @source = mkclassframing + + # make a rails resource + railsresource "file", "/tmp/testing", :owner => "root", :group => "bin", + :mode => "644" + + run_collection_queries(:exported) do |string, result, query| + code = nil + str = nil + + # We don't support anything but the title in rails right now + retval = nil + bad = false + # Figure out if the search is for anything rails will ignore + string.scan(/(\w+) [!=]= \w+/) do |s| + unless s[0] == "title" + bad = true + break + end + end + + # And if it is, make sure we throw an error. + if bad + assert_raise(Puppet::ParseError, "Evaluated '#{string}'") do + str, code = query.evaluate :scope => @scope + end + next + else + assert_nothing_raised("Could not evaluate '#{string}'") do + str, code = query.evaluate :scope => @scope + end + end + assert_nothing_raised("Could not find resource") do + retval = Puppet::Rails::Resource.find(:all, + :include => :param_values, + :conditions => str) + end + + if result + assert_equal(1, retval.length, "Did not find resource with '#{string}'") + res = retval.shift + + assert_equal("file", res.restype) + assert_equal("/tmp/testing", res.title) + else + assert_equal(0, retval.length, "found a resource with '#{string}'") + end + end + end +end + +# $Id$ diff --git a/test/rails/interpreter.rb b/test/rails/interpreter.rb new file mode 100755 index 000000000..0eba3f590 --- /dev/null +++ b/test/rails/interpreter.rb @@ -0,0 +1,91 @@ +#!/usr/bin/env ruby + +$:.unshift("../lib").unshift("../../lib") if __FILE__ =~ /\.rb$/ + +require 'puppettest' +require 'puppet/parser/interpreter' +require 'puppet/parser/parser' +require 'puppet/network/client' +require 'puppet/rails' +require 'puppettest/resourcetesting' +require 'puppettest/parsertesting' +require 'puppettest/servertest' +require 'puppettest/railstesting' + + +class InterpreterRailsTests < PuppetTest::TestCase + include PuppetTest + include PuppetTest::ServerTest + include PuppetTest::ParserTesting + include PuppetTest::ResourceTesting + include PuppetTest::RailsTesting + AST = Puppet::Parser::AST + NodeDef = Puppet::Parser::Interpreter::NodeDef + confine "No rails support" => Puppet.features.rails? + + # We need to make sure finished objects are stored in the db. + def test_finish_before_store + railsinit + interp = mkinterp + + node = interp.newnode ["myhost"], :code => AST::ASTArray.new(:children => [ + resourcedef("file", "/tmp/yay", :group => "root"), + defaultobj("file", :owner => "root") + ]) + + interp.newclass "myclass", :code => AST::ASTArray.new(:children => [ + ]) + + interp.newclass "sub", :parent => "myclass", + :code => AST::ASTArray.new(:children => [ + resourceoverride("file", "/tmp/yay", :owner => "root") + ] + ) + + # Now do the rails crap + Puppet[:storeconfigs] = true + + interp.evaluate("myhost", {}) + + # And then retrieve the object from rails + #res = Puppet::Rails::Resource.find_by_restype_and_title("file", "/tmp/yay", :include => {:param_values => :param_names}) + res = Puppet::Rails::Resource.find_by_restype_and_title("file", "/tmp/yay") + + assert(res, "Did not get resource from rails") + + params = res.parameters + + assert_equal(["root"], params["owner"], "Did not get correct value for owner param") + end + + def test_hoststorage + assert_nothing_raised { + Puppet[:storeconfigs] = true + } + + file = tempfile() + File.open(file, "w") { |f| + f.puts "file { \"/etc\": owner => root }" + } + + interp = nil + assert_nothing_raised { + interp = Puppet::Parser::Interpreter.new( + :Manifest => file, + :UseNodes => false, + :ForkSave => false + ) + } + + facts = {} + Facter.each { |fact, val| facts[fact] = val } + + objects = nil + assert_nothing_raised { + objects = interp.run(facts["hostname"], facts) + } + + obj = Puppet::Rails::Host.find_by_name(facts["hostname"]) + assert(obj, "Could not find host object") + end +end diff --git a/test/rails/railsresource.rb b/test/rails/railsresource.rb index 2a790be39..a7b6bbc02 100755 --- a/test/rails/railsresource.rb +++ b/test/rails/railsresource.rb @@ -118,11 +118,17 @@ class TestExportedResources < PuppetTest::TestCase # Compare a parser resource to a rails resource. def compare_resources(host, res, updating, options = {}) - # to_rails now expects to be passed a resource, else it will create a new one - newobj = host.resources.find_by_restype_and_title(res.type, res.title) - assert_nothing_raised do - #newobj = res.to_rails(host, newobj) - newobj = res.to_rails(host) + newobj = nil + + # If the resource is in the db, then use modify_rails, else use to_rails + if newobj = Puppet::Rails::Resource.find_by_restype_and_title(res.type, res.title) + assert_nothing_raised("Call to modify_rails failed") do + res.modify_rails(newobj) + end + else + assert_nothing_raised("Call to to_rails failed") do + newobj = res.to_rails(host) + end end assert_instance_of(Puppet::Rails::Resource, newobj) @@ -138,7 +144,7 @@ class TestExportedResources < PuppetTest::TestCase count = 0 obj = nil Puppet::Rails::Resource.find(:all).each do |obj| - assert_equal(newobj.id, obj.id, "Found object has a different id than generated object %s" % tail) + assert_equal(newobj.id, obj.id, "A new resource was created instead of modifying an existing resource") count += 1 [:title, :restype, :line, :exported].each do |param| if param == :restype @@ -197,19 +203,25 @@ class TestExportedResources < PuppetTest::TestCase compare_resources(host, res, false, :params => %w{owner source mode}) + # Now make sure our parameters did not change + assert_instance_of(Array, res[:require], "Parameter array changed") + res[:require].each do |ref| + assert_instance_of(Reference, ref, "Resource reference changed") + end + # Now make some changes to our resource. We're removing the mode, # changing the source, and adding 'check'. res = mkresource :type => "file", :title => "/tmp/testing", :source => @source, :scope => @scope, :params => {:owner => "bin", :source => ["/tmp/A", "/tmp/C"], - :check => "checksum"} + :check => "checksum", :require => [ref1, ref2]} res.line = 75 res.exported = true compare_resources(host, res, true, :params => %w{owner source mode check}) - # Now make sure our parameters did not change + # Again make sure our parameters did not change assert_instance_of(Array, res[:require], "Parameter array changed") res[:require].each do |ref| assert_instance_of(Reference, ref, "Resource reference changed") |