~ubuntu-branches/ubuntu/oneiric/puppet/oneiric-security

« back to all changes in this revision

Viewing changes to test/ral/type/property.rb

  • Committer: Bazaar Package Importer
  • Author(s): Micah Anderson
  • Date: 2008-07-26 15:43:45 UTC
  • mto: (3.1.1 lenny) (1.3.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 16.
  • Revision ID: james.westby@ubuntu.com-20080726154345-1fmgo76b4l72ulvc
ImportĀ upstreamĀ versionĀ 0.24.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env ruby
 
2
 
 
3
require File.dirname(__FILE__) + '/../../lib/puppettest'
 
4
 
 
5
require 'puppettest'
 
6
 
 
7
class TestProperty < Test::Unit::TestCase
 
8
        include PuppetTest
 
9
 
 
10
    def newinst(property, resource = nil)
 
11
        inst = nil
 
12
        unless resource
 
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
 
17
        end
 
18
        assert_nothing_raised {
 
19
            newinst = property.new(:resource => resource)
 
20
            def newinst.retrieve(); return @fakeprovidervalue; end;
 
21
            return newinst
 
22
        }
 
23
    end
 
24
    
 
25
    def newproperty(name = :fakeproperty)
 
26
        property = Class.new(Puppet::Property) do
 
27
            @name = name
 
28
        end
 
29
        Object.const_set("FakeProperty", property)
 
30
        property.initvars
 
31
        cleanup do
 
32
            Object.send(:remove_const, "FakeProperty")
 
33
        end
 
34
 
 
35
        return property
 
36
    end
 
37
 
 
38
    def newmodel(name)
 
39
        # Create an object that responds to myproperty as an attr
 
40
        provklass = Class.new { attr_accessor name
 
41
            def pathbuilder
 
42
                ["provklass"]
 
43
            end
 
44
        }
 
45
        prov = provklass.new
 
46
 
 
47
        klass = Class.new { attr_accessor :provider, :path
 
48
            def pathbuilder
 
49
                ["instklass"]
 
50
            end
 
51
        }
 
52
        klassinst = klass.new
 
53
        klassinst.path = "instpath"
 
54
        klassinst.provider = prov
 
55
 
 
56
        return prov, klassinst
 
57
    end
 
58
 
 
59
    # Make sure we correctly look up names.
 
60
    def test_value_name
 
61
        property = newproperty()
 
62
 
 
63
        property.newvalue(:one)
 
64
        property.newvalue(/\d+/)
 
65
 
 
66
        name = nil
 
67
        ["one", :one].each do |value|
 
68
            assert_nothing_raised do
 
69
                name = property.value_name(value)
 
70
            end
 
71
            assert_equal(:one, name)
 
72
        end
 
73
        ["42"].each do |value|
 
74
            assert_nothing_raised do
 
75
                name = property.value_name(value)
 
76
            end
 
77
            assert_equal(/\d+/, name)
 
78
        end
 
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)
 
83
            end
 
84
            assert_nil(name)
 
85
        end
 
86
    end
 
87
 
 
88
    # Test that we correctly look up options for values.
 
89
    def test_value_option
 
90
        property = newproperty()
 
91
 
 
92
        options = {
 
93
            :one => {:event => :yay, :call => :before},
 
94
            /\d+/ => {:event => :fun, :call => :instead}
 
95
        }
 
96
        property.newvalue(:one, options[:one])
 
97
        property.newvalue(/\d+/, options[/\d+/])
 
98
 
 
99
        options.each do |name, opts|
 
100
            opts.each do |param, value|
 
101
                assert_equal(value, property.value_option(name, param))
 
102
            end
 
103
        end
 
104
    end
 
105
 
 
106
    def test_newvalue
 
107
        property = newproperty()
 
108
 
 
109
        # These are bogus because they don't define events. :/
 
110
        assert_nothing_raised {
 
111
            property.newvalue(:one) do
 
112
                @fakeprovidervalue = 1
 
113
            end
 
114
        }
 
