~nvalcarcel/ubuntu/lucid/puppet/fix-546677

« back to all changes in this revision

Viewing changes to spec/unit/type.rb

  • Committer: Bazaar Package Importer
  • Author(s): Chuck Short
  • Date: 2009-12-23 00:48:10 UTC
  • mfrom: (1.1.10 upstream) (3.1.7 squeeze)
  • Revision ID: james.westby@ubuntu.com-20091223004810-3i4oryds922g5n59
Tags: 0.25.1-3ubuntu1
* Merge from debian testing.  Remaining changes:
  - debian/rules:
    + Don't start puppet when first installing puppet.
  - debian/puppet.conf, lib/puppet/defaults.rb:
    + Move templates to /etc/puppet
  - lib/puppet/defaults.rb:
    + Fix /var/lib/puppet/state ownership.
  - man/man8/puppet.conf.8: 
    + Fix broken URL in manpage.
  - debian/control:
    + Update maintainer accordint to spec.
    + Puppetmaster Recommends -> Suggests
    + Created puppet-testsuite as a seperate. Allow the users to run puppet's 
      testsuite.
  - tests/Rakefile: Fix rakefile so that the testsuite can acutally be ran.

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
require File.dirname(__FILE__) + '/../spec_helper'
4
4
 
5
5
describe Puppet::Type do
6
 
    describe "when retrieving current properties" do
 
6
    it "should include the Cacher module" do
 
7
        Puppet::Type.ancestors.should be_include(Puppet::Util::Cacher)
 
8
    end
 
9
 
 
10
    it "should use its catalog as its expirer" do
 
11
        catalog = Puppet::Resource::Catalog.new
 
12
        resource = Puppet::Type.type(:mount).new(:name => "foo", :fstype => "bar", :pass => 1, :ensure => :present)
 
13
        resource.catalog = catalog
 
14
        resource.expirer.should equal(catalog)
 
15
    end
 
16
 
 
17
    it "should do nothing when asked to expire when it has no catalog" do
 
18
        resource = Puppet::Type.type(:mount).new(:name => "foo", :fstype => "bar", :pass => 1, :ensure => :present)
 
19
        lambda { resource.expire }.should_not raise_error
 
20
    end
 
21
 
 
22
    it "should be able to retrieve a property by name" do
 
23
        resource = Puppet::Type.type(:mount).new(:name => "foo", :fstype => "bar", :pass => 1, :ensure => :present)
 
24
        resource.property(:fstype).must be_instance_of(Puppet::Type.type(:mount).attrclass(:fstype))
 
25
    end
 
26
 
 
27
    it "should be able to retrieve a parameter by name" do
 
28
        resource = Puppet::Type.type(:mount).new(:name => "foo", :fstype => "bar", :pass => 1, :ensure => :present)
 
29
        resource.parameter(:name).must be_instance_of(Puppet::Type.type(:mount).attrclass(:name))
 
30
    end
 
31
 
 
32
    it "should be able to retrieve a property by name using the :parameter method" do
 
33
        resource = Puppet::Type.type(:mount).new(:name => "foo", :fstype => "bar", :pass => 1, :ensure => :present)
 
34
        resource.parameter(:fstype).must be_instance_of(Puppet::Type.type(:mount).attrclass(:fstype))
 
35
    end
 
36
 
 
37
    it "should have a method for setting default values for resources" do
 
38
        Puppet::Type.type(:mount).new(:name => "foo").should respond_to(:set_default)
 
39
    end
 
40
 
 
41
    it "should do nothing for attributes that have no defaults and no specified value" do
 
42
        Puppet::Type.type(:mount).new(:name => "foo").parameter(:noop).should be_nil
 
43
    end
 
44
 
 
45
    it "should have a method for adding tags" do
 
46
        Puppet::Type.type(:mount).new(:name => "foo").should respond_to(:tags)
 
47
    end
 
48
 
 
49
    it "should use the tagging module" do
 
50
        Puppet::Type.type(:mount).ancestors.should be_include(Puppet::Util::Tagging)
 
51
    end
 
