summaryrefslogtreecommitdiffstats
path: root/spec/unit/network/client/master.rb
blob: fda729cae59ed997c6f2a6760e1fb1f1501a3d28 (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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
#!/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 configuration" do
    before do
        @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 configuration 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 use the cached configuration if it is up to date" do
        file = "/path/to/cachefile"
        @client.stubs(:cachefile).returns(file)
        FileTest.expects(:exist?).with(file).returns(true)
        @client.expects(:fresh?).with(@facts).returns true
        @client.class.stubs(:facts).returns(@facts)
        @client.expects(:use_cached_config).returns(true)
        Puppet.stubs(:info)

        @client.getconfig
    end

    it "should log that the configuration does not need a recompile" do
        file = "/path/to/cachefile"
        @client.stubs(:cachefile).returns(file)
        FileTest.stubs(:exist?).with(file).returns(true)
        @client.stubs(:fresh?).with(@facts).returns true
        @client.stubs(:use_cached_config).returns(true)
        @client.class.stubs(:facts).returns(@facts)
        Puppet.expects(:info).with { |m| m.include?("up to date") }

        @client.getconfig
    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(:fresh?).with(@facts).returns true
        @client.stubs(:use_cached_config).returns(true)
        @client.class.stubs(:facts).returns(@facts)
        @client.stubs(:add_default_resources)
        @client.getconfig
    end

    it "should use the cached configuration if no configuration 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

    it "should load the retrieved configuration 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_configuration).returns(config)
        config.stubs(:host_config=)
        config.stubs(:from_cache).returns(true)
        @client.stubs(:add_default_resources)

        @client.getconfig
    end

    it "should use the cached configuration if the retrieved configuration 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

    it "should set the classes.txt file with the classes listed in the retrieved configuration" 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_configuration).returns(config)
        config.stubs(:host_config=)
        config.stubs(:from_cache).returns(true)
        @client.stubs(:add_default_resources)

        @client.getconfig
    end

    it "should convert the retrieved configuration to a RAL configuration" 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_configuration).returns(config)
        config.stubs(:host_config=)
        config.stubs(:from_cache).returns(true)
        @client.stubs(:add_default_resources)

        @client.getconfig
    end

    it "should use the cached configuration if the retrieved configuration cannot be converted to a RAL configuration" 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_configuration).raises(ArgumentError)

        @client.expects(:use_cached_config).with(true)

        @client.getconfig
    end

    it "should clear the failed configuration if using the cached configuration after failing to instantiate the retrieved configuration" 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_configuration).raises(ArgumentError)

        @client.stubs(:use_cached_config).with(true)

        @client.expects(:clear)

        @client.getconfig
    end

    it "should cache the retrieved yaml configuration 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_configuration).returns(config)

        config.stubs(:host_config=)
        @client.stubs(:add_default_resources)

        config.expects(:from_cache).returns(false)

        @client.expects(:cache).with("myconfig")

        @client.getconfig
    end

    it "should mark the configuration as a host configuration" 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_configuration).returns(config)

        config.stubs(:from_cache).returns(true)

        config.expects(:host_config=).with(true)
        @client.stubs(:add_default_resources)

        @client.getconfig
    end

    it "should add the default resources to the configuration" 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_configuration).returns(config)

        config.stubs(:from_cache).returns(true)

        config.stubs(:host_config=).with(true)

        @client.expects(:add_default_resources).with(config)

        @client.getconfig
    end
end

