~mathiaz/puppet/reductivelabs-master

« back to all changes in this revision

Viewing changes to spec/unit/ssl/certificate_authority.rb

  • Committer: Luke Kanies
  • Date: 2008-04-20 00:08:36 UTC
  • Revision ID: git-v1:ebdbe4880d8c20965ac21a473b2bfc1ab953b6d4
Added an Interface class to the CA to model puppetca's usage.

This class provides all of the semantics from puppetca,
and appears to entirely duplicate the behaviour of the existing
executable, with basically all of the code in a library
file, instead of the executable.

As such, I've deleted the test for the executable.  We should have
one, but it's not nearly as important.

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
 
5
5
require 'puppet/ssl/certificate_authority'
6
6
 
 
7
describe "a normal interface method", :shared => true do
 
8
    it "should call the method on the CA for each host specified if an array was provided" do
 
9
        @ca.expects(@method).with("host1")
 
10
        @ca.expects(@method).with("host2")
 
11
 
 
12
        @applier = Puppet::SSL::CertificateAuthority::Interface.new(@method, %w{host1 host2})
 
13
 
 
14
        @applier.apply(@ca)
 
15
    end
 
16
 
 
17
    it "should call the method on the CA for all existing certificates if :all was provided" do
 
18
        @ca.expects(:list).returns %w{host1 host2}
 
19
 
 
20
        @ca.expects(@method).with("host1")
 
21
        @ca.expects(@method).with("host2")
 
22
 
 
23
        @applier = Puppet::SSL::CertificateAuthority::Interface.new(@method, :all)
 
24
 
 
25
        @applier.apply(@ca)
 
26
    end
 
27
end
 
28
 
7
29
describe Puppet::SSL::CertificateAuthority do
8
30
    describe "when initializing" do
9
31
        before do
381
403
            @cacert.stubs(:content).returns "cacertificate"
382
404
            @ca = Puppet::SSL::CertificateAuthority.new
383
405
        end
384
 
        
 
406
 
 
407
        it "should have a method for acting on the SSL files" do
 
408
            @ca.should respond_to(:apply)
 
409
        end
 
410
 
 
411
        describe "when applying a method to a set of hosts" do
 
412
            it "should fail if no subjects have been specified" do
 
413
                lambda { @ca.apply(:generate) }.should raise_error(ArgumentError)
 
414
            end
 
415
 
 
416
            it "should create an Interface instance with the specified method and the subjects" do
 
417
                Puppet::SSL::CertificateAuthority::Interface.expects(:new).with(:generate, :hosts).returns(stub('applier', :apply => nil))
 
418
                @ca.apply(:generate, :to => :hosts)
 
419
            end
 
420
 
 
421
            it "should apply the Interface with itself as the argument" do
 
422
                applier = stub('applier')
 
423
                applier.expects(:apply).with(@ca)
 
424
                Puppet::SSL::CertificateAuthority::Interface.expects(:new).returns applier
 
425
                @ca.apply(:generate, :to => :whatever)
 
426
            end
 
427
        end
 
428
 
385
429
        it "should be able to list waiting certificate requests" do
386
430
            req1 = stub 'req1', :name => "one"
387
431
            req2 = stub 'req2', :name => "two"
565
609
        end
566
610
    end
567
611
end
 
612
 
 
613
describe Puppet::SSL::CertificateAuthority::Interface do
 
614
    before do
 
615
        @class = Puppet::SSL::CertificateAuthority::Interface
 
616
    end
 
617
    describe "when initializing" do
 
618
        it "should set its method using its settor" do
 
619
            @class.any_instance.expects(:method=).with(:generate)
 
620
            @class.new(:generate, :all)
 
621
        end
 
622
 
 
623
        it "should set its subjects using the settor" do
 
624
            @class.any_instance.expects(:subjects=).with(:all)
 
625
            @class.new(:generate, :all)
 
626
        end
 
627
    end
 
628
 
 
629
    describe "when setting the method" do
 
630
        it "should set the method" do
 
631
            @class.new(:generate, :all).method.should == :generate
 
632
        end
 
633
 
 
634
        it "should fail if the method isn't a member of the INTERFACE_METHODS array" do
 
635
            Puppet::SSL::CertificateAuthority::Interface::INTERFACE_METHODS.expects(:include?).with(:thing).returns false
 
