summaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorDaniel Pittman <daniel@rimspace.net>2008-07-22 17:53:06 +1000
committerJames Turnbull <james@lovedthanlost.net>2008-08-01 08:54:05 +1000
commit404450a71581f1ac2a13ff9340992e4f999f3091 (patch)
treeb05f83e245c88a6efe4a8a6d01cf7c16782b8e4c /test
parent03c76deb60927375e9b35c74a903130930ffddf2 (diff)
downloadpuppet-404450a71581f1ac2a13ff9340992e4f999f3091.tar.gz
puppet-404450a71581f1ac2a13ff9340992e4f999f3091.tar.xz
puppet-404450a71581f1ac2a13ff9340992e4f999f3091.zip
Add testing for the changes to resolve redmine #1427, where Kernel methods shadow
variables that the puppet template should otherwise see. Specific changes: * Added testing of the Scope#to_hash method, which returns a hash of name and value pairs in a scope or, optionally, in the scope and enclosing scopes. * Use member variables rather than methods in the function tests. * Fix up tests that fail once we move over to instance variables rather than methods: Puppet can no longer detect an undefined variable reference, so we end up failing any test that expected to get a parser error. * Several tests have manual checks introduced to simulate an end user manually writing the checks that used to be automatic, and others drop the validation that parsing fails when a variable is not in scope. * Added tests for legacy variable lookup and that the shadowing of local variables by Kernel methods is still in effect. Signed-off-by: Daniel Pittman <daniel@rimspace.net> (cherry picked from commit 5c5f315cceadc52203e53883b77bc01c1d7a2e7f)
Diffstat (limited to 'test')
-rwxr-xr-xtest/language/functions.rb91
-rwxr-xr-xtest/language/scope.rb28
2 files changed, 108 insertions, 11 deletions
diff --git a/test/language/functions.rb b/test/language/functions.rb
index a5d52d7ac..97429802b 100755
--- a/test/language/functions.rb
+++ b/test/language/functions.rb
@@ -96,11 +96,12 @@ class TestLangFunctions < Test::Unit::TestCase
twop = File.join(Puppet[:templatedir], "two")
File.open(onep, "w") do |f|
- f.puts "template <%= one %>"
+ f.puts "<%- if @one.nil? then raise '@one undefined' end -%>" +
+ "template <%= @one %>"
end
File.open(twop, "w") do |f|
- f.puts "template <%= two %>"
+ f.puts "template <%= @two %>"
end
func = nil
assert_nothing_raised do
@@ -116,15 +117,27 @@ class TestLangFunctions < Test::Unit::TestCase
ast = varobj("output", func)
scope = mkscope
+
+ # Test that our manual exception throw fails the parse
assert_raise(Puppet::ParseError) do
ast.evaluate(scope)
end
+ # Test that our use of an undefined instance variable does not throw
+ # an exception, but only safely continues.
scope.setvar("one", "One")
- assert_raise(Puppet::ParseError) do
+ assert_nothing_raised do
ast.evaluate(scope)
end
+
+ # Ensure that we got the output we expected from that evaluation.
+ assert_equal("template One\ntemplate \n", scope.lookupvar("output"),
+ "Undefined template variables do not raise exceptions")
+
+ # Now, fill in the last variable and make sure the whole thing
+ # evaluates correctly.
scope.setvar("two", "Two")
+ scope.unsetvar("output")
assert_nothing_raised do
ast.evaluate(scope)
end
@@ -138,7 +151,7 @@ class TestLangFunctions < Test::Unit::TestCase
template = tempfile()
File.open(template, "w") do |f|
- f.puts "template <%= yayness %>"
+ f.puts "template <%= @yay.nil?() ? raise('yay undefined') : @yay %>"
end
func = nil
@@ -158,17 +171,77 @@ class TestLangFunctions < Test::Unit::TestCase
ast.evaluate(scope)
end
- scope.setvar("yayness", "this is yayness")
+ scope.setvar("yay", "this is yay")
assert_nothing_raised do
ast.evaluate(scope)
end
- assert_equal("template this is yayness\n", scope.lookupvar("output"),
+ assert_equal("template this is yay\n", scope.lookupvar("output"),
"Templates were not handled correctly")
end
+ # Make sure that legacy template variable access works as expected.
+ def test_legacyvariables
+ template = tempfile()
+
+ File.open(template, "w") do |f|
+ f.puts "template <%= deprecated %>"
+ end
+
+ func = nil
+ assert_nothing_raised do
+ func = Puppet::Parser::AST::Function.new(
+ :name => "template",
+ :ftype => :rvalue,
+ :arguments => AST::ASTArray.new(
+ :children => [stringobj(template)]
+ )
+ )
+ end
+ ast = varobj("output", func)
+
+ # Verify that we get an exception using old-style accessors.
+ scope = mkscope
+ assert_raise(Puppet::ParseError) do
+ ast.evaluate(scope)
+ end
+
+ # Verify that we evaluate and return their value correctly.
+ scope.setvar("deprecated", "deprecated value")
+ assert_nothing_raised do
+ ast.evaluate(scope)
+ end
+
+ assert_equal("template deprecated value\n", scope.lookupvar("output"),
+ "Deprecated template variables were not handled correctly")
+ end
+
+ # Make sure that problems with kernel method visibility still exist.
+ def test_kernel_module_shadows_deprecated_var_lookup
+ template = tempfile()
+ File.open(template, "w").puts("<%= binding %>")
+
+ func = nil
+ assert_nothing_raised do
+ func = Puppet::Parser::AST::Function.new(
+ :name => "template",
+ :ftype => :rvalue,
+ :arguments => AST::ASTArray.new(
+ :children => [stringobj(template)]
+ )
+ )
+ end
+ ast = varobj("output", func)
+
+ # Verify that Kernel methods still shadow deprecated variable lookups.
+ scope = mkscope
+ assert_nothing_raised("No exception for Kernel shadowed variable names") do
+ ast.evaluate(scope)
+ end
+ end
+
def test_tempatefunction_cannot_see_scopes
template = tempfile()
@@ -243,7 +316,7 @@ class TestLangFunctions < Test::Unit::TestCase
template = tempfile()
File.open(template, "w") do |f|
- f.puts "template <%= yayness %>"
+ f.puts "template <%= @yayness %>"
end
func = nil
@@ -263,10 +336,6 @@ class TestLangFunctions < Test::Unit::TestCase
false => "false",
}.each do |string, value|
scope = mkscope
- assert_raise(Puppet::ParseError) do
- ast.evaluate(scope)
- end
-
scope.setvar("yayness", string)
assert_equal(string, scope.lookupvar("yayness", false))
diff --git a/test/language/scope.rb b/test/language/scope.rb
index c96581a23..0fa211fc3 100755
--- a/test/language/scope.rb
+++ b/test/language/scope.rb
@@ -53,6 +53,34 @@ class TestScope < Test::Unit::TestCase
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")
+
+
+ # Test that the scopes convert to hash structures correctly.
+ # For topscope recursive vs non-recursive should be identical
+ assert_equal(topscope.to_hash(false), topscope.to_hash(true),
+ "Recursive and non-recursive hash is identical for topscope")
+
+ # Check the variable we expect is present.
+ assert_equal({"first" => "topval"}, topscope.to_hash(),
+ "topscope returns the expected hash of variables")
+
+ # Now, check that midscope does the right thing in all cases.
+ assert_equal({"second" => "midval"},
+ midscope.to_hash(false),
+ "midscope non-recursive hash contains only midscope variable")
+ assert_equal({"first" => "topval", "second" => "midval"},
+ midscope.to_hash(true),
+ "midscope recursive hash contains topscope variable also")
+
+ # Finally, check the ability to shadow symbols by adding a shadow to
+ # bottomscope, then checking that we see the right stuff.
+ botscope.setvar("first", "shadowval")
+ assert_equal({"third" => "botval", "first" => "shadowval"},
+ botscope.to_hash(false),
+ "botscope has the right non-recursive hash")
+ assert_equal({"third" => "botval", "first" => "shadowval", "second" => "midval"},
+ botscope.to_hash(true),
+ "botscope values shadow parent scope values")
end
def test_lookupvar