summaryrefslogtreecommitdiffstats
path: root/spec/unit/indirector/indirection.rb
blob: f33444bbf0debbf2f21cb323f4053c7cd7379f4e (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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
#!/usr/bin/env ruby

require File.dirname(__FILE__) + '/../../spec_helper'

require 'puppet/indirector/indirection'

describe Puppet::Indirector::Indirection do
    describe "when initializing" do
        # (LAK) I've no idea how to test this, really.
        it "should store a reference to itself before it consumes its options" do
            proc { @indirection = Puppet::Indirector::Indirection.new(Object.new, :testingness, :not_valid_option) }.should raise_error
            Puppet::Indirector::Indirection.instance(:testingness).should be_instance_of(Puppet::Indirector::Indirection)
            Puppet::Indirector::Indirection.instance(:testingness).delete
        end

        it "should keep a reference to the indirecting model" do
            model = mock 'model'
            @indirection = Puppet::Indirector::Indirection.new(model, :myind)
            @indirection.model.should equal(model)
        end

        it "should set the name" do
            @indirection = Puppet::Indirector::Indirection.new(mock('model'), :myind)
            @indirection.name.should == :myind
        end

        it "should require indirections to have unique names" do
            @indirection = Puppet::Indirector::Indirection.new(mock('model'), :test)
            proc { Puppet::Indirector::Indirection.new(:test) }.should raise_error(ArgumentError)
        end

        it "should extend itself with any specified module" do
            mod = Module.new
            @indirection = Puppet::Indirector::Indirection.new(mock('model'), :test, :extend => mod)
            @indirection.metaclass.included_modules.should include(mod)
        end

        after do
            @indirection.delete if defined? @indirection
        end
    end

    describe "when an instance" do
        before :each do
            @terminus_class = mock 'terminus_class'
            @terminus = mock 'terminus'
            @terminus_class.stubs(:new).returns(@terminus)
            @cache = mock 'cache'
            @cache_class = mock 'cache_class'
            Puppet::Indirector::Terminus.stubs(:terminus_class).with(:test, :cache_terminus).returns(@cache_class)
            Puppet::Indirector::Terminus.stubs(:terminus_class).with(:test, :test_terminus).returns(@terminus_class)

            @indirection = Puppet::Indirector::Indirection.new(mock('model'), :test)
            @indirection.terminus_class = :test_terminus

            @instance = stub 'instance', :expiration => nil, :expiration= => nil, :name => "whatever"
            @name = :mything

            @request = stub 'instance', :key => "/my/key", :instance => @instance, :options => {}
        end

        it "should allow setting the ttl" do
            @indirection.ttl = 300
            @indirection.ttl.should == 300
        end

        it "should default to the :runinterval setting, converted to an integer, for its ttl" do
            Puppet.settings.expects(:value).returns "1800"
            @indirection.ttl.should == 1800
        end

        it "should calculate the current expiration by adding the TTL to the current time" do
            @indirection.stubs(:ttl).returns(100)
            now = Time.now
            Time.stubs(:now).returns now
            @indirection.expiration.should == (Time.now + 100)
        end

        it "should have a method for creating an indirection request instance" do
            @indirection.should respond_to(:request)
        end

        describe "creates a request" do
            it "should create it with its name as the request's indirection name" do
                Puppet::Indirector::Request.expects(:new).with { |name, *other| @indirection.name == name }
                @indirection.request(:funtest, "yayness")
            end

            it "should require a method and key" do
                Puppet::Indirector::Request.expects(:new).with { |name, method, key, *other| method == :funtest and key == "yayness" }
                @indirection.request(:funtest, "yayness")
            end

            it "should support optional arguments" do
                Puppet::Indirector::Request.expects(:new).with { |name, method, key, other| other == {:one => :two} }
                @indirection.request(:funtest, "yayness", :one => :two)
            end

            it "should default to the arguments being nil" do
                Puppet::Indirector::Request.expects(:new).with { |name, method, key, args| args.nil? }
                @indirection.request(:funtest, "yayness")
            end

            it "should return the request" do
                request = mock 'request'
                Puppet::Indirector::Request.expects(:new).returns request
                @indirection.request(:funtest, "yayness").should equal(request)
            end
        end
      
        describe "and looking for a model instance" do
            it "should let the :select_terminus method choose the terminus if the method is defined" do
                # Define the method, so our respond_to? hook matches.
                class << @indirection
                    def select_terminus(request)
                    end
                end

                @indirection.expects(:select_terminus).with(@request).returns :test_terminus

                @indirection.stubs(:check_authorization)
                @terminus.expects(:find)

                @indirection.find(@request)

            end

            it "should let the appropriate terminus perform the lookup" do
                @terminus.expects(:find).with(@request).returns(@instance)
                @indirection.find(@request).should == @instance
            end

            it "should return nil if nothing is returned by the terminus" do
                @terminus.expects(:find).with(@request).returns(nil)
                @indirection.find(@request).should be_nil
            end

            it "should set the expiration date on any instances without one set" do
                @terminus.stubs(:find).returns(@instance)

                @indirection.expects(:expiration).returns :yay

                @instance.expects(:expiration).returns(nil)
                @instance.expects(:expiration=).with(:yay)

                @indirection.find(@request)
            end

            it "should not override an already-set expiration date on returned instances" do
                @terminus.stubs(:find).returns(@instance)

                @indirection.expects(:expiration).never

                @instance.expects(:expiration).returns(:yay)
                @instance.expects(:expiration=).never

                @indirection.find(@request)
            end

            describe "when caching is enabled" do
                before do
                    @indirection.cache_class = :cache_terminus
                    @cache_class.expects(:new).returns(@cache)

                    @instance.stubs(:expired?).returns false
                end

                it "should first look in the cache for an instance" do
                    @terminus.expects(:find).never
                    @cache.expects(:find).with(@request).returns @instance

                    @indirection.find(@request)
                end

                it "should return the cached object if it is not expired" do
                    @instance.stubs(:expired?).returns false

                    @cache.stubs(:find).returns @instance
                    @indirection.find(@request).should equal(@instance)
                end

                it "should send a debug log if it is using the cached object" do
                    Puppet.expects(:debug)
                    @cache.stubs(:find).returns @instance

                    @indirection.find(@request)
                end

                it "should not return the cached object if it is expired" do
                    @instance.stubs(:expired?).returns true

                    @cache.stubs(:find).returns @instance
                    @terminus.stubs(:find).returns nil
                    @indirection.find(@request).should be_nil
                end

                it "should send an info log if it is using the cached object" do
                    Puppet.expects(:info)
                    @instance.stubs(:expired?).returns true

                    @cache.stubs(:find).returns @instance
                    @terminus.stubs(:find).returns nil
                    @indirection.find(@request)
                end

                it "should cache any objects not retrieved from the cache" do
                    @cache.expects(:find).with(@request).returns nil

                    @terminus.expects(:find).with(@request).returns(@instance)
                    @cache.expects(:save)

                    @request.expects(:clone).returns(stub('newreq', :instance= => nil))

                    @indirection.find(@request)
                end

                it "should cache a clone of the request with the instance set to the cached object" do
                    @cache.expects(:find).with(@request).returns nil

                    newreq = mock 'request'
                    @request.expects(:clone).returns newreq
                    newreq.expects(:instance=).with(@instance)

                    @terminus.expects(:find).with(@request).returns(@instance)
                    @cache.expects(:save).with(newreq)

                    @indirection.find(@request)
                end

                it "should send an info log that the object is being cached" do
                    @cache.stubs(:find).returns nil

                    @request.expects(:clone).returns(stub('newreq', :instance= => nil))

                    @terminus.stubs(:find).returns(@instance)
                    @cache.stubs(:save)

                    Puppet.expects(:info)

                    @indirection.find(@request)
                end
            end
        end

        describe "and storing a model instance" do
            it "should let the :select_terminus method choose the terminus if the method is defined" do
                # Define the method, so our respond_to? hook matches.
                class << @indirection
                    def select_terminus(req)
                    end
                end

                @indirection.expects(:select_terminus).with(@request).returns :test_terminus

                @indirection.stubs(:check_authorization)
                @terminus.expects(:save)

                @indirection.save(@request)
            end

            it "should let the appropriate terminus store the instance" do
                @terminus.expects(:save).with(@request).returns(@instance)
                @indirection.save(@request).should == @instance
            end

            describe "when caching is enabled" do
                before do
                    @indirection.cache_class = :cache_terminus
                    @cache_class.expects(:new).returns(@cache)

                    @instance.stubs(:expired?).returns false
                end

                it "should save the object to the cache" do
                    @cache.expects(:save).with(@request)
                    @terminus.stubs(:save)
                    @indirection.save(@request)
                end
            end
        end
        
        describe "and removing a model instance" do
            it "should let the :select_terminus method choose the terminus if the method is defined" do
                # Define the method, so our respond_to? hook matches.
                class << @indirection
                    def select_terminus(request)
                    end
                end

                @indirection.expects(:select_terminus).with(@request).returns :test_terminus

                @indirection.stubs(:check_authorization)
                @terminus.expects(:destroy)

                @indirection.destroy(@request)
            end

            it "should delegate the instance removal to the appropriate terminus" do
                @terminus.expects(:destroy).with(@request)
                @indirection.destroy(@request)
            end

            it "should return nil" do
                @terminus.stubs(:destroy)
                @indirection.destroy(@request).should be_nil
            end

            describe "when caching is enabled" do
                before do
                    @indirection.cache_class = :cache_terminus
                    @cache_class.expects(:new).returns(@cache)

                    @instance.stubs(:expired?).returns false
                end

                it "should destroy any found object in the cache" do
                    cached = mock 'cache'
                    @cache.expects(:find).with(@request).returns cached
                    @cache.expects(:destroy).with(@request)
                    @terminus.stubs(:destroy)

                    @indirection.destroy(@request)
                end
            end
        end

        describe "and searching for multiple model instances" do
            it "should let the :select_terminus method choose the terminus if the method is defined" do
                # Define the method, so our respond_to? hook matches.
                class << @indirection
                    def select_terminus(req)
                    end
                end

                @indirection.expects(:select_terminus).with(@request).returns :test_terminus

                @indirection.stubs(:check_authorization)
                @terminus.expects(:search)

                @indirection.search(@request)
            end

            it "should let the appropriate terminus find the matching instances" do
                @terminus.expects(:search).with(@request).returns(@instance)
                @indirection.search(@request).should == @instance
            end
        end

        describe "and an authorization hook is present" do
            before do
                # So the :respond_to? turns out correctly.
                class << @terminus
                    def authorized?
                    end
                end
                
                @request = stub 'instance', :key => "/my/key", :instance => @instance, :options => {:node => "mynode"}
            end

            it "should not check authorization if a node name is not provided" do
                @terminus.expects(:authorized?).never
                @terminus.stubs(:find)

                # The quotes are necessary here, else it looks like a block.
                @request.stubs(:options).returns({})
                @indirection.find(@request)
            end

            it "should pass the request to the terminus's authorization method" do
                @terminus.expects(:authorized?).with(@request).returns(true)
                @terminus.stubs(:find)

                @indirection.find(@request)
            end

            it "should fail while finding instances if authorization returns false" do
                @terminus.expects(:authorized?).returns(false)
                @terminus.stubs(:find)
                proc { @indirection.find(@request) }.should raise_error(ArgumentError)
            end

            it "should continue finding instances if authorization returns true" do
                @terminus.expects(:authorized?).returns(true)
                @terminus.stubs(:find)
                @indirection.find(@request)
            end

            it "should fail while saving instances if authorization returns false" do
                @terminus.expects(:authorized?).returns(false)
                @terminus.stubs(:save)
                proc { @indirection.save(@request) }.should raise_error(ArgumentError)
            end

            it "should continue saving instances if authorization returns true" do
                @terminus.expects(:authorized?).returns(true)
                @terminus.stubs(:save)
                @indirection.save(@request)
            end

            it "should fail while destroying instances if authorization returns false" do
                @terminus.expects(:authorized?).returns(false)
                @terminus.stubs(:destroy)
                proc { @indirection.destroy(@request) }.should raise_error(ArgumentError)
            end

            it "should continue destroying instances if authorization returns true" do
                @terminus.expects(:authorized?).returns(true)
                @terminus.stubs(:destroy)
                @indirection.destroy(@request)
            end

            it "should fail while searching for instances if authorization returns false" do
                @terminus.expects(:authorized?).returns(false)
                @terminus.stubs(:search)
                proc { @indirection.search(@request) }.should raise_error(ArgumentError)
            end

            it "should continue searching for instances if authorization returns true" do
                @terminus.expects(:authorized?).returns(true)
                @terminus.stubs(:search)
                @indirection.search(@request)
            end
        end

        after :each do
            @indirection.delete
            Puppet::Indirector::Indirection.clear_cache
        end
    end


    describe "when managing indirection instances" do
        it "should allow an indirection to be retrieved by name" do
            @indirection = Puppet::Indirector::Indirection.new(mock('model'), :test)
            Puppet::Indirector::Indirection.instance(:test).should equal(@indirection)
        end
        
        it "should return nil when the named indirection has not been created" do
            Puppet::Indirector::Indirection.instance(:test).should be_nil
        end

        it "should allow an indirection's model to be retrieved by name" do
            mock_model = mock('model')
            @indirection = Puppet::Indirector::Indirection.new(mock_model, :test)
            Puppet::Indirector::Indirection.model(:test).should equal(mock_model)
        end
        
        it "should return nil when no model matches the requested name" do
            Puppet::Indirector::Indirection.model(:test).should be_nil
        end

        after do
            @indirection.delete if defined? @indirection
        end
    end

    describe "when routing to the correct the terminus class" do
        before do
            @indirection = Puppet::Indirector::Indirection.new(mock('model'), :test)
            @terminus = mock 'terminus'
            @terminus_class = stub 'terminus class', :new => @terminus
            Puppet::Indirector::Terminus.stubs(:terminus_class).with(:test, :default).returns(@terminus_class)
        end

        it "should fail if no terminus class can be picked" do
            proc { @indirection.terminus_class }.should raise_error(Puppet::DevError)
        end

        it "should choose the default terminus class if one is specified" do
            @indirection.terminus_class = :default
            @indirection.terminus_class.should equal(:default)
        end

        it "should use the provided Puppet setting if told to do so" do
            Puppet::Indirector::Terminus.stubs(:terminus_class).with(:test, :my_terminus).returns(mock("terminus_class2"))
            Puppet.settings.expects(:value).with(:my_setting).returns("my_terminus")
            @indirection.terminus_setting = :my_setting
            @indirection.terminus_class.should equal(:my_terminus)
        end

        it "should fail if the provided terminus class is not valid" do
            Puppet::Indirector::Terminus.stubs(:terminus_class).with(:test, :nosuchclass).returns(nil)
            proc { @indirection.terminus_class = :nosuchclass }.should raise_error(ArgumentError)
        end

        after do
            @indirection.delete if defined? @indirection
        end
    end

    describe "when specifying the terminus class to use" do
        before do
            @indirection = Puppet::Indirector::Indirection.new(mock('model'), :test)
            @terminus = mock 'terminus'
            @terminus_class = stub 'terminus class', :new => @terminus
        end

        it "should allow specification of a terminus type" do
            @indirection.should respond_to(:terminus_class=)
        end

        it "should fail to redirect if no terminus type has been specified" do
            proc { @indirection.find("blah") }.should raise_error(Puppet::DevError)
        end

        it "should fail when the terminus class name is an empty string" do
            proc { @indirection.terminus_class = "" }.should raise_error(ArgumentError)
        end

        it "should fail when the terminus class name is nil" do
            proc { @indirection.terminus_class = nil }.should raise_error(ArgumentError)
        end

        it "should fail when the specified terminus class cannot be found" do
            Puppet::Indirector::Terminus.expects(:terminus_class).with(:test, :foo).returns(nil)
            proc { @indirection.terminus_class = :foo }.should raise_error(ArgumentError)
        end

        it "should select the specified terminus class if a terminus class name is provided" do
            Puppet::Indirector::Terminus.expects(:terminus_class).with(:test, :foo).returns(@terminus_class)
            @indirection.terminus(:foo).should equal(@terminus)
        end

        it "should use the configured terminus class if no terminus name is specified" do
            Puppet::Indirector::Terminus.stubs(:terminus_class).with(:test, :foo).returns(@terminus_class)
            @indirection.terminus_class = :foo
            @indirection.terminus().should equal(@terminus)
        end

        after do
            @indirection.delete if defined? @indirection
        end
    end

    describe "when managing terminus instances" do
        before do
            @indirection = Puppet::Indirector::Indirection.new(mock('model'), :test)
            @terminus = mock 'terminus'
            @terminus_class = mock 'terminus class'
            Puppet::Indirector::Terminus.stubs(:terminus_class).with(:test, :foo).returns(@terminus_class)
        end

        it "should create an instance of the chosen terminus class" do
            @terminus_class.stubs(:new).returns(@terminus)
            @indirection.terminus(:foo).should equal(@terminus)
        end

        it "should allow the clearance of cached terminus instances" do
            terminus1 = mock 'terminus1'
            terminus2 = mock 'terminus2'
            @terminus_class.stubs(:new).returns(terminus1, terminus2, ArgumentError)
            @indirection.terminus(:foo).should equal(terminus1)
            @indirection.class.clear_cache
            @indirection.terminus(:foo).should equal(terminus2)
        end

        # Make sure it caches the terminus.
        it "should return the same terminus instance each time for a given name" do
            @terminus_class.stubs(:new).returns(@terminus)
            @indirection.terminus(:foo).should equal(@terminus)
            @indirection.terminus(:foo).should equal(@terminus)
        end

        it "should not create a terminus instance until one is actually needed" do
            Puppet::Indirector.expects(:terminus).never
            indirection = Puppet::Indirector::Indirection.new(mock('model'), :lazytest)
        end

        after do
            @indirection.delete
            Puppet::Indirector::Indirection.clear_cache
        end
    end

    describe "when deciding whether to cache" do
        before do
            @indirection = Puppet::Indirector::Indirection.new(mock('model'), :test)
            @terminus = mock 'terminus'
            @terminus_class = mock 'terminus class'
            @terminus_class.stubs(:new).returns(@terminus)
            Puppet::Indirector::Terminus.stubs(:terminus_class).with(:test, :foo).returns(@terminus_class)
            @indirection.terminus_class = :foo
        end

        it "should provide a method for setting the cache terminus class" do
            @indirection.should respond_to(:cache_class=)
        end

        it "should fail to cache if no cache type has been specified" do
            proc { @indirection.cache }.should raise_error(Puppet::DevError)
        end

        it "should fail to set the cache class when the cache class name is an empty string" do
            proc { @indirection.cache_class = "" }.should raise_error(ArgumentError)
        end

        it "should fail to set the cache class when the cache class name is nil" do
            proc { @indirection.cache_class = nil }.should raise_error(ArgumentError)
        end

        it "should fail to set the cache class when the specified cache class cannot be found" do
            Puppet::Indirector::Terminus.expects(:terminus_class).with(:test, :foo).returns(nil)
            proc { @indirection.cache_class = :foo }.should raise_error(ArgumentError)
        end

        after do
            @indirection.delete
            Puppet::Indirector::Indirection.clear_cache
        end
    end

    describe "when using a cache" do
        before :each do
            Puppet.settings.stubs(:value).with("test_terminus").returns("test_terminus")
            @terminus_class = mock 'terminus_class'
            @terminus = mock 'terminus'
            @terminus_class.stubs(:new).returns(@terminus)
            @cache = mock 'cache'
            @cache_class = mock 'cache_class'
            Puppet::Indirector::Terminus.stubs(:terminus_class).with(:test, :cache_terminus).returns(@cache_class)
            Puppet::Indirector::Terminus.stubs(:terminus_class).with(:test, :test_terminus).returns(@terminus_class)
            @indirection = Puppet::Indirector::Indirection.new(mock('model'), :test)
            @indirection.terminus_class = :test_terminus
        end

        describe "and managing the cache terminus" do
            it "should not create a cache terminus at initialization" do
                # This is weird, because all of the code is in the setup.  If we got
                # new() called on the cache class, we'd get an exception here.
            end

            it "should reuse the cache terminus" do
                @cache_class.expects(:new).returns(@cache)
                Puppet.settings.stubs(:value).with("test_cache").returns("cache_terminus")
                @indirection.cache_class = :cache_terminus
                @indirection.cache.should equal(@cache)
                @indirection.cache.should equal(@cache)
            end

            it "should remove the cache terminus when all other terminus instances are cleared" do
                cache2 = mock 'cache2'
                @cache_class.stubs(:new).returns(@cache, cache2)
                @indirection.cache_class = :cache_terminus
                @indirection.cache.should equal(@cache)
                @indirection.clear_cache
                @indirection.cache.should equal(cache2)
            end
        end

        describe "and saving" do
        end
        
        describe "and finding" do
        end
        
        after :each do
            @indirection.delete
            Puppet::Indirector::Indirection.clear_cache
        end
    end
end