summaryrefslogtreecommitdiffstats
path: root/test/language/resource.rb
blob: 892d8c293882c1b0f5c78b98f3c6492cc2e5797c (plain)
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
#!/usr/bin/env ruby

$:.unshift("../lib").unshift("../../lib") if __FILE__ =~ /\.rb$/

require 'puppettest'
require 'puppettest/resourcetesting'

class TestResource < PuppetTest::TestCase
	include PuppetTest
    include PuppetTest::ParserTesting
    include PuppetTest::ResourceTesting
    Parser = Puppet::Parser
    AST = Parser::AST
    Resource = Puppet::Parser::Resource
    Reference = Puppet::Parser::Resource::Reference

    def setup
        super
        Puppet[:trace] = false
    end

    def teardown
        mocha_verify
    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(ArgumentError, "Did not fail when %s was missing" % name) do
                Parser::Resource.new(try)
            end
        end

        Reference.expects(:new).with(:type => "resource", :title => "testing", :scope => "scope").returns(:ref)

        res = nil
        assert_nothing_raised do
            res = Parser::Resource.new(args)
        end
    end

    def test_merge
        res = mkresource
        other = mkresource

        # First try the case where the resource is not allowed to override
        res.source = "source1"
        other.source = "source2"
        other.source.expects(:child_of?).with("source1").returns(false)
        assert_raise(Puppet::ParseError, "Allowed unrelated resources to override") do
            res.merge(other)
        end

        # Next try it when the sources are equal.
        res.source = "source3"
        other.source = res.source
        other.source.expects(:child_of?).with("source3").never
        params = {:a => :b, :c => :d}
        other.expects(:params).returns(params)
        res.expects(:override_parameter).with(:b)
        res.expects(:override_parameter).with(:d)
        res.merge(other)

        # And then parentage is involved
        other = mkresource
        res.source = "source3"
        other.source = "source4"
        other.source.expects(:child_of?).with("source3").returns(true)
        params = {:a => :b, :c => :d}
        other.expects(:params).returns(params)
        res.expects(:override_parameter).with(:b)
        res.expects(:override_parameter).with(:d)
        res.merge(other)
    end

    # the [] method
    def test_array_accessors
        res = mkresource
        params = res.instance_variable_get("@params")
        assert_nil(res[:missing], "Found a missing parameter somehow")
        params[:something] = stub(:value => "yay")
        assert_equal("yay", res[:something], "Did not correctly call value on the parameter")

        res.expects(:title).returns(:mytitle)
        assert_equal(:mytitle, res[:title], "Did not call title when asked for it as a param")
    end

    # Make sure any defaults stored in the scope get added to our resource.
    def test_add_defaults
        res = mkresource
        params = res.instance_variable_get("@params")
        params[:a] = :b
        res.scope.expects(:lookupdefaults).with(res.type).returns(:a => :replaced, :c => :d)
        res.expects(:debug)

        res.send(:add_defaults)
        assert_equal(:d, params[:c], "Did not set default")
        assert_equal(:b, params[:a], "Replaced parameter with default")
    end

    def test_finish
        res = mkresource
        res.expects(:add_overrides)
        res.expects(:add_defaults)
        res.expects(:add_metaparams)
        res.expects(:validate)
        res.finish
    end

    # Make sure we paramcheck our params
    def test_validate
        res = mkresource
        params = res.instance_variable_get("@params")
        params[:one] = :two
        params[:three] = :four
        res.expects(:paramcheck).with(:one)
        res.expects(:paramcheck).with(:three)
        res.send(:validate)
    end

    def test_override_parameter
        res = mkresource
        params = res.instance_variable_get("@params")

        # There are three cases, with the second having two options:

        # No existing parameter.
        param = stub(:name => "myparam")
        res.send(:override_parameter, param)
        assert_equal(param, params["myparam"], "Override was not added to param list")

        # An existing parameter that we can override.
        source = stub(:child_of? => true)
        # Start out without addition
        params["param2"] = stub(:source => :whatever)
        param = stub(:name => "param2", :source => source, :add => false)
        res.send(:override_parameter, param)
        assert_equal(param, params["param2"], "Override was not added to param list")

        # Try with addition.
        params["param2"] = stub(:value => :a, :source => :whatever)
        param = stub(:name => "param2", :source => source, :add => true, :value => :b)
        param.expects(:value=).with([:a, :b])
        res.send(:override_parameter, param)
        assert_equal(param, params["param2"], "Override was not added to param list")

        # And finally, make sure we throw an exception when the sources aren't related
        source = stub(:child_of? => false)
        params["param2"] = stub(:source => :whatever, :file => :f, :line => :l)
        old = params["param2"]
        param = stub(:name => "param2", :source => source, :file => :f, :line => :l)
        assert_raise(Puppet::ParseError, "Did not fail when params conflicted") do
            res.send(:override_parameter, param)
        end
        assert_equal(old, params["param2"], "Param was replaced irrespective of conflict")
    end

    def test_set_parameter
        res = mkresource
        params = res.instance_variable_get("@params")

        # First test the simple case:  It's already a parameter
        param = mock('param')
        param.expects(:is_a?).with(Resource::Param).returns(true)
        param.expects(:name).returns("pname")
        res.send(:set_parameter, param)
        assert_equal(param, params["pname"], "Parameter was not added to hash")

        # Now the case where there's no value but it's not a param
        param = mock('param')
        param.expects(:is_a?).with(Resource::Param).returns(false)
        assert_raise(ArgumentError, "Did not fail when a non-param was passed") do
            res.send(:set_parameter, param)
        end

        # and the case where a value is passed in
        param = stub :name => "pname", :value => "whatever"
        Resource::Param.expects(:new).with(:name => "pname", :value => "myvalue", :source => res.source).returns(param)
        res.send(:set_parameter, "pname", "myvalue")
        assert_equal(param, params["pname"], "Did not put param in hash")
    end

    def test_paramcheck
        # There are three cases here:

        # It's a valid parameter
        res = mkresource
        ref = mock('ref')
        res.instance_variable_set("@ref", ref)
        klass = mock("class")
        ref.expects(:typeclass).returns(klass).times(4)
        klass.expects(:validattr?).with("good").returns(true)
        assert(res.send(:paramcheck, :good), "Did not allow valid param")

        # It's name or title
        klass.expects(:validattr?).with("name").returns(false)
        assert(res.send(:paramcheck, :name), "Did not allow name")
        klass.expects(:validattr?).with("title").returns(false)
        assert(res.send(:paramcheck, :title), "Did not allow title")

        # It's not actually allowed
        klass.expects(:validattr?).with("other").returns(false)
        res.expects(:fail)
        ref.expects(:type)
        res.send(:paramcheck, :other)
    end

    def test_to_trans
        # First try translating a builtin resource.  Make sure we use some references
        # and arrays, to make sure they translate correctly.
        source = mock("source")
        scope = mock("scope")
        scope.expects(:tags).returns([])
        refs = []
        4.times { |i| refs << Puppet::Parser::Resource::Reference.new(:title => "file%s" % i, :type => "file") }
        res = Parser::Resource.new :type => "file", :title => "/tmp",
            :source => source, :scope => scope,
            :params => paramify(source, :owner => "nobody", :group => %w{you me},
            :require => refs[0], :ignore => %w{svn},
            :subscribe => [refs[1], refs[2]], :notify => [refs[3]])

        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
        assert_equal("nobody", obj["owner"], "Single-value string was not passed correctly")
        assert_equal(%w{you me}, obj["group"], "Array of strings was not passed correctly")
        assert_equal("svn", obj["ignore"], "Array with single string was not turned into single value")
        assert_equal(["file", refs[0].title], obj["require"], "Resource reference was not passed correctly")
        assert_equal([["file", refs[1].title], ["file", refs[2].title]], obj["subscribe"], "Array of resource references was not passed correctly")
        assert_equal(["file", refs[3].title], obj["notify"], "Array with single resource reference was not turned into single value")
    end

    def test_evaluate
        # First try the most common case, we're not a builtin type.
        res = mkresource
        ref = res.instance_variable_get("@ref")
        type = mock("type")
        ref.expects(:definedtype).returns(type)
        res.expects(:finish)
        res.scope = mock("scope")
        config = mock("config")
        res.scope.expects(:compile).returns(config)
        config.expects(:delete_resource).with(res)

        args = {:scope => res.scope, :resource => res}
        type.expects(:evaluate).with(args)

        res.evaluate
    end

    def test_add_overrides
        # Try it with nil
        res = mkresource
        res.scope = mock('scope')
        config = mock("config")
        res.scope.expects(:compile).returns(config)
        config.expects(:resource_overrides).with(res).returns(nil)
        res.expects(:merge).never
        res.send(:add_overrides)

        # And an empty array
        res = mkresource
        res.scope = mock('scope')
        config = mock("config")
        res.scope.expects(:compile).returns(config)
        config.expects(:resource_overrides).with(res).returns([])
        res.expects(:merge).never
        res.send(:add_overrides)

        # And with some overrides
        res = mkresource
        res.scope = mock('scope')
        config = mock("config")
        res.scope.expects(:compile).returns(config)
        returns = %w{a b}
        config.expects(:resource_overrides).with(res).returns(returns)
        res.expects(:merge).with("a")
        res.expects(:merge).with("b")
        res.send(:add_overrides)
        assert(returns.empty?, "Did not clear overrides")
    end

    def test_proxymethods
        res = Parser::Resource.new :type => "evaltest", :title => "yay",
            :source => mock("source"), :scope => mock('scope')

        assert_equal("evaltest", res.type)
        assert_equal("yay", res.title)
        assert_equal(false, res.builtin?)
    end

    def test_add_metaparams
        res = mkresource
        params = res.instance_variable_get("@params")
        params[:a] = :b
        Puppet::Type.expects(:eachmetaparam).multiple_yields(:a, :b, :c)
        res.scope.expects(:lookupvar).with("b", false).returns(:something)
        res.scope.expects(:lookupvar).with("c", false).returns(:undefined)
        res.expects(:set_parameter).with(:b, :something)

        res.send(:add_metaparams)

        assert_nil(params[:c], "A value was created somehow for an unset 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}
        res.scope = stub(:tags => [])

        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]}
        res.scope = stub(:tags => [])

        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, "Definition was considered builtin")
    end

    # The second part of #539 - make sure resources pass the arguments
    # correctly.
    def test_title_with_definitions
        parser = mkparser
        define = parser.newdefine "yayness",
            :code => resourcedef("file", "/tmp",
                "owner" => varref("name"), "mode" => varref("title"))


        klass = parser.findclass("", "")
        should = {:name => :owner, :title => :mode}
        [
        {:name => "one", :title => "two"},
        {:title => "three"},
        ].each do |hash|
            config = mkconfig parser
            args = {:type => "yayness", :title => hash[:title],
                :source => klass, :scope => config.topscope}
            if hash[:name]
                args[:params] = {:name => hash[:name]}
            else
                args[:params] = {} # override the defaults
            end

            res = nil
            assert_nothing_raised("Could not create res with %s" % hash.inspect) do
                res = mkresource(args)
            end
            assert_nothing_raised("Could not eval res with %s" % hash.inspect) do
                res.evaluate
            end

            made = config.topscope.findresource("File[/tmp]")
            assert(made, "Did not create resource with %s" % hash.inspect)
            should.each do |orig, param|
                assert_equal(hash[orig] || hash[:title], made[param],
                    "%s was not set correctly with %s" % [param, hash.inspect])
            end
        end
    end

    # part of #629 -- the undef keyword.  Make sure 'undef' params get skipped.
    def test_undef_and_to_hash
        res = mkresource :type => "file", :title => "/tmp/testing",
            :source => mock("source"), :scope => mock("scope"),
            :params => {:owner => :undef, :mode => "755"}

        hash = nil
        assert_nothing_raised("Could not convert resource with undef to hash") do
            hash = res.to_hash
        end

        assert_nil(hash[:owner], "got a value for an undef parameter")
    end

    # #643 - Make sure virtual defines result in virtual resources
    def test_virtual_defines
        parser = mkparser
        define = parser.newdefine("yayness",
            :code => resourcedef("file", varref("name"),
                "mode" => "644"))

        config = mkconfig(parser)

        res = mkresource :type => "yayness", :title => "foo", :params => {}, :scope => config.topscope
        res.virtual = true

        result = nil
        assert_nothing_raised("Could not evaluate defined resource") do
            result = res.evaluate
        end

        scope = res.scope
        newres = scope.findresource("File[foo]")
        assert(newres, "Could not find resource")

        assert(newres.virtual?, "Virtual defined resource generated non-virtual resources")

        # Now try it with exported resources
        res = mkresource :type => "yayness", :title => "bar", :params => {}, :scope => config.topscope
        res.exported = true

        result = nil
        assert_nothing_raised("Could not evaluate exported resource") do
            result = res.evaluate
        end

        scope = res.scope
        newres = scope.findresource("File[bar]")
        assert(newres, "Could not find resource")

        assert(newres.exported?, "Exported defined resource generated non-exported resources")
        assert(newres.virtual?, "Exported defined resource generated non-virtual resources")
    end
end

# $Id$