3
$:.unshift("../lib").unshift("../../lib") if __FILE__ =~ /\.rb$/
3
require File.dirname(__FILE__) + '/../lib/puppettest'
6
6
require 'puppettest/resourcetesting'
7
require 'puppettest/railstesting'
9
class TestResource < Test::Unit::TestCase
8
class TestResource < PuppetTest::TestCase
11
10
include PuppetTest::ParserTesting
12
11
include PuppetTest::ResourceTesting
13
include PuppetTest::RailsTesting
14
12
Parser = Puppet::Parser
14
Resource = Puppet::Parser::Resource
15
Reference = Puppet::Parser::Resource::Reference
19
19
Puppet[:trace] = false
20
@interp, @scope, @source = mkclassframing
24
args = {:type => "resource", :title => "testing",
25
:source => @source, :scope => @scope}
26
# Check our arg requirements
27
args.each do |name, value|
30
assert_raise(Puppet::DevError) do
31
Parser::Resource.new(try)
35
args[:params] = paramify @source, :one => "yay", :three => "rah"
38
assert_nothing_raised do
39
res = Parser::Resource.new(args)
42
# Make sure it got the parameters correctly.
43
assert_equal("yay", res[:one])
44
assert_equal("rah", res[:three])
46
assert_equal({:one => "yay", :three => "rah"}, res.to_hash)
52
# Now verify we can't override with any random class
53
assert_raise(Puppet::ParseError) do
54
res.set paramify(@scope.findclass("other"), "one" => "boo").shift
57
# And that we can with a subclass
58
assert_nothing_raised do
59
res.set paramify(@scope.findclass("sub1"), "one" => "boo").shift
62
# And that a different subclass can override a different parameter
63
assert_nothing_raised do
64
res.set paramify(@scope.findclass("sub2"), "three" => "boo").shift
67
# But not the same one
68
assert_raise(Puppet::ParseError) do
69
res.set paramify(@scope.findclass("sub2"), "one" => "something").shift
74
# Start with the normal one
77
# Now create a resource from a different scope
78
other = mkresource :source => other, :params => {"one" => "boo"}
80
# Make sure we can't merge it
81
assert_raise(Puppet::ParseError) do
85
# Make one from a subscope
86
other = mkresource :source => "sub1", :params => {"one" => "boo"}
89
assert_nothing_raised do
93
assert_equal("boo", res["one"])
26
# Make sure we paramcheck our params
29
params = res.instance_variable_get("@params")
31
params[:three] = :four
32
res.expects(:paramcheck).with(:one)
33
res.expects(:paramcheck).with(:three)
37
def test_set_parameter
39
params = res.instance_variable_get("@params")
41
# First test the simple case: It's already a parameter
43
param.expects(:is_a?).with(Resource::Param).returns(true)
44
param.expects(:name).returns("pname")
45
res.send(:set_parameter, param)
46
assert_equal(param, params["pname"], "Parameter was not added to hash")
48
# Now the case where there's no value but it's not a param
50
param.expects(:is_a?).with(Resource::Param).returns(false)
51
assert_raise(ArgumentError, "Did not fail when a non-param was passed") do
52
res.send(:set_parameter, param)
55
# and the case where a value is passed in
56
param = stub :name => "pname", :value => "whatever"
57
Resource::Param.expects(:new).with(:name => "pname", :value => "myvalue", :source => res.source).returns(param)
58
res.send(:set_parameter, "pname", "myvalue")
59
assert_equal(param, params["pname"], "Did not put param in hash")
96
62
def test_paramcheck
97
# First make a builtin resource
99
assert_nothing_raised do
100
res = Parser::Resource.new :type => "file", :title => tempfile(),
101
:source => @source, :scope => @scope
104
%w{path group source schedule subscribe}.each do |param|
105
assert_nothing_raised("Param %s was considered invalid" % param) do
106
res.paramcheck(param)
110
%w{this bad noness}.each do |param|
111
assert_raise(Puppet::ParseError, "%s was considered valid" % param) do
112
res.paramcheck(param)
116
# Now create a defined resource
117
assert_nothing_raised do
118
res = Parser::Resource.new :type => "resource", :title => "yay",
119
:source => @source, :scope => @scope
122
%w{one two three schedule subscribe}.each do |param|
123
assert_nothing_raised("Param %s was considered invalid" % param) do
124
res.paramcheck(param)
128
%w{this bad noness}.each do |param|
129
assert_raise(Puppet::ParseError, "%s was considered valid" % param) do
130
res.paramcheck(param)
63
# There are three cases here:
65
# It's a valid parameter
68
res.instance_variable_set("@ref", ref)
70
ref.expects(:typeclass).returns(klass).times(4)
71
klass.expects(:validattr?).with("good").returns(true)
72
assert(res.send(:paramcheck, :good), "Did not allow valid param")
75
klass.expects(:validattr?).with("name").returns(false)
76
assert(res.send(:paramcheck, :name), "Did not allow name")
77
klass.expects(:validattr?).with("title").returns(false)
78
assert(res.send(:paramcheck, :title), "Did not allow title")
80
# It's not actually allowed
81
klass.expects(:validattr?).with("other").returns(false)
84
res.send(:paramcheck, :other)
136
# First try translating a builtin resource
87
def test_to_transobject
88
# First try translating a builtin resource. Make sure we use some references
89
# and arrays, to make sure they translate correctly.
90
source = mock("source")
92
scope.stubs(:tags).returns([])
94
4.times { |i| refs << Puppet::Parser::Resource::Reference.new(:title => "file%s" % i, :type => "file") }
137
95
res = Parser::Resource.new :type => "file", :title => "/tmp",
138
:source => @source, :scope => @scope,
139
:params => paramify(@source, :owner => "nobody", :mode => "644")
96
:source => source, :scope => scope,
97
:params => paramify(source, :owner => "nobody", :group => %w{you me},
98
:require => refs[0], :ignore => %w{svn},
99
:subscribe => [refs[1], refs[2]], :notify => [refs[3]])
142
102
assert_nothing_raised do
146
106
assert_instance_of(Puppet::TransObject, obj)
148
assert_equal(obj.type, res.type)
108
assert_equal(obj.type, res.type.downcase)
149
109
assert_equal(obj.name, res.title)
151
111
# TransObjects use strings, resources use symbols
152
hash = obj.to_hash.inject({}) { |h,a| h[a[0].intern] = a[1]; h }
153
assert_equal(hash, res.to_hash)
112
assert_equal("nobody", obj["owner"], "Single-value string was not passed correctly")
113
assert_equal(%w{you me}, obj["group"], "Array of strings was not passed correctly")
114
assert_equal("svn", obj["ignore"], "Array with single string was not turned into single value")
115
assert_equal(["file", refs[0].title], obj["require"], "Resource reference was not passed correctly")
116
assert_equal([["file", refs[1].title], ["file", refs[2].title]], obj["subscribe"], "Array of resource references was not passed correctly")
117
assert_equal(["file", refs[3].title], obj["notify"], "Array with single resource reference was not turned into single value")
157
# Set some defaults at the top level
158
top = {:one => "fun", :two => "shoe"}
160
@scope.setdefaults("resource", paramify(@source, top))
162
# Make a resource at that level
163
res = Parser::Resource.new :type => "resource", :title => "yay",
164
:source => @source, :scope => @scope
167
assert_nothing_raised do
171
# And make sure we got them
173
assert_equal(v, res[p])
176
# Now got a bit lower
177
other = @scope.newscope
179
# And create a resource
180
lowerres = Parser::Resource.new :type => "resource", :title => "funtest",
181
:source => @source, :scope => other
183
assert_nothing_raised do
189
assert_equal(v, lowerres[p])
192
# Now add some of our own defaults
193
lower = {:one => "shun", :three => "free"}
194
other.setdefaults("resource", paramify(@source, lower))
195
otherres = Parser::Resource.new :type => "resource", :title => "yaytest",
196
:source => @source, :scope => other
199
# Make sure the lower defaults beat the higher ones.
200
lower.each do |p, v| should[p] = v end
205
assert_equal(v, otherres[p])
120
# FIXME This isn't a great test, but I need to move on.
121
def test_to_transbucket
122
bucket = mock("transbucket")
123
source = mock("source")
125
res = Parser::Resource.new :type => "mydefine", :title => "yay",
126
:source => source, :scope => scope
129
result = res.to_trans
130
assert_equal("yay", result.name, "did not set bucket name correctly")
131
assert_equal("Mydefine", result.type, "did not set bucket type correctly")
209
134
def test_evaluate
210
# Make a definition that we know will, um, do something
211
@interp.newdefine "evaltest",
212
:arguments => [%w{one}, ["two", stringobj("755")]],
213
:code => resourcedef("file", "/tmp",
214
"owner" => varref("one"), "mode" => varref("two"))
216
res = Parser::Resource.new :type => "evaltest", :title => "yay",
217
:source => @source, :scope => @scope,
218
:params => paramify(@source, :one => "nobody")
222
assert_nothing_raised do
226
# Make sure we can find our object now
227
result = @scope.findresource("file[/tmp]")
229
# Now make sure we got the code we expected.
230
assert_instance_of(Puppet::Parser::Resource, result)
232
assert_equal("file", result.type)
233
assert_equal("/tmp", result.title)
234
assert_equal("nobody", result["owner"])
235
assert_equal("755", result["mode"])
237
# And that we cannot find the old resource
238
assert_nil(@scope.findresource("evaltest[yay]"),
239
"Evaluated resource was not deleted")
242
def test_addoverrides
243
# First create an override for an object that doesn't yet exist
244
over1 = mkresource :source => "sub1", :params => {:one => "yay"}
246
assert_nothing_raised do
247
@scope.setoverride(over1)
250
assert(over1.override, "Override was not marked so")
252
# Now make the resource
253
res = mkresource :source => "base", :params => {:one => "rah",
256
# And add it to our scope
257
@scope.setresource(res)
259
# And make sure over1 has not yet taken affect
260
assert_equal("foo", res[:three], "Lost value")
262
# Now add an immediately binding override
263
over2 = mkresource :source => "sub1", :params => {:three => "yay"}
265
assert_nothing_raised do
266
@scope.setoverride(over2)
269
# And make sure it worked
270
assert_equal("yay", res[:three], "Override 2 was ignored")
272
# Now add our late-binding override
273
assert_nothing_raised do
277
# And make sure they're still around
278
assert_equal("yay", res[:one], "Override 1 lost")
279
assert_equal("yay", res[:three], "Override 2 lost")
281
# And finally, make sure that there are no remaining overrides
282
assert_nothing_raised do
135
# First try the most common case, we're not a builtin type.
137
ref = res.instance_variable_get("@ref")
139
ref.expects(:definedtype).returns(type)
141
res.scope = mock("scope")
143
type.expects(:evaluate_code).with(res)
287
148
def test_proxymethods
288
149
res = Parser::Resource.new :type => "evaltest", :title => "yay",
289
:source => @source, :scope => @scope
150
:source => mock("source"), :scope => mkscope
291
assert_equal("evaltest", res.type)
152
assert_equal("Evaltest", res.type)
292
153
assert_equal("yay", res.title)
293
154
assert_equal(false, res.builtin?)
296
def test_addmetaparams
298
res = Parser::Resource.new :type => "evaltest", :title => "yay",
299
:source => @source, :scope => @scope,
300
:params => paramify(@source, :tag => "yay")
302
assert_nil(res[:schedule], "Got schedule already")
303
assert_nothing_raised do
306
@scope.setvar("schedule", "daily")
308
# This is so we can test that it won't override already-set metaparams
309
@scope.setvar("tag", "funtest")
311
assert_nothing_raised do
315
assert_equal("daily", res[:schedule], "Did not get metaparam")
316
assert_equal("yay", res[:tag], "Overrode explicitly-set metaparam")
317
assert_nil(res[:noop], "Got invalid metaparam")
320
157
def test_reference_conversion
321
158
# First try it as a normal string
322
159
ref = Parser::Resource::Reference.new(:type => "file", :title => "/tmp/ref1")
358
197
def test_components_are_not_builtin
359
198
ref = Parser::Resource::Reference.new(:type => "component", :title => "yay")
361
assert_nil(ref.builtintype, "Component was considered builtin")
363
if defined? ActiveRecord::Base
200
assert_nil(ref.builtintype, "Definition was considered builtin")
203
# The second part of #539 - make sure resources pass the arguments
205
def test_title_with_definitions
207
define = parser.newdefine "yayness",
208
:code => resourcedef("file", "/tmp",
209
"owner" => varref("name"), "mode" => varref("title"))
212
klass = parser.findclass("", "")
213
should = {:name => :owner, :title => :mode}
215
{:name => "one", :title => "two"},
218
config = mkcompiler parser
219
args = {:type => "yayness", :title => hash[:title],
220
:source => klass, :scope => config.topscope}
222
args[:params] = {:name => hash[:name]}
224
args[:params] = {} # override the defaults
228
assert_nothing_raised("Could not create res with %s" % hash.inspect) do
229
res = mkresource(args)
231
assert_nothing_raised("Could not eval res with %s" % hash.inspect) do
235
made = config.topscope.findresource("File[/tmp]")
236
assert(made, "Did not create resource with %s" % hash.inspect)
237
should.each do |orig, param|
238
assert_equal(hash[orig] || hash[:title], made[param],
239
"%s was not set correctly with %s" % [param, hash.inspect])
244
# part of #629 -- the undef keyword. Make sure 'undef' params get skipped.
245
def test_undef_and_to_hash
366
246
res = mkresource :type => "file", :title => "/tmp/testing",
367
:source => @source, :scope => @scope,
368
:params => {:owner => "root", :mode => "755"}
370
# We also need a Rails Host to store under
371
host = Puppet::Rails::Host.new(:name => Facter.hostname)
374
assert_nothing_raised do
375
obj = res.store(host)
378
assert_instance_of(Puppet::Rails::RailsResource, obj)
380
assert_nothing_raised do
381
Puppet::Util.benchmark(:info, "Saved host") do
386
# Now make sure we can find it again
387
assert_nothing_raised do
388
obj = Puppet::Rails::RailsResource.find_by_host_id_and_restype_and_title(
389
host.id, res.type, res.title
392
assert_instance_of(Puppet::Rails::RailsResource, obj)
394
# Make sure we get the parameters back
395
obj.rails_parameters.each do |param|
396
assert_equal(res[param[:name]], param[:value],
397
"%s was different" % param[:name])
247
:source => mock("source"), :scope => mkscope,
248
:params => {:owner => :undef, :mode => "755"}
251
assert_nothing_raised("Could not convert resource with undef to hash") do
255
assert_nil(hash[:owner], "got a value for an undef parameter")
258
# #643 - Make sure virtual defines result in virtual resources
259
def test_virtual_defines
261
define = parser.newdefine("yayness",
262
:code => resourcedef("file", varref("name"),
265
config = mkcompiler(parser)
267
res = mkresource :type => "yayness", :title => "foo", :params => {}, :scope => config.topscope
271
assert_nothing_raised("Could not evaluate defined resource") do
272
result = res.evaluate
276
newres = scope.findresource("File[foo]")
277
assert(newres, "Could not find resource")
279
assert(newres.virtual?, "Virtual defined resource generated non-virtual resources")
281
# Now try it with exported resources
282
res = mkresource :type => "yayness", :title => "bar", :params => {}, :scope => config.topscope
286
assert_nothing_raised("Could not evaluate exported resource") do
287
result = res.evaluate
291
newres = scope.findresource("File[bar]")
292
assert(newres, "Could not find resource")
294
assert(newres.exported?, "Exported defined resource generated non-exported resources")
295
assert(newres.virtual?, "Exported defined resource generated non-virtual resources")
403
# $Id: resource.rb 1869 2006-11-13 06:11:39Z luke $