summaryrefslogtreecommitdiffstats
path: root/test/ral/type/host.rb
blob: 5cafddbc09430d542b5cfbd4e1a647ddeb472437 (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/usr/bin/env ruby

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

require 'puppettest'
require 'test/unit'
require 'facter'

class TestHost < Test::Unit::TestCase
	include PuppetTest

    def setup
        super
        @hosttype = Puppet::Type.type(:host)

        @provider = @hosttype.defaultprovider

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

        cleanup do @hosttype.defaultprovider = nil end

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

    def mkhost
        if defined? @hcount
            @hcount += 1
        else
            @hcount = 1
        end

        @catalog ||= mk_catalog

        host = nil
        assert_nothing_raised {
            host = Puppet::Type.type(:host).new(
                :name => "fakehost%s" % @hcount,
                :ip => "192.168.27.%s" % @hcount,
                :alias => "alias%s" % @hcount,
                :catalog => @catalog
            )
        }

        return host
    end

    def test_list
        list = nil
        assert_nothing_raised do
            list = @hosttype.defaultprovider.instances
        end

        assert_equal(0, list.length, "Found hosts in empty file somehow")
    end

    # Darwin will actually write to netinfo here.
    if Facter.value(:operatingsystem) != "Darwin" or Process.uid == 0
    def test_simplehost
        host = nil
        # We want to actually use the netinfo provider on darwin
        if Facter.value(:operatingsystem) == "Darwin"
            Puppet::Type.type(:host).defaultprovider = nil
        end

        assert_nothing_raised {
            host = Puppet::Type.type(:host).new(
                :name => "culain",
                :ip => "192.168.0.3"
            )
        }

        current_values = nil
        assert_nothing_raised { current_values = host.retrieve }
        assert_events([:host_created], host)

        assert_nothing_raised { current_values = host.retrieve }

        assert_equal(:present, current_values[host.property(:ensure)])

        host[:ensure] = :absent

        assert_events([:host_removed], host)

        assert_nothing_raised { current_values = host.retrieve }

        assert_equal(:absent, current_values[host.property(:ensure)])
    end

    def test_moddinghost
        # We want to actually use the netinfo provider on darwin
        if Facter.value(:operatingsystem) == "Darwin"
            Puppet::Type.type(:host).defaultprovider = nil
        end
        host = mkhost()
        if Facter.value(:operatingsystem) == "Darwin"
            assert_equal(:netinfo, host[:provider], "Got incorrect provider")
        end
        cleanup do
            host[:ensure] = :absent
            assert_apply(host)
        end

        assert_events([:host_created], host)

        current_values = nil 
        assert_nothing_raised {
            current_values = host.retrieve
        }

        # This was a hard bug to track down.
        assert_instance_of(String, current_values[host.property(:ip)])

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

        assert_events([:host_changed], host)

        assert_nothing_raised {
            current_values = host.retrieve
        }

        assert_equal(%w{madstop kirby yayness}, 
                     current_values[host.property(:alias)])
        
        host[:ensure] = :absent
        assert_events([:host_removed], host)
    end
 
    def test_invalid_ipaddress
        host = mkhost()

        assert_raise(Puppet::Error) {
            host[:ip] = "abc.def.ghi.jkl"
        }
    end

    def test_invalid_hostname
        host = mkhost()

        assert_raise(Puppet::Error) {
            host[:name] = "!invalid.hostname.$$$"
        }

        assert_raise(Puppet::Error) {
            host[:name] = "-boo"
        }

        assert_raise(Puppet::Error) {
            host[:name] = "boo-.ness"
        }

        assert_raise(Puppet::Error) {
            host[:name] = "boo..ness"
        }
    end

    def test_valid_hostname
        host = mkhost()

        assert_nothing_raised {
            host[:name] = "yayness"
        }

        assert_nothing_raised {
            host[:name] = "yay-ness"
        }

        assert_nothing_raised {
            host[:name] = "yay.ness"
        }

        assert_nothing_raised {
            host[:name] = "yay.ne-ss"
        }

        assert_nothing_raised {
            host[:name] = "y.ay-ne-ss.com"
        }

        assert_nothing_raised {
            host[:name] = "y4y.n3-ss"
        }

        assert_nothing_raised {
            host[:name] = "y"
        }
    end

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

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

    def test_puppetalias
        host = mkhost()
        catalog = mk_catalog(host)

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

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