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
|
#!/usr/bin/env ruby
#
# Created by Luke Kanies on 2007-11-12.
# Copyright (c) 2007. All rights reserved.
require File.dirname(__FILE__) + '/../spec_helper'
require 'puppet/agent'
describe Puppet::Agent do
it "should include the Plugin Handler module" do
Puppet::Agent.ancestors.should be_include(Puppet::Agent::PluginHandler)
end
it "should include the Fact Handler module" do
Puppet::Agent.ancestors.should be_include(Puppet::Agent::FactHandler)
end
it "should include the Locker module" do
Puppet::Agent.ancestors.should be_include(Puppet::Agent::Locker)
end
end
describe Puppet::Agent, "when executing a catalog run" do
before do
Puppet.settings.stubs(:use).returns(true)
@agent = Puppet::Agent.new
@agent.stubs(:splay)
@agent.stubs(:lock).yields.then.returns true
end
it "should splay" do
Puppet::Util.sync(:puppetrun).stubs(:synchronize)
@agent.expects(:splay)
@agent.run
end
it "should use a global mutex to make sure no other thread is executing the catalog" do
sync = mock 'sync'
Puppet::Util.expects(:sync).with(:puppetrun).returns sync
sync.expects(:synchronize)
@agent.expects(:retrieve_config).never # i.e., if we don't yield, we don't retrieve the config
@agent.run
end
it "should retrieve the catalog if a lock is attained" do
@agent.expects(:lock).yields.then.returns true
@agent.expects(:retrieve_catalog)
@agent.run
end
it "should log and do nothing if the lock cannot be acquired" do
@agent.expects(:lock).returns false
@agent.expects(:retrieve_catalog).never
Puppet.expects(:notice)
@agent.run
end
it "should retrieve the catalog" do
@agent.expects(:retrieve_catalog)
@agent.run
end
it "should log a failure and do nothing if no catalog can be retrieved" do
@agent.expects(:retrieve_catalog).returns nil
Puppet.expects(:err)
@agent.run
end
it "should apply the catalog with all options to :run" do
catalog = stub 'catalog', :retrieval_duration= => nil
@agent.expects(:retrieve_catalog).returns catalog
catalog.expects(:apply).with(:one => true)
@agent.run :one => true
end
it "should benchmark how long it takes to apply the catalog" do
@agent.expects(:benchmark).with(:notice, "Finished catalog run")
catalog = stub 'catalog', :retrieval_duration= => nil
@agent.expects(:retrieve_catalog).returns catalog
catalog.expects(:apply).never # because we're not yielding
@agent.run
end
it "should HUP itself if it should be restarted" do
catalog = stub 'catalog', :retrieval_duration= => nil, :apply => nil
@agent.expects(:retrieve_catalog).returns catalog
Process.expects(:kill).with(:HUP, $$)
@agent.expects(:restart?).returns true
@agent.run
end
it "should not HUP itself if it should not be restarted" do
catalog = stub 'catalog', :retrieval_duration= => nil, :apply => nil
@agent.expects(:retrieve_catalog).returns catalog
Process.expects(:kill).never
@agent.expects(:restart?).returns false
@agent.run
end
end
describe Puppet::Agent, "when retrieving a catalog" do
before do
Puppet.settings.stubs(:use).returns(true)
@agent = Puppet::Agent.new
@catalog = Puppet::Resource::Catalog.new
end
it "should use the Catalog class to get its catalog" do
Puppet::Resource::Catalog.expects(:get).returns @catalog
@agent.retrieve_catalog
end
it "should use its Facter name to retrieve the catalog" do
Facter.stubs(:value).returns "eh"
Facter.expects(:value).with("hostname").returns "myhost"
Puppet::Resource::Catalog.expects(:get).with { |name, options| name == "myhost" }.returns @catalog
@agent.retrieve_catalog
end
it "should default to returning a catalog retrieved directly from the server, skipping the cache" do
Puppet::Resource::Catalog.expects(:get).with { |name, options| options[:use_cache] == false }.returns @catalog
@agent.retrieve_catalog.should == @catalog
end
it "should return the cached catalog when no catalog can be retrieved from the server" do
Puppet::Resource::Catalog.expects(:get).with { |name, options| options[:use_cache] == false }.returns nil
Puppet::Resource::Catalog.expects(:get).with { |name, options| options[:use_cache] == true }.returns @catalog
@agent.retrieve_catalog.should == @catalog
end
it "should return the cached catalog when retrieving the remote catalog throws an exception" do
Puppet::Resource::Catalog.expects(:get).with { |name, options| options[:use_cache] == false }.raises "eh"
Puppet::Resource::Catalog.expects(:get).with { |name, options| options[:use_cache] == true }.returns @catalog
@agent.retrieve_catalog.should == @catalog
end
it "should return nil if no cached catalog is available and no catalog can be retrieved from the server" do
Puppet::Resource::Catalog.expects(:get).with { |name, options| options[:use_cache] == false }.returns nil
Puppet::Resource::Catalog.expects(:get).with { |name, options| options[:use_cache] == true }.returns nil
@agent.retrieve_catalog.should be_nil
end
it "should record the retrieval time with the catalog" do
@agent.expects(:thinmark).yields.then.returns 10
Puppet::Resource::Catalog.expects(:get).returns @catalog
@catalog.expects(:retrieval_duration=).with 10
@agent.retrieve_catalog
end
it "should write the catalog's class file" do
@catalog.expects(:write_class_file)
Puppet::Resource::Catalog.expects(:get).returns @catalog
@agent.retrieve_catalog
end
it "should mark the catalog as a host catalog" do
@catalog.expects(:host_config=).with true
Puppet::Resource::Catalog.expects(:get).returns @catalog
@agent.retrieve_catalog
end
it "should return nil if there is an error while retrieving the catalog" do
Puppet::Resource::Catalog.expects(:get).raises "eh"
@agent.retrieve_catalog.should be_nil
end
end
describe Puppet::Agent, "when preparing for a run" do
before do
Puppet.settings.stubs(:use).returns(true)
@agent = Puppet::Agent.new
@agent.stubs(:dostorage)
@agent.stubs(:upload_facts)
@facts = {"one" => "two", "three" => "four"}
end
it "should initialize the metadata store" do
@agent.class.stubs(:facts).returns(@facts)
@agent.expects(:dostorage)
@agent.prepare
end
it "should download fact plugins" do
@agent.stubs(:dostorage)
@agent.expects(:download_fact_plugins)
@agent.prepare
end
it "should download plugins" do
@agent.stubs(:dostorage)
@agent.expects(:download_plugins)
@agent.prepare
end
it "should upload facts to use for catalog retrieval" do
@agent.stubs(:dostorage)
@agent.expects(:upload_facts)
@agent.prepare
end
end
describe Puppet::Agent, " when using the cached catalog" do
before do
Puppet.settings.stubs(:use).returns(true)
@agent = Puppet::Agent.new
@facts = {"one" => "two", "three" => "four"}
end
it "should return do nothing and true if there is already an in-memory catalog" do
@agent.catalog = :whatever
Puppet::Agent.publicize_methods :use_cached_config do
@agent.use_cached_config.should be_true
end
end
it "should return do nothing and false if it has been told there is a failure and :nocacheonfailure is enabled" do
Puppet.settings.expects(:value).with(:usecacheonfailure).returns(false)
Puppet::Agent.publicize_methods :use_cached_config do
@agent.use_cached_config(true).should be_false
end
end
it "should return false if no cached catalog can be found" do
@agent.expects(:retrievecache).returns(nil)
Puppet::Agent.publicize_methods :use_cached_config do
@agent.use_cached_config().should be_false
end
end
it "should return false if the cached catalog cannot be instantiated" do
YAML.expects(:load).raises(ArgumentError)
@agent.expects(:retrievecache).returns("whatever")
Puppet::Agent.publicize_methods :use_cached_config do
@agent.use_cached_config().should be_false
end
end
it "should warn if the cached catalog cannot be instantiated" do
YAML.stubs(:load).raises(ArgumentError)
@agent.stubs(:retrievecache).returns("whatever")
Puppet.expects(:warning).with { |m| m.include?("Could not load cache") }
Puppet::Agent.publicize_methods :use_cached_config do
@agent.use_cached_config().should be_false
end
end
it "should clear the client if the cached catalog cannot be instantiated" do
YAML.stubs(:load).raises(ArgumentError)
@agent.stubs(:retrievecache).returns("whatever")
@agent.expects(:clear)
Puppet::Agent.publicize_methods :use_cached_config do
@agent.use_cached_config().should be_false
end
end
it "should return true if the cached catalog can be instantiated" do
config = mock 'config'
YAML.stubs(:load).returns(config)
ral_config = mock 'ral config'
ral_config.stubs(:from_cache=)
ral_config.stubs(:host_config=)
config.expects(:to_catalog).returns(ral_config)
@agent.stubs(:retrievecache).returns("whatever")
Puppet::Agent.publicize_methods :use_cached_config do
@agent.use_cached_config().should be_true
end
end
it "should set the catalog instance variable if the cached catalog can be instantiated" do
config = mock 'config'
YAML.stubs(:load).returns(config)
ral_config = mock 'ral config'
ral_config.stubs(:from_cache=)
ral_config.stubs(:host_config=)
config.expects(:to_catalog).returns(ral_config)
@agent.stubs(:retrievecache).returns("whatever")
Puppet::Agent.publicize_methods :use_cached_config do
@agent.use_cached_config()
end
@agent.catalog.should equal(ral_config)
end
it "should mark the catalog as a host_config if valid" do
config = mock 'config'
YAML.stubs(:load).returns(config)
ral_config = mock 'ral config'
ral_config.stubs(:from_cache=)
ral_config.expects(:host_config=).with(true)
config.expects(:to_catalog).returns(ral_config)
@agent.stubs(:retrievecache).returns("whatever")
Puppet::Agent.publicize_methods :use_cached_config do
@agent.use_cached_config()
end
@agent.catalog.should equal(ral_config)
end
it "should mark the catalog as from the cache if valid" do
config = mock 'config'
YAML.stubs(:load).returns(config)
ral_config = mock 'ral config'
ral_config.expects(:from_cache=).with(true)
ral_config.stubs(:host_config=)
config.expects(:to_catalog).returns(ral_config)
@agent.stubs(:retrievecache).returns("whatever")
Puppet::Agent.publicize_methods :use_cached_config do
@agent.use_cached_config()
end
@agent.catalog.should equal(ral_config)
end
describe "when calling splay" do
it "should do nothing if splay is not enabled" do
Puppet.stubs(:[]).with(:splay).returns(false)
@agent.expects(:rand).never
@agent.send(:splay)
end
describe "when splay is enabled" do
before do
Puppet.stubs(:[]).with(:splay).returns(true)
Puppet.stubs(:[]).with(:splaylimit).returns(42)
end
it "should sleep for a random time plus 1" do
@agent.expects(:rand).with(43).returns(43)
@agent.expects(:sleep).with(43)
@agent.send(:splay)
end
it "should inform that it is splayed" do
@agent.stubs(:rand).with(43).returns(43)
@agent.stubs(:sleep).with(43)
Puppet.expects(:info)
@agent.send(:splay)
end
it "should set splay = true" do
@agent.stubs(:rand).returns(43)
@agent.stubs(:sleep)
@agent.send(:splay)
@agent.send(:splayed?).should == true
end
it "should do nothing if already splayed" do
@agent.stubs(:rand).returns(43).at_most_once
@agent.stubs(:sleep).at_most_once
@agent.send(:splay)
@agent.send(:splay)
end
end
end
end
|