636
 
 
637
            lambda { @class.new(:thing, :all) }.should raise_error(ArgumentError)
 
638
        end
 
639
    end
 
640
 
 
641
    describe "when setting the subjects" do
 
642
        it "should set the subjects" do
 
643
            @class.new(:generate, :all).subjects.should == :all
 
644
        end
 
645
 
 
646
        it "should fail if the subjects setting isn't :all or an array" do
 
647
            lambda { @class.new(:generate, "other") }.should raise_error(ArgumentError)
 
648
        end
 
649
    end
 
650
 
 
651
    it "should have a method for triggering the application" do
 
652
        @class.new(:generate, :all).should respond_to(:apply)
 
653
    end
 
654
 
 
655
    describe "when applying" do
 
656
        before do
 
657
            # We use a real object here, because :verify can't be stubbed, apparently.
 
658
            @ca = Object.new
 
659
        end
 
660
 
 
661
        it "should raise InterfaceErrors" do
 
662
            @applier = @class.new(:revoke, :all)
 
663
 
 
664
            @ca.expects(:list).raises Puppet::SSL::CertificateAuthority::Interface::InterfaceError
 
665
 
 
666
            lambda { @applier.apply(@ca) }.should raise_error(Puppet::SSL::CertificateAuthority::Interface::InterfaceError)
 
667
        end
 
668
 
 
669
        it "should log non-Interface failures rather than failing" do
 
670
            @applier = @class.new(:revoke, :all)
 
671
 
 
672
            @ca.expects(:list).raises ArgumentError
 
673
 
 
674
            Puppet.expects(:err)
 
675
 
 
676
            lambda { @applier.apply(@ca) }.should_not raise_error
 
677
        end
 
678
 
 
679
        describe "with an empty array specified and the method is not list" do
 
680
            it "should fail" do
 
681
                @applier = @class.new(:sign, [])
 
682
                lambda { @applier.apply(@ca) }.should raise_error(ArgumentError)
 
683
            end
 
684
        end
 
685
 
 
686
        describe ":generate" do
 
687
            it "should fail if :all was specified" do
 
688
                @applier = @class.new(:generate, :all)
 
689
                lambda { @applier.apply(@ca) }.should raise_error(ArgumentError)
 
690
            end
 
691
 
 
692
            it "should call :generate on the CA for each host specified" do
 
693
                @applier = @class.new(:generate, %w{host1 host2})
 
694
                
 
695
                @ca.expects(:generate).with("host1")
 
696
                @ca.expects(:generate).with("host2")
 
697
 
 
698
                @applier.apply(@ca)
 
699
            end
 
700
        end
 
701
 
 
702
        describe ":verify" do
 
703
            before { @method = :verify }
 
704
            #it_should_behave_like "a normal interface method"
 
705
 
 
706
            it "should call the method on the CA for each host specified if an array was provided" do
 
707
                # LAK:NOTE Mocha apparently doesn't allow you to mock :verify, but I'm confident this works in real life.
 
708
            end
 
709
 
 
710
            it "should call the method on the CA for all existing certificates if :all was provided" do
 
711
                # LAK:NOTE Mocha apparently doesn't allow you to mock :verify, but I'm confident this works in real life.
 
712
            end
 
713
        end
 
714
 
 
715
        describe ":destroy" do
 
716
            before { @method = :destroy }
 
717
            it_should_behave_like "a normal interface method"
 
718
        end
 
719
 
 
720
        describe ":revoke" do
 
721
            before { @method = :revoke }
 
722
            it_should_behave_like "a normal interface method"
 
723
        end
 
724
 
 
725
        describe ":sign" do
 
726
            describe "and an array of names was provided" do
 
727
                before do
 
728
                    @applier = @class.new(:sign, %w{host1 host2})
 
729
                end
 
730
 
 
731
                it "should sign the specified waiting certificate requests" do
 
732
                    @ca.expects(:sign).with("host1")
 
733
                    @ca.expects(:sign).with("host2")
 
734
 
 
735
                    @applier.apply(@ca)
 
736
                end
 
737
            end
 
738
 
 
739
            describe "and :all was provided" do
 
740
                it "should sign all waiting certificate requests" do
 
741
                    @ca.stubs(:waiting?).returns(%w{cert1 cert2})
 
742
 
 
743
                    @ca.expects(:sign).with("cert1")
 
