diff options
author | Luke Kanies <luke@madstop.com> | 2007-09-01 09:51:03 -0500 |
---|---|---|
committer | Luke Kanies <luke@madstop.com> | 2007-09-01 09:51:03 -0500 |
commit | 25f6d7c521cb0189cf691fb1c4bce4b675568300 (patch) | |
tree | 582dd091cbaf62135ce7e81902bb582f154c363f /test | |
parent | 62806bb8749d001354078f176fad8a0a54316efb (diff) | |
download | puppet-25f6d7c521cb0189cf691fb1c4bce4b675568300.tar.gz puppet-25f6d7c521cb0189cf691fb1c4bce4b675568300.tar.xz puppet-25f6d7c521cb0189cf691fb1c4bce4b675568300.zip |
Deleting old documentation that somehow made it back into the tree in the switch to git, and refactoring the evaluate_classes method on the compile object so I can use resources as intermediaries, thus making classes do late-binding evaluation.
Diffstat (limited to 'test')
-rwxr-xr-x | test/language/ast/definition.rb | 156 | ||||
-rwxr-xr-x | test/language/ast/hostclass.rb | 2 | ||||
-rwxr-xr-x | test/language/compile.rb | 18 | ||||
-rwxr-xr-x | test/language/parser.rb | 4 | ||||
-rwxr-xr-x | test/language/resource.rb | 2 | ||||
-rwxr-xr-x | test/language/snippets.rb | 14 |
6 files changed, 187 insertions, 9 deletions
diff --git a/test/language/ast/definition.rb b/test/language/ast/definition.rb new file mode 100755 index 000000000..1bbc1a099 --- /dev/null +++ b/test/language/ast/definition.rb @@ -0,0 +1,156 @@ +#!/usr/bin/env ruby +# +# Created by Luke A. Kanies on 2006-02-20. +# Copyright (c) 2006. All rights reserved. + +$:.unshift("../../lib") if __FILE__ =~ /\.rb$/ + +require 'puppettest' +require 'mocha' +require 'puppettest/parsertesting' +require 'puppettest/resourcetesting' + +class TestASTDefinition < Test::Unit::TestCase + include PuppetTest + include PuppetTest::ParserTesting + include PuppetTest::ResourceTesting + AST = Puppet::Parser::AST + + def test_initialize + parser = mkparser + + # Create a new definition + 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"))] + ) + + # Test validattr? a couple different ways + [:owner, "owner", :schedule, "schedule"].each do |var| + assert(klass.validattr?(var), "%s was not considered valid" % var.inspect) + end + + [: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, "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(config.findresource("File[/tmp/bad]"), + "Made file with invalid params") + + assert_nothing_raised do + klass.evaluate_resource(:scope => scope, + :title => "first", + :arguments => {"mode" => "755"} + ) + end + + firstobj = config.findresource("File[/tmp/first]") + assert(firstobj, "Did not create /tmp/first obj") + + assert_equal("file", firstobj.type) + assert_equal("/tmp/first", firstobj.title) + assert_equal("nobody", firstobj[:owner]) + assert_equal("755", firstobj[:mode]) + + # Make sure we can't evaluate it with the same args + assert_raise(Puppet::ParseError) do + klass.evaluate_resource(:scope => scope, + :title => "first", + :arguments => {"mode" => "755"} + ) + end + + # Now create another with different args + assert_nothing_raised do + klass.evaluate_resource(:scope => scope, + :title => "second", + :arguments => {"mode" => "755", "owner" => "daemon"} + ) + end + + secondobj = config.findresource("File[/tmp/second]") + assert(secondobj, "Did not create /tmp/second obj") + + assert_equal("file", secondobj.type) + assert_equal("/tmp/second", secondobj.title) + assert_equal("daemon", secondobj[:owner]) + assert_equal("755", secondobj[:mode]) + end + + # #539 - definitions should support both names and titles + def test_names_and_titles + parser, scope, source = mkclassframing + + [ + {:name => "one", :title => "two"}, + {:title => "mytitle"}, + ].each_with_index do |hash, i| + + # Create a definition that uses both name and title + klass = parser.newdefine "yayness%s" % i + + subscope = klass.subscope(scope, "yayness%s" % i) + + klass.expects(:subscope).returns(subscope) + + args = {:title => hash[:title]} + if hash[:name] + args[:arguments] = {:name => hash[:name]} + end + args[:scope] = scope + assert_nothing_raised("Could not evaluate definition with %s" % hash.inspect) do + klass.evaluate_resource(args) + end + + name = hash[:name] || hash[:title] + title = hash[:title] + args[:name] ||= name + + assert_equal(name, subscope.lookupvar("name"), + "Name did not get set correctly") + assert_equal(title, subscope.lookupvar("title"), + "title did not get set correctly") + + [:name, :title].each do |param| + val = args[param] + assert(subscope.tags.include?(val), + "Scope was not tagged with %s" % val) + end + end + end + + # Testing the root cause of #615. We should be using the fqname for the type, instead + # of just the short name. + def test_fully_qualified_types + parser = mkparser + klass = parser.newclass("one::two") + + assert_equal("one::two", klass.classname, "Class did not get fully qualified class name") + end +end +# $Id$ diff --git a/test/language/ast/hostclass.rb b/test/language/ast/hostclass.rb index a91d4bb97..1d2121dad 100755 --- a/test/language/ast/hostclass.rb +++ b/test/language/ast/hostclass.rb @@ -140,7 +140,7 @@ class TestASTHostClass < Test::Unit::TestCase ret = nil assert_nothing_raised do - ret = scope.compile.evaluate_classes(["sub"]) + ret = scope.compile.evaluate_classes(["sub"], scope) end subscope = scope.class_scope(scope.findclass("sub")) diff --git a/test/language/compile.rb b/test/language/compile.rb index 90cbc292e..5fde2500e 100755 --- a/test/language/compile.rb +++ b/test/language/compile.rb @@ -148,7 +148,7 @@ class TestCompile < Test::Unit::TestCase # The heart of the action. def test_compile config = mkconfig - [:set_node_parameters, :evaluate_main, :evaluate_ast_node, :evaluate_classes, :evaluate_generators, :fail_on_unevaluated, :finish].each do |method| + [:set_node_parameters, :evaluate_main, :evaluate_ast_node, :evaluate_node_classes, :evaluate_generators, :fail_on_unevaluated, :finish].each do |method| config.expects(method) end config.expects(:extract).returns(:config) @@ -268,6 +268,17 @@ class TestCompile < Test::Unit::TestCase assert_equal(%w{one two three}, config.send(:tags), "Allowed duplicate tag") end + def test_evaluate_node_classes + config = mkconfig + main = mock 'main' + config.parser.expects(:findclass).with("", "").returns(main) + @node.classes = %w{one two three four} + config.expects(:evaluate_classes).with(%w{one two three four}, main) + assert_nothing_raised("could not call evaluate_node_classes") do + config.send(:evaluate_node_classes) + end + end + def test_evaluate_classes config = mkconfig classes = { @@ -286,10 +297,9 @@ class TestCompile < Test::Unit::TestCase config.expects(:tag).with("two") config.expects(:tag).with("four") - @node.classes = %w{one two three four} result = nil - assert_nothing_raised("could not call evaluate_classes") do - result = config.send(:evaluate_classes) + assert_nothing_raised("could not call evaluate_node_classes") do + result = config.send(:evaluate_classes, %w{one two three four}, config.topscope) end assert_equal(%w{one three}, result, "Did not return the list of evaluated classes") end diff --git a/test/language/parser.rb b/test/language/parser.rb index 7c43746e7..0406e99ba 100755 --- a/test/language/parser.rb +++ b/test/language/parser.rb @@ -785,7 +785,7 @@ file { "/tmp/yayness": assert_instance_of(AST::HostClass, result.classes["yay"], "Did not create 'yay' class") assert_instance_of(AST::HostClass, result.classes[""], "Did not create main class") - assert_instance_of(AST::Component, result.definitions["bar"], "Did not create 'bar' definition") + assert_instance_of(AST::Definition, result.definitions["bar"], "Did not create 'bar' definition") assert_instance_of(AST::Node, result.nodes["foo"], "Did not create 'foo' node") end @@ -1118,7 +1118,7 @@ file { "/tmp/yayness": mk_module(name, :define => true, :init => [name]) klass = parser.finddefine("", name) - assert_instance_of(AST::Component, klass, "Did not autoload class from module init file") + assert_instance_of(AST::Definition, klass, "Did not autoload class from module init file") assert_equal(name, klass.classname, "Incorrect class was returned") # Now try it with namespace classes where both classes are in the init file diff --git a/test/language/resource.rb b/test/language/resource.rb index 37b0b7e1e..320b958ff 100755 --- a/test/language/resource.rb +++ b/test/language/resource.rb @@ -362,7 +362,7 @@ class TestResource < PuppetTest::TestCase def test_components_are_not_builtin ref = Parser::Resource::Reference.new(:type => "component", :title => "yay") - assert_nil(ref.builtintype, "Component was considered builtin") + assert_nil(ref.builtintype, "Definition was considered builtin") end # The second part of #539 - make sure resources pass the arguments diff --git a/test/language/snippets.rb b/test/language/snippets.rb index 5fb11e8cd..c3c60e77f 100755 --- a/test/language/snippets.rb +++ b/test/language/snippets.rb @@ -11,7 +11,6 @@ require 'puppettest' class TestSnippets < Test::Unit::TestCase include PuppetTest - include ObjectSpace def setup super @@ -458,15 +457,28 @@ class TestSnippets < Test::Unit::TestCase #eval("alias %s %s" % [testname, mname]) testname = ("test_" + mname).intern self.send(:define_method, testname) { + facts = { + "hostname" => "testhost", + "domain" => "domain.com", + "ipaddress" => "127.0.0.1", + "fqdn" => "testhost.domain.com" + } + Facter.stubs(:each) + facts.each do |name, value| + Facter.stubs(:value).with(name).returns(value) + end # first parse the file server = Puppet::Network::Handler.master.new( :Manifest => snippet(file), :Local => true ) + server.send(:fact_handler).stubs(:set) + server.send(:fact_handler).stubs(:get).returns(facts) client = Puppet::Network::Client.master.new( :Master => server, :Cache => false ) + client.class.stubs(:facts).returns(facts) assert(client.local) assert_nothing_raised { |