summaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorLuke Kanies <luke@madstop.com>2007-09-03 18:01:00 -0500
committerLuke Kanies <luke@madstop.com>2007-09-03 18:01:00 -0500
commitb021587e309f237bd16bd4f5cc51e79266cbd222 (patch)
treeb46de15108c7f1fcf50b67a54bcb8f528854e566 /test
parent25f6d7c521cb0189cf691fb1c4bce4b675568300 (diff)
downloadpuppet-b021587e309f237bd16bd4f5cc51e79266cbd222.tar.gz
puppet-b021587e309f237bd16bd4f5cc51e79266cbd222.tar.xz
puppet-b021587e309f237bd16bd4f5cc51e79266cbd222.zip
Doing a small amount of refactoring, toward being able to use Parser resources to evaluate classes and nodes, not just definitions. This will hopefully simplify some of the parsing work, and it will enable the use of a Configuration object that more completely models a configuration.
Diffstat (limited to 'test')
-rwxr-xr-xtest/language/ast/component.rb156
-rwxr-xr-xtest/language/ast/definition.rb77
-rwxr-xr-xtest/language/ast/hostclass.rb5
-rwxr-xr-xtest/language/resource.rb8
4 files changed, 43 insertions, 203 deletions
diff --git a/test/language/ast/component.rb b/test/language/ast/component.rb
deleted file mode 100755
index cf0cce976..000000000
--- a/test/language/ast/component.rb
+++ /dev/null
@@ -1,156 +0,0 @@
-#!/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 TestASTComponent < 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/definition.rb b/test/language/ast/definition.rb
index 1bbc1a099..5a2e6ffea 100755
--- a/test/language/ast/definition.rb
+++ b/test/language/ast/definition.rb
@@ -49,24 +49,17 @@ class TestASTDefinition < Test::Unit::TestCase
"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")
-
+ resource = stub 'resource',
+ :title => "first",
+ :name => "first",
+ :type => "yayness",
+ :to_hash => {"mode" => "755"},
+ :exported => false,
+ :virtual => false
+
+ resource.stubs(:title)
assert_nothing_raised do
- klass.evaluate_resource(:scope => scope,
- :title => "first",
- :arguments => {"mode" => "755"}
- )
+ klass.evaluate(:scope => scope, :resource => resource)
end
firstobj = config.findresource("File[/tmp/first]")
@@ -79,18 +72,20 @@ class TestASTDefinition < Test::Unit::TestCase
# 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"}
- )
+ klass.evaluate(:scope => scope, :resource => resource)
end
# Now create another with different args
+ resource2 = stub 'resource',
+ :title => "second",
+ :name => "second",
+ :type => "yayness",
+ :to_hash => {"mode" => "755", "owner" => "daemon"},
+ :exported => false,
+ :virtual => false
+
assert_nothing_raised do
- klass.evaluate_resource(:scope => scope,
- :title => "second",
- :arguments => {"mode" => "755", "owner" => "daemon"}
- )
+ klass.evaluate(:scope => scope, :resource => resource2)
end
secondobj = config.findresource("File[/tmp/second]")
@@ -104,32 +99,39 @@ class TestASTDefinition < Test::Unit::TestCase
# #539 - definitions should support both names and titles
def test_names_and_titles
- parser, scope, source = mkclassframing
+ parser = mkparser
+ scope = mkscope :parser => parser
[
- {:name => "one", :title => "two"},
- {:title => "mytitle"},
+ {:name => "one", :title => "two"},
+ {:title => "mytitle"}
].each_with_index do |hash, i|
-
- # Create a definition that uses both name and title
+ # Create a definition that uses both name and title. Put this
+ # inside the loop so the subscope expectations work.
klass = parser.newdefine "yayness%s" % i
subscope = klass.subscope(scope, "yayness%s" % i)
klass.expects(:subscope).returns(subscope)
- args = {:title => hash[:title]}
+ resource = stub 'resource',
+ :title => hash[:title],
+ :name => hash[:name] || hash[:title],
+ :type => "yayness%s" % i,
+ :to_hash => {},
+ :exported => false,
+ :virtual => false
+
if hash[:name]
- args[:arguments] = {:name => hash[:name]}
+ resource.stubs(:to_hash).returns({:name => hash[:name]})
end
- args[:scope] = scope
+
assert_nothing_raised("Could not evaluate definition with %s" % hash.inspect) do
- klass.evaluate_resource(args)
+ klass.evaluate(:scope => scope, :resource => resource)
end
name = hash[:name] || hash[:title]
title = hash[:title]
- args[:name] ||= name
assert_equal(name, subscope.lookupvar("name"),
"Name did not get set correctly")
@@ -137,9 +139,9 @@ class TestASTDefinition < Test::Unit::TestCase
"title did not get set correctly")
[:name, :title].each do |param|
- val = args[param]
+ val = resource.send(param)
assert(subscope.tags.include?(val),
- "Scope was not tagged with %s" % val)
+ "Scope was not tagged with %s '%s'" % [param, val])
end
end
end
@@ -153,4 +155,3 @@ class TestASTDefinition < Test::Unit::TestCase
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 1d2121dad..62483730b 100755
--- a/test/language/ast/hostclass.rb
+++ b/test/language/ast/hostclass.rb
@@ -132,6 +132,7 @@ class TestASTHostClass < Test::Unit::TestCase
scope = mkscope
parser = scope.compile.parser
+ source = parser.newclass ""
parser.newclass("base")
fun = parser.newdefine("base::fun")
parser.newclass("middle", :parent => "base")
@@ -140,7 +141,7 @@ class TestASTHostClass < Test::Unit::TestCase
ret = nil
assert_nothing_raised do
- ret = scope.compile.evaluate_classes(["sub"], scope)
+ ret = scope.compile.evaluate_classes(["sub"], source)
end
subscope = scope.class_scope(scope.findclass("sub"))
@@ -162,5 +163,3 @@ class TestASTHostClass < Test::Unit::TestCase
assert(fun == result, "found incorrect parent-defined definition from sub")
end
end
-
-# $Id$
diff --git a/test/language/resource.rb b/test/language/resource.rb
index 320b958ff..892d8c293 100755
--- a/test/language/resource.rb
+++ b/test/language/resource.rb
@@ -254,12 +254,8 @@ class TestResource < PuppetTest::TestCase
res.scope.expects(:compile).returns(config)
config.expects(:delete_resource).with(res)
- 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)
+ args = {:scope => res.scope, :resource => res}
+ type.expects(:evaluate).with(args)
res.evaluate
end