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
|
#!/usr/bin/env rspec
require 'spec_helper'
describe Puppet::Type.type(:service) do
it "should have an :enableable feature that requires the :enable, :disable, and :enabled? methods" do
Puppet::Type.type(:service).provider_feature(:enableable).methods.should == [:disable, :enable, :enabled?]
end
it "should have a :refreshable feature that requires the :restart method" do
Puppet::Type.type(:service).provider_feature(:refreshable).methods.should == [:restart]
end
end
describe Puppet::Type.type(:service), "when validating attributes" do
[:name, :binary, :hasstatus, :path, :pattern, :start, :restart, :stop, :status, :hasrestart, :control].each do |param|
it "should have a #{param} parameter" do
Puppet::Type.type(:service).attrtype(param).should == :param
end
end
[:ensure, :enable].each do |param|
it "should have an #{param} property" do
Puppet::Type.type(:service).attrtype(param).should == :property
end
end
end
describe Puppet::Type.type(:service), "when validating attribute values" do
before do
@provider = stub 'provider', :class => Puppet::Type.type(:service).defaultprovider, :clear => nil, :controllable? => false
Puppet::Type.type(:service).defaultprovider.stubs(:new).returns(@provider)
end
it "should support :running as a value to :ensure" do
Puppet::Type.type(:service).new(:name => "yay", :ensure => :running)
end
it "should support :stopped as a value to :ensure" do
Puppet::Type.type(:service).new(:name => "yay", :ensure => :stopped)
end
it "should alias the value :true to :running in :ensure" do
svc = Puppet::Type.type(:service).new(:name => "yay", :ensure => true)
svc.should(:ensure).should == :running
end
it "should alias the value :false to :stopped in :ensure" do
svc = Puppet::Type.type(:service).new(:name => "yay", :ensure => false)
svc.should(:ensure).should == :stopped
end
it "should support :true as a value to :enable" do
Puppet::Type.type(:service).new(:name => "yay", :enable => :true)
end
it "should support :false as a value to :enable" do
Puppet::Type.type(:service).new(:name => "yay", :enable => :false)
end
it "should support :true as a value to :hasstatus" do
Puppet::Type.type(:service).new(:name => "yay", :hasstatus => :true)
end
it "should support :false as a value to :hasstatus" do
Puppet::Type.type(:service).new(:name => "yay", :hasstatus => :false)
end
it "should specify :true as the default value of hasstatus" do
Puppet::Type.type(:service).new(:name => "yay")[:hasstatus].should == :true
end
it "should support :true as a value to :hasrestart" do
Puppet::Type.type(:service).new(:name => "yay", :hasrestart => :true)
end
it "should support :false as a value to :hasrestart" do
Puppet::Type.type(:service).new(:name => "yay", :hasrestart => :false)
end
it "should allow setting the :enable parameter if the provider has the :enableable feature" do
Puppet::Type.type(:service).defaultprovider.stubs(:supports_parameter?).returns(true)
Puppet::Type.type(:service).defaultprovider.expects(:supports_parameter?).with(Puppet::Type.type(:service).attrclass(:enable)).returns(true)
svc = Puppet::Type.type(:service).new(:name => "yay", :enable => true)
svc.should(:enable).should == :true
end
it "should not allow setting the :enable parameter if the provider is missing the :enableable feature" do
Puppet::Type.type(:service).defaultprovider.stubs(:supports_parameter?).returns(true)
Puppet::Type.type(:service).defaultprovider.expects(:supports_parameter?).with(Puppet::Type.type(:service).attrclass(:enable)).returns(false)
svc = Puppet::Type.type(:service).new(:name => "yay", :enable => true)
svc.should(:enable).should be_nil
end
it "should split paths on ':'" do
FileTest.stubs(:exist?).returns(true)
FileTest.stubs(:directory?).returns(true)
svc = Puppet::Type.type(:service).new(:name => "yay", :path => "/one/two:/three/four")
svc[:path].should == %w{/one/two /three/four}
end
it "should accept arrays of paths joined by ':'" do
FileTest.stubs(:exist?).returns(true)
FileTest.stubs(:directory?).returns(true)
svc = Puppet::Type.type(:service).new(:name => "yay", :path => ["/one:/two", "/three:/four"])
svc[:path].should == %w{/one /two /three /four}
end
end
describe Puppet::Type.type(:service), "when setting default attribute values" do
it "should default to the provider's default path if one is available" do
FileTest.stubs(:directory?).returns(true)
FileTest.stubs(:exist?).returns(true)
Puppet::Type.type(:service).defaultprovider.stubs(:respond_to?).returns(true)
Puppet::Type.type(:service).defaultprovider.stubs(:defpath).returns("testing")
svc = Puppet::Type.type(:service).new(:name => "other")
svc[:path].should == ["testing"]
end
it "should default 'pattern' to the binary if one is provided" do
svc = Puppet::Type.type(:service).new(:name => "other", :binary => "/some/binary")
svc[:pattern].should == "/some/binary"
end
it "should default 'pattern' to the name if no pattern is provided" do
svc = Puppet::Type.type(:service).new(:name => "other")
svc[:pattern].should == "other"
end
it "should default 'control' to the upcased service name with periods replaced by underscores if the provider supports the 'controllable' feature" do
provider = stub 'provider', :controllable? => true, :class => Puppet::Type.type(:service).defaultprovider, :clear => nil
Puppet::Type.type(:service).defaultprovider.stubs(:new).returns(provider)
svc = Puppet::Type.type(:service).new(:name => "nfs.client")
svc[:control].should == "NFS_CLIENT_START"
end
end
describe Puppet::Type.type(:service), "when retrieving the host's current state" do
before do
@service = Puppet::Type.type(:service).new(:name => "yay")
end
it "should use the provider's status to determine whether the service is running" do
@service.provider.expects(:status).returns(:yepper)
@service[:ensure] = :running
@service.property(:ensure).retrieve.should == :yepper
end
it "should ask the provider whether it is enabled" do
@service.provider.class.stubs(:supports_parameter?).returns(true)
@service.provider.expects(:enabled?).returns(:yepper)
@service[:enable] = true
@service.property(:enable).retrieve.should == :yepper
end
end
describe Puppet::Type.type(:service), "when changing the host" do
before do
@service = Puppet::Type.type(:service).new(:name => "yay")
end
it "should start the service if it is supposed to be running" do
@service[:ensure] = :running
@service.provider.expects(:start)
@service.property(:ensure).sync
end
it "should stop the service if it is supposed to be stopped" do
@service[:ensure] = :stopped
@service.provider.expects(:stop)
@service.property(:ensure).sync
end
it "should enable the service if it is supposed to be enabled" do
@service.provider.class.stubs(:supports_parameter?).returns(true)
@service[:enable] = true
@service.provider.expects(:enable)
@service.property(:enable).sync
end
it "should disable the service if it is supposed to be disabled" do
@service.provider.class.stubs(:supports_parameter?).returns(true)
@service[:enable] = false
@service.provider.expects(:disable)
@service.property(:enable).sync
end
it "should sync the service's enable state when changing the state of :ensure if :enable is being managed" do
@service.provider.class.stubs(:supports_parameter?).returns(true)
@service[:enable] = false
@service[:ensure] = :stopped
@service.property(:enable).expects(:retrieve).returns("whatever")
@service.property(:enable).expects(:insync?).returns(false)
@service.property(:enable).expects(:sync)
@service.provider.stubs(:stop)
@service.property(:ensure).sync
end
end
describe Puppet::Type.type(:service), "when refreshing the service" do
before do
@service = Puppet::Type.type(:service).new(:name => "yay")
end
it "should restart the service if it is running" do
@service[:ensure] = :running
@service.provider.expects(:status).returns(:running)
@service.provider.expects(:restart)
@service.refresh
end
it "should restart the service if it is running, even if it is supposed to stopped" do
@service[:ensure] = :stopped
@service.provider.expects(:status).returns(:running)
@service.provider.expects(:restart)
@service.refresh
end
it "should not restart the service if it is not running" do
@service[:ensure] = :running
@service.provider.expects(:status).returns(:stopped)
@service.refresh
end
it "should add :ensure as a property if it is not being managed" do
@service.provider.expects(:status).returns(:running)
@service.provider.expects(:restart)
@service.refresh
end
end
|