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
|
#!/usr/bin/env ruby
$:.unshift("../lib").unshift("../../lib") if __FILE__ =~ /\.rb$/
require 'puppet'
require 'puppet/parser/interpreter'
require 'puppet/parser/parser'
require 'puppet/client'
require 'puppettest'
require 'puppettest/resourcetesting'
class TestLangFunctions < Test::Unit::TestCase
include PuppetTest::ParserTesting
include PuppetTest::ResourceTesting
def test_functions
assert_raise(Puppet::ParseError) do
Puppet::Parser::AST::Function.new(
:name => "fakefunction",
:arguments => AST::ASTArray.new(
:children => [nameobj("avalue")]
)
)
end
assert_nothing_raised do
Puppet::Parser::Functions.newfunction(:fakefunction, :type => :rvalue) do |input|
return "output %s" % input[0]
end
end
func = nil
assert_nothing_raised do
func = Puppet::Parser::AST::Function.new(
:name => "fakefunction",
:ftype => :rvalue,
:arguments => AST::ASTArray.new(
:children => [nameobj("avalue")]
)
)
end
scope = mkscope
val = nil
assert_nothing_raised do
val = func.evaluate(:scope => scope)
end
assert_equal("output avalue", val)
end
def test_taggedfunction
scope = mkscope
tag = "yayness"
scope.tag(tag)
{"yayness" => true, "booness" => false}.each do |tag, retval|
func = taggedobj(tag, :rvalue)
val = nil
assert_nothing_raised do
val = func.evaluate(:scope => scope)
end
assert_equal(retval, val, "'tagged' returned %s for %s" % [val, tag])
end
end
def test_failfunction
func = nil
assert_nothing_raised do
func = Puppet::Parser::AST::Function.new(
:name => "fail",
:ftype => :statement,
:arguments => AST::ASTArray.new(
:children => [stringobj("this is a failure"),
stringobj("and another")]
)
)
end
scope = mkscope
val = nil
assert_raise(Puppet::ParseError) do
val = func.evaluate(:scope => scope)
end
end
def test_multipletemplates
Dir.mkdir(Puppet[:templatedir])
onep = File.join(Puppet[:templatedir], "one")
twop = File.join(Puppet[:templatedir], "two")
File.open(onep, "w") do |f|
f.puts "template <%= one %>"
end
File.open(twop, "w") do |f|
f.puts "template <%= two %>"
end
func = nil
assert_nothing_raised do
func = Puppet::Parser::AST::Function.new(
:name => "template",
:ftype => :rvalue,
:arguments => AST::ASTArray.new(
:children => [stringobj("one"),
stringobj("two")]
)
)
end
ast = varobj("output", func)
scope = mkscope
assert_raise(Puppet::ParseError) do
ast.evaluate(:scope => scope)
end
scope.setvar("one", "One")
assert_raise(Puppet::ParseError) do
ast.evaluate(:scope => scope)
end
scope.setvar("two", "Two")
assert_nothing_raised do
ast.evaluate(:scope => scope)
end
assert_equal("template One\ntemplate Two\n", scope.lookupvar("output"),
"Templates were not handled correctly")
end
# Now make sure we can fully qualify files, and specify just one
def test_singletemplates
template = tempfile()
File.open(template, "w") do |f|
f.puts "template <%= yayness %>"
end
func = nil
assert_nothing_raised do
func = Puppet::Parser::AST::Function.new(
:name => "template",
:ftype => :rvalue,
:arguments => AST::ASTArray.new(
:children => [stringobj(template)]
)
)
end
ast = varobj("output", func)
scope = mkscope
assert_raise(Puppet::ParseError) do
ast.evaluate(:scope => scope)
end
scope.setvar("yayness", "this is yayness")
assert_nothing_raised do
ast.evaluate(:scope => scope)
end
assert_equal("template this is yayness\n", scope.lookupvar("output"),
"Templates were not handled correctly")
end
def test_tempatefunction_cannot_see_scopes
template = tempfile()
File.open(template, "w") do |f|
f.puts "<%= lookupvar('myvar') %>"
end
func = nil
assert_nothing_raised do
func = Puppet::Parser::AST::Function.new(
:name => "template",
:ftype => :rvalue,
:arguments => AST::ASTArray.new(
:children => [stringobj(template)]
)
)
end
ast = varobj("output", func)
scope = mkscope
scope.setvar("myvar", "this is yayness")
assert_raise(Puppet::ParseError) do
ast.evaluate(:scope => scope)
end
end
def test_template_reparses
template = tempfile()
File.open(template, "w") do |f|
f.puts "original text"
end
manifest = tempfile()
file = tempfile()
File.open(manifest, "w") do |f|
f.puts %{file { "#{file}": content => template("#{template}") }}
end
interpreter = Puppet::Parser::Interpreter.new(
:Manifest => manifest,
:UseNodes => false
)
parsedate = interpreter.parsedate()
objects = nil
assert_nothing_raised {
objects = interpreter.run("myhost", {})
}
fileobj = objects[0]
assert_equal("original text\n", fileobj["content"],
"Template did not work")
Puppet[:filetimeout] = 0
# Have to sleep because one second is the fs's time granularity.
sleep(1)
# Now modify the template
File.open(template, "w") do |f|
f.puts "new text"
end
assert_nothing_raised {
objects = interpreter.run("myhost", {})
}
newdate = interpreter.parsedate()
assert(parsedate != newdate, "Parse date did not change")
end
def test_template_defined_vars
template = tempfile()
File.open(template, "w") do |f|
f.puts "template <%= yayness %>"
end
func = nil
assert_nothing_raised do
func = Puppet::Parser::AST::Function.new(
:name => "template",
:ftype => :rvalue,
:arguments => AST::ASTArray.new(
:children => [stringobj(template)]
)
)
end
ast = varobj("output", func)
{
"" => "",
false => "false",
}.each do |string, value|
scope = mkscope
assert_raise(Puppet::ParseError) do
ast.evaluate(:scope => scope)
end
scope.setvar("yayness", string)
assert_equal(string, scope.lookupvar("yayness", false))
assert_nothing_raised("An empty string was not a valid variable value") do
ast.evaluate(:scope => scope)
end
assert_equal("template #{value}\n", scope.lookupvar("output"),
"%s did not get evaluated correctly" % string.inspect)
end
end
def test_autoloading_functions
assert_equal(false, Puppet::Parser::Functions.function(:autofunc),
"Got told autofunc already exists")
dir = tempfile()
$: << dir
newpath = File.join(dir, "puppet", "parser", "functions")
FileUtils.mkdir_p(newpath)
File.open(File.join(newpath, "autofunc.rb"), "w") { |f|
f.puts %{
Puppet::Parser::Functions.newfunction(:autofunc, :type => :rvalue) do |vals|
Puppet.wanring vals.inspect
end
}
}
obj = nil
assert_nothing_raised {
obj = Puppet::Parser::Functions.function(:autofunc)
}
assert(obj, "Did not autoload function")
assert(Puppet::Parser::Scope.method_defined?(:function_autofunc),
"Did not set function correctly")
end
def test_realize
@interp, @scope, @source = mkclassframing
# Make a virtual resource
virtual = mkresource(:type => "file", :title => "/tmp/virtual",
:virtual => true, :params => {:owner => "root"})
@scope.setresource virtual
ref = Puppet::Parser::Resource::Reference.new(
:type => "file", :title => "/tmp/virtual",
:scope => @scope
)
# Now call the realize function
assert_nothing_raised do
@scope.function_realize(ref)
end
# Make sure it created a collection
assert_equal(1, @scope.collections.length,
"Did not set collection")
assert_nothing_raised do
@scope.collections.each do |coll| coll.evaluate end
end
@scope.collections.clear
# Now make sure the virtual resource is no longer virtual
assert(! virtual.virtual?, "Did not make virtual resource real")
# Make sure we puke on any resource that doesn't exist
none = Puppet::Parser::Resource::Reference.new(
:type => "file", :title => "/tmp/nosuchfile",
:scope => @scope
)
# The function works
assert_nothing_raised do
@scope.function_realize(none)
end
# Make sure it created a collection
assert_equal(1, @scope.collections.length,
"Did not set collection")
# But the collection fails
assert_raise(Puppet::ParseError) do
@scope.collections.each do |coll| coll.evaluate end
end
end
end
# $Id: functions.rb 1844 2006-11-09 21:06:47Z luke $
|