diff options
author | Luke Kanies <luke@reductivelabs.com> | 2010-01-29 20:57:21 -0600 |
---|---|---|
committer | test branch <puppet-dev@googlegroups.com> | 2010-02-17 06:50:53 -0800 |
commit | 7089446697ad550c22012bc2b5572030727d67e1 (patch) | |
tree | 40d160f11839fe6e20311186ded4e621d23e1242 /test | |
parent | 4871c909cd28c82b64d0b62d8a27e62737d8733d (diff) | |
download | puppet-7089446697ad550c22012bc2b5572030727d67e1.tar.gz puppet-7089446697ad550c22012bc2b5572030727d67e1.tar.xz puppet-7089446697ad550c22012bc2b5572030727d67e1.zip |
Removing Resource::Reference classes
This commit is hopefully less messy than it
first appears, but it's certainly cross-cutting.
The reason for all of this is that we previously only
looked up builtin resource types from outside the parser,
but now that the defined resource types are available globally
via environments, we can push that lookup code to Resource.
Once we do that, however, we have to have environment and
namespace information in every resource.
Here I remove the Resource::Reference classes (except
the AST class), and use Resource instances instead. I
did this because the shared code between the two classes
got incredibly complicated, such that they should have had
a hierarchical relationship disallowed by their constants.
This complexity convinced me just to get rid of References
entirely.
I also make Puppet::Parser::Resource a subclass
of Puppet::Resource.
There are still broken tests in test/, but this was a big
enough commit I wanted to get it in.
Signed-off-by: Luke Kanies <luke@reductivelabs.com>
Diffstat (limited to 'test')
-rwxr-xr-x | test/language/functions.rb | 108 | ||||
-rwxr-xr-x | test/language/parser.rb | 339 | ||||
-rwxr-xr-x | test/language/resource.rb | 172 | ||||
-rwxr-xr-x | test/language/scope.rb | 207 | ||||
-rw-r--r-- | test/lib/puppettest/parsertesting.rb | 17 | ||||
-rw-r--r-- | test/lib/puppettest/resourcetesting.rb | 11 | ||||
-rwxr-xr-x | test/other/relationships.rb | 2 | ||||
-rwxr-xr-x | test/other/transactions.rb | 12 | ||||
-rwxr-xr-x | test/ral/type/exec.rb | 4 |
9 files changed, 73 insertions, 799 deletions
diff --git a/test/language/functions.rb b/test/language/functions.rb index af0732559..70605f8f6 100755 --- a/test/language/functions.rb +++ b/test/language/functions.rb @@ -289,16 +289,12 @@ class TestLangFunctions < Test::Unit::TestCase Puppet[:code] = %{file { "#{file}": content => template("#{template}") }} Puppet[:environment] = "yay" - interp = Puppet::Parser::Interpreter.new node = mknode - node.stubs(:environment).returns("yay") + node.stubs(:environment).returns Puppet::Node::Environment.new Puppet[:environment] = "yay" - catalog = nil - assert_nothing_raised { - catalog = interp.compile(node) - } + catalog = Puppet::Parser::Compiler.new(node).compile version = catalog.version @@ -317,7 +313,7 @@ class TestLangFunctions < Test::Unit::TestCase f.puts "new text" end - newversion = interp.compile(node).version + newversion = Puppet::Parser::Compiler.new(node).compile.version assert(version != newversion, "Parse date did not change") end @@ -385,100 +381,6 @@ class TestLangFunctions < Test::Unit::TestCase "Did not set function correctly") end - def test_realize - scope = mkscope - parser = scope.compiler.parser - - realize = Puppet::Parser::Functions.function(:realize) - - # Make a definition - 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 => scope) - - scope.compiler.add_resource(scope, virtual) - - ref = Puppet::Parser::Resource::Reference.new( - :type => type, :title => title, - :scope => scope - ) - # Now call the realize function - assert_nothing_raised do - scope.function_realize(ref) - end - - # Make sure it created a collection - assert_equal(1, scope.compiler.collections.length, - "Did not set collection") - - assert_nothing_raised do - scope.compiler.collections.each do |coll| coll.evaluate end - end - scope.compiler.collections.clear - - # Now make sure the virtual resource is no longer virtual - assert(! virtual.virtual?, "Did not make virtual resource real") - end - - # Make sure we puke on any resource that doesn't exist - none = Puppet::Parser::Resource::Reference.new( - :type => "file", :title => "/tmp/nosuchfile", - :scope => scope - ) - - # The function works - assert_nothing_raised do - scope.function_realize(none.to_s) - end - - # Make sure it created a collection - assert_equal(1, scope.compiler.collections.length, - "Did not set collection") - - # And the collection has our resource in it - assert_equal([none.to_s], scope.compiler.collections[0].resources, - "Did not set resources in collection") - end - - def test_defined - scope = mkscope - parser = scope.compiler.parser - - defined = Puppet::Parser::Functions.function(:defined) - - parser.newclass("yayness") - parser.newdefine("rahness") - - assert_nothing_raised do - assert(scope.function_defined("yayness"), "yayness class was not considered defined") - assert(scope.function_defined("rahness"), "rahness definition was not considered defined") - assert(scope.function_defined("service"), "service type was not considered defined") - assert(! scope.function_defined("fakness"), "fakeness was considered defined") - end - - # Now make sure any match in a list will work - assert(scope.function_defined(["booness", "yayness", "fakeness"]), - "A single answer was not sufficient to return true") - - # and make sure multiple falses are still false - assert(! scope.function_defined(%w{no otherno stillno}), - "Multiple falses were somehow true") - - # Now make sure we can test resources - scope.compiler.add_resource(scope, mkresource(:type => "file", :title => "/tmp/rahness", - :scope => scope, :source => scope.source, - :params => {:owner => "root"})) - - yep = Puppet::Parser::Resource::Reference.new(:type => "file", :title => "/tmp/rahness") - nope = Puppet::Parser::Resource::Reference.new(:type => "file", :title => "/tmp/fooness") - - assert(scope.function_defined([yep]), "valid resource was not considered defined") - assert(! scope.function_defined([nope]), "invalid resource was considered defined") - end - def test_search parser = mkparser scope = mkscope(:parser => parser) @@ -503,7 +405,7 @@ class TestLangFunctions < Test::Unit::TestCase def test_include scope = mkscope - parser = scope.compiler.parser + parser = mkparser include = Puppet::Parser::Functions.function(:include) @@ -569,7 +471,7 @@ class TestLangFunctions < Test::Unit::TestCase generate = Puppet::Parser::Functions.function(:generate) scope = mkscope - parser = scope.compiler.parser + parser = mkparser val = nil assert_nothing_raised("Could not call generator with no args") do diff --git a/test/language/parser.rb b/test/language/parser.rb index a3311f423..b721490c3 100755 --- a/test/language/parser.rb +++ b/test/language/parser.rb @@ -18,8 +18,14 @@ class TestParser < Test::Unit::TestCase #@lexer = Puppet::Parser::Lexer.new() end + def teardown + super + Puppet::Node::Environment.clear + end + def test_each_file textfiles { |file| + Puppet::Node::Environment.clear parser = mkparser Puppet.debug("parsing %s" % file) if __FILE__ == $0 assert_nothing_raised() { @@ -379,9 +385,7 @@ file { "/tmp/yayness": assert_instance_of(Puppet::Parser::AST::IfStatement, ret) parser = mkparser str2 = %{if true { #{exec.call("true")} } else { #{exec.call("false")} }} - assert_nothing_raised { - ret = parser.parse(str2).hostclass("").code[0] - } + ret = parser.parse(str2).hostclass("").code[0] assert_instance_of(Puppet::Parser::AST::IfStatement, ret) assert_instance_of(Puppet::Parser::AST::Else, ret.else) end @@ -410,7 +414,7 @@ file { "/tmp/yayness": } sub = parser.hostclass("container::one") assert(sub, "Could not find one") - assert_equal("container::deep::sub", sub.parentclass) + assert_equal("container::deep::sub", sub.parent) # Finally, try including a qualified class assert_nothing_raised("Could not include fully qualified class") { @@ -435,7 +439,7 @@ file { "/tmp/yayness": assert_nothing_raised do out = parser.parse "Exec { path => '/usr/bin:/usr/sbin' }" assert_instance_of(Puppet::Resource::TypeCollection, out) - assert_equal("", parser.hostclass("").classname) + assert_equal("", parser.hostclass("").name) assert_equal("", parser.hostclass("").namespace) end end @@ -520,6 +524,7 @@ file { "/tmp/yayness": end tests.each do |form| + Puppet::Node::Environment.clear parser = mkparser if form == :virtual @@ -549,6 +554,7 @@ file { "/tmp/yayness": def test_collectionexpressions %w{== !=}.each do |oper| + Puppet::Node::Environment.clear str = "File <| title #{oper} '/tmp/testing' |>" parser = mkparser @@ -616,30 +622,6 @@ file { "/tmp/yayness": end end - # We've had problems with files other than site.pp importing into main. - def test_importing_into_main - top = tempfile() - other = tempfile() - File.open(top, "w") do |f| - f.puts "import '#{other}'" - end - - file = tempfile() - File.open(other, "w") do |f| - f.puts "file { '#{file}': ensure => present }" - end - - Puppet[:manifest] = top - interp = Puppet::Parser::Interpreter.new - - code = nil - assert_nothing_raised do - code = interp.compile(mknode).extract.flatten - end - assert(code.length == 1, "Did not get the file") - assert_instance_of(Puppet::TransObject, code[0]) - end - def test_fully_qualified_definitions parser = mkparser @@ -803,196 +785,10 @@ file { "/tmp/yayness": end assert_instance_of(Puppet::Resource::TypeCollection, result, "Did not get a ASTSet back from parsing") - assert_instance_of(AST::HostClass, result.hostclass("yay"), "Did not create 'yay' class") - assert_instance_of(AST::HostClass, result.hostclass(""), "Did not create main class") - assert_instance_of(AST::Definition, result.definition("bar"), "Did not create 'bar' definition") - assert_instance_of(AST::Node, result.node("foo"), "Did not create 'foo' node") - end - - # Make sure our node gets added to the node table. - def test_newnode - parser = mkparser - - # First just try calling it directly - assert_nothing_raised { - parser.newnode("mynode", :code => :yay) - } - - assert_equal(:yay, parser.node("mynode").code) - - # Now make sure that trying to redefine it throws an error. - assert_raise(Puppet::ParseError) { - parser.newnode("mynode", {}) - } - - # Now try one with no code - assert_nothing_raised { - parser.newnode("simplenode", :parent => :foo) - } - - # Now define the parent node - parser.newnode(:foo) - - # And make sure we get things back correctly - assert_equal(:foo, parser.node("simplenode").parentclass) - assert_nil(parser.node("simplenode").code) - - # Now make sure that trying to redefine it throws an error. - assert_raise(Puppet::ParseError) { - parser.newnode("mynode", {}) - } - - # Test multiple names - names = ["one", "two", "three"] - assert_nothing_raised { - parser.newnode(names, {:code => :yay, :parent => :foo}) - } - - names.each do |name| - assert_equal(:yay, parser.node(name).code) - assert_equal(:foo, parser.node(name).parentclass) - # Now make sure that trying to redefine it throws an error. - assert_raise(Puppet::ParseError) { - parser.newnode(name, {}) - } - end - end - - def test_newdefine - parser = mkparser - - assert_nothing_raised { - parser.newdefine("mydefine", :code => :yay, - :arguments => ["a", stringobj("b")]) - } - - mydefine = parser.definition("mydefine") - assert(mydefine, "Could not find definition") - assert_equal("", mydefine.namespace) - assert_equal("mydefine", mydefine.classname) - - assert_raise(Puppet::ParseError) do - parser.newdefine("mydefine", :code => :yay, - :arguments => ["a", stringobj("b")]) - end - - # Now define the same thing in a different scope - assert_nothing_raised { - parser.newdefine("other::mydefine", :code => :other, - :arguments => ["a", stringobj("b")]) - } - other = parser.definition("other::mydefine") - assert(other, "Could not find definition") - assert(parser.definition("other::mydefine"), - "Could not find other::mydefine") - assert_equal(:other, other.code) - assert_equal("other", other.namespace) - assert_equal("other::mydefine", other.classname) - end - - def test_newclass - scope = mkscope - parser = scope.compiler.parser - - mkcode = proc do |ary| - classes = ary.collect do |string| - AST::FlatString.new(:value => string) - end - AST::ASTArray.new(:children => classes) - end - - - # First make sure that code is being appended - code = mkcode.call(%w{original code}) - - klass = nil - assert_nothing_raised { - klass = parser.newclass("myclass", :code => code) - } - - assert(klass, "Did not return class") - - assert(parser.hostclass("myclass"), "Could not find definition") - assert_equal("myclass", parser.hostclass("myclass").classname) - assert_equal(%w{original code}, - parser.hostclass("myclass").code.evaluate(scope)) - - # Newclass behaves differently than the others -- it just appends - # the code to the existing class. - code = mkcode.call(%w{something new}) - assert_nothing_raised do - klass = parser.newclass("myclass", :code => code) - end - assert(klass, "Did not return class when appending") - assert_equal(%w{original code something new}, - parser.hostclass("myclass").code.evaluate(scope)) - - # Now create the same class name in a different scope - assert_nothing_raised { - klass = parser.newclass("other::myclass", - :code => mkcode.call(%w{something diff})) - } - assert(klass, "Did not return class") - other = parser.hostclass("other::myclass") - assert(other, "Could not find class") - assert_equal("other::myclass", other.classname) - assert_equal("other::myclass", other.namespace) - assert_equal(%w{something diff}, - other.code.evaluate(scope)) - - # Make sure newclass deals correctly with nodes with no code - klass = parser.newclass("nocode") - assert(klass, "Did not return class") - - assert_nothing_raised do - klass = parser.newclass("nocode", :code => mkcode.call(%w{yay test})) - end - assert(klass, "Did not return class with no code") - assert_equal(%w{yay test}, - parser.hostclass("nocode").code.evaluate(scope)) - - # Then try merging something into nothing - parser.newclass("nocode2", :code => mkcode.call(%w{foo test})) - assert(klass, "Did not return class with no code") - - assert_nothing_raised do - klass = parser.newclass("nocode2") - end - assert(klass, "Did not return class with no code") - assert_equal(%w{foo test}, - parser.hostclass("nocode2").code.evaluate(scope)) - - # And lastly, nothing and nothing - klass = parser.newclass("nocode3") - assert(klass, "Did not return class with no code") - - assert_nothing_raised do - klass = parser.newclass("nocode3") - end - assert(klass, "Did not return class with no code") - assert_nil(parser.hostclass("nocode3").code) - end - - # Make sure you can't have classes and defines with the same name in the - # same scope. - def test_classes_beat_defines - parser = mkparser - - assert_nothing_raised { - parser.newclass("yay::funtest") - } - - assert_raise(Puppet::ParseError) do - parser.newdefine("yay::funtest") - end - - assert_nothing_raised { - parser.newdefine("yay::yaytest") - } - - assert_raise(Puppet::ParseError) do - parser.newclass("yay::yaytest") - end + assert_instance_of(Puppet::Resource::Type, result.hostclass("yay"), "Did not create 'yay' class") + assert_instance_of(Puppet::Resource::Type, result.hostclass(""), "Did not create main class") + assert_instance_of(Puppet::Resource::Type, result.definition("bar"), "Did not create 'bar' definition") + assert_instance_of(Puppet::Resource::Type, result.node("foo"), "Did not create 'foo' node") end def test_namesplit @@ -1009,50 +805,6 @@ file { "/tmp/yayness": end end - # Now make sure we get appropriate behaviour with parent class conflicts. - def test_newclass_parentage - parser = mkparser - parser.newclass("base1") - parser.newclass("one::two::three") - - # First create it with no parentclass. - assert_nothing_raised { - parser.newclass("sub") - } - assert(parser.hostclass("sub"), "Could not find definition") - assert_nil(parser.hostclass("sub").parentclass) - - # Make sure we can't set the parent class to ourself. - assert_raise(Puppet::ParseError) { - parser.newclass("sub", :parent => "sub") - } - - # Now create another one, with a parentclass. - assert_nothing_raised { - parser.newclass("sub", :parent => "base1") - } - - # Make sure we get the right parent class, and make sure it's not an object. - assert_equal("base1", - parser.hostclass("sub").parentclass) - - # Now make sure we get a failure if we try to conflict. - assert_raise(Puppet::ParseError) { - parser.newclass("sub", :parent => "one::two::three") - } - - # Make sure that failure didn't screw us up in any way. - assert_equal("base1", - parser.hostclass("sub").parentclass) - # But make sure we can create a class with a fq parent - assert_nothing_raised { - parser.newclass("another", :parent => "one::two::three") - } - assert_equal("one::two::three", - parser.hostclass("another").parentclass) - - end - # Setup a module. def mk_module(name, files = {}) mdir = File.join(@dir, name) @@ -1095,22 +847,22 @@ file { "/tmp/yayness": # Try to load the module automatically now klass = parser.find_hostclass("", name) - assert_instance_of(AST::HostClass, klass, "Did not autoload class from module init file") - assert_equal(name, klass.classname, "Incorrect class was returned") + assert_instance_of(Puppet::Resource::Type, klass, "Did not autoload class from module init file") + assert_equal(name, klass.name, "Incorrect class was returned") # Try loading the simple module when we're in something other than the base namespace. parser = mkparser klass = parser.find_hostclass("something::else", name) - assert_instance_of(AST::HostClass, klass, "Did not autoload class from module init file") - assert_equal(name, klass.classname, "Incorrect class was returned") + assert_instance_of(Puppet::Resource::Type, klass, "Did not autoload class from module init file") + assert_equal(name, klass.name, "Incorrect class was returned") # Now try it with a definition as the base file name = "simpdef" mk_module(name, :define => true, :init => [name]) klass = parser.find_definition("", name) - assert_instance_of(AST::Definition, klass, "Did not autoload class from module init file") - assert_equal(name, klass.classname, "Incorrect class was returned") + assert_instance_of(Puppet::Resource::Type, klass, "Did not autoload class from module init file") + assert_equal(name, klass.name, "Incorrect class was returned") # Now try it with namespace classes where both classes are in the init file parser = mkparser @@ -1120,14 +872,14 @@ file { "/tmp/yayness": # First try it with a namespace klass = parser.find_hostclass("both", name) - assert_instance_of(AST::HostClass, klass, "Did not autoload sub class from module init file with a namespace") - assert_equal("both::sub", klass.classname, "Incorrect class was returned") + assert_instance_of(Puppet::Resource::Type, klass, "Did not autoload sub class from module init file with a namespace") + assert_equal("both::sub", klass.name, "Incorrect class was returned") # Now try it using the fully qualified name parser = mkparser klass = parser.find_hostclass("", "both::sub") - assert_instance_of(AST::HostClass, klass, "Did not autoload sub class from module init file with no namespace") - assert_equal("both::sub", klass.classname, "Incorrect class was returned") + assert_instance_of(Puppet::Resource::Type, klass, "Did not autoload sub class from module init file with no namespace") + assert_equal("both::sub", klass.name, "Incorrect class was returned") # Now try it with the class in a different file @@ -1138,14 +890,14 @@ file { "/tmp/yayness": # First try it with a namespace klass = parser.find_hostclass("separate", name) - assert_instance_of(AST::HostClass, klass, "Did not autoload sub class from separate file with a namespace") - assert_equal("separate::sub", klass.classname, "Incorrect class was returned") + assert_instance_of(Puppet::Resource::Type, klass, "Did not autoload sub class from separate file with a namespace") + assert_equal("separate::sub", klass.name, "Incorrect class was returned") # Now try it using the fully qualified name parser = mkparser klass = parser.find_hostclass("", "separate::sub") - assert_instance_of(AST::HostClass, klass, "Did not autoload sub class from separate file with no namespace") - assert_equal("separate::sub", klass.classname, "Incorrect class was returned") + assert_instance_of(Puppet::Resource::Type, klass, "Did not autoload sub class from separate file with no namespace") + assert_equal("separate::sub", klass.name, "Incorrect class was returned") # Now make sure we don't get a failure when there's no module file parser = mkparser @@ -1157,22 +909,22 @@ file { "/tmp/yayness": assert_nothing_raised("Could not autoload file when module file is missing") do klass = parser.find_hostclass("alone", name) end - assert_instance_of(AST::HostClass, klass, "Did not autoload sub class from alone file with a namespace") - assert_equal("alone::sub", klass.classname, "Incorrect class was returned") + assert_instance_of(Puppet::Resource::Type, klass, "Did not autoload sub class from alone file with a namespace") + assert_equal("alone::sub", klass.name, "Incorrect class was returned") # Now try it using the fully qualified name parser = mkparser klass = parser.find_hostclass("", "alone::sub") - assert_instance_of(AST::HostClass, klass, "Did not autoload sub class from alone file with no namespace") - assert_equal("alone::sub", klass.classname, "Incorrect class was returned") + assert_instance_of(Puppet::Resource::Type, klass, "Did not autoload sub class from alone file with no namespace") + assert_equal("alone::sub", klass.name, "Incorrect class was returned") # and with the definition in its own file name = "mymod" mk_module(name, :define => true, :mydefine => ["mymod::mydefine"]) klass = parser.find_definition("", "mymod::mydefine") - assert_instance_of(AST::Definition, klass, "Did not autoload definition from its own file") - assert_equal("mymod::mydefine", klass.classname, "Incorrect definition was returned") + assert_instance_of(Puppet::Resource::Type, klass, "Did not autoload definition from its own file") + assert_equal("mymod::mydefine", klass.name, "Incorrect definition was returned") end # Make sure class, node, and define methods are case-insensitive @@ -1191,25 +943,4 @@ file { "/tmp/yayness": assert_equal(result, parser.find_definition("", "fUntEst"), "%s was not matched" % "fUntEst") end - - def test_manifests_with_multiple_environments - parser = mkparser :environment => "something" - - # We use an exception to cut short the processing to simplify our stubbing - #Puppet::Module.expects(:find_manifests).with("test", {:cwd => ".", :environment => "something"}).raises(Puppet::ParseError) - Puppet::Parser::Files.expects(:find_manifests).with("test", {:cwd => ".", :environment => "something"}).returns([]) - - assert_raise(Puppet::ImportError) do - parser.import("test") - end - end - - def test_watch_file_only_once - FileTest.stubs(:exists?).returns(true) - parser = mkparser - parser.watch_file("doh") - parser.watch_file("doh") - assert_equal(1, parser.files.select { |name, file| file.file == "doh" }.length, "Length of watched 'doh' files was not 1") - end end - diff --git a/test/language/resource.rb b/test/language/resource.rb deleted file mode 100755 index c1d116f9e..000000000 --- a/test/language/resource.rb +++ /dev/null @@ -1,172 +0,0 @@ -#!/usr/bin/env ruby - -require File.dirname(__FILE__) + '/../lib/puppettest' - -require 'puppettest' -require 'puppettest/resourcetesting' - -class TestResource < PuppetTest::TestCase - include PuppetTest - include PuppetTest::ParserTesting - include PuppetTest::ResourceTesting - Parser = Puppet::Parser - AST = Parser::AST - Resource = Puppet::Parser::Resource - Reference = Puppet::Parser::Resource::Reference - - def setup - super - Puppet[:trace] = false - end - - def teardown - mocha_verify - end - - # Make sure we paramcheck our params - def test_validate - res = mkresource - params = res.instance_variable_get("@params") - params[:one] = :two - params[:three] = :four - res.expects(:paramcheck).with(:one) - res.expects(:paramcheck).with(:three) - res.send(:validate) - end - - def test_set_parameter - res = mkresource - params = res.instance_variable_get("@params") - - # First test the simple case: It's already a parameter - param = stub('param', :name => "pname") - param.expects(:is_a?).with(Resource::Param).returns(true) - res.send(:set_parameter, param) - assert_equal(param, params["pname"], "Parameter was not added to hash") - - # Now the case where there's no value but it's not a param - param = mock('param') - param.expects(:is_a?).with(Resource::Param).returns(false) - assert_raise(ArgumentError, "Did not fail when a non-param was passed") do - res.send(:set_parameter, param) - end - - # and the case where a value is passed in - param = stub :name => "pname", :value => "whatever" - Resource::Param.expects(:new).with(:name => "pname", :value => "myvalue", :source => res.source).returns(param) - res.send(:set_parameter, "pname", "myvalue") - assert_equal(param, params["pname"], "Did not put param in hash") - end - - def test_paramcheck - # There are three cases here: - - # It's a valid parameter - res = mkresource - ref = mock('ref') - res.instance_variable_set("@ref", ref) - klass = mock("class") - ref.expects(:typeclass).returns(klass).times(4) - klass.expects(:valid_parameter?).with("good").returns(true) - assert(res.send(:paramcheck, :good), "Did not allow valid param") - - # It's name or title - klass.expects(:valid_parameter?).with("name").returns(false) - assert(res.send(:paramcheck, :name), "Did not allow name") - klass.expects(:valid_parameter?).with("title").returns(false) - assert(res.send(:paramcheck, :title), "Did not allow title") - - # It's not actually allowed - klass.expects(:valid_parameter?).with("other").returns(false) - res.expects(:fail) - ref.expects(:type) - res.send(:paramcheck, :other) - end - - def test_evaluate - # 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") - - type.expects(:evaluate_code).with(res) - - res.evaluate - end - - def test_proxymethods - res = Parser::Resource.new :type => "evaltest", :title => "yay", - :source => mock("source"), :scope => mkscope - - assert_equal("Evaltest", res.type) - assert_equal("yay", res.title) - assert_equal(false, res.builtin?) - end - - # This is a bit of a weird one -- the user should not actually know - # that components exist, so we want references to act like they're not - # builtin - def test_components_are_not_builtin - ref = Parser::Resource::Reference.new(:type => "component", :title => "yay") - - assert_nil(ref.builtintype, "Definition was considered builtin") - end - - # The second part of #539 - make sure resources pass the arguments - # correctly. - def test_title_with_definitions - parser = mkparser - define = parser.newdefine "yayness", - :code => resourcedef("file", "/tmp", - "owner" => varref("name"), "mode" => varref("title")) - - - klass = parser.find_hostclass("", "") - should = {:name => :owner, :title => :mode} - [ - {:name => "one", :title => "two"}, - {:title => "three"}, - ].each do |hash| - config = mkcompiler parser - args = {:type => "yayness", :title => hash[:title], - :source => klass, :scope => config.topscope} - if hash[:name] - args[:params] = {:name => hash[:name]} - else - args[:params] = {} # override the defaults - end - - res = nil - assert_nothing_raised("Could not create res with %s" % hash.inspect) do - res = mkresource(args) - end - assert_nothing_raised("Could not eval res with %s" % hash.inspect) do - res.evaluate - end - - 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], - "%s was not set correctly with %s" % [param, hash.inspect]) - end - end - end - - # 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 => mock("source"), :scope => mkscope, - :params => {:owner => :undef, :mode => "755"} - - hash = nil - assert_nothing_raised("Could not convert resource with undef to hash") do - hash = res.to_hash - end - - assert_nil(hash[:owner], "got a value for an undef parameter") - end -end diff --git a/test/language/scope.rb b/test/language/scope.rb index 8819de66e..b1ba63de4 100755 --- a/test/language/scope.rb +++ b/test/language/scope.rb @@ -20,6 +20,11 @@ class TestScope < Test::Unit::TestCase include PuppetTest::ParserTesting include PuppetTest::ResourceTesting + def setup + Puppet::Node::Environment.clear + super + end + def to_ary(hash) hash.collect { |key,value| [key,value] @@ -102,60 +107,6 @@ class TestScope < Test::Unit::TestCase } end - def test_setdefaults - config = mkcompiler - - scope = config.topscope - - defaults = scope.instance_variable_get("@defaults") - - # First the case where there are no defaults and we pass a single param - 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", :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", :file => "f", :line => "l" - 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 - - def test_lookupdefaults - config = mkcompiler - top = config.topscope - - # Make a subscope - sub = config.newscope(top) - - topdefs = top.instance_variable_get("@defaults") - subdefs = sub.instance_variable_get("@defaults") - - # First add some defaults to our top scope - topdefs[:t1] = {:p1 => :p2, :p3 => :p4} - topdefs[:t2] = {:p5 => :p6} - - # Then the sub scope - subdefs[:t1] = {:p1 => :p7, :p8 => :p9} - subdefs[:t2] = {:p5 => :p10, :p11 => :p12} - - # 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 - def test_parent config = mkcompiler top = config.topscope @@ -167,86 +118,6 @@ class TestScope < Test::Unit::TestCase assert_equal(top, sub.parent, "Did not find parent scope on second call") end - def test_strinterp - # Make and evaluate our classes so the qualified lookups work - parser = mkparser - klass = parser.newclass("") - scope = mkscope(:parser => parser) - Puppet::Parser::Resource.new(:type => "class", :title => :main, :scope => scope, :source => mock('source')).evaluate - - assert_nothing_raised { - scope.setvar("test","value") - } - - scopes = {"" => scope} - - %w{one one::two one::two::three}.each do |name| - klass = parser.newclass(name) - Puppet::Parser::Resource.new(:type => "class", :title => name, :scope => scope, :source => mock('source')).evaluate - scopes[name] = scope.compiler.class_scope(klass) - scopes[name].setvar("test", "value-%s" % name.sub(/.+::/,'')) - end - - assert_equal("value", scope.lookupvar("::test"), "did not look up qualified value correctly") - tests = { - "string ${test}" => "string value", - "string ${one::two::three::test}" => "string value-three", - "string $one::two::three::test" => "string value-three", - "string ${one::two::test}" => "string value-two", - "string $one::two::test" => "string value-two", - "string ${one::test}" => "string value-one", - "string $one::test" => "string value-one", - "string ${::test}" => "string value", - "string $::test" => "string value", - "string ${test} ${test} ${test}" => "string value value value", - "string $test ${test} $test" => "string value value value", - "string \\$test" => "string $test", - '\\$test string' => "$test string", - '$test string' => "value string", - 'a testing $' => "a testing $", - 'a testing \$' => "a testing $", - "an escaped \\\n carriage return" => "an escaped carriage return", - '\$' => "$", - '\s' => "\s", - '\t' => "\t", - '\n' => "\n" - } - - tests.each do |input, output| - assert_nothing_raised("Failed to scan %s" % input.inspect) do - assert_equal(output, scope.strinterp(input), - 'did not parserret %s correctly' % input.inspect) - end - end - - logs = [] - Puppet::Util::Log.close - Puppet::Util::Log.newdestination(logs) - - # #523 - %w{d f h l w z}.each do |l| - string = "\\" + l - assert_nothing_raised do - assert_equal(string, scope.strinterp(string), - 'did not parserret %s correctly' % string) - end - - assert(logs.detect { |m| m.message =~ /Unrecognised escape/ }, - "Did not get warning about escape sequence with %s" % string) - logs.clear - end - end - - def test_tagfunction - Puppet::Parser::Functions.function(:tag) - scope = mkscope - resource = mock 'resource' - scope.resource = resource - resource.expects(:tag).with("yayness", "booness") - - scope.function_tag(%w{yayness booness}) - end - def test_includefunction parser = mkparser scope = mkscope :parser => parser @@ -270,7 +141,7 @@ class TestScope < Test::Unit::TestCase [myclass, otherclass].each do |klass| assert(scope.compiler.class_scope(klass), - "%s was not set" % klass.classname) + "%s was not set" % klass.name) end end @@ -312,13 +183,14 @@ class TestScope < Test::Unit::TestCase # components. def test_virtual_definitions_do_not_get_evaluated config = mkcompiler - parser = config.parser + parser = mkparser # Create a default source - config.topscope.source = parser.newclass "" + parser.newclass("") + config.topscope.source = parser.known_resource_types.hostclass("") # And a scope resource - scope_res = stub 'scope_resource', :virtual? => true, :exported? => false, :tags => [], :builtin? => true, :type => "eh", :title => "bee" + scope_res = Puppet::Parser::Resource.new(:file, "/file", :scope => "scope", :source => "source") config.topscope.resource = scope_res args = AST::ASTArray.new( @@ -374,20 +246,13 @@ include yay @@host { puppet: ip => \"192.168.0.3\" } Host <<||>>" - interp = nil - assert_nothing_raised { - interp = Puppet::Parser::Interpreter.new - } - config = 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} + node.merge "hostname" => node.name 2.times { |i| - assert_nothing_raised { - config = interp.compile(node) - } + config = Puppet::Parser::Compiler.new(node).compile flat = config.extract.flatten @@ -418,54 +283,6 @@ Host <<||>>" "Did not add extra namespace correctly") end - def test_find_hostclass_and_find_definition - parser = mkparser - - # Make sure our scope calls the parser find_hostclass method with - # the right namespaces - scope = mkscope :parser => parser - - parser.metaclass.send(:attr_accessor, :last) - - methods = [:find_hostclass, :find_definition] - methods.each do |m| - parser.meta_def(m) do |namespace, name| - @checked ||= [] - @checked << [namespace, name] - - # Only return a value on the last call. - if @last == namespace - ret = @checked.dup - @checked.clear - return ret - else - return nil - end - end - end - - test = proc do |should| - parser.last = scope.namespaces[-1] - methods.each do |method| - result = scope.send(method, "testing") - assert_equal(should, result, - "did not get correct value from %s with namespaces %s" % - [method, scope.namespaces.inspect]) - end - end - - # Start with the empty namespace - assert_nothing_raised { test.call([["", "testing"]]) } - - # Now add a namespace - scope.add_namespace("a") - assert_nothing_raised { test.call([["a", "testing"]]) } - - # And another - scope.add_namespace("b") - assert_nothing_raised { test.call([["a", "testing"], ["b", "testing"]]) } - end - # #629 - undef should be "" or :undef def test_lookupvar_with_undef scope = mkscope diff --git a/test/lib/puppettest/parsertesting.rb b/test/lib/puppettest/parsertesting.rb index dee38eb3a..44c78f2c3 100644 --- a/test/lib/puppettest/parsertesting.rb +++ b/test/lib/puppettest/parsertesting.rb @@ -47,9 +47,8 @@ module PuppetTest::ParserTesting end def mkcompiler(parser = nil) - parser ||= mkparser node = mknode - return Compiler.new(node, parser) + return Compiler.new(node) end def mknode(name = nil) @@ -59,12 +58,9 @@ module PuppetTest::ParserTesting Puppet::Node.new(name) end - def mkinterp - Puppet::Parser::Interpreter.new - end - - def mkparser(args = {}) - Puppet::Parser::Parser.new(args) + def mkparser + Puppet::Node::Environment.clear + Puppet::Parser::Parser.new(Puppet::Node::Environment.new) end def mkscope(hash = {}) @@ -306,13 +302,10 @@ module PuppetTest::ParserTesting interp = nil oldmanifest = Puppet[:manifest] Puppet[:manifest] = manifest - assert_nothing_raised { - interp = Puppet::Parser::Interpreter.new - } trans = nil assert_nothing_raised { - trans = interp.compile(mknode) + trans = Puppet::Parser::Compiler.new(mknode).compile } config = nil diff --git a/test/lib/puppettest/resourcetesting.rb b/test/lib/puppettest/resourcetesting.rb index d4469a203..95fe5bcb7 100644 --- a/test/lib/puppettest/resourcetesting.rb +++ b/test/lib/puppettest/resourcetesting.rb @@ -15,9 +15,12 @@ module PuppetTest::ResourceTesting args[:source] ||= "source" args[:scope] ||= mkscope - {:type => "resource", :title => "testing", - :source => "source", :scope => "scope"}.each do |param, value| - args[param] ||= value + type = args[:type] || "resource" + title = args[:title] || "testing" + args.delete(:type) + args.delete(:title) + {:source => "source", :scope => "scope"}.each do |param, value| + args[param] ||= value end params = args[:params] || {:one => "yay", :three => "rah"} @@ -27,7 +30,7 @@ module PuppetTest::ResourceTesting args[:params] = paramify args[:source], params end - Parser::Resource.new(args) + Parser::Resource.new(type, title, args) end def param(name, value, source) diff --git a/test/other/relationships.rb b/test/other/relationships.rb index b15ff5062..e9d3a9376 100755 --- a/test/other/relationships.rb +++ b/test/other/relationships.rb @@ -80,7 +80,7 @@ class TestRelationships < Test::Unit::TestCase # Testing #411. It was a problem with builddepends. def test_missing_deps - file = Puppet::Type.type(:file).new :path => tempfile, :require => Puppet::Resource::Reference.new("file", "/no/such/file") + file = Puppet::Type.type(:file).new :path => tempfile, :require => Puppet::Resource.new("file", "/no/such/file") assert_raise(Puppet::Error) do file.builddepends diff --git a/test/other/transactions.rb b/test/other/transactions.rb index 94deeab26..6f4f5d913 100755 --- a/test/other/transactions.rb +++ b/test/other/transactions.rb @@ -125,12 +125,12 @@ class TestTransactions < Test::Unit::TestCase file = Puppet::Type.type(:file).new(:title => "file", :path => path, :content => "yayness") first = Puppet::Type.type(:exec).new(:title => "first", :command => "/bin/echo first > #{firstpath}", - :subscribe => Puppet::Resource::Reference.new(:file, path), + :subscribe => Puppet::Resource.new(:file, path), :refreshonly => true ) second = Puppet::Type.type(:exec).new(:title => "second", :command => "/bin/echo second > #{secondpath}", - :subscribe => Puppet::Resource::Reference.new(:exec, "first"), + :subscribe => Puppet::Resource.new(:exec, "first"), :refreshonly => true ) @@ -218,7 +218,7 @@ class TestTransactions < Test::Unit::TestCase @@tmpfiles << execfile # 'subscribe' expects an array of arrays - exec[:subscribe] = Puppet::Resource::Reference.new(file.class.name,file.name) + exec[:subscribe] = Puppet::Resource.new(file.class.name,file.name) exec[:refreshonly] = true assert_nothing_raised() { @@ -302,13 +302,13 @@ class TestTransactions < Test::Unit::TestCase :path => ENV["PATH"], :command => "touch %s" % file1, :refreshonly => true, - :subscribe => Puppet::Resource::Reference.new(:file, path) + :subscribe => Puppet::Resource.new(:file, path) ) exec2 = Puppet::Type.type(:exec).new( :path => ENV["PATH"], :command => "touch %s" % file2, :refreshonly => true, - :subscribe => Puppet::Resource::Reference.new(:file, path) + :subscribe => Puppet::Resource.new(:file, path) ) assert_apply(file, exec1, exec2) @@ -361,7 +361,7 @@ class TestTransactions < Test::Unit::TestCase :name => "touch %s" % fname, :path => "/usr/bin:/bin", :schedule => "monthly", - :subscribe => Puppet::Resource::Reference.new("file", file.name) + :subscribe => Puppet::Resource.new("file", file.name) ) config = mk_catalog(file, exec) diff --git a/test/ral/type/exec.rb b/test/ral/type/exec.rb index c533aff1c..27a3de4d1 100755 --- a/test/ral/type/exec.rb +++ b/test/ral/type/exec.rb @@ -181,7 +181,7 @@ class TestExec < Test::Unit::TestCase exec = Puppet::Type.type(:exec).new( :command => oexe, - :require => Puppet::Resource::Reference.new(:file, oexe) + :require => Puppet::Resource.new(:file, oexe) ) comp = mk_catalog("Testing", file, exec) @@ -404,7 +404,7 @@ class TestExec < Test::Unit::TestCase :path => basedir, :recurse => true, :mode => "755", - :require => Puppet::Resource::Reference.new("exec", "mkdir") + :require => Puppet::Resource.new("exec", "mkdir") ) } |