52
 
 
53
    it "should delegate to the tagging module when tags are added" do
 
54
        resource = Puppet::Type.type(:mount).new(:name => "foo")
 
55
        resource.stubs(:tag).with(:mount)
 
56
 
 
57
        resource.expects(:tag).with(:tag1, :tag2)
 
58
 
 
59
        resource.tags = [:tag1,:tag2]
 
60
    end
 
61
 
 
62
    it "should add the current type as tag" do
 
63
        resource = Puppet::Type.type(:mount).new(:name => "foo")
 
64
        resource.stubs(:tag)
 
65
 
 
66
        resource.expects(:tag).with(:mount)
 
67
 
 
68
        resource.tags = [:tag1,:tag2]
 
69
    end
 
70
 
 
71
    it "should have a method to know if the resource is exported" do
 
72
        Puppet::Type.type(:mount).new(:name => "foo").should respond_to(:exported?)
 
73
    end
 
74
 
 
75
    it "should have a method to know if the resource is virtual" do
 
76
        Puppet::Type.type(:mount).new(:name => "foo").should respond_to(:virtual?)
 
77
    end
 
78
 
 
79
    it "should consider its version to be its catalog version" do
 
80
        resource = Puppet::Type.type(:mount).new(:name => "foo")
 
81
        catalog = Puppet::Resource::Catalog.new
 
82
        catalog.version = 50
 
83
        catalog.add_resource resource
 
84
 
 
85
        resource.version.should == 50
 
86
    end
 
87
 
 
88
    it "should consider its version to be zero if it has no catalog" do
 
89
        Puppet::Type.type(:mount).new(:name => "foo").version.should == 0
 
90
    end
 
91
 
 
92
    describe "when choosing a default provider" do
 
93
        it "should choose the provider with the highest specificity" do
 
94
            # Make a fake type
 
95
            type = Puppet::Type.newtype(:defaultprovidertest) do
 
96
                newparam(:name) do end
 
97
            end
 
98
 
 
99
            basic = type.provide(:basic) {}
 
100
            greater = type.provide(:greater) {}
 
101
 
 
102
            basic.stubs(:specificity).returns 1
 
103
            greater.stubs(:specificity).returns 2
 
104
 
 
105
            type.defaultprovider.should equal(greater)
 
106
        end
 
107
    end
 
108
 
 
109
    describe "when initializing" do
 
110
        describe "and passed a TransObject" do
 
111
            it "should fail" do
 
112
                trans = Puppet::TransObject.new("/foo", :mount)
 
113
                lambda { Puppet::Type.type(:mount).new(trans) }.should raise_error(Puppet::DevError)
 
114
            end
 
115
        end
 
116
 
 
117
        describe "and passed a Puppet::Resource instance" do
 
118
            it "should set its title to the title of the resource if the resource type is equal to the current type" do
 
119
                resource = Puppet::Resource.new(:mount, "/foo", :name => "/other")
 
120
                Puppet::Type.type(:mount).new(resource).title.should == "/foo"
 
121
            end
 
122
 
 
123
            it "should set its title to the resource reference if the resource type is not equal to the current type" do
 
124
                resource = Puppet::Resource.new(:user, "foo")
 
125
                Puppet::Type.type(:mount).new(resource).title.should == "User[foo]"
 
126
            end
 
127
 
 
128
            [:line, :file, :catalog, :exported, :virtual].each do |param|
 
129
                it "should copy '#{param}' from the resource if present" do
 
130
                    resource = Puppet::Resource.new(:mount, "/foo")
 
131
                    resource.send(param.to_s + "=", "foo")
 
132
                    resource.send(param.to_s + "=", "foo")
 
133
                    Puppet::Type.type(:mount).new(resource).send(param).should == "foo"
 
134
                end
 
135
            end
 
136
 
 
137
            it "should copy any tags from the resource" do
 
138
                resource = Puppet::Resource.new(:mount, "/foo")
 
139
                resource.tag "one", "two"
 
140
                tags = Puppet::Type.type(:mount).new(resource).tags
 
