summaryrefslogtreecommitdiffstats
path: root/test/ral/manager/attributes.rb
blob: 8b0b8df95f2e8a23ab8dd001a594a1148fde1b0f (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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#!/usr/bin/env ruby
require File.expand_path(File.dirname(__FILE__) + '/../../lib/puppettest')

require 'puppettest'
require 'mocha'

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

  def mktype
    type = Puppet::Type.newtype(:faketype) {}
    cleanup { Puppet::Type.rmtype(:faketype) }
    type
  end

  def test_bracket_methods
    type = mktype

    # make a namevar
    type.newparam(:name) {}

    # make a property
    type.newproperty(:property) {}

    # and a param
    type.newparam(:param)

    inst = type.new(:name => "yay")

    # Make sure we can set each of them, including a metaparam
    [:param, :property, :noop].each do |param|
      assert_nothing_raised("Failed to set symbol") do
        inst[param] = true
      end

      assert_nothing_raised("Failed to set string") do
        inst[param.to_s] = true
      end

      if param == :property
        assert(inst.property(param), "did not get obj for #{param}")

              assert_equal(
        true, inst.should(param),
        
          "should value did not get set")
      else
        assert_equal(true, inst[param], "did not get correct value for #{param} from symbol")
        assert_equal(true, inst[param.to_s], "did not get correct value for #{param} from string")
      end
    end
  end

  def test_properties
    type = mktype

    # make a namevar
    type.newparam(:name) {}

    # make a couple of properties
    props = [:one, :two, :three]
    props.each do |prop|
      type.newproperty(prop) {}
    end

    inst = type.new(:name => "yay")

    inst[:one] = "boo"
    one = inst.property(:one)
    assert(one, "did not get obj for one")
    assert_equal([one], inst.send(:properties), "got wrong properties")

    inst[:three] = "rah"
    three = inst.property(:three)
    assert(three, "did not get obj for three")
    assert_equal([one, three], inst.send(:properties), "got wrong properties")

    inst[:two] = "whee"
    two = inst.property(:two)
    assert(two, "did not get obj for two")
    assert_equal([one, two, three], inst.send(:properties), "got wrong properties")
  end

  def attr_check(type)
    @num ||= 0
    @num += 1
    name = "name#{@num}"
    inst = type.new(:name => name)
    [:meta, :param, :prop].each do |name|
      klass = type.attrclass(name)
      assert(klass, "did not get class for #{name}")
      obj = yield inst, klass
      assert_instance_of(klass, obj, "did not get object back")

            assert_equal(
        "value", inst.value(klass.name),
        
        "value was not correct from value method")
      assert_equal("value", obj.value, "value was not correct")
    end
  end

  def test_newattr
    type = mktype
    type.newparam(:name) {}

    # Make one of each param type
    {
      :meta => :newmetaparam, :param => :newparam, :prop => :newproperty
    }.each do |name, method|
      assert_nothing_raised("Could not make #{name} of type #{method}") do
        type.send(method, name) {}
      end
    end

    # Now set each of them
    attr_check(type) do |inst, klass|
      property = inst.newattr(klass.name)
      property.value = "value"
      property
    end

    # Now try it passing the class in
    attr_check(type) do |inst, klass|
      property = inst.newattr(klass)
      property.value = "value"
      property
    end

    # Lastly, make sure we can create and then set, separately
    attr_check(type) do |inst, klass|
      obj = inst.newattr(klass.name)
      assert_nothing_raised("Could not set value after creation") do
        obj.value = "value"
      end

      # Make sure we can't create a new param object
      new_attr = inst.newattr(klass.name)
      assert_equal(new_attr, obj, "newattr should return the same object if called a second time")

      obj
    end
  end

  def test_alias_parameter
    type = mktype
    type.newparam(:name) {}
    type.newparam(:one) {}
    type.newproperty(:two) {}

    aliases = {
      :three => :one,
      :four => :two
    }
    aliases.each do |new, old|
      assert_nothing_raised("Could not create alias parameter #{new}") do
        type.set_attr_alias new => old
      end
    end

    aliases.each do |new, old|
      assert_equal(old, type.attr_alias(new), "did not return alias info for #{new}")
    end

    assert_nil(type.attr_alias(:name), "got invalid alias info for name")

    inst = type.new(:name => "my name")
    assert(inst, "could not create instance")

    aliases.each do |new, old|
      val = "value #{new}"
      assert_nothing_raised do
        inst[new] = val
      end

      case old
      when :one # param

              assert_equal(
        val, inst[new],
        
          "Incorrect alias value for #{new} in []")
      else
        assert_equal(val, inst.should(new), "Incorrect alias value for #{new} in should")
      end
      assert_equal(val, inst.value(new), "Incorrect alias value for #{new}")
      assert_equal(val, inst.value(old), "Incorrect orig value for #{old}")
    end
  end

  # Make sure newattr handles required features correctly.
  def test_newattr_and_required_features
    # Make a type with some features
    type = mktype
    type.feature :fone, "Something"
    type.feature :ftwo, "Something else"
    type.newparam(:name) {}

    # Make three properties: one with no requirements, one with one, and one with two
    none = type.newproperty(:none) {}
    one = type.newproperty(:one, :required_features => :fone) {}
    two = type.newproperty(:two, :required_features => [:fone, :ftwo]) {}

    # Now make similar providers
    nope = type.provide(:nope) {}
    maybe = type.provide(:maybe) { has_feature :fone}
    yep = type.provide(:yep) { has_features :fone, :ftwo}

    attrs = [:none, :one, :two]

    # Now make sure that we get warnings and no properties in those cases where our providers do not support the features requested
    [nope, maybe, yep].each_with_index do |prov, i|
      resource = type.new(:provider => prov.name, :name => "test#{i}", :none => "a", :one => "b", :two => "c")

      case prov.name
      when :nope
        yes = [:none]
        no = [:one, :two]
      when :maybe
        yes = [:none, :one]
        no = [:two]
      when :yep
        yes = [:none, :one, :two]
        no = []
      end
      yes.each { |a| assert(resource.should(a), "Did not get value for #{a} in #{prov.name}") }
      no.each do |a|
        assert_nil(resource.should(a), "Got value for unsupported %s in %s" % [a, prov.name])
        if Puppet::Util::Log.sendlevel?(:info)
          assert(@logs.find { |l| l.message =~ /not managing attribute #{a}/ and l.level == :info }, "No warning about failed %s" % a)
        end
      end

      @logs.clear
    end
  end

  # Make sure the 'check' metaparam just ignores non-properties, rather than failing.
  def test_check_allows_parameters
    file = Puppet::Type.type(:file)
    klass = file.attrclass(:check)

    resource = file.new(:path => tempfile)
    inst = klass.new(:resource => resource)

    {:property => [:owner, :group], :parameter => [:ignore, :recurse], :metaparam => [:require, :subscribe]}.each do |attrtype, attrs|
      assert_nothing_raised("Could not set check to a single #{attrtype} value") do
        inst.value = attrs[0]
      end

      if attrtype == :property
        assert(resource.property(attrs[0]), "Check did not create property instance during single check")
      end
      assert_nothing_raised("Could not set check to multiple #{attrtype} values") do
        inst.value = attrs
      end
      if attrtype == :property
        assert(resource.property(attrs[1]), "Check did not create property instance during multiple check")
      end
    end

    # But make sure actually invalid attributes fail
    assert_raise(Puppet::Error, ":check did not fail on invalid attribute") do
      inst.value = :nosuchattr
    end
  end

  def test_check_ignores_unsupported_params
    type = Puppet::Type.newtype(:unsupported) do
      feature :nosuchfeat, "testing"
      newparam(:name) {}
      newproperty(:yep) {}
      newproperty(:nope,  :required_features => :nosuchfeat) {}
    end
    $yep = :absent
    type.provide(:only) do
      def self.supports_parameter?(param)
        param.name != :nope
      end

      def yep
        $yep
      end
      def yep=(v)
        $yep = v
      end
    end
    cleanup { Puppet::Type.rmtype(:unsupported) }

    obj = type.new(:name => "test", :check => :yep)
    obj.stubs(:newattr).returns(stub_everything("newattr"))
    obj.expects(:newattr).with(:nope).never
    obj[:check] = :all
  end
end