~ubuntu-branches/ubuntu/oneiric/puppet/oneiric-security

« back to all changes in this revision

Viewing changes to spec/unit/executables/client/certhandler.rb

  • Committer: Bazaar Package Importer
  • Author(s): Micah Anderson
  • Date: 2008-07-26 15:43:45 UTC
  • mto: (3.1.1 lenny) (1.3.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 16.
  • Revision ID: james.westby@ubuntu.com-20080726154345-1fmgo76b4l72ulvc
ImportĀ upstreamĀ versionĀ 0.24.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env ruby
 
2
 
 
3
Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") }
 
4
 
 
5
require 'puppet/executables/client/certhandler'
 
6
 
 
7
cert_handler = Puppet::Executables::Client::CertHandler
 
8
 
 
9
describe cert_handler, "when handling certificates" do
 
10
    before do 
 
11
        @caclient = mock('caclient')
 
12
        caclient_class = mock('caclient_class')
 
13
        caclient_class.stubs(:new).returns(@caclient)
 
14
        Puppet::Network::Client.stubs(:ca).returns(caclient_class)
 
15
    end
 
16
 
 
17
    describe "when reading or retrieving the certificate" do
 
18
        before do
 
19
            @handler = cert_handler.new(1,true)
 
20
        end
 
21
 
 
22
        it "should attempt to read the certificate" do
 
23
            @handler.expects(:read_cert).returns true
 
24
            @handler.read_retrieve
 
25
        end
 
26
 
 
27
        it "should delegate to Puppet::Network::HttpPool to read the certificate" do
 
28
            Puppet::Network::HttpPool.expects(:read_cert).returns(true)
 
29
            @handler.read_retrieve
 
30
        end
 
31
 
 
32
        it "should not attempt to retrieve a certificate if one can be read" do
 
33
            @handler.stubs(:read_cert).returns true
 
34
            @handler.expects(:retrieve_cert).never
 
35
            @handler.read_retrieve
 
36
        end
 
37
 
 
38
        it "should attempt to retrieve a certificate if none can be read" do
 
39
            @handler.stubs(:read_cert).returns false
 
40
            @handler.expects(:retrieve_cert)
 
41
            @handler.read_retrieve
 
42
        end
 
43
 
 
44
        it "should delegate to caclient to retrieve a certificate" do
 
45
            @handler.stubs(:read_cert).returns false
 
46
            @caclient.expects(:request_cert).returns(true)
 
47
            @handler.stubs(:read_new_cert).returns(true)
 
48
            @handler.read_retrieve
 
49
        end
 
50
 
 
51
        it "should return true if the certificate exists" do
 
52
            @handler.stubs(:read_cert).returns true
 
53
            @handler.read_retrieve.should be_true
 
54
        end
 
55
 
 
56
        it "should return false when getting a new cert" do
 
57
            #This is the second call to httppool that happens in 'read_new_cert'
 
58
            Puppet::Network::HttpPool.expects(:read_cert).returns(true)
 
59
            @caclient.stubs(:request_cert).returns(true)
 
60
            @handler.stubs(:read_cert).returns(false)
 
61
            @handler.read_retrieve.should be_false
 
62
        end
 
63
    end
 
64
 
 
65
    describe "when waiting for cert" do
 
66
        before do
 
67
            @handler = cert_handler.new(1,false)
 
68
            @handler.stubs(:read_cert).returns false
 
69
            #all waiting for cert tests should loop, which will always happen if sleep is called
 
70
            #yeah, I put the expectation in the setup, deal with it
 
71
            @handler.expects(:sleep).with(1)
 
72
 
 
73
            #This is needed to get out of the loop
 
74
            @handler.stubs(:read_new_cert).returns(true)
 
75
        end
 
76
 
 
77
        it "should loop when the cert request does not return a certificate" do
 
78
            @caclient.stubs(:request_cert).times(2).returns(false).then.returns(true)
 
79
            @handler.retrieve_cert
 
80
        end
 
81
 
 
82
        it "should loop when the cert request raises an Error" do
 
83
            @caclient.stubs(:request_cert).times(2).raises(StandardError, 'Testing').then.returns(true)
 
84
            @handler.retrieve_cert
 
85
        end
 
86
        
 
87
        it "should loop when the new cert can't be read" do
 
88
            @caclient.stubs(:request_cert).returns(true)
 
89
            @handler.stubs(:read_new_cert).times(2).returns(false).then.returns(true)
 
90
            @handler.retrieve_cert
 
91
        end
 
92
    end
 
93
 
 
94
    describe "when in one time mode" do
 
95
        before do
 
96
            #true puts us in onetime mode
 
97
            @handler = cert_handler.new(1,true)
 
98
            @handler.stubs(:read_cert).returns false
 
99
        end
 
100
 
 
101
        it "should exit if the cert request does not return a certificate" do
 
102
            @caclient.stubs(:request_cert).returns(false)
 
103
            @handler.expects(:exit).with(1).raises(SystemExit)
 
104
            lambda { @handler.retrieve_cert }.should raise_error(SystemExit)
 
105
        end
 
106
 
 
107
 
 
108
        it "should exit if the cert request raises an exception" do
 
109
            @caclient.stubs(:request_cert).raises(StandardError, 'Testing')
 
110
            @handler.expects(:exit).with(23).raises(SystemExit)
 
111
            lambda { @handler.retrieve_cert }.should raise_error(SystemExit)
 
112
        end
 
113
        
 
114
        it "should exit if the new cert can't be read" do
 
115
            @caclient.stubs(:request_cert).returns(true)
 
116
            #this is the second, call to httppool inside read_new_cert
 
117
            Puppet::Network::HttpPool.stubs(:read_cert).returns(false)
 
118
            @handler.expects(:exit).with(34).raises(SystemExit)
 
119
            lambda { @handler.retrieve_cert }.should raise_error(SystemExit)
 
120
        end
 
121
    end
 
122
end