diff options
Diffstat (limited to 'test/language/scope.rb')
-rwxr-xr-x | test/language/scope.rb | 250 |
1 files changed, 106 insertions, 144 deletions
diff --git a/test/language/scope.rb b/test/language/scope.rb index f0feee156..bd90caa53 100755 --- a/test/language/scope.rb +++ b/test/language/scope.rb @@ -98,8 +98,8 @@ class TestScope < Test::Unit::TestCase end def test_lookupvar - interp = mkinterp - scope = mkscope :interp => interp + parser = mkparser + scope = mkscope :parser => parser # first do the plain lookups assert_equal("", scope.lookupvar("var"), "scope did not default to string") @@ -111,7 +111,7 @@ class TestScope < Test::Unit::TestCase assert_equal("yep", scope.lookupvar("var"), "did not retrieve value correctly") # Now test the parent lookups - subscope = mkscope :interp => interp + subscope = mkscope :parser => parser subscope.parent = scope assert_equal("", subscope.lookupvar("nope"), "scope did not default to string with parent") assert_equal("", subscope.lookupvar("nope", true), "scope ignored usestring setting with parent") @@ -129,12 +129,12 @@ class TestScope < Test::Unit::TestCase end def test_lookup_qualified_var - interp = mkinterp - scope = mkscope :interp => interp + parser = mkparser + scope = mkscope :parser => parser scopes = {} classes = ["", "one", "one::two", "one::two::three"].each do |name| - klass = interp.newclass(name) + klass = parser.newclass(name) klass.evaluate(:scope => scope) scopes[name] = scope.class_scope(klass) end @@ -149,7 +149,7 @@ class TestScope < Test::Unit::TestCase def test_declarative # set to declarative - top = mkscope(:declarative => true) + top = mkscope sub = mkscope(:parent => top) assert_nothing_raised { @@ -166,93 +166,89 @@ class TestScope < Test::Unit::TestCase } end - def test_notdeclarative - # set to not declarative - top = mkscope(:declarative => false) - sub = mkscope(:parent => top) + def test_setdefaults + config = mkconfig - assert_nothing_raised { - top.setvar("test","value") - } - assert_nothing_raised { - top.setvar("test","other") - } - assert_nothing_raised { - sub.setvar("test","later") - } - assert_nothing_raised { - sub.setvar("test","yayness") - } - end + scope = config.topscope - def test_setdefaults - interp, scope, source = mkclassframing + defaults = scope.instance_variable_get("@defaults") - # The setdefaults method doesn't really check what we're doing, - # so we're just going to use fake defaults here. + # First the case where there are no defaults and we pass a single param + param = stub :name => "myparam" + scope.setdefaults(:mytype, param) + assert_equal({"myparam" => param}, defaults[:mytype], "Did not set default correctly") - # First do a simple local lookup - params = paramify(source, :one => "fun", :two => "shoe") - origshould = {} - params.each do |p| origshould[p.name] = p end - assert_nothing_raised do - scope.setdefaults(:file, params) - end + # Now the case where we pass in multiple parameters + param1 = stub :name => "one" + param2 = stub :name => "two" + scope.setdefaults(:newtype, [param1, param2]) + assert_equal({"one" => param1, "two" => param2}, defaults[:newtype], "Did not set multiple defaults correctly") - ret = nil - assert_nothing_raised do - ret = scope.lookupdefaults(:file) + # And the case where there's actually a conflict. Use the first default for this. + newparam = stub :name => "myparam" + assert_raise(Puppet::ParseError, "Allowed resetting of defaults") do + scope.setdefaults(:mytype, param) end + assert_equal({"myparam" => param}, defaults[:mytype], "Replaced default even though there was a failure") + end - assert_equal(origshould, ret) + def test_lookupdefaults + config = mkconfig + top = config.topscope - # Now create a subscope and add some more params. - newscope = scope.newscope + # Make a subscope + sub = config.newscope(top) - newparams = paramify(source, :one => "shun", :three => "free") - assert_nothing_raised { - newscope.setdefaults(:file, newparams) - } + topdefs = top.instance_variable_get("@defaults") + subdefs = sub.instance_variable_get("@defaults") - # And make sure we get the appropriate ones back - should = {} - params.each do |p| should[p.name] = p end - newparams.each do |p| should[p.name] = p end + # First add some defaults to our top scope + topdefs[:t1] = {:p1 => :p2, :p3 => :p4} + topdefs[:t2] = {:p5 => :p6} - assert_nothing_raised do - ret = newscope.lookupdefaults(:file) - end - - assert_equal(should, ret) + # Then the sub scope + subdefs[:t1] = {:p1 => :p7, :p8 => :p9} + subdefs[:t2] = {:p5 => :p10, :p11 => :p12} - # Make sure we still only get the originals from the top scope - assert_nothing_raised do - ret = scope.lookupdefaults(:file) + # Now make sure we get the correct list back + result = nil + assert_nothing_raised("Could not get defaults") do + result = sub.lookupdefaults(:t1) end + assert_equal(:p9, result[:p8], "Did not get child defaults") + assert_equal(:p4, result[:p3], "Did not override parent defaults with child default") + assert_equal(:p7, result[:p1], "Did not get parent defaults") + end - assert_equal(origshould, ret) + def test_parent + config = mkconfig + top = config.topscope - # Now create another scope and make sure we only get the top defaults - otherscope = scope.newscope - assert_equal(origshould, otherscope.lookupdefaults(:file)) + # Make a subscope + sub = config.newscope(top) - # And make sure none of the scopes has defaults for other types - [scope, newscope, otherscope].each do |sc| - assert_equal({}, sc.lookupdefaults(:exec)) - end + assert_equal(top, sub.parent, "Did not find parent scope correctly") + assert_equal(top, sub.parent, "Did not find parent scope on second call") + end + + def test_class_scope + config = mkconfig + scope = config.topscope + config.expects(:class_scope).with(:testing).returns(:myscope) + assert_equal(:myscope, scope.class_scope(:testing), "Did not pass back the results of config.class_scope") end - def test_strinterp + def test_strparser # Make and evaluate our classes so the qualified lookups work - interp = mkinterp - klass = interp.newclass("") - scope = mkscope(:interp => interp) + parser = mkparser + klass = parser.newclass("") + scope = mkscope(:parser => parser) klass.evaluate(:scope => scope) - klass = interp.newclass("one") + klass = parser.newclass("one") klass.evaluate(:scope => scope) - klass = interp.newclass("one::two") + klass = parser.newclass("one::two") klass.evaluate(:scope => scope) @@ -264,7 +260,7 @@ class TestScope < Test::Unit::TestCase scopes = {"" => scope} %w{one one::two one::two::three}.each do |name| - klass = interp.newclass(name) + klass = parser.newclass(name) klass.evaluate(:scope => scope) scopes[name] = scope.class_scope(klass) scopes[name].setvar("test", "value-%s" % name.sub(/.+::/,'')) @@ -297,8 +293,8 @@ class TestScope < Test::Unit::TestCase tests.each do |input, output| assert_nothing_raised("Failed to scan %s" % input.inspect) do - assert_equal(output, scope.strinterp(input), - 'did not interpret %s correctly' % input.inspect) + assert_equal(output, scope.strparser(input), + 'did not parserret %s correctly' % input.inspect) end end @@ -310,8 +306,8 @@ class TestScope < Test::Unit::TestCase %w{d f h l w z}.each do |l| string = "\\" + l assert_nothing_raised do - assert_equal(string, scope.strinterp(string), - 'did not interpret %s correctly' % string) + assert_equal(string, scope.strparser(string), + 'did not parserret %s correctly' % string) end assert(logs.detect { |m| m.message =~ /Unrecognised escape/ }, @@ -321,7 +317,7 @@ class TestScope < Test::Unit::TestCase end def test_setclass - interp, scope, source = mkclassframing + parser, scope, source = mkclassframing base = scope.findclass("base") assert(base, "Could not find base class") @@ -392,11 +388,11 @@ class TestScope < Test::Unit::TestCase end def test_includefunction - interp = mkinterp - scope = mkscope :interp => interp + parser = mkparser + scope = mkscope :parser => parser - myclass = interp.newclass "myclass" - otherclass = interp.newclass "otherclass" + myclass = parser.newclass "myclass" + otherclass = parser.newclass "otherclass" function = Puppet::Parser::AST::Function.new( :name => "include", @@ -417,12 +413,12 @@ class TestScope < Test::Unit::TestCase end def test_definedfunction - interp = mkinterp + parser = mkparser %w{one two}.each do |name| - interp.newdefine name + parser.newdefine name end - scope = mkscope :interp => interp + scope = mkscope :parser => parser assert_nothing_raised { %w{one two file user}.each do |type| @@ -503,7 +499,7 @@ class TestScope < Test::Unit::TestCase # Verify that we recursively mark as exported the results of collectable # components. def test_exportedcomponents - interp, scope, source = mkclassframing + parser, scope, source = mkclassframing children = [] args = AST::ASTArray.new( @@ -513,7 +509,7 @@ class TestScope < Test::Unit::TestCase ) # Create a top-level component - interp.newdefine "one", :arguments => [%w{arg}], + parser.newdefine "one", :arguments => [%w{arg}], :code => AST::ASTArray.new( :children => [ resourcedef("file", "/tmp", {"owner" => varref("arg")}) @@ -521,7 +517,7 @@ class TestScope < Test::Unit::TestCase ) # And a component that calls it - interp.newdefine "two", :arguments => [%w{arg}], + parser.newdefine "two", :arguments => [%w{arg}], :code => AST::ASTArray.new( :children => [ resourcedef("one", "ptest", {"arg" => varref("arg")}) @@ -529,7 +525,7 @@ class TestScope < Test::Unit::TestCase ) # And then a third component that calls the second - interp.newdefine "three", :arguments => [%w{arg}], + parser.newdefine "three", :arguments => [%w{arg}], :code => AST::ASTArray.new( :children => [ resourcedef("two", "yay", {"arg" => varref("arg")}) @@ -545,7 +541,7 @@ class TestScope < Test::Unit::TestCase obj.evaluate :scope => scope # And then evaluate it - interp.evaliterate(scope) + parser.evaliterate(scope) %w{file}.each do |type| objects = scope.lookupexported(type) @@ -585,9 +581,11 @@ Host <<||>>" objects = nil # We run it twice because we want to make sure there's no conflict # if we pull it up from the database. + node = mknode + node.parameters = {"hostname" => node.name} 2.times { |i| assert_nothing_raised { - objects = interp.run("localhost", {"hostname" => "localhost"}) + objects = interp.compile(node) } flat = objects.flatten @@ -603,7 +601,7 @@ Host <<||>>" # Make sure tags behave appropriately. def test_tags - interp, scope, source = mkclassframing + parser, scope, source = mkclassframing # First make sure we can only set legal tags ["an invalid tag", "-anotherinvalid", "bad*tag"].each do |tag| @@ -637,58 +635,22 @@ Host <<||>>" assert_equal((ptags + %w{onemore subscope}).sort, newscope.tags.sort) end - # Make sure we successfully translate objects - def test_translate - interp, scope, source = mkclassframing - - # Create a define that we'll be using - interp.newdefine("wrapper", :code => AST::ASTArray.new(:children => [ - resourcedef("file", varref("name"), "owner" => "root") - ])) - - # Now create a resource that uses that define - define = mkresource(:type => "wrapper", :title => "/tmp/testing", - :scope => scope, :source => source, :params => :none) - - scope.setresource define - - # And a normal resource - scope.setresource mkresource(:type => "file", :title => "/tmp/rahness", - :scope => scope, :source => source, - :params => {:owner => "root"}) - - # Evaluate the the define thing. - define.evaluate - - # Now the scope should have a resource and a subscope. Translate the - # whole thing. - ret = nil - assert_nothing_raised do - ret = scope.translate - end - - assert_instance_of(Puppet::TransBucket, ret) - - ret.each do |obj| - assert(obj.is_a?(Puppet::TransBucket) || obj.is_a?(Puppet::TransObject), - "Got a non-transportable object %s" % obj.class) - end - - rahness = ret.find { |c| c.type == "file" and c.name == "/tmp/rahness" } - assert(rahness, "Could not find top-level file") - assert_equal("root", rahness["owner"]) - - bucket = ret.find { |c| c.class == Puppet::TransBucket and c.name == "/tmp/testing" } - assert(bucket, "Could not find define bucket") + # FIXME This isn't a great test, but I need to move on. + def test_to_trans + bucket = mock("transbucket") + Puppet::TransBucket.expects(:new).with([]).returns(bucket) + scope = mkscope + scope.type = "mytype" + scope.name = "myname" - testing = bucket.find { |c| c.type == "file" and c.name == "/tmp/testing" } - assert(testing, "Could not find define file") - assert_equal("root", testing["owner"]) + bucket.expects(:name=).with("myname") + bucket.expects(:type=).with("mytype") + scope.to_trans end def test_namespaces - interp, scope, source = mkclassframing + parser, scope, source = mkclassframing assert_equal([""], scope.namespaces, "Started out with incorrect namespaces") @@ -701,17 +663,17 @@ Host <<||>>" end def test_findclass_and_finddefine - interp = mkinterp + parser = mkparser - # Make sure our scope calls the interp findclass method with + # Make sure our scope calls the parser findclass method with # the right namespaces - scope = mkscope :interp => interp + scope = mkscope :parser => parser - interp.metaclass.send(:attr_accessor, :last) + parser.metaclass.send(:attr_accessor, :last) methods = [:findclass, :finddefine] methods.each do |m| - interp.meta_def(m) do |namespace, name| + parser.meta_def(m) do |namespace, name| @checked ||= [] @checked << [namespace, name] @@ -727,7 +689,7 @@ Host <<||>>" end test = proc do |should| - interp.last = scope.namespaces[-1] + parser.last = scope.namespaces[-1] methods.each do |method| result = scope.send(method, "testing") assert_equal(should, result, |