115
 
 
116
        assert_nothing_raised {
 
117
            property.newvalue("two") do
 
118
                @fakeprovidervalue = 2
 
119
            end
 
120
        }
 
121
 
 
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")
 
125
 
 
126
        inst = newinst(property)
 
127
 
 
128
        assert_nothing_raised {
 
129
            inst.should = "one"
 
130
        }
 
131
 
 
132
        assert_equal(:one, inst.should)
 
133
        ret = nil
 
134
        assert_nothing_raised { inst.set_one }
 
135
        assert_equal(1, inst.retrieve)
 
136
 
 
137
        assert_nothing_raised {
 
138
            inst.should = :two
 
139
        }
 
140
 
 
141
        assert_equal(:two, inst.should)
 
142
        assert_nothing_raised { inst.set_two }
 
143
        assert_equal(2, inst.retrieve)
 
144
    end
 
145
 
 
146
    def test_newpropertyvaluewithregexes
 
147
        property = newproperty()
 
148
 
 
149
        assert_nothing_raised {
 
150
            property.newvalue(/^\w+$/) do
 
151
                return :regex_matched
 
152
            end
 
153
        }
 
154
 
 
155
        inst = newinst(property)
 
156
 
 
157
        assert_nothing_raised {
 
158
            inst.should = "yayness"
 
159
        }
 
160
 
 
161
        assert_equal("yayness", inst.should)
 
162
 
 
163
        assert_nothing_raised {
 
164
            inst.sync
 
165
        }
 
166
 
 
167
        assert_equal("yayness".upcase, inst.retrieve)
 
168
    end
 
169
 
 
170
    def test_newvalue_event_option
 
171
        property = newproperty()
 
172
 
 
173
        assert_nothing_raised do
 
174
            property.newvalue(:myvalue, :event => :fake_valued) do
 
175
            end
 
176
            property.newvalue(:other, :event => "fake_other") do
 
177
            end
 
178
        end
 
179
        inst = newinst(property)
 
180
 
 
181
        assert_nothing_raised {
 
182
            inst.should = :myvalue
 
183
        }
 
184
 
 
185
        ret = nil
 
186
        assert_nothing_raised {
 
187
            ret = inst.sync
 
188
        }
 
189
 
 
190
        assert_equal(:fake_valued, ret,
 
191
                     "Event did not get returned correctly")
 
192
 
 
193
        assert_nothing_raised {
 
194
            inst.should = :other
 
195
        }
 
196
 
 
197
        assert_nothing_raised {
 
198
            ret = inst.sync
 
199
        }
 
200
 
 
201
        assert_equal(:fake_other, ret,
 
202
                     "Event did not get returned correctly")
 
203
    end
 
204
 
 
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)
 
210
 
 
211
        assert_nothing_raised {
 
212
            property.newvalue(:value, :event => :matched_value)
 
213
        }
 
214
        assert_nothing_raised {
 
215
            property.newvalue(/^\d+$/, :event => :matched_number)
 
216
        }
 
217
 
 
218
        assert_equal(:none, property.value_option(:value, :call),
 
219
            ":call was not set to none when no block is provided")
 
220
 
 
221
        prov, klassinst = newmodel(:myproperty)
 
222
 
 
223
        inst = newinst(property, klassinst)
 
224
 
 
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
 
227
        # a method
 
228
        {:value => :matched_value, "27" => :matched_number}.each do |value, event|
 
229
            assert_nothing_raised do
 
230
                inst.should = value
 
231
            end
 
232
            ret = nil
 
233
            assert_nothing_raised do
 
234
                ret = inst.sync
 
235
            end
 
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)
 
238
        end
 
239
 
 
240
        # And make sure we still fail validations
 
241
        assert_raise(ArgumentError) do
 
242
            inst.should = "invalid"
 
243
        end
 
244
    end
 
245
 
 
246
    def test_tags
 
247
        obj = "yay"
 
248
        metaobj = class << obj; self; end
 
249
 
 
250
        metaobj.send(:attr_accessor, :tags)
 
