3
require File.dirname(__FILE__) + '/../../spec_helper'
5
require 'puppet/ssl/inventory'
7
describe Puppet::SSL::Inventory do
9
@class = Puppet::SSL::Inventory
12
it "should use the :certinventory setting for the path to the inventory file" do
13
Puppet.settings.expects(:value).with(:cert_inventory).returns "/inven/tory"
15
@class.any_instance.stubs(:rebuild)
17
@class.new.path.should == "/inven/tory"
20
describe "when initializing" do
21
it "should set its path to the inventory file" do
22
Puppet.settings.stubs(:value).with(:cert_inventory).returns "/inven/tory"
23
@class.new.path.should == "/inven/tory"
27
describe "when managing an inventory" do
29
Puppet.settings.stubs(:value).with(:cert_inventory).returns "/inven/tory"
31
FileTest.stubs(:exist?).with("/inven/tory").returns true
33
@inventory = @class.new
38
describe "and creating the inventory file" do
40
Puppet.settings.stubs(:write)
41
FileTest.stubs(:exist?).with("/inven/tory").returns false
43
Puppet::SSL::Certificate.stubs(:search).returns []
46
it "should log that it is building a new inventory file" do
47
Puppet.expects(:notice)
52
it "should use the Settings to write to the file" do
53
Puppet.settings.expects(:write).with(:cert_inventory)
58
it "should add a header to the file" do
59
fh = mock 'filehandle'
60
Puppet.settings.stubs(:write).yields fh
61
fh.expects(:print).with { |str| str =~ /^#/ }
66
it "should add formatted information on all existing certificates" do
70
Puppet::SSL::Certificate.expects(:search).with("*").returns [cert1, cert2]
72
@class.any_instance.expects(:add).with(cert1)
73
@class.any_instance.expects(:add).with(cert2)
79
describe "and adding a certificate" do
80
it "should build the inventory file if one does not exist" do
81
Puppet.settings.stubs(:value).with(:cert_inventory).returns "/inven/tory"
82
Puppet.settings.stubs(:write)
84
FileTest.expects(:exist?).with("/inven/tory").returns false
86
@inventory.expects(:rebuild)
91
it "should use the Settings to write to the file" do
92
Puppet.settings.expects(:write).with(:cert_inventory, "a")
97
it "should use the actual certificate if it was passed a Puppet certificate" do
98
cert = Puppet::SSL::Certificate.new("mycert")
101
fh = stub 'filehandle', :print => nil
102
Puppet.settings.stubs(:write).yields fh
104
@inventory.expects(:format).with(@cert)
106
@inventory.add(@cert)
109
it "should add formatted certificate information to the end of the file" do
110
fh = mock 'filehandle'
112
Puppet.settings.stubs(:write).yields fh
114
@inventory.expects(:format).with(@cert).returns "myformat"
116
fh.expects(:print).with("myformat")
118
@inventory.add(@cert)
122
describe "and formatting a certificate" do
124
@cert = stub 'cert', :not_before => Time.now, :not_after => Time.now, :subject => "mycert", :serial => 15
127
it "should print the serial number as a 4 digit hex number in the first field" do
128
@inventory.format(@cert).split[0].should == "0x000f" # 15 in hex
131
it "should print the not_before date in '%Y-%m-%dT%H:%M:%S%Z' format in the second field" do
132
@cert.not_before.expects(:strftime).with('%Y-%m-%dT%H:%M:%S%Z').returns "before_time"
134
@inventory.format(@cert).split[1].should == "before_time"
137
it "should print the not_after date in '%Y-%m-%dT%H:%M:%S%Z' format in the third field" do
138
@cert.not_after.expects(:strftime).with('%Y-%m-%dT%H:%M:%S%Z').returns "after_time"
140
@inventory.format(@cert).split[2].should == "after_time"
143
it "should print the subject in the fourth field" do
144
@inventory.format(@cert).split[3].should == "mycert"
147
it "should add a carriage return" do
148
@inventory.format(@cert).should =~ /\n$/
151
it "should produce a line consisting of the serial number, start date, expiration date, and subject" do
152
# Just make sure our serial and subject bracket the lines.
153
@inventory.format(@cert).should =~ /^0x.+mycert$/
157
it "should be able to find a given host's serial number" do
158
@inventory.should respond_to(:serial)
161
describe "and finding a serial number" do
162
it "should return nil if the inventory file is missing" do
163
FileTest.expects(:exist?).with("/inven/tory").returns false
164
@inventory.serial(:whatever).should be_nil
167
it "should return the serial number from the line matching the provided name" do
168
File.expects(:readlines).with("/inven/tory").returns ["0x00f blah blah /CN=me\n", "0x001 blah blah /CN=you\n"]
170
@inventory.serial("me").should == 15
173
it "should return the number as an integer" do
174
File.expects(:readlines).with("/inven/tory").returns ["0x00f blah blah /CN=me\n", "0x001 blah blah /CN=you\n"]
176
@inventory.serial("me").should == 15