~ubuntu-branches/ubuntu/lucid/puppet/lucid

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Chuck Short
  • Date: 2009-12-23 00:48:10 UTC
  • mfrom: (1.1.10 upstream) (3.1.7 squeeze)
  • Revision ID: james.westby@ubuntu.com-20091223004810-3i4oryds922g5n59
Tags: 0.25.1-3ubuntu1
* Merge from debian testing.  Remaining changes:
  - debian/rules:
    + Don't start puppet when first installing puppet.
  - debian/puppet.conf, lib/puppet/defaults.rb:
    + Move templates to /etc/puppet
  - lib/puppet/defaults.rb:
    + Fix /var/lib/puppet/state ownership.
  - man/man8/puppet.conf.8: 
    + Fix broken URL in manpage.
  - debian/control:
    + Update maintainer accordint to spec.
    + Puppetmaster Recommends -> Suggests
    + Created puppet-testsuite as a seperate. Allow the users to run puppet's 
      testsuite.
  - tests/Rakefile: Fix rakefile so that the testsuite can acutally be ran.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env ruby
 
2
 
 
3
require File.dirname(__FILE__) + '/../../spec_helper'
 
4
 
 
5
require 'puppet/ssl/inventory'
 
6
 
 
7
describe Puppet::SSL::Inventory do
 
8
    before do
 
9
        @class = Puppet::SSL::Inventory
 
10
    end
 
11
 
 
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"
 
14
 
 
15
        @class.any_instance.stubs(:rebuild)
 
16
 
 
17
        @class.new.path.should == "/inven/tory"
 
18
    end
 
19
 
 
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"
 
24
        end
 
25
    end
 
26
 
 
27
    describe "when managing an inventory" do
 
28
        before do
 
29
            Puppet.settings.stubs(:value).with(:cert_inventory).returns "/inven/tory"
 
30
 
 
31
            FileTest.stubs(:exist?).with("/inven/tory").returns true
 
32
 
 
33
            @inventory = @class.new
 
34
 
 
35
            @cert = mock 'cert'
 
36
        end
 
37
 
 
38
        describe "and creating the inventory file" do
 
39
            before do
 
40
                Puppet.settings.stubs(:write)
 
41
                FileTest.stubs(:exist?).with("/inven/tory").returns false
 
42
 
 
43
                Puppet::SSL::Certificate.stubs(:search).returns []
 
44
            end
 
45
 
 
46
            it "should log that it is building a new inventory file" do
 
47
                Puppet.expects(:notice)
 
48
 
 
49
                @inventory.rebuild
 
50
            end
 
51
 
 
52
            it "should use the Settings to write to the file" do
 
53
                Puppet.settings.expects(:write).with(:cert_inventory)
 
54
 
 
55
                @inventory.rebuild
 
56
            end
 
57
 
 
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 =~ /^#/ }
 
62
 
 
63
                @inventory.rebuild
 
64
            end
 
65
 
 
66
            it "should add formatted information on all existing certificates" do
 
67
                cert1 = mock 'cert1'
 
68
                cert2 = mock 'cert2'
 
69
 
 
70
                Puppet::SSL::Certificate.expects(:search).with("*").returns [cert1, cert2]
 
71
 
 
72
                @class.any_instance.expects(:add).with(cert1)
 
73
                @class.any_instance.expects(:add).with(cert2)
 
74
 
 
75
                @inventory.rebuild
 
76
            end
 
77
        end
 
78
 
 
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)
 
83
 
 
84
                FileTest.expects(:exist?).with("/inven/tory").returns false
 
85
 
 
86
                @inventory.expects(:rebuild)
 
87
 
 
88
                @inventory.add(@cert)
 
89
            end
 
90
 
 
91
            it "should use the Settings to write to the file" do
 
92
                Puppet.settings.expects(:write).with(:cert_inventory, "a")
 
93
 
 
94
                @inventory.add(@cert)
 
95
            end
 
96
 
 
97
            it "should use the actual certificate if it was passed a Puppet certificate" do
 
98
                cert = Puppet::SSL::Certificate.new("mycert")
 
99
                cert.content = @cert
 
100
 
 
101
                fh = stub 'filehandle', :print => nil
 
102
                Puppet.settings.stubs(:write).yields fh
 
103
 
 
104
                @inventory.expects(:format).with(@cert)
 
105
 
 
106
                @inventory.add(@cert)
 
107
            end
 
108
 
 
109
            it "should add formatted certificate information to the end of the file" do
 
110
                fh = mock 'filehandle'
 
111
 
 
112
                Puppet.settings.stubs(:write).yields fh
 
113
 
 
114
                @inventory.expects(:format).with(@cert).returns "myformat"
 
115
 
 
116
                fh.expects(:print).with("myformat")
 
117
 
 
118
                @inventory.add(@cert)
 
119
            end
 
120
        end
 
121
 
 
122
        describe "and formatting a certificate" do
 
123
            before do
 
124
                @cert = stub 'cert', :not_before => Time.now, :not_after => Time.now, :subject => "mycert", :serial => 15
 
125
            end
 
126
 
 
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
 
129
            end
 
130
 
 
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"
 
133
 
 
134
                @inventory.format(@cert).split[1].should == "before_time"
 
135
            end
 
136
 
 
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"
 
139
 
 
140
                @inventory.format(@cert).split[2].should == "after_time"
 
141
            end
 
142
 
 
143
            it "should print the subject in the fourth field" do
 
144
                @inventory.format(@cert).split[3].should == "mycert"
 
145
            end
 
146
 
 
147
            it "should add a carriage return" do
 
148
                @inventory.format(@cert).should =~ /\n$/
 
149
            end
 
150
 
 
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$/
 
154
            end
 
155
        end
 
156
 
 
157
        it "should be able to find a given host's serial number" do
 
158
            @inventory.should respond_to(:serial)
 
159
        end
 
160
 
 
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
 
165
            end
 
166
 
 
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"]
 
169
 
 
170
                @inventory.serial("me").should == 15
 
171
            end
 
172
 
 
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"]
 
175
 
 
176
                @inventory.serial("me").should == 15
 
177
            end
 
178
        end
 
179
    end
 
180
end