blob: f55ba316cff3af6fe4b80f980d7c2927d810676c (
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
438
439
440
441
442
|
#!/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/network/client/master'
describe Puppet::Network::Client::Master, " when retrieving the catalog" do
before do
Puppet.settings.stubs(:use).returns(true)
@master = mock 'master'
@client = Puppet::Network::Client.master.new(
:Master => @master
)
@facts = {"one" => "two", "three" => "four"}
end
it "should initialize the metadata store" do
@client.class.stubs(:facts).returns(@facts)
@client.expects(:dostorage)
@master.stubs(:getconfig).returns(nil)
@client.getconfig
end
it "should collect facts to use for catalog retrieval" do
@client.stubs(:dostorage)
@client.class.expects(:facts).returns(@facts)
@master.stubs(:getconfig).returns(nil)
@client.getconfig
end
it "should fail if no facts could be collected" do
@client.stubs(:dostorage)
@client.class.expects(:facts).returns({})
@master.stubs(:getconfig).returns(nil)
proc { @client.getconfig }.should raise_error(Puppet::Network::ClientError)
end
it "should retrieve plugins if :pluginsync is enabled" do
file = "/path/to/cachefile"
@client.stubs(:cachefile).returns(file)
@client.stubs(:dostorage)
@client.class.stubs(:facts).returns(@facts)
Puppet.settings.expects(:value).with(:pluginsync).returns(true)
@client.expects(:getplugins)
@client.stubs(:get_actual_config).returns(nil)
FileTest.stubs(:exist?).with(file).returns(true)
@client.stubs(:use_cached_config).returns(true)
@client.class.stubs(:facts).returns(@facts)
@client.getconfig
end
it "should use the cached catalog if no catalog could be retrieved" do
@client.stubs(:dostorage)
@client.class.stubs(:facts).returns(@facts)
@master.stubs(:getconfig).raises(ArgumentError.new("whev"))
@client.expects(:use_cached_config).with(true)
@client.getconfig
end
describe "when the catalog format is set to yaml" do
before do
Puppet.settings.stubs(:value).returns "foo"
Puppet.settings.stubs(:value).with(:pluginsync).returns false
Puppet.settings.stubs(:value).with(:configtimeout).returns 10
Puppet.settings.stubs(:value).with(:factsync).returns false
Puppet.settings.stubs(:value).with(:catalog_format).returns "yaml"
end
it "should request a yaml-encoded catalog" do
@client.stubs(:dostorage)
@client.class.stubs(:facts).returns(@facts)
@master.expects(:getconfig).with { |*args| args[1] == "yaml" }
@client.getconfig
end
it "should load the retrieved catalog using YAML" do
@client.stubs(:dostorage)
@client.class.stubs(:facts).returns(@facts)
@master.stubs(:getconfig).returns("myconfig")
config = mock 'config'
YAML.expects(:load).with("myconfig").returns(config)
@client.stubs(:setclasses)
config.stubs(:classes)
config.stubs(:to_catalog).returns(config)
config.stubs(:host_config=)
config.stubs(:from_cache).returns(true)
@client.getconfig
end
it "should use the cached catalog if the retrieved catalog cannot be converted from YAML" do
@client.stubs(:dostorage)
@client.class.stubs(:facts).returns(@facts)
@master.stubs(:getconfig).returns("myconfig")
YAML.expects(:load).with("myconfig").raises(ArgumentError)
@client.expects(:use_cached_config).with(true)
@client.getconfig
end
end
describe "from Marshal" do
before do
Puppet.settings.stubs(:value).returns "foo"
Puppet.settings.stubs(:value).with(:pluginsync).returns false
Puppet.settings.stubs(:value).with(:configtimeout).returns 10
Puppet.settings.stubs(:value).with(:factsync).returns false
Puppet.settings.stubs(:value).with(:catalog_format).returns "marshal"
end
it "should load the retrieved catalog using Marshal" do
@client.stubs(:dostorage)
@client.class.stubs(:facts).returns(@facts)
@master.stubs(:getconfig).returns("myconfig")
config = mock 'config'
Marshal.expects(:load).with("myconfig").returns(config)
@client.stubs(:setclasses)
config.stubs(:classes)
config.stubs(:to_catalog).returns(config)
config.stubs(:host_config=)
config.stubs(:from_cache).returns(true)
@client.getconfig
end
it "should use the cached catalog if the retrieved catalog cannot be converted from Marshal" do
@client.stubs(:dostorage)
@client.class.stubs(:facts).returns(@facts)
@master.stubs(:getconfig).returns("myconfig")
Marshal.expects(:load).with("myconfig").raises(ArgumentError)
@client.expects(:use_cached_config).with(true)
@client.getconfig
end
end
it "should set the classes.txt file with the classes listed in the retrieved catalog" do
@client.stubs(:dostorage)
@client.class.stubs(:facts).returns(@facts)
@master.stubs(:getconfig).returns("myconfig")
config = mock 'config'
YAML.expects(:load).with("myconfig").returns(config)
config.expects(:classes).returns(:myclasses)
@client.expects(:setclasses).with(:myclasses)
config.stubs(:to_catalog).returns(config)
config.stubs(:host_config=)
config.stubs(:from_cache).returns(true)
@client.getconfig
end
it "should convert the retrieved catalog to a RAL catalog" do
@client.stubs(:dostorage)
@client.class.stubs(:facts).returns(@facts)
@master.stubs(:getconfig).returns("myconfig")
yamlconfig = mock 'yaml config'
YAML.stubs(:load).returns(yamlconfig)
@client.stubs(:setclasses)
config = mock 'config'
yamlconfig.stubs(:classes)
yamlconfig.expects(:to_catalog).returns(config)
config.stubs(:host_config=)
config.stubs(:from_cache).returns(true)
@client.getconfig
end
it "should use the cached catalog if the retrieved catalog cannot be converted to a RAL catalog" do
@client.stubs(:dostorage)
@client.class.stubs(:facts).returns(@facts)
@master.stubs(:getconfig).returns("myconfig")
yamlconfig = mock 'yaml config'
YAML.stubs(:load).returns(yamlconfig)
@client.stubs(:setclasses)
config = mock 'config'
yamlconfig.stubs(:classes)
yamlconfig.expects(:to_catalog).raises(ArgumentError)
@client.expects(:use_cached_config).with(true)
@client.getconfig
end
it "should clear the failed catalog if using the cached catalog after failing to instantiate the retrieved catalog" do
@client.stubs(:dostorage)
@client.class.stubs(:facts).returns(@facts)
@master.stubs(:getconfig).returns("myconfig")
yamlconfig = mock 'yaml config'
YAML.stubs(:load).returns(yamlconfig)
@client.stubs(:setclasses)
config = mock 'config'
yamlconfig.stubs(:classes)
yamlconfig.stubs(:to_catalog).raises(ArgumentError)
@client.stubs(:use_cached_config).with(true)
@client.expects(:clear)
@client.getconfig
end
it "should cache the retrieved yaml catalog if it is not from the cache and is valid" do
@client.stubs(:dostorage)
@client.class.stubs(:facts).returns(@facts)
@master.stubs(:getconfig).returns("myconfig")
yamlconfig = mock 'yaml config'
YAML.stubs(:load).returns(yamlconfig)
@client.stubs(:setclasses)
config = mock 'config'
yamlconfig.stubs(:classes)
yamlconfig.expects(:to_catalog).returns(config)
config.stubs(:host_config=)
config.expects(:from_cache).returns(false)
@client.expects(:cache).with("myconfig")
@client.getconfig
end
it "should mark the catalog as a host catalog" do
@client.stubs(:dostorage)
@client.class.stubs(:facts).returns(@facts)
@master.stubs(:getconfig).returns("myconfig")
yamlconfig = mock 'yaml config'
YAML.stubs(:load).returns(yamlconfig)
@client.stubs(:setclasses)
config = mock 'config'
yamlconfig.stubs(:classes)
yamlconfig.expects(:to_catalog).returns(config)
config.stubs(:from_cache).returns(true)
config.expects(:host_config=).with(true)
@client.getconfig
end
end
describe Puppet::Network::Client::Master, " when using the cached catalog" do
before do
Puppet.settings.stubs(:use).returns(true)
@master = mock 'master'
@client = Puppet::Network::Client.master.new(
:Master => @master
)
@facts = {"one" => "two", "three" => "four"}
end
it "should return do nothing and true if there is already an in-memory catalog" do
@client.catalog = :whatever
Puppet::Network::Client::Master.publicize_methods :use_cached_config do
@client.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::Network::Client::Master.publicize_methods :use_cached_config do
@client.use_cached_config(true).should be_false
end
end
it "should return false if no cached catalog can be found" do
@client.expects(:retrievecache).returns(nil)
Puppet::Network::Client::Master.publicize_methods :use_cached_config do
@client.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)
@client.expects(:retrievecache).returns("whatever")
Puppet::Network::Client::Master.publicize_methods :use_cached_config do
@client.use_cached_config().should be_false
end
end
it "should warn if the cached catalog cannot be instantiated" do
YAML.stubs(:load).raises(ArgumentError)
@client.stubs(:retrievecache).returns("whatever")
Puppet.expects(:warning).with { |m| m.include?("Could not load cache") }
Puppet::Network::Client::Master.publicize_methods :use_cached_config do
@client.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)
@client.stubs(:retrievecache).returns("whatever")
@client.expects(:clear)
Puppet::Network::Client::Master.publicize_methods :use_cached_config do
@client.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)
@client.stubs(:retrievecache).returns("whatever")
Puppet::Network::Client::Master.publicize_methods :use_cached_config do
@client.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)
@client.stubs(:retrievecache).returns("whatever")
Puppet::Network::Client::Master.publicize_methods :use_cached_config do
@client.use_cached_config()
end
@client.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)
@client.stubs(:retrievecache).returns("whatever")
Puppet::Network::Client::Master.publicize_methods :use_cached_config do
@client.use_cached_config()
end
@client.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)
@client.stubs(:retrievecache).returns("whatever")
Puppet::Network::Client::Master.publicize_methods :use_cached_config do
@client.use_cached_config()
end
@client.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)
@client.expects(:rand).never
@client.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" do
@client.expects(:rand).with(42).returns(42)
@client.expects(:sleep).with(42)
@client.send(:splay)
end
it "should inform that it is splayed" do
@client.stubs(:rand).with(42).returns(42)
@client.stubs(:sleep).with(42)
Puppet.expects(:info)
@client.send(:splay)
end
it "should set splay = true" do
@client.stubs(:rand).with(42).returns(42)
@client.stubs(:sleep).with(42)
@client.send(:splay)
@client.send(:splayed?).should == true
end
it "should do nothing if already splayed" do
@client.stubs(:rand).with(42).returns(42).at_most_once
@client.stubs(:sleep).with(42).at_most_once
@client.send(:splay)
@client.send(:splay)
end
end
end
end
|