3
3
require File.dirname(__FILE__) + '/../spec_helper'
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)
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)
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
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))
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))
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))
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)
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
45
it "should have a method for adding tags" do
46
Puppet::Type.type(:mount).new(:name => "foo").should respond_to(:tags)
49
it "should use the tagging module" do
50
Puppet::Type.type(:mount).ancestors.should be_include(Puppet::Util::Tagging)
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)
57
resource.expects(:tag).with(:tag1, :tag2)
59
resource.tags = [:tag1,:tag2]
62
it "should add the current type as tag" do
63
resource = Puppet::Type.type(:mount).new(:name => "foo")
66
resource.expects(:tag).with(:mount)
68
resource.tags = [:tag1,:tag2]
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?)
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?)
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
83
catalog.add_resource resource
85
resource.version.should == 50
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
92
describe "when choosing a default provider" do
93
it "should choose the provider with the highest specificity" do
95
type = Puppet::Type.newtype(:defaultprovidertest) do
96
newparam(:name) do end
99
basic = type.provide(:basic) {}
100
greater = type.provide(:greater) {}
102
basic.stubs(:specificity).returns 1
103
greater.stubs(:specificity).returns 2
105
type.defaultprovider.should equal(greater)
109
describe "when initializing" do
110
describe "and passed a TransObject" do
112
trans = Puppet::TransObject.new("/foo", :mount)
113
lambda { Puppet::Type.type(:mount).new(trans) }.should raise_error(Puppet::DevError)
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"
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]"
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"
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")
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
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"
158
it "should work when hash keys are provided as strings" do
159
Puppet::Type.type(:mount).new("title" => "/yay").title.should == "/yay"
162
it "should work when hash keys are provided as symbols" do
163
Puppet::Type.type(:mount).new(:title => "/yay").title.should == "/yay"
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"
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"
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
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"
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
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)
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")
199
Puppet::Type.type(:mount).new(resource).name.should == "/foo"
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)
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")
212
Puppet::Type.type(:mount).any_instance.stubs(:newattr).with do |param, hash|
217
Puppet::Type.type(:mount).new(resource)
219
set[-1].should == :noop
220
set[-2].should == :atboot
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")
229
Puppet::Type.type(:mount).any_instance.stubs(:newattr).with do |param, hash|
234
Puppet::Type.type(:mount).new(resource)
235
set[0].should == :name
236
set[1].should == :provider
239
# This one is really hard to test :/
240
it "should each default immediately if no value is provided" do
242
Puppet::Type.type(:package).any_instance.stubs(:set_default).with { |value| defaults << value; true }
244
Puppet::Type.type(:package).new :name => "whatever"
246
defaults[0].should == :provider
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}
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
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)
262
describe "when converting a hash to a Puppet::Resource instance" do
264
@type = Puppet::Type.type(:mount)
267
it "should treat a :title key as the title of the resource" do
268
@type.hash2resource(:name => "/foo", :title => "foo").title.should == "foo"
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"
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
278
@type.hash2resource(:myname => "foo").title.should == "foo"
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
284
lambda { @type.hash2resource(:myname => "foo", :name => 'bar') }.should raise_error(Puppet::Error)
287
[:catalog].each do |attr|
288
it "should use any provided #{attr}" do
289
@type.hash2resource(:name => "foo", attr => "eh").send(attr).should == "eh"
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"}
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
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
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"
313
describe "when retrieving current property values" do
7
314
# Use 'mount' as an example, because it doesn't override 'retrieve'
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)
13
after { Puppet::Type.type(:mount).clear }
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) }