diff options
Diffstat (limited to 'test/language')
-rwxr-xr-x | test/language/tc_ast.rb | 318 | ||||
-rwxr-xr-x | test/language/tc_scope.rb | 46 | ||||
-rwxr-xr-x | test/language/tc_snippets.rb | 2 |
3 files changed, 358 insertions, 8 deletions
diff --git a/test/language/tc_ast.rb b/test/language/tc_ast.rb new file mode 100755 index 000000000..f93feb4f1 --- /dev/null +++ b/test/language/tc_ast.rb @@ -0,0 +1,318 @@ +#!/usr/bin/ruby + +if __FILE__ == $0 + $:.unshift '../../lib' + $:.unshift '..' + $puppetbase = "../.." +end + +require 'puppet' +require 'puppet/parser/interpreter' +require 'puppet/parser/parser' +require 'puppet/client' +require 'test/unit' +require 'puppettest' + +class TestAST < TestPuppet + AST = Puppet::Parser::AST + + def astarray + AST::ASTArray.new( + :children => [] + ) + end + + def fileobj(path, hash = {"owner" => "root"}) + assert_nothing_raised("Could not create file %s" % path) { + return AST::ObjectDef.new( + :name => stringobj(path), + :type => nameobj("file"), + :params => objectinst(hash) + ) + } + end + + def nameobj(name) + assert_nothing_raised("Could not create name %s" % name) { + return AST::Name.new( + :value => name + ) + } + end + + def objectinst(hash) + assert_nothing_raised("Could not create object instance") { + params = hash.collect { |param, value| + objectparam(param, value) + } + return AST::ObjectInst.new( + :children => params + ) + } + end + + def objectparam(param, value) + assert_nothing_raised("Could not create param %s" % param) { + return AST::ObjectParam.new( + :param => nameobj(param), + :value => stringobj(value) + ) + } + end + + def stringobj(value) + AST::String.new(:value => value) + end + + def varobj(name, value) + assert_nothing_raised("Could not create %s code" % name) { + return AST::VarDef.new( + :name => nameobj(name), + :value => stringobj(value) + ) + } + end + + # Test that classes behave like singletons + def test_classsingleton + parent = child1 = child2 = nil + children = [] + + # create the parent class + assert_nothing_raised("Could not create parent object") { + children << AST::ClassDef.new( + :name => nameobj("parent"), + :code => AST::ASTArray.new( + :children => [ + varobj("parentvar", "parentval"), + fileobj("/etc") + ] + ) + ) + } + + # Create child class one + assert_nothing_raised("Could not create child1 object") { + children << AST::ClassDef.new( + :name => nameobj("child1"), + :parentclass => nameobj("parent"), + :code => AST::ASTArray.new( + :children => [ + varobj("child1var", "child1val"), + fileobj("/tmp") + ] + ) + ) + } + + # Create child class two + assert_nothing_raised("Could not create child2 object") { + children << AST::ClassDef.new( + :name => nameobj("child2"), + :parentclass => nameobj("parent"), + :code => AST::ASTArray.new( + :children => [ + varobj("child2var", "child2val"), + fileobj("/var") + ] + ) + ) + } + + # Now call the two classes + assert_nothing_raised("Could not add AST nodes for calling") { + children << AST::ObjectDef.new( + :type => nameobj("child1"), + :name => nameobj("yayness"), + :params => astarray() + ) + children << AST::ObjectDef.new( + :type => nameobj("child2"), + :name => nameobj("booness"), + :params => astarray() + ) + } + + top = nil + assert_nothing_raised("Could not create top object") { + top = AST::ASTArray.new( + :children => children + ) + } + + scope = nil + assert_nothing_raised("Could not evaluate") { + scope = Puppet::Parser::Scope.new() + objects = top.evaluate(scope) + } + + assert_equal(1, scope.find_all { |child| + child.lookupobject("/etc", "file") + #child.lookupobject("file", "/etc") + }.length, "Found incorrect number of '/etc' objects") + end + + # Test that 'setobject' collects all of an object's parameters and stores + # them in one TransObject, rather than many. This is probably a bad idea. + def test_setobject + top = nil + assert_nothing_raised("Could not create top object") { + top = AST::ASTArray.new( + :children => [ + fileobj("/etc", "owner" => "root"), + fileobj("/etc", "group" => "root") + ] + ) + } + scope = nil + assert_nothing_raised("Could not evaluate") { + scope = Puppet::Parser::Scope.new() + objects = top.evaluate(scope) + } + + obj = nil + assert_nothing_raised("Could not retrieve file object") { + obj = scope.lookupobject("file", "/etc") + } + + assert(obj, "could not retrieve file object") + + %w{owner group}.each { |param| + assert(obj.include?(param), "Object did not include %s" % param) + } + + end + + # Verify that objects can only have parents of the same type. + def test_validparent + parent = child1 = nil + children = [] + + # create the parent class + assert_nothing_raised("Could not create parent object") { + children << AST::CompDef.new( + :name => nameobj("parent"), + :args => AST::ASTArray.new(:children => []), + :code => AST::ASTArray.new( + :children => [ + varobj("parentvar", "parentval"), + fileobj("/etc") + ] + ) + ) + } + + # Create child class one + assert_nothing_raised("Could not create child1 object") { + children << AST::ClassDef.new( + :name => nameobj("child1"), + :parentclass => nameobj("parent"), + :code => AST::ASTArray.new( + :children => [ + varobj("child1var", "child1val"), + fileobj("/tmp") + ] + ) + ) + } + + # Now call the two classes + assert_nothing_raised("Could not add AST nodes for calling") { + children << AST::ObjectDef.new( + :type => nameobj("child1"), + :name => nameobj("yayness"), + :params => astarray() + ) + } + + top = nil + assert_nothing_raised("Could not create top object") { + top = AST::ASTArray.new( + :children => children + ) + } + + scope = nil + assert_raise(Puppet::ParseError, "Invalid parent type was allowed") { + scope = Puppet::Parser::Scope.new() + objects = top.evaluate(scope) + } + end + + # Verify that classes are correctly defined in node scopes. + def test_nodeclasslookup + parent = child1 = nil + children = [] + + # create the parent class + assert_nothing_raised("Could not create parent object") { + children << AST::ClassDef.new( + :name => nameobj("parent"), + :code => AST::ASTArray.new( + :children => [ + varobj("parentvar", "parentval"), + fileobj("/etc") + ] + ) + ) + } + + # Create child class one + assert_nothing_raised("Could not create child1 object") { + children << AST::ClassDef.new( + :name => nameobj("child1"), + :parentclass => nameobj("parent"), + :code => AST::ASTArray.new( + :children => [ + varobj("child1var", "child1val"), + fileobj("/tmp") + ] + ) + ) + } + + # Now call the two classes + assert_nothing_raised("Could not add AST nodes for calling") { + children << AST::ObjectDef.new( + :type => nameobj("child1"), + :name => nameobj("yayness"), + :params => astarray() + ) + } + + # create the node + node = nil + assert_nothing_raised("Could not create parent object") { + node = AST::NodeDef.new( + :names => nameobj("node"), + :code => AST::ASTArray.new( + :children => children + ) + ) + } + + top = nil + assert_nothing_raised("Could not create top object") { + top = AST::ASTArray.new( + :children => [node] + ) + } + + scope = nil + assert_nothing_raised("Could not evaluate node") { + scope = Puppet::Parser::Scope.new() + objects = top.evaluate(scope) + } + + assert(scope.topscope?, "Scope is not top scope") + assert(! scope.nodescope?, "Scope is mistakenly node scope") + assert(! scope.lookupclass("parent"), "Found parent class in top scope") + + nodescope = scope.find { |child| child.nodescope? } + + assert(nodescope, "Could not find nodescope") + + assert(nodescope.lookupclass("parent"), + "Could not find parent class in node scope") + end +end diff --git a/test/language/tc_scope.rb b/test/language/tc_scope.rb index ece621629..161dee9c6 100755 --- a/test/language/tc_scope.rb +++ b/test/language/tc_scope.rb @@ -2,8 +2,7 @@ if __FILE__ == $0 $:.unshift '../../lib' - $:.unshift '../../../../library/trunk/lib/' - $:.unshift '../../../../library/trunk/test/' + $:.unshift '..' $puppetbase = "../.." end @@ -23,11 +22,8 @@ require 'puppettest' # so really, we want to do things like test that our ast is correct # and test whether we've got things in the right scopes -class TestScope < Test::Unit::TestCase - def setup - Puppet[:loglevel] = :debug if __FILE__ == $0 - end - +class TestScope < TestPuppet + AST = Puppet::Parser::AST def to_ary(hash) hash.collect { |key,value| [key,value] @@ -261,4 +257,40 @@ class TestScope < Test::Unit::TestCase } assert_equal("string value value value", val) end + + # Test some of the host manipulations + def test_zhostlookup + top = Puppet::Parser::Scope.new(nil) + + child1 = Puppet::Parser::Scope.new(top) + child2 = Puppet::Parser::Scope.new(top) + sub1 = Puppet::Parser::Scope.new(child1) + + # verify we can set a host + assert_nothing_raised("Could not create host") { + child1.sethost("testing", AST::Node.new( + :name => "testing", + :code => :notused + ) + ) + } + + # Verify we cannot redefine it + assert_raise(Puppet::ParseError, "Duplicate host creation succeeded") { + child2.sethost("testing", AST::Node.new( + :name => "testing", + :code => :notused + ) + ) + } + + # Now verify we can find the host again + host = nil + assert_nothing_raised("Host lookup failed") { + host = top.lookuphost("testing") + } + + assert(host, "Could not find host") + assert(host.code == :notused, "Host is not what we stored") + end end diff --git a/test/language/tc_snippets.rb b/test/language/tc_snippets.rb index 5fd75e62a..9cb2755a6 100755 --- a/test/language/tc_snippets.rb +++ b/test/language/tc_snippets.rb @@ -355,7 +355,7 @@ class TestSnippets < TestPuppet assert(FileTest.exists?(file), "File %s does not exist" % file) end - def snippet_classargtest(trans) + def disabled_snippet_classargtest(trans) [1,2].each { |num| file = "/tmp/classargtest%s" % num @@tmpfiles << file |