1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
|
#!/usr/bin/env ruby
$:.unshift("../lib").unshift("../../lib") if __FILE__ =~ /\.rb$/
require 'puppettest'
require 'puppettest/resourcetesting'
require 'puppettest/railstesting'
class TestResource < Test::Unit::TestCase
include PuppetTest
include PuppetTest::ParserTesting
include PuppetTest::ResourceTesting
include PuppetTest::RailsTesting
Parser = Puppet::Parser
AST = Parser::AST
def setup
super
Puppet[:trace] = false
@interp, @scope, @source = mkclassframing
end
def test_initialize
args = {:type => "resource", :title => "testing",
:source => @source, :scope => @scope}
# Check our arg requirements
args.each do |name, value|
try = args.dup
try.delete(name)
assert_raise(Puppet::DevError) do
Parser::Resource.new(try)
end
end
args[:params] = paramify @source, :one => "yay", :three => "rah"
res = nil
assert_nothing_raised do
res = Parser::Resource.new(args)
end
# Make sure it got the parameters correctly.
assert_equal("yay", res[:one])
assert_equal("rah", res[:three])
assert_equal({:one => "yay", :three => "rah"}, res.to_hash)
end
def test_override
res = mkresource
# Now verify we can't override with any random class
assert_raise(Puppet::ParseError) do
res.set paramify(@scope.findclass("other"), "one" => "boo").shift
end
# And that we can with a subclass
assert_nothing_raised do
res.set paramify(@scope.findclass("sub1"), "one" => "boo").shift
end
# And that a different subclass can override a different parameter
assert_nothing_raised do
res.set paramify(@scope.findclass("sub2"), "three" => "boo").shift
end
# But not the same one
assert_raise(Puppet::ParseError) do
res.set paramify(@scope.findclass("sub2"), "one" => "something").shift
end
end
def test_merge
# Start with the normal one
res = mkresource
# Now create a resource from a different scope
other = mkresource :source => other, :params => {"one" => "boo"}
# Make sure we can't merge it
assert_raise(Puppet::ParseError) do
res.merge(other)
end
# Make one from a subscope
other = mkresource :source => "sub1", :params => {"one" => "boo"}
# Make sure it merges
assert_nothing_raised do
res.merge(other)
end
assert_equal("boo", res["one"])
end
def test_paramcheck
# First make a builtin resource
res = nil
assert_nothing_raised do
res = Parser::Resource.new :type => "file", :title => tempfile(),
:source => @source, :scope => @scope
end
%w{path group source schedule subscribe}.each do |param|
assert_nothing_raised("Param %s was considered invalid" % param) do
res.paramcheck(param)
end
end
%w{this bad noness}.each do |param|
assert_raise(Puppet::ParseError, "%s was considered valid" % param) do
res.paramcheck(param)
end
end
# Now create a defined resource
assert_nothing_raised do
res = Parser::Resource.new :type => "resource", :title => "yay",
:source => @source, :scope => @scope
end
%w{one two three schedule subscribe}.each do |param|
assert_nothing_raised("Param %s was considered invalid" % param) do
res.paramcheck(param)
end
end
%w{this bad noness}.each do |param|
assert_raise(Puppet::ParseError, "%s was considered valid" % param) do
res.paramcheck(param)
end
end
end
def test_to_trans
# First try translating a builtin resource
res = Parser::Resource.new :type => "file", :title => "/tmp",
:source => @source, :scope => @scope,
:params => paramify(@source, :owner => "nobody", :mode => "644")
obj = nil
assert_nothing_raised do
obj = res.to_trans
end
assert_instance_of(Puppet::TransObject, obj)
assert_equal(obj.type, res.type)
assert_equal(obj.name, res.title)
# TransObjects use strings, resources use symbols
hash = obj.to_hash.inject({}) { |h,a| h[a[0].intern] = a[1]; h }
assert_equal(hash, res.to_hash)
end
def test_adddefaults
# Set some defaults at the top level
top = {:one => "fun", :two => "shoe"}
@scope.setdefaults("resource", paramify(@source, top))
# Make a resource at that level
res = Parser::Resource.new :type => "resource", :title => "yay",
:source => @source, :scope => @scope
# Add the defaults
assert_nothing_raised do
res.adddefaults
end
# And make sure we got them
top.each do |p, v|
assert_equal(v, res[p])
end
# Now got a bit lower
other = @scope.newscope
# And create a resource
lowerres = Parser::Resource.new :type => "resource", :title => "funtest",
:source => @source, :scope => other
assert_nothing_raised do
lowerres.adddefaults
end
# And check
top.each do |p, v|
assert_equal(v, lowerres[p])
end
# Now add some of our own defaults
lower = {:one => "shun", :three => "free"}
other.setdefaults("resource", paramify(@source, lower))
otherres = Parser::Resource.new :type => "resource", :title => "yaytest",
:source => @source, :scope => other
should = top.dup
# Make sure the lower defaults beat the higher ones.
lower.each do |p, v| should[p] = v end
otherres.adddefaults
should.each do |p,v|
assert_equal(v, otherres[p])
end
end
def test_evaluate
# Make a definition that we know will, um, do something
@interp.newdefine "evaltest",
:arguments => [%w{one}, ["two", stringobj("755")]],
:code => resourcedef("file", "/tmp",
"owner" => varref("one"), "mode" => varref("two"))
res = Parser::Resource.new :type => "evaltest", :title => "yay",
:source => @source, :scope => @scope,
:params => paramify(@source, :one => "nobody")
# Now try evaluating
ret = nil
assert_nothing_raised do
ret = res.evaluate
end
# Make sure we can find our object now
result = @scope.findresource("file[/tmp]")
# Now make sure we got the code we expected.
assert_instance_of(Puppet::Parser::Resource, result)
assert_equal("file", result.type)
assert_equal("/tmp", result.title)
assert_equal("nobody", result["owner"])
assert_equal("755", result["mode"])
# And that we cannot find the old resource
assert_nil(@scope.findresource("evaltest[yay]"),
"Evaluated resource was not deleted")
end
def test_addoverrides
# First create an override for an object that doesn't yet exist
over1 = mkresource :source => "sub1", :params => {:one => "yay"}
assert_nothing_raised do
@scope.setoverride(over1)
end
assert(over1.override, "Override was not marked so")
# Now make the resource
res = mkresource :source => "base", :params => {:one => "rah",
:three => "foo"}
# And add it to our scope
@scope.setresource(res)
# And make sure over1 has not yet taken affect
assert_equal("foo", res[:three], "Lost value")
# Now add an immediately binding override
over2 = mkresource :source => "sub1", :params => {:three => "yay"}
assert_nothing_raised do
@scope.setoverride(over2)
end
# And make sure it worked
assert_equal("yay", res[:three], "Override 2 was ignored")
# Now add our late-binding override
assert_nothing_raised do
res.addoverrides
end
# And make sure they're still around
assert_equal("yay", res[:one], "Override 1 lost")
assert_equal("yay", res[:three], "Override 2 lost")
# And finally, make sure that there are no remaining overrides
assert_nothing_raised do
res.addoverrides
end
end
def test_proxymethods
res = Parser::Resource.new :type => "evaltest", :title => "yay",
:source => @source, :scope => @scope
assert_equal("evaltest", res.type)
assert_equal("yay", res.title)
assert_equal(false, res.builtin?)
end
def test_addmetaparams
mkevaltest @interp
res = Parser::Resource.new :type => "evaltest", :title => "yay",
:source => @source, :scope => @scope,
:params => paramify(@source, :tag => "yay")
assert_nil(res[:schedule], "Got schedule already")
assert_nothing_raised do
res.addmetaparams
end
@scope.setvar("schedule", "daily")
# This is so we can test that it won't override already-set metaparams
@scope.setvar("tag", "funtest")
assert_nothing_raised do
res.addmetaparams
end
assert_equal("daily", res[:schedule], "Did not get metaparam")
assert_equal("yay", res[:tag], "Overrode explicitly-set metaparam")
assert_nil(res[:noop], "Got invalid metaparam")
end
def test_reference_conversion
# First try it as a normal string
ref = Parser::Resource::Reference.new(:type => "file", :title => "/tmp/ref1")
# Now create an obj that uses it
res = mkresource :type => "file", :title => "/tmp/resource",
:params => {:require => ref}
trans = nil
assert_nothing_raised do
trans = res.to_trans
end
assert_instance_of(Array, trans["require"])
assert_equal(["file", "/tmp/ref1"], trans["require"])
# Now try it when using an array of references.
two = Parser::Resource::Reference.new(:type => "file", :title => "/tmp/ref2")
res = mkresource :type => "file", :title => "/tmp/resource2",
:params => {:require => [ref, two]}
trans = nil
assert_nothing_raised do
trans = res.to_trans
end
assert_instance_of(Array, trans["require"][0])
trans["require"].each do |val|
assert_instance_of(Array, val)
assert_equal("file", val[0])
assert(val[1] =~ /\/tmp\/ref[0-9]/,
"Was %s instead of the file name" % val[1])
end
end
# This is a bit of a weird one -- the user should not actually know
# that components exist, so we want references to act like they're not
# builtin
def test_components_are_not_builtin
ref = Parser::Resource::Reference.new(:type => "component", :title => "yay")
assert_nil(ref.builtintype, "Component was considered builtin")
end
if defined? ActiveRecord::Base
def test_store
railsinit
res = mkresource :type => "file", :title => "/tmp/testing",
:source => @source, :scope => @scope,
:params => {:owner => "root", :mode => "755"}
# We also need a Rails Host to store under
host = Puppet::Rails::Host.new(:name => Facter.hostname)
obj = nil
assert_nothing_raised do
obj = res.store(host)
end
assert_instance_of(Puppet::Rails::RailsResource, obj)
assert_nothing_raised do
Puppet::Util.benchmark(:info, "Saved host") do
host.save
end
end
# Now make sure we can find it again
assert_nothing_raised do
obj = Puppet::Rails::RailsResource.find_by_host_id_and_restype_and_title(
host.id, res.type, res.title
)
end
assert_instance_of(Puppet::Rails::RailsResource, obj)
# Make sure we get the parameters back
obj.rails_parameters.each do |param|
assert_equal(res[param[:name]], param[:value],
"%s was different" % param[:name])
end
end
end
end
# $Id: resource.rb 1869 2006-11-13 06:11:39Z luke $
|