describe Puppet::Network::Client::Master, " when using the cached configuration" do
    before do
        @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 configuration" do
        @client.configuration = :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 configuration 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 configuration 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 configuration 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 configuration 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 configuration 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_configuration).returns(ral_config)

        @client.stubs(:retrievecache).returns("whatever")
        @client.stubs(:add_default_resources)
        Puppet::Network::Client::Master.publicize_methods :use_cached_config do
            @client.use_cached_config().should be_true
        end
    end

    it "should set the configuration instance variable if the cached configuration 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_configuration).returns(ral_config)

        @client.stubs(:retrievecache).returns("whatever")
        @client.stubs(:add_default_resources)
        Puppet::Network::Client::Master.publicize_methods :use_cached_config do
            @client.use_cached_config()
        end

        @client.configuration.should equal(ral_config)
    end

    it "should mark the configuration 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_configuration).returns(ral_config)

        @client.stubs(:retrievecache).returns("whatever")
        @client.stubs(:add_default_resources)
        Puppet::Network::Client::Master.publicize_methods :use_cached_config do
            @client.use_cached_config()
        end

        @client.configuration.should equal(ral_config)
    end

    it "should mark the configuration 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_configuration).returns(ral_config)

        @client.stubs(:retrievecache).returns("whatever")
        @client.stubs(:add_default_resources)
        Puppet::Network::Client::Master.publicize_methods :use_cached_config do
            @client.use_cached_config()
        end

        @client.configuration.should equal(ral_config)
    end

    it "should add the default resources to the configuration" 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.stubs(:to_configuration).returns(ral_config)

        @client.stubs(:retrievecache).returns("whatever")
        @client.expects(:add_default_resources).with(ral_config)
        Puppet::Network::Client::Master.publicize_methods :use_cached_config do
            @client.use_cached_config()
        end
    end
end

describe Puppet::Network::Client::Master, " when adding default resources" do
    before do
        @master = mock 'master'
        @client = Puppet::Network::Client.master.new(
            :Master => @master
        )
        @facts = {"one" => "two", "three" => "four"}
    end

    it "should add the default schedules" do
        config = mock 'config'
        one = stub 'one', :title => "one"
        two = stub 'two', :title => "two"
        Puppet::Type.type(:schedule).expects(:create_default_resources).with().returns([one, two])
        config.expects(:add_resource).with(one)
        config.expects(:add_resource).with(two)
        config.stubs(:resource).returns(false)
        Puppet::Type.type(:filebucket).stubs(:create_default_resources).returns([])
        Puppet::Network::Client::Master.publicize_methods :add_default_resources do
            @client.add_default_resources(config)
        end
    end

    it "should add the default filebucket" do
        config = mock 'config'
        Puppet::Type.type(:schedule).stubs(:create_default_resources).returns([])
        one = stub 'one', :title => "one"
        two = stub 'two', :title => "two"
        Puppet::Type.type(:filebucket).expects(:create_default_resources).with().returns([one, two])
        config.expects(:add_resource).with(one)
        config.expects(:add_resource).with(two)
        config.stubs(:resource).returns(false)
        Puppet::Network::Client::Master.publicize_methods :add_default_resources do
            @client.add_default_resources(config)
        end
    end

    it "should only add a default filebucket if no similarly named bucket already exists" do
        config = mock 'config'
        Puppet::Type.type(:schedule).stubs(:create_default_resources).returns([])
        one = stub 'one', :title => "one"
        Puppet::Type.type(:filebucket).expects(:create_default_resources).with().returns([one])
        config.expects(:resource).with(:filebucket, "one").returns(true)
        config.expects(:add_resource).with(one).never
        Puppet::Network::Client::Master.publicize_methods :add_default_resources do
            @client.add_default_resources(config)
        end
    end

    it "should only add default schedules if no similarly named schedule already exists" do
        config = mock 'config'
        one = stub 'one', :title => "one"
        Puppet::Type.type(:schedule).stubs(:create_default_resources).returns([one])
        Puppet::Type.type(:filebucket).stubs(:create_default_resources).with().returns([])
        config.expects(:resource).with(:schedule, "one").returns(true)
        config.expects(:add_resource).with(one).never
        Puppet::Network::Client::Master.publicize_methods :add_default_resources do
            @client.add_default_resources(config)
        end
    end
end