~ubuntu-branches/ubuntu/lucid/puppet/lucid-security

« back to all changes in this revision

Viewing changes to spec/unit/indirector/indirection.rb

  • Committer: Bazaar Package Importer
  • Author(s): Chuck Short
  • Date: 2009-12-23 00:48:10 UTC
  • mfrom: (1.1.10 upstream) (3.1.7 squeeze)
  • Revision ID: james.westby@ubuntu.com-20091223004810-3i4oryds922g5n59
Tags: 0.25.1-3ubuntu1
* Merge from debian testing.  Remaining changes:
  - debian/rules:
    + Don't start puppet when first installing puppet.
  - debian/puppet.conf, lib/puppet/defaults.rb:
    + Move templates to /etc/puppet
  - lib/puppet/defaults.rb:
    + Fix /var/lib/puppet/state ownership.
  - man/man8/puppet.conf.8: 
    + Fix broken URL in manpage.
  - debian/control:
    + Update maintainer accordint to spec.
    + Puppetmaster Recommends -> Suggests
    + Created puppet-testsuite as a seperate. Allow the users to run puppet's 
      testsuite.
  - tests/Rakefile: Fix rakefile so that the testsuite can acutally be ran.

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
 
7
7
describe "Indirection Delegator", :shared => true do
8
8
    it "should create a request object with the appropriate method name and all of the passed arguments" do
9
 
        request = stub 'request', :node => nil
 
9
        request = Puppet::Indirector::Request.new(:indirection, :find, "me")
10
10
 
11
11
        @indirection.expects(:request).with(@method, "mystuff", :one => :two).returns request
12
12
 
22
22
            end
23
23
        end
24
24
 
25
 
        request = stub 'request', :key => "me", :options => {}
 
25
        request = Puppet::Indirector::Request.new(:indirection, :find, "me")
26
26
 
27
27
        @indirection.stubs(:request).returns request
28
28
 
34
34
        @indirection.send(@method, "me")
35
35
    end
36
36
 
 
37
    it "should fail if the :select_terminus hook does not return a terminus name" do
 
38
        # Define the method, so our respond_to? hook matches.
 
39
        class << @indirection
 
40
            def select_terminus(request)
 
41
            end
 
42
        end
 
43
 
 
44
        request = stub 'request', :key => "me", :options => {}
 
45
 
 
46
        @indirection.stubs(:request).returns request
 
47
 
 
48
        @indirection.expects(:select_terminus).with(request).returns nil
 
49
 
 
50
        lambda { @indirection.send(@method, "me") }.should raise_error(ArgumentError)
 
51
    end
 
52
 
37
53
    it "should choose the terminus returned by the :terminus_class method if no :select_terminus method is available" do
38
54
        @indirection.expects(:terminus_class).returns :test_terminus
39
55
 
87
103
end
88
104
 
89
105
describe Puppet::Indirector::Indirection do
 
106
    after do
 
107
        Puppet::Util::Cacher.expire
 
108
    end
90
109
    describe "when initializing" do
91
110
        # (LAK) I've no idea how to test this, really.
92
111
        it "should store a reference to itself before it consumes its options" do
127
146
            @terminus_class = mock 'terminus_class'
128
147
            @terminus = mock 'terminus'
129
148
            @terminus_class.stubs(:new).returns(@terminus)
130
 
            @cache = mock 'cache'
 
149
            @cache = stub 'cache', :name => "mycache"
131
150
            @cache_class = mock 'cache_class'
132
151
            Puppet::Indirector::Terminus.stubs(:terminus_class).with(:test, :cache_terminus).returns(@cache_class)
133
152
            Puppet::Indirector::Terminus.stubs(:terminus_class).with(:test, :test_terminus).returns(@terminus_class)
190
209
                @indirection.request(:funtest, "yayness").should equal(request)
191
210
            end
192
211
        end
193
 
      
 
212
 
194
213
        describe "and looking for a model instance" do
195
214
            before { @method = :find }
196
215
 
224
243
                @indirection.find("/my/key")
225
244
            end
226
245
 
 
246
            it "should filter the result instance if the terminus supports it" do
 
247
                @terminus.stubs(:find).returns(@instance)
 
