3
require File.dirname(__FILE__) + '/../../../spec_helper'
5
describe Puppet::Parser::AST::Definition, "when initializing" do
8
describe Puppet::Parser::AST::Definition, "when evaluating" do
10
@type = Puppet::Parser::Resource
11
@parser = Puppet::Parser::Parser.new :Code => ""
12
@source = @parser.newclass ""
13
@definition = @parser.newdefine "mydefine"
14
@node = Puppet::Node.new("yaynode")
15
@compiler = Puppet::Parser::Compiler.new(@node, @parser)
16
@scope = @compiler.topscope
18
@resource = Puppet::Parser::Resource.new(:type => "mydefine", :title => "myresource", :scope => @scope, :source => @source)
21
it "should create a new scope" do
24
code.expects(:safeevaluate).with do |scope|
25
scope.object_id.should_not == @scope.object_id
28
@definition.stubs(:code).returns(code)
29
@definition.evaluate_code(@resource)
32
# it "should copy its namespace to the scope"
34
# it "should mark the scope virtual if the resource is virtual"
36
# it "should mark the scope exported if the resource is exported"
38
# it "should set the resource's parameters as variables in the scope"
40
# it "should set the resource's title as a variable in the scope"
42
# it "should copy the resource's title in a 'name' variable in the scope"
44
# it "should not copy the resource's title as the name if 'name' is one of the resource parameters"
46
# it "should evaluate the associated code with the new scope"
48
def old_test_initialize
51
# Create a new definition
52
klass = parser.newdefine "yayness",
53
:arguments => [["owner", stringobj("nobody")], %w{mode}],
54
:code => AST::ASTArray.new(
55
:children => [resourcedef("file", "/tmp/$name",
56
"owner" => varref("owner"), "mode" => varref("mode"))]
59
# Test validattr? a couple different ways
60
[:owner, "owner", :schedule, "schedule"].each do |var|
61
assert(klass.validattr?(var), "%s was not considered valid" % var.inspect)
64
[:random, "random"].each do |var|
65
assert(! klass.validattr?(var), "%s was considered valid" % var.inspect)
73
config.send(:evaluate_main)
74
scope = config.topscope
75
klass = parser.newdefine "yayness",
76
:arguments => [["owner", stringobj("nobody")], %w{mode}],
77
:code => AST::ASTArray.new(
78
:children => [resourcedef("file", "/tmp/$name",
79
"owner" => varref("owner"), "mode" => varref("mode"))]
82
resource = Puppet::Parser::Resource.new(
88
:source => scope.source
90
resource.send(:set_parameter, "name", "first")
91
resource.send(:set_parameter, "mode", "755")
93
resource.stubs(:title)
94
assert_nothing_raised do
95
klass.evaluate_code(resource)
98
firstobj = config.findresource("File[/tmp/first]")
99
assert(firstobj, "Did not create /tmp/first obj")
101
assert_equal("File", firstobj.type)
102
assert_equal("/tmp/first", firstobj.title)
103
assert_equal("nobody", firstobj[:owner])
104
assert_equal("755", firstobj[:mode])
106
# Make sure we can't evaluate it with the same args
107
assert_raise(Puppet::ParseError) do
108
klass.evaluate_code(resource)
111
# Now create another with different args
112
resource2 = Puppet::Parser::Resource.new(
118
:source => scope.source
120
resource2.send(:set_parameter, "name", "second")
121
resource2.send(:set_parameter, "mode", "755")
122
resource2.send(:set_parameter, "owner", "daemon")
124
assert_nothing_raised do
125
klass.evaluate_code(resource2)
128
secondobj = config.findresource("File[/tmp/second]")
129
assert(secondobj, "Did not create /tmp/second obj")
131
assert_equal("File", secondobj.type)
132
assert_equal("/tmp/second", secondobj.title)
133
assert_equal("daemon", secondobj[:owner])
134
assert_equal("755", secondobj[:mode])
137
# #539 - definitions should support both names and titles
138
def oldtest_names_and_titles
140
scope = mkscope :parser => parser
143
{:name => "one", :title => "two"},
144
{:title => "mytitle"}
145
].each_with_index do |hash, i|
146
# Create a definition that uses both name and title. Put this
147
# inside the loop so the subscope expectations work.
148
klass = parser.newdefine "yayness%s" % i
150
resource = Puppet::Parser::Resource.new(
151
:title => hash[:title],
152
:type => "yayness%s" % i,
156
:source => scope.source
159
subscope = klass.subscope(scope, resource)
161
klass.expects(:subscope).returns(subscope)
164
resource.stubs(:to_hash).returns({:name => hash[:name]})
167
assert_nothing_raised("Could not evaluate definition with %s" % hash.inspect) do
168
klass.evaluate_code(resource)
171
name = hash[:name] || hash[:title]
174
assert_equal(name, subscope.lookupvar("name"),
175
"Name did not get set correctly")
176
assert_equal(title, subscope.lookupvar("title"),
177
"title did not get set correctly")
179
[:name, :title].each do |param|
180
val = resource.send(param)
181
assert(subscope.tags.include?(val),
182
"Scope was not tagged with %s '%s'" % [param, val])
187
# Testing the root cause of #615. We should be using the fqname for the type, instead
188
# of just the short name.
189
def oldtest_fully_qualified_types
191
klass = parser.newclass("one::two")
193
assert_equal("one::two", klass.classname, "Class did not get fully qualified class name")