141
                tags.should be_include("one")
 
142
                tags.should be_include("two")
 
143
            end
 
144
 
 
145
            it "should copy the resource's parameters as its own" do
 
146
                resource = Puppet::Resource.new(:mount, "/foo", :atboot => true, :fstype => "boo")
 
147
                params = Puppet::Type.type(:mount).new(resource).to_hash
 
148
                params[:fstype].should == "boo"
 
149
                params[:atboot].should == true
 
150
            end
 
151
        end
 
152
 
 
153
        describe "and passed a Hash" do
 
154
            it "should extract the title from the hash" do
 
155
                Puppet::Type.type(:mount).new(:title => "/yay").title.should == "/yay"
 
156
            end
 
157
 
 
158
            it "should work when hash keys are provided as strings" do
 
159
                Puppet::Type.type(:mount).new("title" => "/yay").title.should == "/yay"
 
160
            end
 
161
 
 
162
            it "should work when hash keys are provided as symbols" do
 
163
                Puppet::Type.type(:mount).new(:title => "/yay").title.should == "/yay"
 
164
            end
 
165
 
 
166
            it "should use the name from the hash as the title if no explicit title is provided" do
 
167
                Puppet::Type.type(:mount).new(:name => "/yay").title.should == "/yay"
 
168
            end
 
169
 
 
170
            it "should use the Resource Type's namevar to determine how to find the name in the hash" do
 
171
                Puppet::Type.type(:file).new(:path => "/yay").title.should == "/yay"
 
172
            end
 
173
 
 
174
            it "should fail if the namevar is not equal to :name and both :name and the namevar are provided" do
 
175
                lambda { Puppet::Type.type(:file).new(:path => "/yay", :name => "/foo") }.should raise_error(Puppet::Error)
 
176
                @type.stubs(:namevar).returns :myname
 
177
            end
 
178
 
 
179
            [:catalog].each do |param|
 
180
                it "should extract '#{param}' from the hash if present" do
 
181
                    Puppet::Type.type(:mount).new(:name => "/yay", param => "foo").send(param).should == "foo"
 
182
                end
 
183
            end
 
184
 
 
185
            it "should use any remaining hash keys as its parameters" do
 
186
                resource = Puppet::Type.type(:mount).new(:title => "/foo", :catalog => "foo", :atboot => true, :fstype => "boo")
 
187
                resource[:fstype].must == "boo"
 
188
                resource[:atboot].must == true
 
189
            end
 
190
        end
 
191
 
 
192
        it "should fail if any invalid attributes have been provided" do
 
193
            lambda { Puppet::Type.type(:mount).new(:title => "/foo", :nosuchattr => "whatever") }.should raise_error(Puppet::Error)
 
194
        end
 
195
 
 
196
        it "should set its name to the resource's title if the resource does not have a :name or namevar parameter set" do
 
197
            resource = Puppet::Resource.new(:mount, "/foo")
 
198
 
 
199
            Puppet::Type.type(:mount).new(resource).name.should == "/foo"
 
200
        end
 
201
 
 
202
        it "should fail if no title, name, or namevar are provided" do
 
203
            lambda { Puppet::Type.type(:file).new(:atboot => true) }.should raise_error(Puppet::Error)
 
204
        end
 
205
 
 
206
        it "should set the attributes in the order returned by the class's :allattrs method" do
 
207
            Puppet::Type.type(:mount).stubs(:allattrs).returns([:name, :atboot, :noop])
 
208
            resource = Puppet::Resource.new(:mount, "/foo", :name => "myname", :atboot => "myboot", :noop => "whatever")
 
209
 
 
210
            set = []
 
211
 
 
212
            Puppet::Type.type(:mount).any_instance.stubs(:newattr).with do |param, hash|
 
213
                set << param
 
214
                true
 
215
            end
 
216
 
 
217
            Puppet::Type.type(:mount).new(resource)
 
218
 
 
219
            set[-1].should == :noop
 
220
            set[-2].should == :atboot
 
221
        end
 
222
 
 
223
        it "should always set the name and then default provider before anything else" do
 
