summaryrefslogtreecommitdiffstats
path: root/test/ral/type/parameter.rb
blob: e1b8e00b3fbbc5492379ff3e4894b8789484a29c (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
#!/usr/bin/env ruby

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

require 'puppettest'

class TestParameter < Test::Unit::TestCase
	include PuppetTest
    
    def newparam(name = :fakeparam)
        assert_nothing_raised {
            param = Class.new(Puppet::Parameter) do
                @name = :fakeparam
            end
            param.initvars

            return param
        }
    end

    def newinst(param)
        assert_nothing_raised {
            return param.new(:resource => "yay")
        }
    end

    # Test the basic newvalue stuff.
    def test_newvalue
        param = newparam()

        # Try it with both symbols and strings.
        assert_nothing_raised {
            param.newvalues(:one, "two")
        }

        inst = newinst(param)

        assert_nothing_raised {
            inst.value = "one"
        }

        assert_equal(:one, inst.value)

        assert_nothing_raised {
            inst.value = :two
        }
        assert_equal(:two, inst.value)

        assert_raise(ArgumentError) {
            inst.value = :three
        }
        assert_equal(:two, inst.value)
    end

    # Test using regexes.
    def test_regexvalues
        param = newparam

        assert_nothing_raised {
            param.newvalues(/^\d+$/)
        }
        assert(param.match?("14"))
        assert(param.match?(14))

        inst = newinst(param)

        assert_nothing_raised {
            inst.value = 14
        }

        assert_nothing_raised {
            inst.value = "14"
        }

        assert_raise(ArgumentError) {
            inst.value = "a14"
        }
    end

    # Test using both.  Equality should beat matching.
    def test_regexesandnormals
        param = newparam

        assert_nothing_raised {
            param.newvalues(:one, /^\w+$/)
        }

        inst = newinst(param)

        assert_nothing_raised {
            inst.value = "one"
        }

        assert_equal(:one, inst.value, "Value used regex instead of equality")

        assert_nothing_raised {
            inst.value = "two"
        }
        assert_equal("two", inst.value, "Matched value didn't take")
    end

    def test_shadowing
        type = Puppet::Type.newtype(:faketype) { newparam(:name) {} }

        cleanup { Puppet::Type.rmtype(:faketype) }

        param = nil
        assert_nothing_raised do
            param = type.newproperty(:alias)
        end

        assert(param, "did not create param")

        inst = type.create(:name => "test")

        config = mk_catalog
        inst.catalog = config

        assert_nothing_raised("Could not create shadowed param") {
            inst[:alias] = "foo"
        }

        # Get the parameter hash from the instance so we can check the shadow
        params = inst.instance_variable_get("@parameters")
        obj = params[:alias]
        assert(obj, "did not get alias parameter")
        assert(obj.shadow, "shadow was not created for alias param")

        assert(obj.is_a?(Puppet::Property),
            "alias instance is not a property")
        assert_instance_of(param, obj, "alias is an instance of the wrong class")

        # Make sure the alias got created
        assert(type["foo"], "Did not retrieve object by its alias")
        
        # Now try it during initialization
        other = nil
        assert_nothing_raised("Could not create instance with shadow") do
            other = type.create(:name => "rah", :alias => "one", :catalog => config)
        end
        params = other.instance_variable_get("@parameters")
        obj = params[:alias]
        assert(obj, "did not get alias parameter")
        assert(obj.shadow, "shadow was not created for alias param")

        assert_instance_of(param, obj, "alias is an instance of the wrong class")
        assert(obj.is_a?(Puppet::Property),
            "alias instance is not a property")

        # Now change the alias and make sure it works out well
        assert_nothing_raised("Could not modify shadowed alias") do
            other[:alias] = "two"
        end

        obj = params[:alias]
        assert(obj, "did not get alias parameter")
        assert_instance_of(param, obj, "alias is now an instance of the wrong class")
        assert(obj.is_a?(Puppet::Property),
            "alias instance is now not a property")
    end

    # Make sure properties can correctly require features and behave appropriately when
    # those features are missing.
    def test_requires_features
        param = newparam(:feature_tests)

        assert_nothing_raised("could not add feature requirements to property") do
            param.required_features = "testing"
        end

        assert_equal([:testing], param.required_features, "required features value was not arrayfied and interned")
    end
end