summaryrefslogtreecommitdiffstats
path: root/spec/unit/ssl/inventory_spec.rb
blob: bf1dbfb4875d1ae9a1423eae00430c9bbe886966 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/env ruby

require File.dirname(__FILE__) + '/../../spec_helper'

require 'puppet/ssl/inventory'

describe Puppet::SSL::Inventory do
    before do
        @class = Puppet::SSL::Inventory
    end

    it "should use the :certinventory setting for the path to the inventory file" do
        Puppet.settings.expects(:value).with(:cert_inventory).returns "/inven/tory"

        @class.any_instance.stubs(:rebuild)

        @class.new.path.should == "/inven/tory"
    end

    describe "when initializing" do
        it "should set its path to the inventory file" do
            Puppet.settings.stubs(:value).with(:cert_inventory).returns "/inven/tory"
            @class.new.path.should == "/inven/tory"
        end
    end

    describe "when managing an inventory" do
        before do
            Puppet.settings.stubs(:value).with(:cert_inventory).returns "/inven/tory"

            FileTest.stubs(:exist?).with("/inven/tory").returns true

            @inventory = @class.new

            @cert = mock 'cert'
        end

        describe "and creating the inventory file" do
            before do
                Puppet.settings.stubs(:write)
                FileTest.stubs(:exist?).with("/inven/tory").returns false

                Puppet::SSL::Certificate.stubs(:search).returns []
            end

            it "should log that it is building a new inventory file" do
                Puppet.expects(:notice)

                @inventory.rebuild
            end

            it "should use the Settings to write to the file" do
                Puppet.settings.expects(:write).with(:cert_inventory)

                @inventory.rebuild
            end

            it "should add a header to the file" do
                fh = mock 'filehandle'
                Puppet.settings.stubs(:write).yields fh
                fh.expects(:print).with { |str| str =~ /^#/ }

                @inventory.rebuild
            end

            it "should add formatted information on all existing certificates" do
                cert1 = mock 'cert1'
                cert2 = mock 'cert2'

                Puppet::SSL::Certificate.expects(:search).with("*").returns [cert1, cert2]

                @class.any_instance.expects(:add).with(cert1)
                @class.any_instance.expects(:add).with(cert2)

                @inventory.rebuild
            end
        end

        describe "and adding a certificate" do
            it "should build the inventory file if one does not exist" do
                Puppet.settings.stubs(:value).with(:cert_inventory).returns "/inven/tory"
                Puppet.settings.stubs(:write)

                FileTest.expects(:exist?).with("/inven/tory").returns false

                @inventory.expects(:rebuild)

                @inventory.add(@cert)
            end

            it "should use the Settings to write to the file" do
                Puppet.settings.expects(:write).with(:cert_inventory, "a")

                @inventory.add(@cert)
            end

            it "should use the actual certificate if it was passed a Puppet certificate" do
                cert = Puppet::SSL::Certificate.new("mycert")
                cert.content = @cert

                fh = stub 'filehandle', :print => nil
                Puppet.settings.stubs(:write).yields fh

                @inventory.expects(:format).with(@cert)

                @inventory.add(@cert)
            end

            it "should add formatted certificate information to the end of the file" do
                fh = mock 'filehandle'

                Puppet.settings.stubs(:write).yields fh

                @inventory.expects(:format).with(@cert).returns "myformat"

                fh.expects(:print).with("myformat")

                @inventory.add(@cert)
            end
        end

        describe "and formatting a certificate" do
            before do
                @cert = stub 'cert', :not_before => Time.now, :not_after => Time.now, :subject => "mycert", :serial => 15
            end

            it "should print the serial number as a 4 digit hex number in the first field" do
                @inventory.format(@cert).split[0].should == "0x000f" # 15 in hex
            end

            it "should print the not_before date in '%Y-%m-%dT%H:%M:%S%Z' format in the second field" do
                @cert.not_before.expects(:strftime).with('%Y-%m-%dT%H:%M:%S%Z').returns "before_time"

                @inventory.format(@cert).split[1].should == "before_time"
            end

            it "should print the not_after date in '%Y-%m-%dT%H:%M:%S%Z' format in the third field" do
                @cert.not_after.expects(:strftime).with('%Y-%m-%dT%H:%M:%S%Z').returns "after_time"

                @inventory.format(@cert).split[2].should == "after_time"
            end

            it "should print the subject in the fourth field" do
                @inventory.format(@cert).split[3].should == "mycert"
            end

            it "should add a carriage return" do
                @inventory.format(@cert).should =~ /\n$/
            end

            it "should produce a line consisting of the serial number, start date, expiration date, and subject" do
                # Just make sure our serial and subject bracket the lines.
                @inventory.format(@cert).should =~ /^0x.+mycert$/
            end
        end

        it "should be able to find a given host's serial number" do
            @inventory.should respond_to(:serial)
        end

        describe "and finding a serial number" do
            it "should return nil if the inventory file is missing" do
                FileTest.expects(:exist?).with("/inven/tory").returns false
                @inventory.serial(:whatever).should be_nil
            end

            it "should return the serial number from the line matching the provided name" do
                File.expects(:readlines).with("/inven/tory").returns ["0x00f blah blah /CN=me\n", "0x001 blah blah /CN=you\n"]

                @inventory.serial("me").should == 15
            end

            it "should return the number as an integer" do
                File.expects(:readlines).with("/inven/tory").returns ["0x00f blah blah /CN=me\n", "0x001 blah blah /CN=you\n"]

                @inventory.serial("me").should == 15
            end
        end
    end
end