224
            Puppet::Type.type(:mount).stubs(:allattrs).returns([:provider, :name, :atboot])
 
225
            resource = Puppet::Resource.new(:mount, "/foo", :name => "myname", :atboot => "myboot")
 
226
 
 
227
            set = []
 
228
 
 
229
            Puppet::Type.type(:mount).any_instance.stubs(:newattr).with do |param, hash|
 
230
                set << param
 
231
                true
 
232
            end
 
233
 
 
234
            Puppet::Type.type(:mount).new(resource)
 
235
            set[0].should == :name
 
236
            set[1].should == :provider
 
237
        end
 
238
 
 
239
        # This one is really hard to test :/
 
240
        it "should each default immediately if no value is provided" do
 
241
            defaults = []
 
242
            Puppet::Type.type(:package).any_instance.stubs(:set_default).with { |value| defaults << value; true }
 
243
 
 
244
            Puppet::Type.type(:package).new :name => "whatever"
 
245
 
 
246
            defaults[0].should == :provider
 
247
        end
 
248
 
 
249
        it "should retain a copy of the originally provided parameters" do
 
250
            Puppet::Type.type(:mount).new(:name => "foo", :atboot => true, :noop => false).original_parameters.should == {:atboot => true, :noop => false}
 
251
        end
 
252
 
 
253
        it "should delete the name via the namevar from the originally provided parameters" do
 
254
            Puppet::Type.type(:file).new(:name => "/foo").original_parameters[:path].should be_nil
 
255
        end
 
256
    end
 
257
 
 
258
    it "should have a class method for converting a hash into a Puppet::Resource instance" do
 
259
        Puppet::Type.type(:mount).must respond_to(:hash2resource)
 
260
    end
 
261
 
 
262
    describe "when converting a hash to a Puppet::Resource instance" do
 
263
        before do
 
264
            @type = Puppet::Type.type(:mount)
 
265
        end
 
266
 
 
267
        it "should treat a :title key as the title of the resource" do
 
268
            @type.hash2resource(:name => "/foo", :title => "foo").title.should == "foo"
 
269
        end
 
270
 
 
271
        it "should use the name from the hash as the title if no explicit title is provided" do
 
272
            @type.hash2resource(:name => "foo").title.should == "foo"
 
273
        end
 
274
 
 
275
        it "should use the Resource Type's namevar to determine how to find the name in the hash" do
 
276
            @type.stubs(:namevar).returns :myname
 
277
 
 
278
            @type.hash2resource(:myname => "foo").title.should == "foo"
 
279
        end
 
280
 
 
281
        it "should fail if the namevar is not equal to :name and both :name and the namevar are provided" do
 
282
            @type.stubs(:namevar).returns :myname
 
283
 
 
284
            lambda { @type.hash2resource(:myname => "foo", :name => 'bar') }.should raise_error(Puppet::Error)
 
285
        end
 
286
 
 
287
        [:catalog].each do |attr|
 
288
            it "should use any provided #{attr}" do
 
289
                @type.hash2resource(:name => "foo", attr => "eh").send(attr).should == "eh"
 
290
            end
 
291
        end
 
292
 
 
293
        it "should set all provided parameters on the resource" do
 
294
            @type.hash2resource(:name => "foo", :fstype => "boo", :boot => "fee").to_hash.should == {:name => "foo", :fstype => "boo", :boot => "fee"}
 
295
        end
 
296
 
 
297
        it "should not set the title as a parameter on the resource" do
 
298
            @type.hash2resource(:name => "foo", :title => "eh")[:title].should be_nil
 
299
        end
 
300
 
 
301
        it "should not set the catalog as a parameter on the resource" do
 
302
            @type.hash2resource(:name => "foo", :catalog => "eh")[:catalog].should be_nil
 
303
        end
 
304
 
 
305
        it "should treat hash keys equivalently whether provided as strings or symbols" do
 
306
            resource = @type.hash2resource("name" => "foo", "title" => "eh", "fstype" => "boo")
 
307
            resource.title.should == "eh"
 
