summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/puppet/parser/ast/component.rb2
-rw-r--r--lib/puppet/parser/configuration.rb12
-rw-r--r--lib/puppet/parser/interpreter.rb32
-rw-r--r--lib/puppet/parser/parser_support.rb11
-rw-r--r--lib/puppet/parser/resource.rb3
-rw-r--r--lib/puppet/parser/scope.rb6
-rw-r--r--lib/puppet/parser/templatewrapper.rb4
-rwxr-xr-xtest/language/ast.rb35
-rwxr-xr-xtest/language/ast/component.rb36
-rwxr-xr-xtest/language/ast/hostclass.rb33
-rwxr-xr-xtest/language/ast/resourceref.rb18
-rwxr-xr-xtest/language/collector.rb19
-rwxr-xr-xtest/language/configuration.rb25
-rwxr-xr-xtest/language/functions.rb77
-rwxr-xr-xtest/language/interpreter.rb263
-rwxr-xr-xtest/language/parser.rb14
-rwxr-xr-xtest/language/resource.rb123
-rwxr-xr-xtest/language/scope.rb246
-rwxr-xr-xtest/lib/puppettest.rb1
-rw-r--r--test/lib/puppettest/parsertesting.rb12
20 files changed, 305 insertions, 667 deletions
diff --git a/lib/puppet/parser/ast/component.rb b/lib/puppet/parser/ast/component.rb
index 65f310212..17cfa9d61 100644
--- a/lib/puppet/parser/ast/component.rb
+++ b/lib/puppet/parser/ast/component.rb
@@ -27,7 +27,7 @@ class Puppet::Parser::AST
false
end
- def evaluate(hash)
+ def evaluate_resource(hash)
origscope = hash[:scope]
title = hash[:title]
args = symbolize_options(hash[:arguments] || {})
diff --git a/lib/puppet/parser/configuration.rb b/lib/puppet/parser/configuration.rb
index 90812899a..ddfc3606f 100644
--- a/lib/puppet/parser/configuration.rb
+++ b/lib/puppet/parser/configuration.rb
@@ -12,7 +12,7 @@ require 'puppet/util/errors'
class Puppet::Parser::Configuration
include Puppet::Util
include Puppet::Util::Errors
- attr_reader :topscope, :parser, :node, :facts
+ attr_reader :topscope, :parser, :node, :facts, :collections
attr_accessor :extraction_format
attr_writer :ast_nodes
@@ -30,6 +30,13 @@ class Puppet::Parser::Configuration
# Store the fact that we've evaluated a class, and store a reference to
# the scope in which it was evaluated, so that we can look it up later.
def class_set(name, scope)
+ if existing = @class_scopes[name]
+ if existing.nodescope? or scope.nodescope?
+ raise Puppet::ParseError, "Cannot have classes, nodes, or definitions with the same name"
+ else
+ raise Puppet::DevError, "Somehow evaluated the same class twice"
+ end
+ end
@class_scopes[name] = scope
tag(name)
end
@@ -89,16 +96,19 @@ class Puppet::Parser::Configuration
# just tag the configuration and move on.
def evaluate_classes(classes = nil)
classes ||= node.classes
+ found = []
classes.each do |name|
if klass = @parser.findclass("", name)
# This will result in class_set getting called, which
# will in turn result in tags. Yay.
klass.safeevaluate(:scope => topscope)
+ found << name
else
Puppet.info "Could not find class %s for %s" % [name, node.name]
tag(name)
end
end
+ found
end
# Make sure we support the requested extraction format.
diff --git a/lib/puppet/parser/interpreter.rb b/lib/puppet/parser/interpreter.rb
index cf3027a5a..54cd9b023 100644
--- a/lib/puppet/parser/interpreter.rb
+++ b/lib/puppet/parser/interpreter.rb
@@ -17,18 +17,6 @@ class Puppet::Parser::Interpreter
include Puppet::Util::Errors
- # Create proxy methods, so the scopes can call the interpreter, since
- # they don't have access to the parser.
- def findclass(namespace, name)
- raise "move findclass() out of the interpreter"
- @parser.findclass(namespace, name)
- end
-
- def finddefine(namespace, name)
- raise "move finddefine() out of the interpreter"
- @parser.finddefine(namespace, name)
- end
-
# create our interpreter
def initialize(hash)
if @code = hash[:Code]
@@ -65,26 +53,6 @@ class Puppet::Parser::Interpreter
parsefiles
end
- # Pass these methods through to the parser.
- [:newclass, :newdefine, :newnode].each do |name|
- define_method(name) do |*args|
- raise("move %s out of the interpreter" % name)
- @parser.send(name, *args)
- end
- end
-
- # Add a new file to be checked when we're checking to see if we should be
- # reparsed.
- def newfile(*files)
- raise "who uses newfile?"
- files.each do |file|
- unless file.is_a? Puppet::Util::LoadedFile
- file = Puppet::Util::LoadedFile.new(file)
- end
- @files << file
- end
- end
-
def parsedate
parsefiles()
@parsedate
diff --git a/lib/puppet/parser/parser_support.rb b/lib/puppet/parser/parser_support.rb
index 6d069dc07..967508e56 100644
--- a/lib/puppet/parser/parser_support.rb
+++ b/lib/puppet/parser/parser_support.rb
@@ -444,6 +444,17 @@ class Puppet::Parser::Parser
def string=(string)
@lexer.string = string
end
+
+ # Add a new file to be checked when we're checking to see if we should be
+ # reparsed.
+ def watch_file(*files)
+ files.each do |file|
+ unless file.is_a? Puppet::Util::LoadedFile
+ file = Puppet::Util::LoadedFile.new(file)
+ end
+ @files << file
+ end
+ end
end
# $Id$
diff --git a/lib/puppet/parser/resource.rb b/lib/puppet/parser/resource.rb
index eace88645..9d3e962f0 100644
--- a/lib/puppet/parser/resource.rb
+++ b/lib/puppet/parser/resource.rb
@@ -52,11 +52,10 @@ class Puppet::Parser::Resource
if klass = @ref.definedtype
finish()
scope.configuration.delete_resource(self)
- return klass.evaluate(:scope => scope,
+ return klass.evaluate_resource(:scope => scope,
:type => self.type,
:title => self.title,
:arguments => self.to_hash,
- :scope => self.scope,
:virtual => self.virtual,
:exported => self.exported
)
diff --git a/lib/puppet/parser/scope.rb b/lib/puppet/parser/scope.rb
index 808362858..527ed4dcd 100644
--- a/lib/puppet/parser/scope.rb
+++ b/lib/puppet/parser/scope.rb
@@ -21,7 +21,7 @@ class Puppet::Parser::Scope
# Proxy accessors
def host
- @configuration.host
+ @configuration.node.name
end
def interpreter
@configuration.interpreter
@@ -241,11 +241,11 @@ class Puppet::Parser::Scope
# can support multiple unrelated classes with the same name.
def setclass(klass)
if klass.is_a?(AST::HostClass)
- unless klass.classname
+ unless name = klass.classname
raise Puppet::DevError, "Got a %s with no fully qualified name" %
klass.class
end
- @configuration.class_set(klass.classname, self)
+ @configuration.class_set(name, self)
else
raise Puppet::DevError, "Invalid class %s" % klass.inspect
end
diff --git a/lib/puppet/parser/templatewrapper.rb b/lib/puppet/parser/templatewrapper.rb
index 3b8cc3a3a..9c3c6c0d0 100644
--- a/lib/puppet/parser/templatewrapper.rb
+++ b/lib/puppet/parser/templatewrapper.rb
@@ -15,8 +15,8 @@ class Puppet::Parser::TemplateWrapper
end
# We'll only ever not have an interpreter in testing, but, eh.
- if @scope.interp
- @scope.interp.newfile(@file)
+ if @scope.parser
+ @scope.parser.watch_file(@file)
end
end
diff --git a/test/language/ast.rb b/test/language/ast.rb
index 9e00c610d..38e658edb 100755
--- a/test/language/ast.rb
+++ b/test/language/ast.rb
@@ -49,24 +49,21 @@ class TestAST < Test::Unit::TestCase
# Make sure our override object behaves "correctly"
def test_override
- interp, scope, source = mkclassframing
+ scope = mkscope
ref = nil
assert_nothing_raised do
- ref = resourceoverride("resource", "yaytest", "one" => "yay", "two" => "boo")
+ ref = resourceoverride("file", "/yayness", "owner" => "blah", "group" => "boo")
end
+ Puppet::Parser::Resource.expects(:new).with { |o| o.is_a?(Hash) }.returns(:override)
+ scope.expects(:setoverride).with(:override)
ret = nil
assert_nothing_raised do
ret = ref.evaluate :scope => scope
end
- assert_instance_of(Puppet::Parser::Resource, ret)
-
- assert(ret.override?, "Resource was not an override resource")
-
- assert(scope.overridetable[ret.ref].include?(ret),
- "Was not stored in the override table")
+ assert_equal(:override, ret, "Did not return override")
end
# make sure our resourcedefaults ast object works correctly.
@@ -97,16 +94,16 @@ class TestAST < Test::Unit::TestCase
end
def test_node
- interp = mkinterp
- scope = mkscope(:interp => interp)
+ scope = mkscope
+ parser = scope.configuration.parser
# Define a base node
- basenode = interp.newnode "basenode", :code => AST::ASTArray.new(:children => [
+ basenode = parser.newnode "basenode", :code => AST::ASTArray.new(:children => [
resourcedef("file", "/tmp/base", "owner" => "root")
])
# Now define a subnode
- nodes = interp.newnode ["mynode", "othernode"],
+ nodes = parser.newnode ["mynode", "othernode"],
:code => AST::ASTArray.new(:children => [
resourcedef("file", "/tmp/mynode", "owner" => "root"),
resourcedef("file", "/tmp/basenode", "owner" => "daemon")
@@ -116,9 +113,9 @@ class TestAST < Test::Unit::TestCase
# Make sure we can find them all.
%w{mynode othernode}.each do |node|
- assert(interp.nodesearch_code(node), "Could not find %s" % node)
+ assert(parser.nodes[node], "Could not find %s" % node)
end
- mynode = interp.nodesearch_code("mynode")
+ mynode = parser.nodes["mynode"]
# Now try evaluating the node
assert_nothing_raised do
@@ -135,9 +132,9 @@ class TestAST < Test::Unit::TestCase
assert_equal("daemon", basefile[:owner])
# Now make sure we can evaluate nodes with parents
- child = interp.newnode(%w{child}, :parent => "basenode").shift
+ child = parser.newnode(%w{child}, :parent => "basenode").shift
- newscope = mkscope :interp => interp
+ newscope = mkscope :parser => parser
assert_nothing_raised do
child.evaluate :scope => newscope
end
@@ -147,8 +144,7 @@ class TestAST < Test::Unit::TestCase
end
def test_collection
- interp = mkinterp
- scope = mkscope(:interp => interp)
+ scope = mkscope
coll = nil
assert_nothing_raised do
@@ -165,7 +161,8 @@ class TestAST < Test::Unit::TestCase
assert_instance_of(Puppet::Parser::Collector, ret)
# Now make sure we get it back from the scope
- assert_equal([ret], scope.collections)
+ colls = scope.configuration.instance_variable_get("@collections")
+ assert_equal([ret], colls, "Did not store collector in config's collection list")
end
def test_virtual_collexp
diff --git a/test/language/ast/component.rb b/test/language/ast/component.rb
index 13cf60857..cf0cce976 100755
--- a/test/language/ast/component.rb
+++ b/test/language/ast/component.rb
@@ -16,8 +16,8 @@ class TestASTComponent < Test::Unit::TestCase
include PuppetTest::ResourceTesting
AST = Puppet::Parser::AST
- def test_component
- parser, scope, source = mkclassframing
+ def test_initialize
+ parser = mkparser
# Create a new definition
klass = parser.newdefine "yayness",
@@ -35,27 +35,41 @@ class TestASTComponent < Test::Unit::TestCase
[:random, "random"].each do |var|
assert(! klass.validattr?(var), "%s was considered valid" % var.inspect)
end
+
+ end
+
+ def test_evaluate
+ parser = mkparser
+ config = mkconfig
+ scope = config.topscope
+ klass = parser.newdefine "yayness",
+ :arguments => [["owner", stringobj("nobody")], %w{mode}],
+ :code => AST::ASTArray.new(
+ :children => [resourcedef("file", "/tmp/$name",
+ "owner" => varref("owner"), "mode" => varref("mode"))]
+ )
+
# Now call it a couple of times
# First try it without a required param
- assert_raise(Puppet::ParseError) do
- klass.evaluate(:scope => scope,
+ assert_raise(Puppet::ParseError, "Did not fail when a required parameter was not provided") do
+ klass.evaluate_resource(:scope => scope,
:name => "bad",
:arguments => {"owner" => "nobody"}
)
end
# And make sure it didn't create the file
- assert_nil(scope.findresource("File[/tmp/bad]"),
+ assert_nil(config.findresource("File[/tmp/bad]"),
"Made file with invalid params")
assert_nothing_raised do
- klass.evaluate(:scope => scope,
+ klass.evaluate_resource(:scope => scope,
:title => "first",
:arguments => {"mode" => "755"}
)
end
- firstobj = scope.findresource("File[/tmp/first]")
+ firstobj = config.findresource("File[/tmp/first]")
assert(firstobj, "Did not create /tmp/first obj")
assert_equal("file", firstobj.type)
@@ -65,7 +79,7 @@ class TestASTComponent < Test::Unit::TestCase
# Make sure we can't evaluate it with the same args
assert_raise(Puppet::ParseError) do
- klass.evaluate(:scope => scope,
+ klass.evaluate_resource(:scope => scope,
:title => "first",
:arguments => {"mode" => "755"}
)
@@ -73,13 +87,13 @@ class TestASTComponent < Test::Unit::TestCase
# Now create another with different args
assert_nothing_raised do
- klass.evaluate(:scope => scope,
+ klass.evaluate_resource(:scope => scope,
:title => "second",
:arguments => {"mode" => "755", "owner" => "daemon"}
)
end
- secondobj = scope.findresource("File[/tmp/second]")
+ secondobj = config.findresource("File[/tmp/second]")
assert(secondobj, "Did not create /tmp/second obj")
assert_equal("file", secondobj.type)
@@ -110,7 +124,7 @@ class TestASTComponent < Test::Unit::TestCase
end
args[:scope] = scope
assert_nothing_raised("Could not evaluate definition with %s" % hash.inspect) do
- klass.evaluate(args)
+ klass.evaluate_resource(args)
end
name = hash[:name] || hash[:title]
diff --git a/test/language/ast/hostclass.rb b/test/language/ast/hostclass.rb
index 051bee36c..f093504ec 100755
--- a/test/language/ast/hostclass.rb
+++ b/test/language/ast/hostclass.rb
@@ -17,10 +17,11 @@ class TestASTHostClass < Test::Unit::TestCase
AST = Puppet::Parser::AST
def test_hostclass
- interp, scope, source = mkclassframing
+ scope = mkscope
+ parser = scope.configuration.parser
# Create the class we're testing, first with no parent
- klass = interp.newclass "first",
+ klass = parser.newclass "first",
:code => AST::ASTArray.new(
:children => [resourcedef("file", "/tmp",
"owner" => "nobody", "mode" => "755")]
@@ -43,13 +44,13 @@ class TestASTHostClass < Test::Unit::TestCase
assert_equal("755", tmp[:mode])
# Now create a couple more classes.
- newbase = interp.newclass "newbase",
+ newbase = parser.newclass "newbase",
:code => AST::ASTArray.new(
:children => [resourcedef("file", "/tmp/other",
"owner" => "nobody", "mode" => "644")]
)
- newsub = interp.newclass "newsub",
+ newsub = parser.newclass "newsub",
:parent => "newbase",
:code => AST::ASTArray.new(
:children => [resourcedef("file", "/tmp/yay",
@@ -60,7 +61,7 @@ class TestASTHostClass < Test::Unit::TestCase
)
# Override a different variable in the top scope.
- moresub = interp.newclass "moresub",
+ moresub = parser.newclass "moresub",
:parent => "newbase",
:code => AST::ASTArray.new(
:children => [resourceoverride("file", "/tmp/other",
@@ -92,19 +93,20 @@ class TestASTHostClass < Test::Unit::TestCase
# Make sure that classes set their namespaces to themselves. This
# way they start looking for definitions in their own namespace.
def test_hostclass_namespace
- interp, scope, source = mkclassframing
+ scope = mkscope
+ parser = scope.configuration.parser
# Create a new class
klass = nil
assert_nothing_raised do
- klass = interp.newclass "funtest"
+ klass = parser.newclass "funtest"
end
# Now define a definition in that namespace
define = nil
assert_nothing_raised do
- define = interp.newdefine "funtest::mydefine"
+ define = parser.newdefine "funtest::mydefine"
end
assert_equal("funtest", klass.namespace,
@@ -127,17 +129,18 @@ class TestASTHostClass < Test::Unit::TestCase
# At the same time, make sure definitions in the parent class can be
# found within the subclass (#517).
def test_parent_scope_from_parentclass
- interp = mkinterp
+ scope = mkscope
+ parser = scope.configuration.parser
- interp.newclass("base")
- fun = interp.newdefine("base::fun")
- interp.newclass("middle", :parent => "base")
- interp.newclass("sub", :parent => "middle")
- scope = mkscope :interp => interp
+ parser.newclass("base")
+ fun = parser.newdefine("base::fun")
+ parser.newclass("middle", :parent => "base")
+ parser.newclass("sub", :parent => "middle")
+ scope = mkscope :parser => parser
ret = nil
assert_nothing_raised do
- ret = scope.evalclasses("sub")
+ ret = scope.configuration.evaluate_classes(["sub"])
end
subscope = scope.class_scope(scope.findclass("sub"))
diff --git a/test/language/ast/resourceref.rb b/test/language/ast/resourceref.rb
index 7b7889dc1..a3d6775a2 100755
--- a/test/language/ast/resourceref.rb
+++ b/test/language/ast/resourceref.rb
@@ -19,13 +19,13 @@ class TestASTResourceRef < Test::Unit::TestCase
def setup
super
- @interp = mkinterp
- @scope = mkscope :interp => @interp
+ @scope = mkscope
+ @parser = @scope.configuration.parser
end
def test_evaluate
- @interp.newdefine "one::two"
- @interp.newdefine "one-two"
+ @parser.newdefine "one::two"
+ @parser.newdefine "one-two"
[%w{file /tmp/yay}, %w{one::two three}, %w{one-two three}].each do |type, title|
ref = newref(type, title)
@@ -41,9 +41,9 @@ class TestASTResourceRef < Test::Unit::TestCase
# Related to #706, make sure resource references correctly translate to qualified types.
def test_scoped_references
- @interp.newdefine "one"
- @interp.newdefine "one::two"
- @interp.newdefine "three"
+ @parser.newdefine "one"
+ @parser.newdefine "one::two"
+ @parser.newdefine "three"
twoscope = @scope.newscope(:type => "one", :namespace => "one")
assert(twoscope.finddefine("two"), "Could not find 'two' definition")
title = "title"
@@ -70,8 +70,8 @@ class TestASTResourceRef < Test::Unit::TestCase
end
# Now run the same tests, but with the classes
- @interp.newclass "four"
- @interp.newclass "one::five"
+ @parser.newclass "four"
+ @parser.newclass "one::five"
# First try an unqualified type
assert_equal("four", newref("class", "four").evaluate(:scope => twoscope).title,
diff --git a/test/language/collector.rb b/test/language/collector.rb
index bdcaf4aec..a4119929f 100755
--- a/test/language/collector.rb
+++ b/test/language/collector.rb
@@ -16,7 +16,8 @@ class TestCollector < Test::Unit::TestCase
def setup
super
Puppet[:trace] = false
- @interp, @scope, @source = mkclassframing
+ @scope = mkscope
+ @config = @scope.configuration
end
# Test just collecting a specific resource. This is used by the 'realize'
@@ -32,7 +33,7 @@ class TestCollector < Test::Unit::TestCase
assert_nothing_raised do
coll.resources = ["File[/tmp/virtual1]", "File[/tmp/virtual3]"]
end
- @scope.newcollection(coll)
+ @config.add_collection(coll)
# Evaluate the collector and make sure it doesn't fail with no resources
# found yet
@@ -62,7 +63,7 @@ class TestCollector < Test::Unit::TestCase
"Resource got realized")
# Make sure that the collection is still there
- assert(@scope.collections.include?(coll), "collection was deleted too soon")
+ assert(@config.collections.include?(coll), "collection was deleted too soon")
# Now add our third resource
three = mkresource(:type => "file", :title => "/tmp/virtual3",
@@ -76,7 +77,7 @@ class TestCollector < Test::Unit::TestCase
assert(! three.virtual?, "three is still virtual")
# And make sure that the collection got deleted from the scope's list
- assert(@scope.collections.empty?, "collection was not deleted")
+ assert(@config.collections.empty?, "collection was not deleted")
end
def test_virtual
@@ -102,10 +103,10 @@ class TestCollector < Test::Unit::TestCase
end
# Set it in our scope
- @scope.newcollection(coll)
+ @config.add_collection(coll)
# Make sure it's in the collections
- assert(@scope.collections.include?(coll), "collection was not added")
+ assert(@config.collections.include?(coll), "collection was not added")
# And try to collect the virtual resources.
ret = nil
@@ -148,7 +149,7 @@ class TestCollector < Test::Unit::TestCase
coll = Puppet::Parser::Collector.new(@scope, "file", nil, nil, :virtual)
end
- @scope.newcollection(coll)
+ @config.add_collection(coll)
# run the collection and make sure it doesn't get deleted, since it
# didn't return anything
@@ -157,7 +158,7 @@ class TestCollector < Test::Unit::TestCase
"Evaluate returned incorrect value")
end
- assert_equal([coll], @scope.collections, "Collection was deleted")
+ assert_equal([coll], @config.collections, "Collection was deleted")
# Make a resource
one = mkresource(:type => "file", :title => "/tmp/virtual1",
@@ -170,7 +171,7 @@ class TestCollector < Test::Unit::TestCase
"Evaluate returned incorrect value")
end
- assert_equal([coll], @scope.collections, "Collection was deleted")
+ assert_equal([coll], @config.collections, "Collection was deleted")
assert_equal(false, one.virtual?, "One was not realized")
end
diff --git a/test/language/configuration.rb b/test/language/configuration.rb
index 688782f7b..fbdf68e73 100755
--- a/test/language/configuration.rb
+++ b/test/language/configuration.rb
@@ -287,9 +287,11 @@ class TestConfiguration < Test::Unit::TestCase
config.expects(:tag).with("four")
@node.classes = %w{one two three four}
+ result = nil
assert_nothing_raised("could not call evaluate_classes") do
- config.send(:evaluate_classes)
+ result = config.send(:evaluate_classes)
end
+ assert_equal(%w{one three}, result, "Did not return the list of evaluated classes")
end
def test_evaluate_collections
@@ -719,4 +721,25 @@ class TestConfiguration < Test::Unit::TestCase
assert_equal(:yay, config.findresource("Foo[bar]"), "Returned a non-existent resource")
end
end
+
+ # #620 - Nodes and classes should conflict, else classes don't get evaluated
+ def test_nodes_and_classes_name_conflict
+ # Test node then class
+ config = mkconfig
+ node = stub :nodescope? => true
+ klass = stub :nodescope? => false
+ config.class_set("one", node)
+ assert_raise(Puppet::ParseError, "Did not fail when replacing node with class") do
+ config.class_set("one", klass)
+ end
+
+ # and class then node
+ config = mkconfig
+ node = stub :nodescope? => true
+ klass = stub :nodescope? => false
+ config.class_set("two", klass)
+ assert_raise(Puppet::ParseError, "Did not fail when replacing node with class") do
+ config.class_set("two", node)
+ end
+ end
end
diff --git a/test/language/functions.rb b/test/language/functions.rb
index 34207de17..42d8d7585 100755
--- a/test/language/functions.rb
+++ b/test/language/functions.rb
@@ -3,7 +3,6 @@
$:.unshift("../lib").unshift("../../lib") if __FILE__ =~ /\.rb$/
require 'puppet'
-require 'puppet/parser/interpreter'
require 'puppet/parser/parser'
require 'puppet/network/client'
require 'puppettest'
@@ -204,16 +203,17 @@ class TestLangFunctions < Test::Unit::TestCase
f.puts %{file { "#{file}": content => template("#{template}") }}
end
- interpreter = Puppet::Parser::Interpreter.new(
+ interp = Puppet::Parser::Interpreter.new(
:Manifest => manifest,
:UseNodes => false
)
+ node = mknode
- parsedate = interpreter.parsedate()
+ parsedate = interp.parsedate()
objects = nil
assert_nothing_raised {
- objects = interpreter.run("myhost", {})
+ objects = interp.compile(node)
}
fileobj = objects[0]
@@ -221,7 +221,7 @@ class TestLangFunctions < Test::Unit::TestCase
assert_equal("original text\n", fileobj["content"],
"Template did not work")
- Puppet[:filetimeout] = 0
+ Puppet[:filetimeout] = -5
# Have to sleep because one second is the fs's time granularity.
sleep(1)
@@ -231,9 +231,9 @@ class TestLangFunctions < Test::Unit::TestCase
end
assert_nothing_raised {
- objects = interpreter.run("myhost", {})
+ objects = interp.compile(node)
}
- newdate = interpreter.parsedate()
+ newdate = interp.parsedate()
assert(parsedate != newdate, "Parse date did not change")
end
@@ -306,35 +306,36 @@ class TestLangFunctions < Test::Unit::TestCase
end
def test_realize
- @interp, @scope, @source = mkclassframing
+ scope = mkscope
+ parser = scope.configuration.parser
# Make a definition
- @interp.newdefine("mytype")
+ parser.newdefine("mytype")
[%w{file /tmp/virtual}, %w{mytype yay}].each do |type, title|
# Make a virtual resource
virtual = mkresource(:type => type, :title => title,
:virtual => true, :params => {})
- @scope.setresource virtual
+ scope.setresource virtual
ref = Puppet::Parser::Resource::Reference.new(
:type => type, :title => title,
- :scope => @scope
+ :scope => scope
)
# Now call the realize function
assert_nothing_raised do
- @scope.function_realize(ref)
+ scope.function_realize(ref)
end
# Make sure it created a collection
- assert_equal(1, @scope.collections.length,
+ assert_equal(1, scope.configuration.collections.length,
"Did not set collection")
assert_nothing_raised do
- @scope.collections.each do |coll| coll.evaluate end
+ scope.configuration.collections.each do |coll| coll.evaluate end
end
- @scope.collections.clear
+ scope.configuration.collections.clear
# Now make sure the virtual resource is no longer virtual
assert(! virtual.virtual?, "Did not make virtual resource real")
@@ -343,29 +344,29 @@ class TestLangFunctions < Test::Unit::TestCase
# Make sure we puke on any resource that doesn't exist
none = Puppet::Parser::Resource::Reference.new(
:type => "file", :title => "/tmp/nosuchfile",
- :scope => @scope
+ :scope => scope
)
# The function works
assert_nothing_raised do
- @scope.function_realize(none.to_s)
+ scope.function_realize(none.to_s)
end
# Make sure it created a collection
- assert_equal(1, @scope.collections.length,
+ assert_equal(1, scope.configuration.collections.length,
"Did not set collection")
# And the collection has our resource in it
- assert_equal([none.to_s], @scope.collections[0].resources,
+ assert_equal([none.to_s], scope.configuration.collections[0].resources,
"Did not set resources in collection")
end
def test_defined
- interp = mkinterp
- scope = mkscope(:interp => interp)
+ scope = mkscope
+ parser = scope.configuration.parser
- interp.newclass("yayness")
- interp.newdefine("rahness")
+ parser.newclass("yayness")
+ parser.newdefine("rahness")
assert_nothing_raised do
assert(scope.function_defined("yayness"), "yayness class was not considered defined")
@@ -395,11 +396,11 @@ class TestLangFunctions < Test::Unit::TestCase
end
def test_search
- interp = mkinterp
- scope = mkscope(:interp => interp)
+ parser = mkparser
+ scope = mkscope(:parser => parser)
- fun = interp.newdefine("yay::ness")
- foo = interp.newdefine("foo::bar")
+ fun = parser.newdefine("yay::ness")
+ foo = parser.newdefine("foo::bar")
search = Puppet::Parser::Functions.function(:search)
assert_nothing_raised do
@@ -417,36 +418,36 @@ class TestLangFunctions < Test::Unit::TestCase
end
def test_include
- interp = mkinterp
- scope = mkscope(:interp => interp)
+ scope = mkscope
+ parser = scope.configuration.parser
assert_raise(Puppet::ParseError, "did not throw error on missing class") do
scope.function_include("nosuchclass")
end
- interp.newclass("myclass")
+ parser.newclass("myclass")
assert_nothing_raised do
scope.function_include "myclass"
end
- assert(scope.classlist.include?("myclass"),
+ assert(scope.configuration.classlist.include?("myclass"),
"class was not evaluated")
# Now try multiple classes at once
- classes = %w{one two three}.each { |c| interp.newclass(c) }
+ classes = %w{one two three}.each { |c| parser.newclass(c) }
assert_nothing_raised do
scope.function_include classes
end
classes.each do |c|
- assert(scope.classlist.include?(c),
+ assert(scope.configuration.classlist.include?(c),
"class %s was not evaluated" % c)
end
# Now try a scoped class
- interp.newclass("os::redhat")
+ parser.newclass("os::redhat")
assert_nothing_raised("Could not include qualified class name") do
scope.function_include("os::redhat")
@@ -454,8 +455,8 @@ class TestLangFunctions < Test::Unit::TestCase
end
def test_file
- interp = mkinterp
- scope = mkscope(:interp => interp)
+ parser = mkparser
+ scope = mkscope(:parser => parser)
file1 = tempfile
file2 = tempfile
@@ -497,8 +498,8 @@ class TestLangFunctions < Test::Unit::TestCase
assert_equal("yay\n", %x{#{command}}, "command did not work")
assert_equal("yay-foo\n", %x{#{command} foo}, "command did not work")
- interp = mkinterp
- scope = mkscope(:interp => interp)
+ scope = mkscope
+ parser = scope.configuration.parser
val = nil
assert_nothing_raised("Could not call generator with no args") do
diff --git a/test/language/interpreter.rb b/test/language/interpreter.rb
index 6b9aa7258..ebbc3f87f 100755
--- a/test/language/interpreter.rb
+++ b/test/language/interpreter.rb
@@ -111,257 +111,16 @@ class TestInterpreter < PuppetTest::TestCase
end
# Make sure our whole chain works.
- def test_evaluate
- 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"})
-
- # Now evaluate everything
- objects = nil
- interp.usenodes = false
- assert_nothing_raised do
- objects = interp.evaluate(nil, {})
- end
-
- assert_instance_of(Puppet::TransBucket, objects)
- end
-
- # Test evaliterate. It's a very simple method, but it's pretty tough
- # to test. It iterates over collections and instances of defined types
- # until there's no more work to do.
- def test_evaliterate
- interp, scope, source = mkclassframing
-
- # Create a top-level definition that creates a builtin object
- interp.newdefine("one", :arguments => [%w{owner}],
- :code => AST::ASTArray.new(:children => [
- resourcedef("file", varref("name"),
- "owner" => varref("owner")
- )
- ])
- )
-
- # Create another definition to call that one
- interp.newdefine("two", :arguments => [%w{owner}],
- :code => AST::ASTArray.new(:children => [
- resourcedef("one", varref("name"),
- "owner" => varref("owner")
- )
- ])
- )
-
- # And then a third
- interp.newdefine("three", :arguments => [%w{owner}],
- :code => AST::ASTArray.new(:children => [
- resourcedef("two", varref("name"),
- "owner" => varref("owner")
- )
- ])
- )
-
- # And create a definition that creates a virtual resource
- interp.newdefine("virtualizer", :arguments => [%w{owner}],
- :code => AST::ASTArray.new(:children => [
- virt_resourcedef("one", varref("name"),
- "owner" => varref("owner")
- )
- ])
- )
-
- # Now create an instance of three
- three = Puppet::Parser::Resource.new(
- :type => "three", :title => "one",
- :scope => scope, :source => source,
- :params => paramify(source, :owner => "root")
- )
- scope.setresource(three)
-
- # An instance of the virtualizer
- virt = Puppet::Parser::Resource.new(
- :type => "virtualizer", :title => "two",
- :scope => scope, :source => source,
- :params => paramify(source, :owner => "root")
- )
- scope.setresource(virt)
-
- # And a virtual instance of three
- virt_three = Puppet::Parser::Resource.new(
- :type => "three", :title => "three",
- :scope => scope, :source => source,
- :params => paramify(source, :owner => "root")
- )
- virt_three.virtual = true
- scope.setresource(virt_three)
-
- # Create a normal, virtual resource
- plainvirt = Puppet::Parser::Resource.new(
- :type => "user", :title => "five",
- :scope => scope, :source => source,
- :params => paramify(source, :uid => "root")
- )
- plainvirt.virtual = true
- scope.setresource(plainvirt)
-
- # Now create some collections for our virtual resources
- %w{Three[three] One[two]}.each do |ref|
- coll = Puppet::Parser::Collector.new(scope, "file", nil, nil, :virtual)
- coll.resources = [ref]
- scope.newcollection(coll)
- end
-
- # And create a generic user collector for our plain resource
- coll = Puppet::Parser::Collector.new(scope, "user", nil, nil, :virtual)
- scope.newcollection(coll)
-
- ret = nil
- assert_nothing_raised do
- ret = scope.unevaluated
- end
-
-
- assert_instance_of(Array, ret)
- assert_equal(3, ret.length,
- "did not get the correct number of unevaled resources")
-
- # Now translate the whole tree
- assert_nothing_raised do
- Timeout::timeout(2) do
- interp.evaliterate(scope)
- end
- end
-
- # Now make sure we've got all of our files
- %w{one two three}.each do |name|
- file = scope.findresource("File[%s]" % name)
- assert(file, "Could not find file %s" % name)
-
- assert_equal("root", file[:owner])
- assert(! file.virtual?, "file %s is still virtual" % name)
- end
-
- # Now make sure we found the user
- assert(! plainvirt.virtual?, "user was not realized")
- end
-
- # Make sure we fail if there are any leftover overrides to perform.
- # This would normally mean that someone is trying to override an object
- # that does not exist.
- def test_failonleftovers
- interp, scope, source = mkclassframing
-
- # Make sure we don't fail, since there are no overrides
- assert_nothing_raised do
- interp.failonleftovers(scope)
- end
-
- # Add an override, and make sure it causes a failure
- over1 = mkresource :scope => scope, :source => source,
- :params => {:one => "yay"}
-
- scope.setoverride(over1)
-
- assert_raise(Puppet::ParseError) do
- interp.failonleftovers(scope)
- end
-
- # Make a new scope to test leftover collections
- scope = mkscope :interp => interp
- interp.meta_def(:check_resource_collections) do
- raise ArgumentError, "yep"
- end
-
- assert_raise(ArgumentError, "did not call check_resource_colls") do
- interp.failonleftovers(scope)
- end
- end
-
- def test_evalnode
+ def test_compile
interp = mkinterp
- interp.usenodes = false
- scope = Parser::Scope.new(:interp => interp)
- facts = Facter.to_hash
-
- # First make sure we get no failures when client is nil
- assert_nothing_raised do
- interp.evalnode(nil, scope, facts)
- end
-
- # Now define a node
- interp.newnode "mynode", :code => AST::ASTArray.new(:children => [
- resourcedef("file", "/tmp/testing", "owner" => "root")
- ])
-
- # Eval again, and make sure it does nothing
- assert_nothing_raised do
- interp.evalnode("mynode", scope, facts)
- end
-
- assert_nil(scope.findresource("File[/tmp/testing]"),
- "Eval'ed node with nodes off")
-
- # Now enable usenodes and make sure it works.
- interp.usenodes = true
- assert_nothing_raised do
- interp.evalnode("mynode", scope, facts)
- end
- file = scope.findresource("File[/tmp/testing]")
-
- assert_instance_of(Puppet::Parser::Resource, file,
- "Could not find file")
- end
-
- # This is mostly used for the cfengine module
- def test_specificclasses
- interp = mkinterp :Classes => %w{klass1 klass2}, :UseNodes => false
-
- # Make sure it's not a failure to be missing classes, since
- # we're using the cfengine class list, which is huge.
- assert_nothing_raised do
- interp.evaluate(nil, {})
- end
-
- interp.newclass("klass1", :code => AST::ASTArray.new(:children => [
- resourcedef("file", "/tmp/klass1", "owner" => "root")
- ]))
- interp.newclass("klass2", :code => AST::ASTArray.new(:children => [
- resourcedef("file", "/tmp/klass2", "owner" => "root")
- ]))
-
- ret = nil
- assert_nothing_raised do
- ret = interp.evaluate(nil, {})
- end
-
- found = ret.flatten.collect do |res| res.name end
-
- assert(found.include?("/tmp/klass1"), "Did not evaluate klass1")
- assert(found.include?("/tmp/klass2"), "Did not evaluate klass2")
- end
-
- def test_check_resource_collections
- interp = mkinterp
- scope = mkscope :interp => interp
- coll = Puppet::Parser::Collector.new(scope, "file", nil, nil, :virtual)
- coll.resources = ["File[/tmp/virtual1]", "File[/tmp/virtual2]"]
- scope.newcollection(coll)
-
- assert_raise(Puppet::ParseError, "Did not fail on remaining resource colls") do
- interp.check_resource_collections(scope)
- end
+ interp.expects(:parsefiles)
+ parser = interp.instance_variable_get("@parser")
+
+ node = mock('node')
+ config = mock('config')
+ config.expects(:compile).returns(:config)
+ Puppet::Parser::Configuration.expects(:new).with(node, parser).returns(config)
+ assert_equal(:config, interp.compile(node), "Did not return the results of config.compile")
end
# Make sure that reparsing is atomic -- failures don't cause a broken state, and we aren't subject
@@ -373,7 +132,7 @@ class TestInterpreter < PuppetTest::TestCase
interp = mkinterp :Manifest => file, :UseNodes => false
assert_nothing_raised("Could not compile the first time") do
- interp.run("yay", {})
+ interp.compile(mknode("yay"))
end
oldparser = interp.send(:instance_variable_get, "@parser")
@@ -381,7 +140,7 @@ class TestInterpreter < PuppetTest::TestCase
# Now add a syntax failure
File.open(file, "w") { |f| f.puts %{file { /tmp: ensure => directory }} }
assert_nothing_raised("Could not compile the first time") do
- interp.run("yay", {})
+ interp.compile(mknode("yay"))
end
# And make sure the old parser is still there
diff --git a/test/language/parser.rb b/test/language/parser.rb
index 77595b155..c172aafca 100755
--- a/test/language/parser.rb
+++ b/test/language/parser.rb
@@ -38,13 +38,13 @@ class TestParser < Test::Unit::TestCase
def test_failers
failers { |file|
parser = mkparser
- interp = mkinterp
Puppet.debug("parsing failer %s" % file) if __FILE__ == $0
- assert_raise(Puppet::ParseError) {
+ assert_raise(Puppet::ParseError, "Did not fail while parsing %s" % file) {
parser.file = file
ast = parser.parse
- scope = mkscope :interp => interp
- ast.classes[""].evaluate :scope => scope
+ config = mkconfig(parser)
+ config.compile
+ #ast.classes[""].evaluate :scope => config.topscope
}
Puppet::Type.allclear
}
@@ -622,7 +622,7 @@ file { "/tmp/yayness":
code = nil
assert_nothing_raised do
- code = interp.run("hostname.domain.com", {}).flatten
+ code = interp.compile(mknode).flatten
end
assert(code.length == 1, "Did not get the file")
assert_instance_of(Puppet::TransObject, code[0])
@@ -871,7 +871,8 @@ file { "/tmp/yayness":
end
def test_newclass
- parser = mkparser
+ scope = mkscope
+ parser = scope.configuration.parser
mkcode = proc do |ary|
classes = ary.collect do |string|
@@ -880,7 +881,6 @@ file { "/tmp/yayness":
AST::ASTArray.new(:children => classes)
end
- scope = Puppet::Parser::Scope.new(:interp => mkinterp)
# First make sure that code is being appended
code = mkcode.call(%w{original code})
diff --git a/test/language/resource.rb b/test/language/resource.rb
index 73d516f51..50d58cf32 100755
--- a/test/language/resource.rb
+++ b/test/language/resource.rb
@@ -19,6 +19,10 @@ class TestResource < PuppetTest::TestCase
Puppet[:trace] = false
end
+ def teardown
+ mocha_verify
+ end
+
def test_initialize
args = {:type => "resource", :title => "testing",
:source => "source", :scope => "scope"}
@@ -208,11 +212,14 @@ class TestResource < PuppetTest::TestCase
def test_to_trans
# First try translating a builtin resource. Make sure we use some references
# and arrays, to make sure they translate correctly.
+ source = mock("source")
+ scope = mock("scope")
+ scope.expects(:tags).returns([])
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", :group => %w{you me},
+ :source => source, :scope => scope,
+ :params => paramify(source, :owner => "nobody", :group => %w{you me},
:require => refs[0], :ignore => %w{svn},
:subscribe => [refs[1], refs[2]], :notify => [refs[3]])
@@ -236,37 +243,25 @@ class TestResource < PuppetTest::TestCase
end
def test_evaluate
- @parser = mkparser
- # Make a definition that we know will, um, do something
- @parser.newdefine "evaltest",
- :arguments => [%w{one}, ["two", stringobj("755")]],
- :code => resourcedef("file", "/tmp",
- "owner" => varref("one"), "mode" => varref("two"))
-
- res = Parser::Resource.new :type => "evaltest", :title => "yay",
- :source => @source, :scope => @scope,
- :params => paramify(@source, :one => "nobody")
+ # First try the most common case, we're not a builtin type.
+ res = mkresource
+ ref = res.instance_variable_get("@ref")
+ type = mock("type")
+ ref.expects(:definedtype).returns(type)
+ res.expects(:finish)
+ res.scope = mock("scope")
+ config = mock("config")
+ res.scope.expects(:configuration).returns(config)
+ config.expects(:delete_resource).with(res)
- # Now try evaluating
- ret = nil
- assert_nothing_raised do
- ret = res.evaluate
+ args = {:scope => res.scope, :arguments => res.to_hash}
+ # This is insane; FIXME we need to redesign how classes and components are evaluated.
+ [:type, :title, :virtual, :exported].each do |param|
+ args[param] = res.send(param)
end
+ type.expects(:evaluate_resource).with(args)
- # Make sure we can find our object now
- result = @scope.findresource("File[/tmp]")
-
- # Now make sure we got the code we expected.
- assert_instance_of(Puppet::Parser::Resource, result)
-
- assert_equal("file", result.type)
- assert_equal("/tmp", result.title)
- assert_equal("nobody", result["owner"])
- assert_equal("755", result["mode"])
-
- # And that we cannot find the old resource
- assert_nil(@scope.findresource("Evaltest[yay]"),
- "Evaluated resource was not deleted")
+ res.evaluate
end
def test_add_overrides
@@ -303,7 +298,7 @@ class TestResource < PuppetTest::TestCase
def test_proxymethods
res = Parser::Resource.new :type => "evaltest", :title => "yay",
- :source => @source, :scope => @scope
+ :source => mock("source"), :scope => mock('scope')
assert_equal("evaltest", res.type)
assert_equal("yay", res.title)
@@ -331,6 +326,7 @@ class TestResource < PuppetTest::TestCase
# Now create an obj that uses it
res = mkresource :type => "file", :title => "/tmp/resource",
:params => {:require => ref}
+ res.scope = stub(:tags => [])
trans = nil
assert_nothing_raised do
@@ -344,6 +340,7 @@ class TestResource < PuppetTest::TestCase
two = Parser::Resource::Reference.new(:type => "file", :title => "/tmp/ref2")
res = mkresource :type => "file", :title => "/tmp/resource2",
:params => {:require => [ref, two]}
+ res.scope = stub(:tags => [])
trans = nil
assert_nothing_raised do
@@ -368,62 +365,24 @@ class TestResource < PuppetTest::TestCase
assert_nil(ref.builtintype, "Component was considered builtin")
end
- # #472. Really, this still isn't the best behaviour, but at least
- # it's consistent with what we have elsewhere.
- def test_defaults_from_parent_classes
- @parser = mkparser
- # Make a parent class with some defaults in it
- @parser.newclass("base",
- :code => defaultobj("file", :owner => "root", :group => "root")
- )
-
- # Now a mid-level class with some different values
- @parser.newclass("middle", :parent => "base",
- :code => defaultobj("file", :owner => "bin", :mode => "755")
- )
-
- # Now a lower class with its own defaults plus a resource
- @parser.newclass("bottom", :parent => "middle",
- :code => AST::ASTArray.new(:children => [
- defaultobj("file", :owner => "adm", :recurse => "true"),
- resourcedef("file", "/tmp/yayness", {})
- ])
- )
-
- # Now evaluate the class.
- assert_nothing_raised("Failed to evaluate class tree") do
- @scope.evalclasses("bottom")
- end
-
- # Make sure our resource got created.
- res = @scope.findresource("File[/tmp/yayness]")
- assert_nothing_raised("Could not add defaults") do
- res.adddefaults
- end
- assert(res, "could not find resource")
- {:owner => "adm", :recurse => "true", :group => "root", :mode => "755"}.each do |param, value|
- assert_equal(value, res[param], "%s => %s did not inherit correctly" %
- [param, value])
- end
- end
-
# The second part of #539 - make sure resources pass the arguments
# correctly.
def test_title_with_definitions
- @parser = mkparser
- define = @parser.newdefine "yayness",
+ parser = mkparser
+ define = parser.newdefine "yayness",
:code => resourcedef("file", "/tmp",
"owner" => varref("name"), "mode" => varref("title"))
- klass = @parser.findclass("", "")
+
+ klass = parser.findclass("", "")
should = {:name => :owner, :title => :mode}
[
{:name => "one", :title => "two"},
{:title => "three"},
].each do |hash|
- scope = mkscope :parser => @parser
+ config = mkconfig parser
args = {:type => "yayness", :title => hash[:title],
- :source => klass, :scope => scope}
+ :source => klass, :scope => config.topscope}
if hash[:name]
args[:params] = {:name => hash[:name]}
else
@@ -438,7 +397,7 @@ class TestResource < PuppetTest::TestCase
res.evaluate
end
- made = scope.findresource("File[/tmp]")
+ made = config.topscope.findresource("File[/tmp]")
assert(made, "Did not create resource with %s" % hash.inspect)
should.each do |orig, param|
assert_equal(hash[orig] || hash[:title], made[param],
@@ -450,7 +409,7 @@ class TestResource < PuppetTest::TestCase
# part of #629 -- the undef keyword. Make sure 'undef' params get skipped.
def test_undef_and_to_hash
res = mkresource :type => "file", :title => "/tmp/testing",
- :source => @source, :scope => @scope,
+ :source => mock("source"), :scope => mock("scope"),
:params => {:owner => :undef, :mode => "755"}
hash = nil
@@ -463,12 +422,14 @@ class TestResource < PuppetTest::TestCase
# #643 - Make sure virtual defines result in virtual resources
def test_virtual_defines
- @parser = mkparser
- define = @parser.newdefine("yayness",
+ parser = mkparser
+ define = parser.newdefine("yayness",
:code => resourcedef("file", varref("name"),
"mode" => "644"))
- res = mkresource :type => "yayness", :title => "foo", :params => {}
+ config = mkconfig(parser)
+
+ res = mkresource :type => "yayness", :title => "foo", :params => {}, :scope => config.topscope
res.virtual = true
result = nil
@@ -483,7 +444,7 @@ class TestResource < PuppetTest::TestCase
assert(newres.virtual?, "Virtual defined resource generated non-virtual resources")
# Now try it with exported resources
- res = mkresource :type => "yayness", :title => "bar", :params => {}
+ res = mkresource :type => "yayness", :title => "bar", :params => {}, :scope => config.topscope
res.exported = true
result = nil
diff --git a/test/language/scope.rb b/test/language/scope.rb
index bd90caa53..fc5e085d4 100755
--- a/test/language/scope.rb
+++ b/test/language/scope.rb
@@ -27,74 +27,32 @@ class TestScope < Test::Unit::TestCase
end
def test_variables
- scope = nil
- over = "over"
-
- scopes = []
- vars = []
- values = {}
- ovalues = []
-
- 10.times { |index|
- # slap some recursion in there
- scope = mkscope(:parent => scope)
- scopes.push scope
-
- var = "var%s" % index
- value = rand(1000)
- ovalue = rand(1000)
-
- ovalues.push ovalue
+ config = mkconfig
+ topscope = config.topscope
+ midscope = config.newscope(topscope)
+ botscope = config.newscope(midscope)
- vars.push var
- values[var] = value
+ scopes = {:top => topscope, :mid => midscope, :bot => botscope}
- # set the variable in the current scope
- assert_nothing_raised {
- scope.setvar(var,value)
- }
+ # Set a variable in the top and make sure all three can get it
+ topscope.setvar("first", "topval")
+ scopes.each do |name, scope|
+ assert_equal("topval", scope.lookupvar("first", false), "Could not find var in %s" % name)
+ end
- # this should override previous values
- assert_nothing_raised {
- scope.setvar(over,ovalue)
- }
+ # Now set a var in the midscope and make sure the mid and bottom can see it but not the top
+ midscope.setvar("second", "midval")
+ assert_equal(:undefined, scopes[:top].lookupvar("second", false), "Found child var in top scope")
+ [:mid, :bot].each do |name|
+ assert_equal("midval", scopes[name].lookupvar("second", false), "Could not find var in %s" % name)
+ end
- assert_equal(value,scope.lookupvar(var))
-
- #puts "%s vars, %s scopes" % [vars.length,scopes.length]
- i = 0
- vars.zip(scopes) { |v,s|
- # this recurses all the way up the tree as necessary
- val = nil
- oval = nil
-
- # look up the values using the bottom scope
- assert_nothing_raised {
- val = scope.lookupvar(v)
- oval = scope.lookupvar(over)
- }
-
- # verify they're correct
- assert_equal(values[v],val)
- assert_equal(ovalue,oval)
-
- # verify that we get the most recent value
- assert_equal(ovalue,scope.lookupvar(over))
-
- # verify that they aren't available in upper scopes
- if parent = s.parent
- val = nil
- assert_nothing_raised {
- val = parent.lookupvar(v)
- }
- assert_equal("", val, "Did not get empty string on missing var")
-
- # and verify that the parent sees its correct value
- assert_equal(ovalues[i - 1],parent.lookupvar(over))
- end
- i += 1
- }
- }
+ # And set something in the bottom, and make sure we only find it there.
+ botscope.setvar("third", "botval")
+ [:top, :mid].each do |name|
+ assert_equal(:undefined, scopes[name].lookupvar("third", false), "Found child var in top scope")
+ end
+ assert_equal("botval", scopes[:bot].lookupvar("third", false), "Could not find var in bottom scope")
end
def test_lookupvar
@@ -174,18 +132,18 @@ class TestScope < Test::Unit::TestCase
defaults = scope.instance_variable_get("@defaults")
# First the case where there are no defaults and we pass a single param
- param = stub :name => "myparam"
+ param = stub :name => "myparam", :file => "f", :line => "l"
scope.setdefaults(:mytype, param)
assert_equal({"myparam" => param}, defaults[:mytype], "Did not set default correctly")
# Now the case where we pass in multiple parameters
- param1 = stub :name => "one"
- param2 = stub :name => "two"
+ param1 = stub :name => "one", :file => "f", :line => "l"
+ param2 = stub :name => "two", :file => "f", :line => "l"
scope.setdefaults(:newtype, [param1, param2])
assert_equal({"one" => param1, "two" => param2}, defaults[:newtype], "Did not set multiple defaults correctly")
# And the case where there's actually a conflict. Use the first default for this.
- newparam = stub :name => "myparam"
+ newparam = stub :name => "myparam", :file => "f", :line => "l"
assert_raise(Puppet::ParseError, "Allowed resetting of defaults") do
scope.setdefaults(:mytype, param)
end
@@ -238,7 +196,7 @@ class TestScope < Test::Unit::TestCase
assert_equal(:myscope, scope.class_scope(:testing), "Did not pass back the results of config.class_scope")
end
- def test_strparser
+ def test_strinterp
# Make and evaluate our classes so the qualified lookups work
parser = mkparser
klass = parser.newclass("")
@@ -293,7 +251,7 @@ class TestScope < Test::Unit::TestCase
tests.each do |input, output|
assert_nothing_raised("Failed to scan %s" % input.inspect) do
- assert_equal(output, scope.strparser(input),
+ assert_equal(output, scope.strinterp(input),
'did not parserret %s correctly' % input.inspect)
end
end
@@ -306,7 +264,7 @@ 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.strparser(string),
+ assert_equal(string, scope.strinterp(string),
'did not parserret %s correctly' % string)
end
@@ -317,37 +275,35 @@ class TestScope < Test::Unit::TestCase
end
def test_setclass
- parser, scope, source = mkclassframing
-
- base = scope.findclass("base")
- assert(base, "Could not find base class")
- assert(! scope.class_scope(base), "Class incorrectly set")
- assert(! scope.classlist.include?("base"), "Class incorrectly in classlist")
- assert_nothing_raised do
- scope.setclass base
- end
-
- assert(scope.class_scope(base), "Class incorrectly unset")
- assert(scope.classlist.include?("base"), "Class not in classlist")
-
- # Make sure we can retrieve the scope.
- assert_equal(scope, scope.class_scope(base),
- "class scope was not set correctly")
-
- # Now try it with a normal string
- Puppet[:trace] = false
- assert_raise(Puppet::DevError) do
- scope.setclass "string"
+ # Run through it when we're a normal class
+ config = mkconfig
+ scope = config.topscope
+ klass = mock("class")
+ klass.expects(:classname).returns(:myclass)
+ klass.expects(:is_a?).with(AST::HostClass).returns(true)
+ klass.expects(:is_a?).with(AST::Node).returns(false)
+ config.expects(:class_set).with(:myclass, scope)
+ scope.setclass(klass)
+
+ # And when we're a node
+ config = mkconfig
+ scope = config.topscope
+ klass = mock("class2")
+ klass.expects(:classname).returns(:myclass)
+ klass.expects(:is_a?).with(AST::HostClass).returns(true)
+ klass.expects(:is_a?).with(AST::Node).returns(true)
+ config.expects(:class_set).with(:myclass, scope)
+ scope.setclass(klass)
+ assert(scope.nodescope?, "Did not set the scope as a node scope when evaluating a node")
+
+ # And when we're invalid
+ config = mkconfig
+ scope = config.topscope
+ klass = mock("class3")
+ klass.expects(:is_a?).with(AST::HostClass).returns(false)
+ assert_raise(Puppet::DevError, "Did not fail when scope got passed a non-component") do
+ scope.setclass(klass)
end
-
- assert(! scope.class_scope("string"), "string incorrectly set")
-
- # Set "" in the class list, and make sure it doesn't show up in the return
- top = scope.findclass("")
- assert(top, "Could not find top class")
- scope.setclass top
-
- assert(! scope.classlist.include?(""), "Class list included empty")
end
def test_validtags
@@ -368,7 +324,7 @@ class TestScope < Test::Unit::TestCase
end
def test_tagfunction
- scope = mkscope()
+ scope = mkscope
assert_nothing_raised {
scope.function_tag(["yayness", "booness"])
@@ -445,62 +401,15 @@ class TestScope < Test::Unit::TestCase
"undef considered true")
end
- # Verify scope context is handled correctly.
- def test_scopeinside
- scope = mkscope()
-
- one = :one
- two = :two
-
- # First just test the basic functionality.
- assert_nothing_raised {
- scope.inside :one do
- assert_equal(:one, scope.inside, "Context did not get set")
- end
- assert_nil(scope.inside, "Context did not revert")
- }
-
- # Now make sure error settings work.
- assert_raise(RuntimeError) {
- scope.inside :one do
- raise RuntimeError, "This is a failure, yo"
- end
- }
- assert_nil(scope.inside, "Context did not revert")
-
- # Now test it a bit deeper in.
- assert_nothing_raised {
- scope.inside :one do
- scope.inside :two do
- assert_equal(:two, scope.inside, "Context did not get set")
- end
- assert_equal(:one, scope.inside, "Context did not get set")
- end
- assert_nil(scope.inside, "Context did not revert")
- }
-
- # And lastly, check errors deeper in
- assert_nothing_raised {
- scope.inside :one do
- begin
- scope.inside :two do
- raise "a failure"
- end
- rescue
- end
- assert_equal(:one, scope.inside, "Context did not get set")
- end
- assert_nil(scope.inside, "Context did not revert")
- }
-
- end
-
if defined? ActiveRecord
# Verify that we recursively mark as exported the results of collectable
# components.
def test_exportedcomponents
- parser, scope, source = mkclassframing
- children = []
+ config = mkconfig
+ parser = config.parser
+
+ # Create a default source
+ config.topscope.source = parser.newclass "", ""
args = AST::ASTArray.new(
:file => tempfile(),
@@ -538,13 +447,14 @@ class TestScope < Test::Unit::TestCase
# And mark it as exported
obj.exported = true
- obj.evaluate :scope => scope
-
# And then evaluate it
- parser.evaliterate(scope)
+ obj.evaluate :scope => config.topscope
+
+ # And run the loop.
+ config.send(:evaluate_generators)
%w{file}.each do |type|
- objects = scope.lookupexported(type)
+ objects = config.resources.find_all { |r| r.type == type and r.exported }
assert(!objects.empty?, "Did not get an exported %s" % type)
end
@@ -722,26 +632,6 @@ Host <<||>>"
assert_equal("", scope.lookupvar("testing", true),
"undef was not returned as '' when string")
end
-
- # #620 - Nodes and classes should conflict, else classes don't get evaluated
- def test_nodes_and_classes_name_conflict
- scope = mkscope
-
- node = AST::Node.new :classname => "test", :namespace => ""
- scope.setclass(node)
-
- assert(scope.nodescope?, "Scope was not marked a node scope when a node was set")
-
- # Now make a subscope that will be a class scope
- klass = AST::HostClass.new :classname => "test", :namespace => ""
- kscope = klass.subscope(scope)
-
- # Now make sure we throw a failure, because we're trying to do a class and node
- # with the same name
- assert_raise(Puppet::ParseError, "Did not fail on class and node with same name") do
- kscope.class_scope(klass)
- end
- end
end
# $Id$
diff --git a/test/lib/puppettest.rb b/test/lib/puppettest.rb
index 5c385afb1..b56bc563e 100755
--- a/test/lib/puppettest.rb
+++ b/test/lib/puppettest.rb
@@ -283,6 +283,7 @@ module PuppetTest
rescue Timeout::Error
# just move on
end
+ mocha_verify
if File.stat("/dev/null").mode & 007777 != 0666
File.open("/tmp/nullfailure", "w") { |f|
f.puts self.class
diff --git a/test/lib/puppettest/parsertesting.rb b/test/lib/puppettest/parsertesting.rb
index 368e112f9..0a695cbaa 100644
--- a/test/lib/puppettest/parsertesting.rb
+++ b/test/lib/puppettest/parsertesting.rb
@@ -45,7 +45,7 @@ module PuppetTest::ParserTesting
require 'puppet/network/handler/node'
parser ||= mkparser
node = mknode
- return Config.new(parser, node)
+ return Config.new(node, parser)
end
def mknode(name = nil)
@@ -65,14 +65,14 @@ module PuppetTest::ParserTesting
end
def mkscope(hash = {})
- hash[:configuration] ||= mkconfig
hash[:parser] ||= mkparser
- hash[:source] ||= (hash[:parser].findclass("", "") || hash[:parser].newclass(""))
+ config ||= mkconfig(hash[:parser])
+ config.topscope.source = (hash[:parser].findclass("", "") || hash[:parser].newclass(""))
- unless hash[:source]
+ unless config.topscope.source
raise "Could not find source for scope"
end
- Puppet::Parser::Scope.new(hash)
+ config.topscope
end
def classobj(name, hash = {})
@@ -308,7 +308,7 @@ module PuppetTest::ParserTesting
config = nil
assert_nothing_raised {
- config = interp.run(Facter["hostname"].value, {})
+ config = interp.compile(mknode)
}
comp = nil