248
                @terminus.stubs(:respond_to?).with(:filter).returns(true)
 
249
 
 
250
                @terminus.expects(:filter).with(@instance)
 
251
 
 
252
                @indirection.find("/my/key")
 
253
            end
227
254
            describe "when caching is enabled" do
228
255
                before do
229
256
                    @indirection.cache_class = :cache_terminus
230
 
                    @cache_class.expects(:new).returns(@cache)
 
257
                    @cache_class.stubs(:new).returns(@cache)
231
258
 
232
259
                    @instance.stubs(:expired?).returns false
233
260
                end
239
266
                    @indirection.find("/my/key")
240
267
                end
241
268
 
 
269
                it "should not look in the cache if the request specifies not to use the cache" do
 
270
                    @terminus.expects(:find).returns @instance
 
271
                    @cache.expects(:find).never
 
272
                    @cache.stubs(:save)
 
273
 
 
274
                    @indirection.find("/my/key", :ignore_cache => true)
 
275
                end
 
276
 
 
277
                it "should still save to the cache even if the cache is being ignored during readin" do
 
278
                    @terminus.expects(:find).returns @instance
 
279
                    @cache.expects(:save)
 
280
 
 
281
                    @indirection.find("/my/key", :ignore_cache => true)
 
282
                end
 
283
 
 
284
                it "should only look in the cache if the request specifies not to use the terminus" do
 
285
                    @terminus.expects(:find).never
 
286
                    @cache.expects(:find)
 
287
 
 
288
                    @indirection.find("/my/key", :ignore_terminus => true)
 
289
                end
 
290
 
242
291
                it "should use a request to look in the cache for cached objects" do
243
292
                    @cache.expects(:find).with { |r| r.method == :find and r.key == "/my/key" }.returns @instance
244
293
 
254
303
                    @indirection.find("/my/key").should equal(@instance)
255
304
                end
256
305
 
 
306
                it "should not fail if the cache fails" do
 
307
                    @terminus.stubs(:find).returns @instance
 
308
 
 
309
                    @cache.expects(:find).raises ArgumentError
 
310
                    @cache.stubs(:save)
 
311
                    lambda { @indirection.find("/my/key") }.should_not raise_error
 
312
                end
 
313
 
 
314
                it "should look in the main terminus if the cache fails" do
 
315
                    @terminus.expects(:find).returns @instance
 
316
                    @cache.expects(:find).raises ArgumentError
 
317
                    @cache.stubs(:save)
 
318
                    @indirection.find("/my/key").should equal(@instance)
 
319
                end
 
320
 
257
321
                it "should send a debug log if it is using the cached object" do
258
322
                    Puppet.expects(:debug)
259
323
                    @cache.stubs(:find).returns @instance
325
389
            it_should_behave_like "Indirection Delegator"
326
390
            it_should_behave_like "Delegation Authorizer"
327
391
 
328
 
            it "should return nil" do
329
 
                @terminus.stubs(:save)
330
 
                @indirection.save(@instance).should be_nil
 
392
            it "should return the result of the save" do
 
393
                @terminus.stubs(:save).returns "foo"
 
394
                @indirection.save(@instance).should == "foo"
331
395
            end
332
396
 
333
397
            describe "when caching is enabled" do
334
398
                before do
335
399
                    @indirection.cache_class = :cache_terminus
336
 
                    @cache_class.expects(:new).returns(@cache)
 
400
                    @cache_class.stubs(:new).returns(@cache)
337
401
 
338
402
                    @instance.stubs(:expired?).returns false
339
403
                end
340
404
 
 
405
                it "should return the result of saving to the terminus" do
 
406
                    request = stub 'request', :instance => @instance, :node => nil
 
407
 
 
408
                    @indirection.expects(:request).returns request
 
409
 
 
410
                    @cache.stubs(:save)
 
411
                    @terminus.stubs(:save).returns @instance
 
412
                    @indirection.save(@instance).should equal(@instance)
 
413
                end
 
414
 
341
415
                it "should use a request to save the object to the cache" do
342
416
                    request = stub 'request', :instance => @instance, :node => nil
343
417
 
347
421
                    @terminus.stubs(:save)
348
422
                    @indirection.save(@instance)
349
423
                end
 
