summaryrefslogtreecommitdiffstats
path: root/test/ral/type/sshkey.rb
blob: 981079e920440bea31a2766b063f1656a50ebb7b (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
181
182
183
184
185
186
187
188
#!/usr/bin/env ruby

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

require 'puppettest'
require 'facter'

class TestSSHKey < Test::Unit::TestCase
    include PuppetTest
    def setup
        super
        # god i'm lazy
        @sshkeytype = Puppet::Type.type(:sshkey)

        @provider = @sshkeytype.defaultprovider

        # Make sure they aren't using something funky like netinfo
        unless @provider.name == :parsed
            @sshkeytype.defaultprovider = @sshkeytype.provider(:parsed)
        end

        cleanup do @sshkeytype.defaultprovider = nil end

        if @provider.respond_to?(:default_target)
            oldpath = @provider.default_target
            cleanup do
                @provider.default_target = oldpath
            end
            @provider.default_target = tempfile()
        end
    end

    def teardown
        super
        if @provider.respond_to?(:clear)
            @provider.clear
        end
    end

    def mkkey
        key = nil

        if defined? @kcount
            @kcount += 1
        else
            @kcount = 1
        end

        @catalog ||= mk_catalog

        key = @sshkeytype.new(
            :name => "host%s.madstop.com" % @kcount,
            :key => "%sAAAAB3NzaC1kc3MAAACBAMnhSiku76y3EGkNCDsUlvpO8tRgS9wL4Eh54WZfQ2lkxqfd2uT/RTT9igJYDtm/+UHuBRdNGpJYW1Nw2i2JUQgQEEuitx4QKALJrBotejGOAWxxVk6xsh9xA0OW8Q3ZfuX2DDitfeC8ZTCl4xodUMD8feLtP+zEf8hxaNamLlt/AAAAFQDYJyf3vMCWRLjTWnlxLtOyj/bFpwAAAIEAmRxxXb4jjbbui9GYlZAHK00689DZuX0EabHNTl2yGO5KKxGC6Esm7AtjBd+onfu4Rduxut3jdI8GyQCIW8WypwpJofCIyDbTUY4ql0AQUr3JpyVytpnMijlEyr41FfIb4tnDqnRWEsh2H7N7peW+8DWZHDFnYopYZJ9Yu4/jHRYAAACAERG50e6aRRb43biDr7Ab9NUCgM9bC0SQscI/xdlFjac0B/kSWJYTGVARWBDWug705hTnlitY9cLC5Ey/t/OYOjylTavTEfd/bh/8FkAYO+pWdW3hx6p97TBffK0b6nrc6OORT2uKySbbKOn0681nNQh4a6ueR3JRppNkRPnTk5c=" % @kcount,
            :type => "ssh-dss",
            :alias => ["192.168.0.%s" % @kcount],
            :catalog => @catalog
        )

        @catalog.add_resource(key)

        return key
    end

    def test_instances
        list = nil
        assert_nothing_raised {
            list = Puppet::Type.type(:sshkey).instances
        }

        count = 0
        list.each do |h|
            count += 1
        end

        assert_equal(0, count, "Found sshkeys in empty file somehow")
    end

    def test_simplekey
        key = mkkey
        file = tempfile()
        key[:target] = file
        key[:provider] = :parsed

        assert_apply(key)
        
        assert_events([], key, "created events on in-sync key")
        
        assert(key.provider.exists?, "Key did not get created")
        
        # Now create a new key object
        name = key.name
        key = nil
        
        key = @sshkeytype.new :name => name, :target => file, :provider => :parsed
        key.retrieve
        
        assert(key.provider.exists?, "key thinks it does not exist")
        
    end

    def test_moddingkey
        key = mkkey()

        @catalog.apply

        key.retrieve

        aliases = %w{madstop kirby yayness}
        key[:alias] = aliases

        @catalog.apply

        aliases.each do |name|
            assert_equal(key.object_id, @catalog.resource(:sshkey, name).object_id, "alias %s was not set" % name)
        end
    end

    def test_aliasisproperty
        assert_equal(:property, @sshkeytype.attrtype(:alias))
    end

    def test_multivalues
        key = mkkey
        assert_raise(Puppet::Error) {
            key[:alias] = "puppetmasterd yayness"
        }
    end

    def test_puppetalias
        key = mkkey()

        assert_nothing_raised {
            key[:alias] = "testing"
        }

        key.finish

        same = @catalog.resource(:sshkey, "testing")
        assert(same, "Could not retrieve by alias")
    end

    def test_removal
        sshkey = mkkey()
        assert_nothing_raised {
            sshkey[:ensure] = :present
        }
        assert_events([:sshkey_created], sshkey)

        assert(sshkey.provider.exists?, "key was not created")
        assert_nothing_raised {
            sshkey[:ensure] = :absent
        }

        assert_events([:sshkey_removed], sshkey)
        assert(! sshkey.provider.exists?, "Key was not deleted")
        assert_events([], sshkey)
    end

    # Make sure changes actually modify the file.
    def test_modifyingfile
        keys = []
        names = []
        3.times {
            k = mkkey()
            #h[:ensure] = :present
            #h.retrieve
            keys << k
            names << k.name
        }
        assert_apply(*keys)
        keys.clear

        @catalog.clear(true)
        @catalog = nil

        newkey = mkkey()
        #newkey[:ensure] = :present
        names << newkey.name
        assert_apply(newkey)

        # Verify we can retrieve that info
        assert_nothing_raised("Could not retrieve after second write") {
            newkey.provider.prefetch
        }

        assert(newkey.provider.exists?, "Did not see key in file")
    end
end