diff options
| author | Luke Kanies <luke@madstop.com> | 2007-08-14 00:09:49 -0500 |
|---|---|---|
| committer | Luke Kanies <luke@madstop.com> | 2007-08-14 00:09:49 -0500 |
| commit | aab419b8c1ad84e51c6f58839290bbe5d1e7b28b (patch) | |
| tree | 2447b704e0b601ffe10562d9eb83e6c9280366ba /test/language/node.rb | |
| parent | ab42534ae243c24c8c702e38195a954ab52eaed9 (diff) | |
An intermediate commit in the work towards adding multi-environment support.
This has required splitting the interpreter up considerably, which is much
cleaner but is a large project. There is now a 'nodes' handler, but it is
currently non-functional, although all the support structure is there. It just
needs to have the individual methods fleshed out, and it needs to be connected
to the 'facts' handler.
Diffstat (limited to 'test/language/node.rb')
| -rwxr-xr-x | test/language/node.rb | 207 |
1 files changed, 106 insertions, 101 deletions
diff --git a/test/language/node.rb b/test/language/node.rb index 61983df92..4cbba8063 100755 --- a/test/language/node.rb +++ b/test/language/node.rb @@ -2,125 +2,130 @@ $:.unshift("../lib").unshift("../../lib") if __FILE__ =~ /\.rb$/ -require 'puppet' -require 'puppet/parser/parser' +require 'mocha' require 'puppettest' +require 'puppettest/parsertesting' +require 'puppet/parser/configuration' -class TestParser < Test::Unit::TestCase +# Test our configuration object. +class TestConfiguration < Test::Unit::TestCase + include PuppetTest include PuppetTest::ParserTesting - def setup - super - Puppet[:parseonly] = true + Config = Puppet::Parser::Configuration + Scope = Puppet::Parser::Scope + + def mkconfig + Config.new(:host => "foo", :interpreter => "interp") + end + + def test_initialize + # Make sure we get an error if we don't send an interpreter + assert_raise(ArgumentError, "Did not fail when missing host") do + Config.new(:interpreter => "yay" ) + end + assert_raise(ArgumentError, "Did not fail when missing interp") do + Config.new(:host => "foo") + end + + # Now check the defaults + config = nil + assert_nothing_raised("Could not init config with all required options") do + config = Config.new(:host => "foo", :interpreter => "interp") + end + + assert_equal("foo", config.host, "Did not set host correctly") + assert_equal("interp", config.interpreter, "Did not set interpreter correctly") + assert_equal({}, config.facts, "Did not set default facts") + + # Now make a new one with facts, to make sure the facts get set appropriately + assert_nothing_raised("Could not init config with all required options") do + config = Config.new(:host => "foo", :interpreter => "interp", :facts => {"a" => "b"}) + end + assert_equal({"a" => "b"}, config.facts, "Did not set facts") end - def test_simple_hostname - check_parseable "host1" - check_parseable "'host2'" - check_parseable "\"host3\"" - check_parseable [ "'host1'", "host2" ] - check_parseable [ "'host1'", "'host2'" ] - check_parseable [ "'host1'", "\"host2\"" ] - check_parseable [ "\"host1\"", "host2" ] - check_parseable [ "\"host1\"", "'host2'" ] - check_parseable [ "\"host1\"", "\"host2\"" ] + def test_initvars + config = mkconfig + [:class_scopes, :resource_table, :exported_resources, :resource_overrides].each do |table| + assert_instance_of(Hash, config.send(:instance_variable_get, "@#{table}"), "Did not set %s table correctly" % table) + end end - def test_qualified_hostname - check_parseable "'host.example.com'" - check_parseable "\"host.example.com\"" - check_parseable [ "'host.example.com'", "host1" ] - check_parseable [ "\"host.example.com\"", "host1" ] - check_parseable "'host-1.37examples.example.com'" - check_parseable "\"host-1.37examples.example.com\"" - check_parseable "'svn.23.nu'" - check_parseable "\"svn.23.nu\"" - check_parseable "'HOST'" - check_parseable "\"HOST\"" + # Make sure we store and can retrieve references to classes and their scopes. + def test_class_set_and_class_scope + klass = Object.new + klass.expects(:classname).returns("myname") + + config = mkconfig + + assert_nothing_raised("Could not set class") do + config.class_set "myname", "myscope" + end + # First try to retrieve it by name. + assert_equal("myscope", config.class_scope("myname"), "Could not retrieve class scope by name") + + # Then by object + assert_equal("myscope", config.class_scope(klass), "Could not retrieve class scope by object") end - def test_inherits_from_default - check_parseable(["default", "host1"], "node default {}\nnode host1 inherits default {}") + def test_classlist + config = mkconfig + + config.class_set "", "empty" + config.class_set "one", "yep" + config.class_set "two", "nope" + + # Make sure our class list is correct + assert_equal(%w{one two}.sort, config.classlist.sort, "Did not get correct class list") end - def test_reject_hostname - check_nonparseable "host.example.com" - check_nonparseable "host@example.com" - check_nonparseable "'$foo.example.com'" - check_nonparseable "\"$foo.example.com\"" - check_nonparseable "'host1 host2'" - check_nonparseable "\"host1 host2\"" - check_nonparseable "HOST" + # Make sure collections get added to our internal array + def test_add_collection + config = mkconfig + assert_nothing_raised("Could not add collection") do + config.add_collection "nope" + end + assert_equal(%w{nope}, config.instance_variable_get("@collections"), "Did not add collection") end - AST = Puppet::Parser::AST + # Make sure we create a graph of scopes. + def test_newscope + config = mkconfig + graph = config.instance_variable_get("@graph") + assert_instance_of(Scope, config.topscope, "Did not create top scope") + assert_instance_of(GRATR::Digraph, graph, "Did not create graph") + + assert(graph.vertex?(config.topscope), "The top scope is not a vertex in the graph") - def check_parseable(hostnames, code = nil) - unless hostnames.is_a?(Array) - hostnames = [ hostnames ] + # Now that we've got the top scope, create a new, subscope + subscope = nil + assert_nothing_raised("Could not create subscope") do + subscope = config.newscope end - interp = nil - code ||= "node #{hostnames.join(", ")} { }" - parser = mkparser - parser.string = code - # Strip quotes - hostnames.map! { |s| s.sub(/^['"](.*)['"]$/, "\\1") } - - # parse - assert_nothing_raised("Could not parse '%s'" % code) { - parser.parse - } - - # Now make sure we can look up each of the names - hostnames.each do |name| - assert(parser.findnode(name), - "Could not find node %s" % name.inspect) + assert_instance_of(Scope, subscope, "Did not create subscope") + assert(graph.edge?(config.topscope, subscope), "An edge between top scope and subscope was not added") + + # Make sure a scope can find its parent. + assert(config.parent(subscope), "Could not look up parent scope on configuration") + assert_equal(config.topscope.object_id, config.parent(subscope).object_id, "Did not get correct parent scope from configuration") + assert_equal(config.topscope.object_id, subscope.parent.object_id, "Scope did not correctly retrieve its parent scope") + + # Now create another, this time specifying the parent scope + another = nil + assert_nothing_raised("Could not create subscope") do + another = config.newscope(subscope) end - end + assert_instance_of(Scope, another, "Did not create second subscope") + assert(graph.edge?(subscope, another), "An edge between parent scope and second subscope was not added") - def check_nonparseable(hostname) - interp = nil - parser = mkparser - parser.string = "node #{hostname} { }" - assert_raise(Puppet::DevError, Puppet::ParseError, "#{hostname} passed") { - parser.parse - } - end + # Make sure it can find its parent. + assert(config.parent(another), "Could not look up parent scope of second subscope on configuration") + assert_equal(subscope.object_id, config.parent(another).object_id, "Did not get correct parent scope of second subscope from configuration") + assert_equal(subscope.object_id, another.parent.object_id, "Second subscope did not correctly retrieve its parent scope") - # Make sure we can find default nodes if there's no other entry - def test_default_node - Puppet[:parseonly] = false - - fileA = tempfile() - fileB = tempfile() - code = %{ -node mynode { - file { "#{fileA}": ensure => file } -} - -node default { - file { "#{fileB}": ensure => file } -} -} - interp = nil - assert_nothing_raised { - interp = mkinterp :Code => code - } - - # First make sure it parses - assert_nothing_raised { - interp.send(:parsefiles) - } - - # Make sure we find our normal node - assert(interp.nodesearch("mynode"), - "Did not find normal node") - - # Now look for the default node - default = interp.nodesearch("someother") - assert(default, - "Did not find default node") - - assert_equal("default", default.classname) + # And make sure both scopes show up in the right order in the search path + assert_equal([another.object_id, subscope.object_id, config.topscope.object_id], another.scope_path.collect { |p| p.object_id }, + "Did not get correct scope path") end end |
