3
Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") }
5
require 'puppet/executables/client/certhandler'
7
cert_handler = Puppet::Executables::Client::CertHandler
9
describe cert_handler, "when handling certificates" 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)
17
describe "when reading or retrieving the certificate" do
19
@handler = cert_handler.new(1,true)
22
it "should attempt to read the certificate" do
23
@handler.expects(:read_cert).returns true
24
@handler.read_retrieve
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
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
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
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
51
it "should return true if the certificate exists" do
52
@handler.stubs(:read_cert).returns true
53
@handler.read_retrieve.should be_true
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
65
describe "when waiting for cert" 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)
73
#This is needed to get out of the loop
74
@handler.stubs(:read_new_cert).returns(true)
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
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
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
94
describe "when in one time mode" do
96
#true puts us in onetime mode
97
@handler = cert_handler.new(1,true)
98
@handler.stubs(:read_cert).returns false
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)
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)
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)