summaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2005-09-21 17:39:43 +0000
committerluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2005-09-21 17:39:43 +0000
commit5dc3cb04bebc79517ae5f64d1d3c652b6dfba7ea (patch)
treef14e075f078b7054ea69418756b5a28ceaed65ef /test
parent0747b4c34ee0aa2ba4430021c1c0e676f71c20ac (diff)
downloadpuppet-5dc3cb04bebc79517ae5f64d1d3c652b6dfba7ea.tar.gz
puppet-5dc3cb04bebc79517ae5f64d1d3c652b6dfba7ea.tar.xz
puppet-5dc3cb04bebc79517ae5f64d1d3c652b6dfba7ea.zip
Okay, significant change -- classes no longer accept arguments (which makes things simpler but encourages the user of global variables, which is bad), and classes are finally singletons, meaning they will only ever be evaluated for each node a single time. I still need to make nodes work correctly, but that is going to involve modifying the parsing system and a bit more
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@694 980ebf18-57e1-0310-9a29-db15c13687c0
Diffstat (limited to 'test')
-rwxr-xr-xtest/language/tc_ast.rb318
-rwxr-xr-xtest/language/tc_scope.rb46
-rwxr-xr-xtest/language/tc_snippets.rb2
-rw-r--r--test/parser/tc_lexer.rb8
-rw-r--r--test/parser/tc_parser.rb20
5 files changed, 366 insertions, 28 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
diff --git a/test/parser/tc_lexer.rb b/test/parser/tc_lexer.rb
index 4a8ccedd4..4473ce492 100644
--- a/test/parser/tc_lexer.rb
+++ b/test/parser/tc_lexer.rb
@@ -9,8 +9,6 @@ require 'puppet/parser/lexer'
require 'test/unit'
require 'puppettest.rb'
-# $Id$
-
#%q{service("telnet") = \{
# port => "23",
# protocol => "tcp",
@@ -18,9 +16,9 @@ require 'puppettest.rb'
#\}
#} => [[:NAME, "service"], [:LPAREN, "("], [:DQUOTE, "\""], [:NAME, "telnet"], [:DQUOTE, "\""], [:RPAREN, ")"], [:EQUALS, "="], [:lbrace, "{"], [:NAME, "port"], [:FARROW, "=>"], [:DQUOTE, "\""], [:NAME, "23"], [:DQUOTE, "\""], [:COMMA, ","], [:NAME, "protocol"], [:FARROW, "=>"], [:DQUOTE, "\""], [:NAME, "tcp"], [:DQUOTE, "\""], [:COMMA, ","], [:NAME, "name"], [:FARROW, "=>"], [:DQUOTE, "\""], [:NAME, "telnet"], [:DQUOTE, "\""], [:COMMA, ","], [:RBRACE, "}"]]
-class TestLexer < Test::Unit::TestCase
+class TestLexer < TestPuppet
def setup
- Puppet[:loglevel] = :debug if __FILE__ == $0
+ super
@lexer = Puppet::Parser::Lexer.new()
end
@@ -117,3 +115,5 @@ class TestLexer < Test::Unit::TestCase
}
end
end
+
+# $Id$
diff --git a/test/parser/tc_parser.rb b/test/parser/tc_parser.rb
index 75dd6db82..de2d75ddc 100644
--- a/test/parser/tc_parser.rb
+++ b/test/parser/tc_parser.rb
@@ -9,26 +9,12 @@ require 'puppet/parser/parser'
require 'test/unit'
require 'puppettest'
-# $Id$
-
-class TestParser < Test::Unit::TestCase
- # hmmm
- # this is complicated, because we store references to the created
- # objects in a central store
+class TestParser < TestPuppet
def setup
- Puppet[:loglevel] = :debug if __FILE__ == $0
+ super
Puppet[:parseonly] = true
#@lexer = Puppet::Parser::Lexer.new()
@parser = Puppet::Parser::Parser.new()
- @@tmpfiles = []
- end
-
- def teardown
- @@tmpfiles.each { |file|
- if FileTest.exist?(file)
- system("rm -rf %s" % file)
- end
- }
end
def test_each_file
@@ -73,3 +59,5 @@ class TestParser < Test::Unit::TestCase
}
end
end
+
+# $Id$