summaryrefslogtreecommitdiffstats
path: root/spec/unit/parser/resource.rb
blob: 92699e040b3d4353de51b1382f7a90b4878da911 (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
#!/usr/bin/env ruby

require File.dirname(__FILE__) + '/../../spec_helper'

# LAK: FIXME This is just new tests for resources; I have
# not moved all tests over yet.

describe Puppet::Parser::Resource do
    before do
        @parser = Puppet::Parser::Parser.new :Code => ""
        @source = @parser.newclass ""
        @node = Puppet::Node.new("yaynode")
        @compiler = Puppet::Parser::Compiler.new(@node, @parser)
        @scope = @compiler.topscope
    end

    def mkresource(args = {})
        args[:source] ||= "source"
        args[:scope] ||= stub('scope', :source => mock('source'))

        {:type => "resource", :title => "testing", :source => "source", :scope => "scope"}.each do |param, value|
            args[param] ||= value
        end

        params = args[:params] || {:one => "yay", :three => "rah"}
        if args[:params] == :none
            args.delete(:params)
        else
            args[:params] = paramify(args[:source], params)
        end

        Puppet::Parser::Resource.new(args)
    end

    def param(name, value, source)
        Puppet::Parser::Resource::Param.new(:name => name, :value => value, :source => source)
    end

    def paramify(source, hash)
        hash.collect do |name, value|
            Puppet::Parser::Resource::Param.new(
                :name => name, :value => value, :source => source
            )
        end
    end

    it "should be isomorphic if it is builtin and models an isomorphic type" do
        Puppet::Type.type(:file).expects(:isomorphic?).returns(true)
        @resource = Puppet::Parser::Resource.new(:type => "file", :title => "whatever", :scope => @scope, :source => @source).isomorphic?.should be_true
    end

    it "should not be isomorphic if it is builtin and models a non-isomorphic type" do
        Puppet::Type.type(:file).expects(:isomorphic?).returns(false)
        @resource = Puppet::Parser::Resource.new(:type => "file", :title => "whatever", :scope => @scope, :source => @source).isomorphic?.should be_false
    end

    it "should be isomorphic if it is not builtin" do
        @parser.newdefine "whatever"
        @resource = Puppet::Parser::Resource.new(:type => "whatever", :title => "whatever", :scope => @scope, :source => @source).isomorphic?.should be_true
    end

    it "should have a array-indexing method for retrieving parameter values" do
        @resource = mkresource
        @resource[:one].should == "yay"
    end

    it "should use a Puppet::Resource for converting to a ral resource" do
        trans = mock 'resource', :to_ral => "yay"
        @resource = mkresource
        @resource.expects(:to_resource).returns trans
        @resource.to_ral.should == "yay"
    end

    describe "when initializing" do
        before do
            @arguments = {:type => "resource", :title => "testing", :scope => stub('scope', :source => mock('source'))}
        end

        [:type, :title, :scope].each do |name|
            it "should fail unless #{name.to_s} is specified" do
                try = @arguments.dup
                try.delete(name)
                lambda { Puppet::Parser::Resource.new(try) }.should raise_error(ArgumentError)
            end
        end

        it "should set the reference correctly" do
            res = Puppet::Parser::Resource.new(@arguments)
            res.ref.should == "Resource[testing]"
        end
    end

    describe "when evaluating" do
        before do
            @type = Puppet::Parser::Resource

            @definition = @parser.newdefine "mydefine"
            @class = @parser.newclass "myclass"
            @nodedef = @parser.newnode("mynode")[0]
        end

        it "should evaluate the associated AST definition" do
            res = @type.new(:type => "mydefine", :title => "whatever", :scope => @scope, :source => @source)
            @definition.expects(:evaluate_code).with(res)

            res.evaluate
        end

        it "should evaluate the associated AST class" do
            res = @type.new(:type => "class", :title => "myclass", :scope => @scope, :source => @source)
            @class.expects(:evaluate_code).with(res)
            res.evaluate
        end

        it "should evaluate the associated AST node" do
            res = @type.new(:type => "node", :title => "mynode", :scope => @scope, :source => @source)
            @nodedef.expects(:evaluate_code).with(res)
            res.evaluate
        end
    end

    describe "when finishing" do
        before do
            @class = @parser.newclass "myclass"
            @nodedef = @parser.newnode("mynode")[0]

            @resource = Puppet::Parser::Resource.new(:type => "file", :title => "whatever", :scope => @scope, :source => @source)
        end

        it "should do nothing if it has already been finished" do
            @resource.finish
            @resource.expects(:add_metaparams).never
            @resource.finish
        end

        it "should add all defaults available from the scope" do
            @resource.scope.expects(:lookupdefaults).with(@resource.type).returns(:owner => param(:owner, "default", @resource.source))
            @resource.finish

            @resource[:owner].should == "default"
        end

        it "should not replace existing parameters with defaults" do
            @resource.set_parameter :owner, "oldvalue"
            @resource.scope.expects(:lookupdefaults).with(@resource.type).returns(:owner => :replaced)
            @resource.finish

            @resource[:owner].should == "oldvalue"
        end

        it "should add a copy of each default, rather than the actual default parameter instance" do
            newparam = param(:owner, "default", @resource.source)
            other = newparam.dup
            other.value = "other"
            newparam.expects(:dup).returns(other)
            @resource.scope.expects(:lookupdefaults).with(@resource.type).returns(:owner => newparam)
            @resource.finish

            @resource[:owner].should == "other"
        end

        it "should copy metaparams from its scope" do
            @scope.setvar("noop", "true")

            @resource.class.publicize_methods(:add_metaparams)  { @resource.add_metaparams }

            @resource["noop"].should == "true"
        end

        it "should not copy metaparams that it already has" do
            @resource.set_parameter("noop", "false")
            @scope.setvar("noop", "true")

            @resource.class.publicize_methods(:add_metaparams)  { @resource.add_metaparams }

            @resource["noop"].should == "false"
        end

        it "should copy all metaparams that it finds" do
            @scope.setvar("require", "container")
            @scope.setvar("notify", "container")

            @resource.class.publicize_methods(:add_metaparams)  { @resource.add_metaparams }

            @resource["require"].should == "container"
            @resource["notify"].should == "container"
        end

        it "should stack relationship metaparams from its container if it already has them" do
            @resource.set_parameter("require", "resource")
            @scope.setvar("require", "container")

            @resource.class.publicize_methods(:add_metaparams)  { @resource.add_metaparams }

            @resource["require"].sort.should == %w{container resource}
        end

        it "should not stack relationship metaparams that are set to 'undef'" do
            @resource.set_parameter("require", :undef)
            @scope.setvar("require", "container")

            @resource.class.publicize_methods(:add_metaparams)  { @resource.add_metaparams }

            @resource["require"].should == :undef
        end

        it "should flatten the array resulting from stacking relationship metaparams" do
            @resource.set_parameter("require", ["resource1", "resource2"])
            @scope.setvar("require", %w{container1 container2})

            @resource.class.publicize_methods(:add_metaparams)  { @resource.add_metaparams }

            @resource["require"].sort.should == %w{container1 container2 resource1 resource2}
        end

        it "should add any tags from the scope resource" do
            scope_resource = stub 'scope_resource', :tags => %w{one two}
            @scope.stubs(:resource).returns(scope_resource)

            @resource.class.publicize_methods(:add_scope_tags)  { @resource.add_scope_tags }

            @resource.tags.should be_include("one")
            @resource.tags.should be_include("two")
        end
    end

    describe "when being tagged" do
        before do
            @scope_resource = stub 'scope_resource', :tags => %w{srone srtwo}
            @scope = stub 'scope', :resource => @scope_resource
            @resource = Puppet::Parser::Resource.new(:type => "file", :title => "yay", :scope => @scope, :source => mock('source'))
        end

        it "should get tagged with the resource type" do
            @resource.tags.should be_include("file")
        end

        it "should get tagged with the title" do
            @resource.tags.should be_include("yay")
        end

        it "should get tagged with each name in the title if the title is a qualified class name" do
            resource = Puppet::Parser::Resource.new(:type => "file", :title => "one::two", :scope => @scope, :source => mock('source'))
            resource.tags.should be_include("one")
            resource.tags.should be_include("two")
        end

        it "should get tagged with each name in the type if the type is a qualified class name" do
            resource = Puppet::Parser::Resource.new(:type => "one::two", :title => "whatever", :scope => @scope, :source => mock('source'))
            resource.tags.should be_include("one")
            resource.tags.should be_include("two")
        end

        it "should not get tagged with non-alphanumeric titles" do
            resource = Puppet::Parser::Resource.new(:type => "file", :title => "this is a test", :scope => @scope, :source => mock('source'))
            resource.tags.should_not be_include("this is a test")
        end

        it "should fail on tags containing '*' characters" do
            lambda { @resource.tag("bad*tag") }.should raise_error(Puppet::ParseError)
        end

        it "should fail on tags starting with '-' characters" do
            lambda { @resource.tag("-badtag") }.should raise_error(Puppet::ParseError)
        end

        it "should fail on tags containing ' ' characters" do
            lambda { @resource.tag("bad tag") }.should raise_error(Puppet::ParseError)
        end

        it "should allow alpha tags" do
            lambda { @resource.tag("good_tag") }.should_not raise_error(Puppet::ParseError)
        end
    end

    describe "when merging overrides" do
        before do
            @source = "source1"
            @resource = mkresource :source => @source
            @override = mkresource :source => @source
        end

        it "should fail when the override was not created by a parent class" do
            @override.source = "source2"
            @override.source.expects(:child_of?).with("source1").returns(false)
            lambda { @resource.merge(@override) }.should raise_error(Puppet::ParseError)
        end

        it "should succeed when the override was created in the current scope" do
            @resource.source = "source3"
            @override.source = @resource.source
            @override.source.expects(:child_of?).with("source3").never
            params = {:a => :b, :c => :d}
            @override.expects(:params).returns(params)
            @resource.expects(:override_parameter).with(:b)
            @resource.expects(:override_parameter).with(:d)
            @resource.merge(@override)
        end

        it "should succeed when a parent class created the override" do
            @resource.source = "source3"
            @override.source = "source4"
            @override.source.expects(:child_of?).with("source3").returns(true)
            params = {:a => :b, :c => :d}
            @override.expects(:params).returns(params)
            @resource.expects(:override_parameter).with(:b)
            @resource.expects(:override_parameter).with(:d)
            @resource.merge(@override)
        end

        it "should add new parameters when the parameter is not set" do
            @source.stubs(:child_of?).returns true
            @override.set_parameter(:testing, "value")
            @resource.merge(@override)

            @resource[:testing].should == "value"
        end

        it "should replace existing parameter values" do
            @source.stubs(:child_of?).returns true
            @resource.set_parameter(:testing, "old")
            @override.set_parameter(:testing, "value")

            @resource.merge(@override)

            @resource[:testing].should == "value"
        end

        it "should add values to the parameter when the override was created with the '+>' syntax" do
            @source.stubs(:child_of?).returns true
            param = Puppet::Parser::Resource::Param.new(:name => :testing, :value => "testing", :source => @resource.source)
            param.add = true

            @override.set_parameter(param)

            @resource.set_parameter(:testing, "other")

            @resource.merge(@override)

            @resource[:testing].should == %w{other testing}
        end


    end

    it "should be able to be converted to a normal resource" do
        @source = stub 'scope', :name => "myscope"
        @resource = mkresource :source => @source
        @resource.should respond_to(:to_resource)
    end

    it "should use its resource converter to convert to a transportable resource" do
        @source = stub 'scope', :name => "myscope"
        @resource = mkresource :source => @source

        newresource = Puppet::Resource.new(:file, "/my")
        Puppet::Resource.expects(:new).returns(newresource)

        newresource.expects(:to_trans).returns "mytrans"

        @resource.to_trans.should == "mytrans"
    end

    it "should return nil if converted to a transportable resource and it is virtual" do
        @source = stub 'scope', :name => "myscope"
        @resource = mkresource :source => @source

        @resource.expects(:virtual?).returns true
        @resource.to_trans.should be_nil
    end

    describe "when being converted to a resource" do
        before do
            @source = stub 'scope', :name => "myscope"
            @parser_resource = mkresource :source => @source, :params => {:foo => "bar", :fee => "fum"}
        end

        it "should create an instance of Puppet::Resource" do
            @parser_resource.to_resource.should be_instance_of(Puppet::Resource)
        end

        it "should set the type correctly on the Puppet::Resource" do
            @parser_resource.to_resource.type.should == @parser_resource.type
        end

        it "should set the title correctly on the Puppet::Resource" do
            @parser_resource.to_resource.title.should == @parser_resource.title
        end

        it "should copy over all of the parameters" do
            result = @parser_resource.to_resource.to_hash

            # The name will be in here, also.
            result[:foo].should == "bar"
            result[:fee].should == "fum"
        end

        it "should copy over the tags" do
            @parser_resource.tag "foo"
            @parser_resource.tag "bar"

            @parser_resource.to_resource.tags.should == @parser_resource.tags
        end

        it "should copy over the line" do
            @parser_resource.line = 40
            @parser_resource.to_resource.line.should == 40
        end

        it "should copy over the file" do
            @parser_resource.file = "/my/file"
            @parser_resource.to_resource.file.should == "/my/file"
        end

        it "should convert any parser resource references to Puppet::Resource::Reference instances" do
            ref = Puppet::Parser::Resource::Reference.new(:title => "/my/file", :type => "file")
            @parser_resource = mkresource :source => @source, :params => {:foo => "bar", :fee => ref}
            result = @parser_resource.to_resource
            result[:fee].should == Puppet::Resource::Reference.new(:file, "/my/file")
        end

        it "should convert any parser resource references to Puppet::Resource::Reference instances even if they are in an array" do
            ref = Puppet::Parser::Resource::Reference.new(:title => "/my/file", :type => "file")
            @parser_resource = mkresource :source => @source, :params => {:foo => "bar", :fee => ["a", ref]}
            result = @parser_resource.to_resource
            result[:fee].should == ["a", Puppet::Resource::Reference.new(:file, "/my/file")]
        end

        it "should convert any parser resource references to Puppet::Resource::Reference instances even if they are in an array of array, and even deeper" do
            ref1 = Puppet::Parser::Resource::Reference.new(:title => "/my/file1", :type => "file")
            ref2 = Puppet::Parser::Resource::Reference.new(:title => "/my/file2", :type => "file")
            @parser_resource = mkresource :source => @source, :params => {:foo => "bar", :fee => ["a", [ref1,ref2]]}
            result = @parser_resource.to_resource
            result[:fee].should == ["a", Puppet::Resource::Reference.new(:file, "/my/file1"), Puppet::Resource::Reference.new(:file, "/my/file2")]
        end
    end
end