3
require File.dirname(__FILE__) + '/../../lib/puppettest'
7
class TestProperty < Test::Unit::TestCase
10
def newinst(property, resource = nil)
13
resource = "fakeresource"
14
resource.meta_def(:pathbuilder) do [self.to_s] end
15
resource.meta_def(:provider) do nil end
16
resource.meta_def(:fakeproperty) do '' end
18
assert_nothing_raised {
19
newinst = property.new(:resource => resource)
20
def newinst.retrieve(); return @fakeprovidervalue; end;
25
def newproperty(name = :fakeproperty)
26
property = Class.new(Puppet::Property) do
29
Object.const_set("FakeProperty", property)
32
Object.send(:remove_const, "FakeProperty")
39
# Create an object that responds to myproperty as an attr
40
provklass = Class.new { attr_accessor name
47
klass = Class.new { attr_accessor :provider, :path
53
klassinst.path = "instpath"
54
klassinst.provider = prov
56
return prov, klassinst
59
# Make sure we correctly look up names.
61
property = newproperty()
63
property.newvalue(:one)
64
property.newvalue(/\d+/)
67
["one", :one].each do |value|
68
assert_nothing_raised do
69
name = property.value_name(value)
71
assert_equal(:one, name)
73
["42"].each do |value|
74
assert_nothing_raised do
75
name = property.value_name(value)
77
assert_equal(/\d+/, name)
79
# these values should not have a name
80
["two", :three, ''].each do |value|
81
assert_nothing_raised do
82
name = property.value_name(value)
88
# Test that we correctly look up options for values.
90
property = newproperty()
93
:one => {:event => :yay, :call => :before},
94
/\d+/ => {:event => :fun, :call => :instead}
96
property.newvalue(:one, options[:one])
97
property.newvalue(/\d+/, options[/\d+/])
99
options.each do |name, opts|
100
opts.each do |param, value|
101
assert_equal(value, property.value_option(name, param))
107
property = newproperty()
109
# These are bogus because they don't define events. :/
110
assert_nothing_raised {
111
property.newvalue(:one) do
112
@fakeprovidervalue = 1
116
assert_nothing_raised {
117
property.newvalue("two") do
118
@fakeprovidervalue = 2
122
# Make sure we default to using the block instead
123
assert_equal(:instead, property.value_option(:one, :call),
124
":call was not set to :instead when a block was provided")
126
inst = newinst(property)
128
assert_nothing_raised {
132
assert_equal(:one, inst.should)
134
assert_nothing_raised { inst.set_one }
135
assert_equal(1, inst.retrieve)
137
assert_nothing_raised {
141
assert_equal(:two, inst.should)
142
assert_nothing_raised { inst.set_two }
143
assert_equal(2, inst.retrieve)
146
def test_newpropertyvaluewithregexes
147
property = newproperty()
149
assert_nothing_raised {
150
property.newvalue(/^\w+$/) do
151
return :regex_matched
155
inst = newinst(property)
157
assert_nothing_raised {
158
inst.should = "yayness"
161
assert_equal("yayness", inst.should)
163
assert_nothing_raised {
167
assert_equal("yayness".upcase, inst.retrieve)
170
def test_newvalue_event_option
171
property = newproperty()
173
assert_nothing_raised do
174
property.newvalue(:myvalue, :event => :fake_valued) do
176
property.newvalue(:other, :event => "fake_other") do
179
inst = newinst(property)
181
assert_nothing_raised {
182
inst.should = :myvalue
186
assert_nothing_raised {
190
assert_equal(:fake_valued, ret,
191
"Event did not get returned correctly")
193
assert_nothing_raised {
197
assert_nothing_raised {
201
assert_equal(:fake_other, ret,
202
"Event did not get returned correctly")
205
# We want to support values with no blocks, either regexes or strings.
206
# If there's no block provided, then we should call the provider mechanism
207
# like we would normally.
208
def test_newvalue_with_no_block
209
property = newproperty(:myproperty)
211
assert_nothing_raised {
212
property.newvalue(:value, :event => :matched_value)
214
assert_nothing_raised {
215
property.newvalue(/^\d+$/, :event => :matched_number)
218
assert_equal(:none, property.value_option(:value, :call),
219
":call was not set to none when no block is provided")
221
prov, klassinst = newmodel(:myproperty)
223
inst = newinst(property, klassinst)
225
# Now make sure we can set the values, they get validated as normal,
226
# and they set the values on the resource rather than trying to call
228
{:value => :matched_value, "27" => :matched_number}.each do |value, event|
229
assert_nothing_raised do
233
assert_nothing_raised do
236
assert_equal(event, ret, "Did not return correct event for %s" % value)
237
assert_equal(value, prov.myproperty, "%s was not set right" % value)
240
# And make sure we still fail validations
241
assert_raise(ArgumentError) do
242
inst.should = "invalid"
248
metaobj = class << obj; self; end
250
metaobj.send(:attr_accessor, :tags)
252
tags = [:some, :tags, :for, :testing]
255
propertyklass = newproperty
258
assert_nothing_raised do
259
inst = propertyklass.new(:resource => obj)
262
assert_nothing_raised do
263
assert_equal(tags + [inst.name], inst.tags)
268
s = Struct.new(:line, :file, :path, :pathbuilder, :name)
269
p = s.new(1, "yay", "rah", "struct", "name")
271
myprovider = Class.new(Puppet::Provider)
273
def p.provider; nil; end;
274
myproperty = Class.new(Puppet::Property) do
279
myproperty.newvalue :mkfailure do
280
raise "It's all broken"
282
property = myproperty.new(:resource => p)
284
assert_raise(Puppet::Error) do
285
property.set(:mkfailure)
289
# Make sure 'set' behaves correctly WRT to call order. This tests that the
290
# :call value is handled correctly in all cases.
292
property = newproperty(:myproperty)
296
newval = proc do |name, call|
299
options[:call] = name
300
block = proc { $setting << name }
302
assert_nothing_raised("Could not create %s value" % name) {
304
property.newvalue(name, options, &block)
306
property.newvalue(name, options)
311
newval.call(:none, false)
313
# Create a value with no block; it should default to :none
314
newval.call(:before, true)
316
# One with a block but after
317
newval.call(:after, true)
319
# One with an explicit instead
320
newval.call(:instead, true)
322
# And one with an implicit instead
323
assert_nothing_raised do
324
property.newvalue(:implicit) do
325
$setting << :implicit
329
# Now create a provider
330
prov, model = newmodel(:myproperty)
331
inst = newinst(property, model)
333
# Mark when we're called
334
prov.meta_def(:myproperty=) do |value| $setting << :provider end
336
# Now run through the list and make sure everything is correct
337
{:before => [:before, :provider],
338
:after => [:provider, :after],
339
:instead => [:instead],
340
:none => [:provider],
341
:implicit => [:implicit]
342
}.each do |name, result|
345
assert_equal(result, $setting, "%s was not handled right" % name)
350
# Make sure we can specify that we want to use the whole array, rather
351
# than just individual values.
352
def test_array_handling
353
property = newproperty(:arraytests)
355
prov, model = newmodel(:array_testing)
356
inst = newinst(property, model)
358
# Make sure it defaults to first
359
assert_equal(:first, property.array_matching, "Property did not default to matching first value in an array")
360
assert(! inst.match_all?, "match_all? returned true when array_matching is :first")
362
vals = %w{one two three}
365
# Make sure we only get the first value back
366
assert_equal("one", inst.should, "Returned wrong value when array_matching == first")
368
# And make sure any of these values is considered in sync
370
assert(inst.insync?(value), "#{value} was not considered in sync when array_matching == first")
373
# Now change it to all
374
property.array_matching = :all
375
assert_equal(:all, property.array_matching, "Property did not change value of array_matching")
376
assert(inst.match_all?, "match_all? returned false when array_matching is :all")
378
# Make sure we only get the first value back
379
assert_equal(vals, inst.should, "Returned wrong value when array_matching == all")
381
# And make sure any of these values is considered in sync
382
%w{one two three}.each do |value|
383
assert(! inst.insync?(value), "individual value #{value} was considered in sync when array_matching == all")
385
assert(inst.insync?(vals), "value array was not considered in sync when array_matching == all")