summaryrefslogtreecommitdiffstats
path: root/test/certmgr/inventory.rb
blob: fa612b2c1e3cc4d9eee2e4d8d1788352f54eb68e (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
#!/usr/bin/env ruby

require File.expand_path(File.dirname(__FILE__) + '/../lib/puppettest')

require 'puppet'
require 'puppettest/certificates'
require 'puppet/sslcertificates/inventory.rb'
require 'mocha'

class TestCertInventory < Test::Unit::TestCase
  include PuppetTest::Certificates

  Inventory = Puppet::SSLCertificates::Inventory

  def setup
    super
    Puppet::Util::SUIDManager.stubs(:asuser).yields
  end

  def test_format
    cert = mksignedcert

    format = nil
    assert_nothing_raised do
      format = Inventory.format(cert)
    end


      assert(
        format =~ /^0x0001 \S+ \S+ #{cert.subject}/,

        "Did not create correct format")
    end

  def test_init
    # First create a couple of certificates
    ca = mkCA

    cert1 = mksignedcert(ca, "host1.madstop.com")
    cert2 = mksignedcert(ca, "host2.madstop.com")

    init = nil
    assert_nothing_raised do
      init = Inventory.init
    end

    [cert1, cert2].each do |cert|
      assert(init.include?(cert.subject.to_s), "Did not catch #{cert.subject}")
    end
  end

  def test_add
    ca = mkCA
    cert = mksignedcert(ca, "host.domain.com")

    assert_nothing_raised do
      file = mock
      file.expects(:puts).with do |written|
        written.include? cert.subject.to_s
      end
      Puppet::Util::Settings.any_instance.stubs(:write)
      Puppet::Util::Settings.any_instance.expects(:write).
        with(:cert_inventory, 'a').yields(file)

      Puppet::SSLCertificates::Inventory.add(cert)
    end
  end
end