744
                    @ca.expects(:sign).with("cert2")
 
745
 
 
746
                    @applier = @class.new(:sign, :all)
 
747
                    @applier.apply(@ca)
 
748
                end
 
749
 
 
750
                it "should fail if there are no waiting certificate requests" do
 
751
                    @ca.stubs(:waiting?).returns([])
 
752
 
 
753
                    @applier = @class.new(:sign, :all)
 
754
                    lambda { @applier.apply(@ca) }.should raise_error(Puppet::SSL::CertificateAuthority::Interface::InterfaceError)
 
755
                end
 
756
            end
 
757
        end
 
758
 
 
759
        describe ":list" do
 
760
            describe "and an empty array was provided" do
 
761
                it "should print a string containing all certificate requests" do
 
762
                    @ca.expects(:waiting?).returns %w{host1 host2}
 
763
 
 
764
                    @applier = @class.new(:list, [])
 
765
 
 
766
                    @applier.expects(:puts).with "host1\nhost2"
 
767
 
 
768
                    @applier.apply(@ca)
 
769
                end
 
770
            end
 
771
 
 
772
            describe "and :all was provided" do
 
773
                it "should print a string containing all certificate requests and certificates" do
 
774
                    @ca.expects(:waiting?).returns %w{host1 host2}
 
775
                    @ca.expects(:list).returns %w{host3 host4}
 
776
 
 
777
                    @applier = @class.new(:list, :all)
 
778
 
 
779
                    @applier.expects(:puts).with "host1"
 
780
                    @applier.expects(:puts).with "host2"
 
781
                    @applier.expects(:puts).with "+ host3"
 
782
                    @applier.expects(:puts).with "+ host4"
 
783
 
 
784
                    @applier.apply(@ca)
 
785
                end
 
786
            end
 
787
 
 
788
            describe "and an array of names was provided" do
 
789
                it "should print a string of all named hosts that have a waiting request" do
 
790
                    @ca.expects(:waiting?).returns %w{host1 host2}
 
791
                    @ca.expects(:list).returns %w{host3 host4}
 
792
 
 
793
                    @applier = @class.new(:list, %w{host1 host2 host3 host4})
 
794
 
 
795
                    @applier.expects(:puts).with "host1"
 
796
                    @applier.expects(:puts).with "host2"
 
797
                    @applier.expects(:puts).with "+ host3"
 
798
                    @applier.expects(:puts).with "+ host4"
 
799
 
 
800
                    @applier.apply(@ca)
 
801
                end
 
802
            end
 
803
        end
 
804
 
 
805
        describe ":print" do
 
806
            describe "and :all was provided" do
 
807
                it "should print all certificates" do
 
808
                    @ca.expects(:list).returns %w{host1 host2}
 
809
 
 
810
                    @applier = @class.new(:print, :all)
 
811
 
 
812
                    @ca.expects(:print).with("host1").returns "h1"
 
813
                    @applier.expects(:puts).with "h1"
 
814
 
 
815
                    @ca.expects(:print).with("host2").returns "h2"
 
816
                    @applier.expects(:puts).with "h2"
 
817
 
 
818
                    @applier.apply(@ca)
 
819
                end
 
820
            end
 
821
 
 
822
            describe "and an array of names was provided" do
 
823
                it "should print each named certificate if found" do
 
824
                    @applier = @class.new(:print, %w{host1 host2})
 
825
 
 
826
                    @ca.expects(:print).with("host1").returns "h1"
 
827
                    @applier.expects(:puts).with "h1"
 
828
 
 
829
                    @ca.expects(:print).with("host2").returns "h2"
 
830
                    @applier.expects(:puts).with "h2"
 
831
 
 
832
                    @applier.apply(@ca)
 
833
                end
 
834
 
 
835
                it "should log any named but not found certificates" do
 
836
                    @applier = @class.new(:print, %w{host1 host2})
 
837
 
 
838
                    @ca.expects(:print).with("host1").returns "h1"
 
839
                    @applier.expects(:puts).with "h1"
 
840
 
 
841
                    @ca.expects(:print).with("host2").returns nil
 
842
                    Puppet.expects(:err).with { |msg| msg.include?("host2") }
 
843
 
 
844
                    @applier.apply(@ca)
 
845
                end
 
846
            end
 
847
        end
 
848
    end
 
849
end