251
 
 
252
        tags = [:some, :tags, :for, :testing]
 
253
        obj.tags = tags
 
254
 
 
255
        propertyklass = newproperty
 
256
 
 
257
        inst = nil
 
258
        assert_nothing_raised do
 
259
            inst = propertyklass.new(:resource => obj)
 
260
        end
 
261
 
 
262
        assert_nothing_raised do
 
263
            assert_equal(tags + [inst.name], inst.tags)
 
264
        end
 
265
    end
 
266
 
 
267
    def test_failure
 
268
        s = Struct.new(:line, :file, :path, :pathbuilder, :name)
 
269
        p = s.new(1, "yay", "rah", "struct", "name")
 
270
 
 
271
        myprovider = Class.new(Puppet::Provider)
 
272
  
 
273
        def p.provider; nil; end;
 
274
        myproperty = Class.new(Puppet::Property) do 
 
275
            @name = 'name'
 
276
        end
 
277
        myproperty.initvars
 
278
 
 
279
        myproperty.newvalue :mkfailure do
 
280
            raise "It's all broken"
 
281
        end
 
282
        property = myproperty.new(:resource => p)
 
283
 
 
284
        assert_raise(Puppet::Error) do
 
285
            property.set(:mkfailure)
 
286
        end
 
287
    end
 
288
 
 
289
    # Make sure 'set' behaves correctly WRT to call order.  This tests that the
 
290
    # :call value is handled correctly in all cases.
 
291
    def test_set
 
292
        property = newproperty(:myproperty)
 
293
 
 
294
        $setting = []
 
295
 
 
296
        newval = proc do |name, call|
 
297
            options = {}
 
298
            if call
 
299
                options[:call] = name
 
300
                block = proc { $setting << name }
 
301
            end
 
302
            assert_nothing_raised("Could not create %s value" % name) {
 
303
                if block
 
304
                    property.newvalue(name, options, &block)
 
305
                else
 
306
                    property.newvalue(name, options)
 
307
                end
 
308
            }
 
309
        end
 
310
 
 
311
        newval.call(:none, false)
 
312
 
 
313
        # Create a value with no block; it should default to :none
 
314
        newval.call(:before, true)
 
315
 
 
316
        # One with a block but after
 
317
        newval.call(:after, true)
 
318
 
 
319
        # One with an explicit instead
 
320
        newval.call(:instead, true)
 
321
 
 
322
        # And one with an implicit instead
 
323
        assert_nothing_raised do
 
324
            property.newvalue(:implicit) do
 
325
                $setting << :implicit
 
326
            end
 
327
        end
 
328
 
 
329
        # Now create a provider
 
330
        prov, model = newmodel(:myproperty)
 
331
        inst = newinst(property, model)
 
332
 
 
333
        # Mark when we're called
 
334
        prov.meta_def(:myproperty=) do |value| $setting << :provider end
 
335
 
 
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|
 
343
            inst.set(name)
 
344
 
 
345
            assert_equal(result, $setting, "%s was not handled right" % name)
 
346
            $setting.clear
 
347
        end
 
348
    end
 
349
 
 
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)
 
354
 
 
355
        prov, model = newmodel(:array_testing)
 
356
        inst = newinst(property, model)
 
357
 
 
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")
 
361
 
 
362
        vals = %w{one two three}
 
363
        inst.should = vals
 
364
 
 
365
        # Make sure we only get the first value back
 
366
        assert_equal("one", inst.should, "Returned wrong value when array_matching == first")
 
367
 
 
368
        # And make sure any of these values is considered in sync
 
369
        vals.each do |value|
 
370
            assert(inst.insync?(value), "#{value} was not considered in sync when array_matching == first")
 
371
        end
 
372
 
 
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")
 
377
 
 
378
        # Make sure we only get the first value back
 
379
        assert_equal(vals, inst.should, "Returned wrong value when array_matching == all")
 
380
 
 
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")
 
384
        end
 
385
        assert(inst.insync?(vals), "value array was not considered in sync when array_matching == all")
 
386
    end
 
387
end
 
388