diff options
| author | luke <luke@980ebf18-57e1-0310-9a29-db15c13687c0> | 2006-10-04 18:24:24 +0000 |
|---|---|---|
| committer | luke <luke@980ebf18-57e1-0310-9a29-db15c13687c0> | 2006-10-04 18:24:24 +0000 |
| commit | 28cee40689440388994a4768bd301ae32c8ecc05 (patch) | |
| tree | c865ab44f4c9247052cf83de16ffc8ebe8b15e54 /test/lib | |
| parent | e0e291332bd4676962a28c7b220ae5c5e9651c0a (diff) | |
Merging the changes from the override-refactor branch. This is a significant rewrite of the parser, but it has little affect on the rest of the code tree.
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@1726 980ebf18-57e1-0310-9a29-db15c13687c0
Diffstat (limited to 'test/lib')
| -rw-r--r-- | test/lib/puppettest.rb | 31 | ||||
| -rw-r--r-- | test/lib/puppettest/parsertesting.rb | 151 | ||||
| -rw-r--r-- | test/lib/puppettest/railstesting.rb | 34 | ||||
| -rw-r--r-- | test/lib/puppettest/resourcetesting.rb | 64 | ||||
| -rw-r--r-- | test/lib/puppettest/support/utils.rb | 4 |
5 files changed, 206 insertions, 78 deletions
diff --git a/test/lib/puppettest.rb b/test/lib/puppettest.rb index e7ee99c35..c6ce54b93 100644 --- a/test/lib/puppettest.rb +++ b/test/lib/puppettest.rb @@ -4,14 +4,14 @@ require 'test/unit' module PuppetTest # Find the root of the Puppet tree; this is not the test directory, but # the parent of that dir. - def basedir + def basedir(*list) unless defined? @@basedir case $0 when /rake_test_loader/ @@basedir = File.dirname(Dir.getwd) else dir = nil - if /^#{File::SEPARATOR}.+\.rb/ + if $0 =~ /^#{File::SEPARATOR}.+\.rb/ dir = $0 else dir = File.join(Dir.getwd, $0) @@ -20,15 +20,19 @@ module PuppetTest @@basedir = dir end end - @@basedir + if list.empty? + @@basedir + else + File.join(@@basedir, *list) + end end def cleanup(&block) @@cleaners << block end - def datadir - File.join(basedir, "test", "data") + def datadir(*list) + File.join(basedir, "test", "data", *list) end def exampledir(*args) @@ -45,6 +49,18 @@ module PuppetTest module_function :basedir, :datadir, :exampledir + # Rails clobbers RUBYLIB, thanks + def libsetup + curlibs = ENV["RUBYLIB"].split(":") + $:.reject do |dir| dir =~ /^\/usr/ end.each do |dir| + unless curlibs.include?(dir) + curlibs << dir + end + end + + ENV["RUBYLIB"] = curlibs.join(":") + end + def rake? $0 =~ /rake_test_loader/ end @@ -90,6 +106,7 @@ module PuppetTest Puppet::Log.level = :debug #$VERBOSE = 1 Puppet.info @method_name + Puppet[:trace] = true end #if $0 =~ /.+\.rb/ or Puppet[:debug] # Puppet::Log.newdestination :console @@ -169,7 +186,9 @@ module PuppetTest @@tmppids.clear Puppet::Type.allclear Puppet::Storage.clear - Puppet::Rails.clear + if defined? Puppet::Rails + Puppet::Rails.clear + end Puppet.clear @memoryatend = Puppet::Util.memory diff --git a/test/lib/puppettest/parsertesting.rb b/test/lib/puppettest/parsertesting.rb index e0606c501..1a6b5b12a 100644 --- a/test/lib/puppettest/parsertesting.rb +++ b/test/lib/puppettest/parsertesting.rb @@ -1,4 +1,5 @@ require 'puppettest' +require 'puppet/rails' module PuppetTest::ParserTesting include PuppetTest @@ -6,23 +7,36 @@ module PuppetTest::ParserTesting def astarray(*args) AST::ASTArray.new( - :children => args - ) + :children => args + ) end - def classobj(name, args = {}) - args[:type] ||= nameobj(name) - args[:code] ||= AST::ASTArray.new( - :file => __FILE__, - :line => __LINE__, - :children => [ - varobj("%svar" % name, "%svalue" % name), - fileobj("/%s" % name) - ] - ) - assert_nothing_raised("Could not create class %s" % name) { - return AST::ClassDef.new(args) - } + def mkinterp(args = {}) + args[:Code] ||= "" + args[:Local] ||= true + Puppet::Parser::Interpreter.new(args) + end + + def mkparser + Puppet::Parser::Parser.new(mkinterp) + end + + def mkscope(hash = {}) + hash[:interp] ||= mkinterp + hash[:source] ||= (hash[:interp].findclass("", "") || + hash[:interp].newclass("")) + + unless hash[:source] + raise "Could not find source for scope" + end + Puppet::Parser::Scope.new(hash) + end + + def classobj(name, hash = {}) + hash[:file] ||= __FILE__ + hash[:line] ||= __LINE__ + hash[:type] ||= name + AST::HostClass.new(hash) end def tagobj(*names) @@ -40,50 +54,47 @@ module PuppetTest::ParserTesting } end - def compobj(name, args = {}) - args[:file] ||= tempfile() - args[:line] ||= rand(100) - args[:type] ||= nameobj(name) - args[:args] ||= AST::ASTArray.new( - :file => tempfile(), - :line => rand(100), - :children => [] - ) - args[:code] ||= AST::ASTArray.new( - :file => tempfile(), - :line => rand(100), - :children => [ - varobj("%svar" % name, "%svalue" % name), - fileobj("/%s" % name) - ] - ) - assert_nothing_raised("Could not create compdef %s" % name) { - return AST::CompDef.new(args) + def resourcedef(type, title, params) + unless title.is_a?(AST) + title = stringobj(title) + end + assert_nothing_raised("Could not create %s %s" % [type, title]) { + return AST::ResourceDef.new( + :file => __FILE__, + :line => __LINE__, + :title => title, + :type => type, + :params => resourceinst(params) + ) } end - def objectdef(type, name, params) + def resourceoverride(type, title, params) assert_nothing_raised("Could not create %s %s" % [type, name]) { - return AST::ObjectDef.new( - :file => __FILE__, - :line => __LINE__, - :name => stringobj(name), - :type => nameobj(type), - :params => objectinst(params) - ) + return AST::ResourceOverride.new( + :file => __FILE__, + :line => __LINE__, + :object => resourceref(type, title), + :type => type, + :params => resourceinst(params) + ) + } + end + + def resourceref(type, title) + assert_nothing_raised("Could not create %s %s" % [type, title]) { + return AST::ResourceRef.new( + :file => __FILE__, + :line => __LINE__, + :type => type, + :title => stringobj(title) + ) } end def fileobj(path, hash = {"owner" => "root"}) assert_nothing_raised("Could not create file %s" % path) { - return objectdef("file", path, hash) - # return AST::ObjectDef.new( - # :file => tempfile(), - # :line => rand(100), - # :name => stringobj(path), - # :type => nameobj("file"), - # :params => objectinst(hash) - # ) + return resourcedef("file", path, hash) } end @@ -123,12 +134,12 @@ module PuppetTest::ParserTesting } end - def objectinst(hash) - assert_nothing_raised("Could not create object instance") { + def resourceinst(hash) + assert_nothing_raised("Could not create resource instance") { params = hash.collect { |param, value| - objectparam(param, value) + resourceparam(param, value) } - return AST::ObjectInst.new( + return AST::ResourceInst.new( :file => tempfile(), :line => rand(100), :children => params @@ -136,16 +147,16 @@ module PuppetTest::ParserTesting } end - def objectparam(param, value) + def resourceparam(param, value) # Allow them to pass non-strings in if value.is_a?(String) value = stringobj(value) end assert_nothing_raised("Could not create param %s" % param) { - return AST::ObjectParam.new( + return AST::ResourceParam.new( :file => tempfile(), :line => rand(100), - :param => nameobj(param), + :param => param, :value => value ) } @@ -194,10 +205,10 @@ module PuppetTest::ParserTesting def defaultobj(type, params) pary = [] params.each { |p,v| - pary << AST::ObjectParam.new( + pary << AST::ResourceParam.new( :file => __FILE__, :line => __LINE__, - :param => nameobj(p), + :param => p, :value => stringobj(v) ) } @@ -208,10 +219,10 @@ module PuppetTest::ParserTesting ) assert_nothing_raised("Could not create defaults for %s" % type) { - return AST::TypeDefaults.new( + return AST::ResourceDefaults.new( :file => __FILE__, :line => __LINE__, - :type => typeobj(type), + :type => type, :params => past ) } @@ -274,7 +285,7 @@ module PuppetTest::ParserTesting return obj end - def mk_transbucket(*objects) + def mk_transbucket(*resources) bucket = nil assert_nothing_raised { bucket = Puppet::TransBucket.new @@ -282,12 +293,12 @@ module PuppetTest::ParserTesting bucket.type = "yaytype" } - objects.each { |o| bucket << o } + resources.each { |o| bucket << o } return bucket end - # Make a tree of objects, yielding if desired + # Make a tree of resources, yielding if desired def mk_transtree(depth = 4, width = 2) top = nil assert_nothing_raised { @@ -300,7 +311,7 @@ module PuppetTest::ParserTesting file = tempfile() depth.times do |i| - objects = [] + resources = [] width.times do |j| path = tempfile + i.to_s obj = Puppet::TransObject.new("file", path) @@ -312,10 +323,10 @@ module PuppetTest::ParserTesting yield(obj, i, j) end - objects << obj + resources << obj end - newbucket = mk_transbucket(*objects) + newbucket = mk_transbucket(*resources) bucket.push newbucket bucket = newbucket @@ -324,13 +335,13 @@ module PuppetTest::ParserTesting return top end - # Take a list of AST objects, evaluate them, and return the results + # Take a list of AST resources, evaluate them, and return the results def assert_evaluate(children) top = nil assert_nothing_raised("Could not create top object") { top = AST::ASTArray.new( - :children => children - ) + :children => children + ) } trans = nil diff --git a/test/lib/puppettest/railstesting.rb b/test/lib/puppettest/railstesting.rb new file mode 100644 index 000000000..3a32d0c9e --- /dev/null +++ b/test/lib/puppettest/railstesting.rb @@ -0,0 +1,34 @@ +module PuppetTest::RailsTesting + Parser = Puppet::Parser + AST = Puppet::Parser::AST + include PuppetTest::ParserTesting + + def railsinit + Puppet::Rails.init + end + + def railsresource(type = "file", title = "/tmp/testing", params = {}) + railsinit + + # We need a host for resources + host = Puppet::Rails::Host.new(:name => Facter.value("hostname")) + + # Now build a resource + resource = host.rails_resources.build( + :title => title, :restype => type, + :exported => true + ) + + # Now add some params + params.each do |param, value| + resource.rails_parameters.build( + :name => param, :value => value + ) + end + + # Now save the whole thing + host.save + end +end + +# $Id$ diff --git a/test/lib/puppettest/resourcetesting.rb b/test/lib/puppettest/resourcetesting.rb new file mode 100644 index 000000000..a7d183ca7 --- /dev/null +++ b/test/lib/puppettest/resourcetesting.rb @@ -0,0 +1,64 @@ +module PuppetTest::ResourceTesting + Parser = Puppet::Parser + AST = Puppet::Parser::AST + def mkclassframing(interp = nil) + interp ||= mkinterp + + interp.newdefine("resource", :arguments => [%w{one}, %w{two value}, %w{three}]) + interp.newclass("") + source = interp.newclass("base") + interp.newclass("sub1", :parent => "base") + interp.newclass("sub2", :parent => "base") + interp.newclass("other") + + scope = Parser::Scope.new(:interp => interp) + scope.source = source + + return interp, scope, source + end + + def mkresource(args = {}) + + if args[:scope] and ! args[:source] + args[:source] = args[:scope].source + end + + unless args[:scope] + unless defined? @scope + raise "Must set @scope to mkresource" + end + end + + {:type => "resource", :title => "testing", + :source => @source, :scope => @scope}.each do |param, value| + args[param] ||= value + end + + unless args[:source].is_a?(Puppet::Parser::AST::HostClass) + args[:source] = args[:scope].findclass(args[:source]) + end + + params = args[:params] || {:one => "yay", :three => "rah"} + if args[:params] == :none + args.delete(:params) + else + args[:params] = paramify args[:source], params + end + + Parser::Resource.new(args) + end + + def param(name, value, source) + Parser::Resource::Param.new(:name => name, :value => value, :source => source) + end + + def paramify(source, hash) + hash.collect do |name, value| + Parser::Resource::Param.new( + :name => name, :value => value, :source => source + ) + end + end +end + +# $Id$ diff --git a/test/lib/puppettest/support/utils.rb b/test/lib/puppettest/support/utils.rb index 00ea1a1c9..ea2d5483c 100644 --- a/test/lib/puppettest/support/utils.rb +++ b/test/lib/puppettest/support/utils.rb @@ -102,7 +102,7 @@ module PuppetTest # a list of files that we can parse for testing def textfiles - textdir = File.join(exampledir,"code", "snippets") + textdir = datadir "snippets" Dir.entries(textdir).reject { |f| f =~ /^\./ or f =~ /fail/ }.each { |f| @@ -111,7 +111,7 @@ module PuppetTest end def failers - textdir = File.join(exampledir,"code", "failers") + textdir = datadir "failers" # only parse this one file now files = Dir.entries(textdir).reject { |file| file =~ %r{\.swp} |