308
            resource[:name].should == "foo"
 
309
            resource[:fstype].should == "boo"
 
310
        end
 
311
    end
 
312
 
 
313
    describe "when retrieving current property values" do
7
314
        # Use 'mount' as an example, because it doesn't override 'retrieve'
8
315
        before do
9
 
            @resource = Puppet::Type.type(:mount).create(:name => "foo", :fstype => "bar", :pass => 1, :ensure => :present)
 
316
            @resource = Puppet::Type.type(:mount).new(:name => "foo", :fstype => "bar", :pass => 1, :ensure => :present)
10
317
            @properties = {}
11
318
        end
12
319
 
13
 
        after { Puppet::Type.type(:mount).clear }
14
 
 
15
320
        it "should return a hash containing values for all set properties" do
16
321
            values = @resource.retrieve
17
322
            [@resource.property(:fstype), @resource.property(:pass)].each { |property| values.should be_include(property) }
38
343
 
39
344
    describe "when in a catalog" do
40
345
        before do
41
 
            @catalog = Puppet::Node::Catalog.new
42
 
            @container = Puppet::Type.type(:component).create(:name => "container")
43
 
            @one = Puppet::Type.type(:file).create(:path => "/file/one")
44
 
            @two = Puppet::Type.type(:file).create(:path => "/file/two")
 
346
            @catalog = Puppet::Resource::Catalog.new
 
347
            @container = Puppet::Type.type(:component).new(:name => "container")
 
348
            @one = Puppet::Type.type(:file).new(:path => "/file/one")
 
349
            @two = Puppet::Type.type(:file).new(:path => "/file/two")
 
350
 
45
351
            @catalog.add_resource @container
46
352
            @catalog.add_resource @one
47
353
            @catalog.add_resource @two
61
367
            @catalog.clear(true)
62
368
        end
63
369
    end
 
370
 
 
371
    describe "when managing relationships" do
 
372
    end
 
373
end
 
374
 
 
375
describe Puppet::Type::RelationshipMetaparam do
 
376
    it "should be a subclass of Puppet::Parameter" do
 
377
        Puppet::Type::RelationshipMetaparam.superclass.should equal(Puppet::Parameter)
 
378
    end
 
379
 
 
380
    it "should be able to produce a list of subclasses" do
 
381
        Puppet::Type::RelationshipMetaparam.should respond_to(:subclasses)
 
382
    end
 
383
 
 
384
    describe "when munging relationships" do
 
385
        before do
 
386
            @resource = Puppet::Type.type(:mount).new :name => "/foo"
 
387
            @metaparam = Puppet::Type.metaparamclass(:require).new :resource => @resource
 
388
        end
 
389
 
 
390
        it "should accept Puppet::Resource::Reference instances" do
 
391
            ref = Puppet::Resource::Reference.new(:file, "/foo")
 
392
            @metaparam.munge(ref)[0].should equal(ref)
 
393
        end
 
394
 
 
395
        it "should turn any string into a Puppet::Resource::Reference" do
 
396
            @metaparam.munge("File[/ref]")[0].should be_instance_of(Puppet::Resource::Reference)
 
397
        end
 
398
    end
 
399
 
 
400
    it "should be able to validate relationships" do
 
401
        Puppet::Type.metaparamclass(:require).new(:resource => mock("resource")).should respond_to(:validate_relationship)
 
402
    end
 
403
 
 
404
    it "should fail if any specified resource is not found in the catalog" do
 
405
        catalog = mock 'catalog'
 
406
        resource = stub 'resource', :catalog => catalog, :ref => "resource"
 
407
 
 
408
        param = Puppet::Type.metaparamclass(:require).new(:resource => resource, :value => %w{Foo[bar] Class[test]})
 
409
 
 
410
        catalog.expects(:resource).with("Foo[bar]").returns "something"
 
411
        catalog.expects(:resource).with("Class[test]").returns nil
 
412
 
 
413
        param.expects(:fail).with { |string| string.include?("Class[test]") }
 
414
 
 
415
        param.validate_relationship
 
416
    end
64
417
end