424
 
 
425
                it "should not save to the cache if the normal save fails" do
 
426
                    request = stub 'request', :instance => @instance, :node => nil
 
427
 
 
428
                    @indirection.expects(:request).returns request
 
429
 
 
430
                    @cache.expects(:save).never
 
431
                    @terminus.expects(:save).raises "eh"
 
432
                    lambda { @indirection.save(@instance) }.should raise_error
 
433
                end
350
434
            end
351
435
        end
352
 
        
 
436
 
353
437
        describe "and removing a model instance" do
354
438
            before { @method = :destroy }
355
439
 
494
578
 
495
579
        after :each do
496
580
            @indirection.delete
497
 
            Puppet::Indirector::Indirection.clear_cache
 
581
            Puppet::Util::Cacher.expire
498
582
        end
499
583
    end
500
584
 
504
588
            @indirection = Puppet::Indirector::Indirection.new(mock('model'), :test)
505
589
            Puppet::Indirector::Indirection.instance(:test).should equal(@indirection)
506
590
        end
507
 
        
 
591
 
508
592
        it "should return nil when the named indirection has not been created" do
509
593
            Puppet::Indirector::Indirection.instance(:test).should be_nil
510
594
        end
514
598
            @indirection = Puppet::Indirector::Indirection.new(mock_model, :test)
515
599
            Puppet::Indirector::Indirection.model(:test).should equal(mock_model)
516
600
        end
517
 
        
 
601
 
518
602
        it "should return nil when no model matches the requested name" do
519
603
            Puppet::Indirector::Indirection.model(:test).should be_nil
520
604
        end
615
699
            @indirection.terminus(:foo).should equal(@terminus)
616
700
        end
617
701
 
618
 
        it "should allow the clearance of cached terminus instances" do
619
 
            terminus1 = mock 'terminus1'
620
 
            terminus2 = mock 'terminus2'
621
 
            @terminus_class.stubs(:new).returns(terminus1, terminus2, ArgumentError)
622
 
            @indirection.terminus(:foo).should equal(terminus1)
623
 
            @indirection.class.clear_cache
624
 
            @indirection.terminus(:foo).should equal(terminus2)
625
 
        end
626
 
 
627
702
        # Make sure it caches the terminus.
628
703
        it "should return the same terminus instance each time for a given name" do
629
704
            @terminus_class.stubs(:new).returns(@terminus)
638
713
 
639
714
        after do
640
715
            @indirection.delete
641
 
            Puppet::Indirector::Indirection.clear_cache
642
716
        end
643
717
    end
644
718
 
664
738
            proc { @indirection.cache_class = "" }.should raise_error(ArgumentError)
665
739
        end
666
740
 
667
 
        it "should fail to set the cache class when the cache class name is nil" do
668
 
            proc { @indirection.cache_class = nil }.should raise_error(ArgumentError)
 
741
        it "should allow resetting the cache_class to nil" do
 
742
            @indirection.cache_class = nil
 
743
            @indirection.cache_class.should be_nil
669
744
        end
670
745
 
671
746
        it "should fail to set the cache class when the specified cache class cannot be found" do
675
750
 
676
751
        after do
677
752
            @indirection.delete
678
 
            Puppet::Indirector::Indirection.clear_cache
679
753
        end
680
754
    end
681
755
 
706
780
                @indirection.cache.should equal(@cache)
707
781
                @indirection.cache.should equal(@cache)
708
782
            end
709
 
 
710
 
            it "should remove the cache terminus when all other terminus instances are cleared" do
711
 
                cache2 = mock 'cache2'
712
 
                @cache_class.stubs(:new).returns(@cache, cache2)
713
 
                @indirection.cache_class = :cache_terminus
714
 
                @indirection.cache.should equal(@cache)
715
 
                @indirection.clear_cache
716
 
                @indirection.cache.should equal(cache2)
717
 
            end
718
783
        end
719
784
 
720
785
        describe "and saving" do
721
786
        end
722
 
        
 
787
 
723
788
        describe "and finding" do
724
789
        end
725
 
        
 
790
 
726
791
        after :each do
727
792
            @indirection.delete
728
 
            Puppet::Indirector::Indirection.clear_cache
729
793
        end
730
794
    end
731
795
end