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

« back to all changes in this revision

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