summaryrefslogtreecommitdiffstats
path: root/test/ral/type/host.rb
blob: b6c4d9c66bf28ecca52cf740bf9a6876bf7e7cd5 (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__) + '/../../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 are using the parsed provider
        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


    def test_simplehost
        host = nil

        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
        host = mkhost()
        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[: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.$PID$"
        }

        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