summaryrefslogtreecommitdiffstats
path: root/spec/unit/property_spec.rb
blob: 7728b5d40454c966aa29fac3dc84d2f7add3aff7 (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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#!/usr/bin/env rspec
require 'spec_helper'
require 'puppet/property'

describe Puppet::Property do
  before do
    @class = Class.new(Puppet::Property) do
      @name = :foo
    end
    @class.initvars
    @provider = mock 'provider'
    @resource = stub 'resource', :provider => @provider
    @resource.stub_everything
    @property = @class.new :resource => @resource
  end

  it "should return its name as a string when converted to a string" do
    @property.to_s.should == @property.name.to_s
  end

  it "should be able to look up the modified name for a given value" do
    @class.newvalue(:foo)
    @class.value_name("foo").should == :foo
  end

  it "should be able to look up the modified name for a given value matching a regex" do
    @class.newvalue(%r{.})
    @class.value_name("foo").should == %r{.}
  end

  it "should be able to look up a given value option" do
    @class.newvalue(:foo, :event => :whatever)
    @class.value_option(:foo, :event).should == :whatever
  end

  it "should be able to specify required features" do
    @class.should respond_to(:required_features=)
  end

  {"one" => [:one],:one => [:one],%w{a} => [:a],[:b] => [:b],%w{one two} => [:one,:two],[:a,:b] => [:a,:b]}.each { |in_value,out_value|
    it "should always convert required features into an array of symbols (e.g. #{in_value.inspect} --> #{out_value.inspect})" do
      @class.required_features = in_value
      @class.required_features.should == out_value
    end
  }

  it "should be able to shadow metaparameters" do
    @property.must respond_to(:shadow)
  end

  describe "when returning the default event name" do
    before do
      @resource = stub 'resource'
      @instance = @class.new(:resource => @resource)
      @instance.stubs(:should).returns "myval"
    end

    it "should use the current 'should' value to pick the event name" do
      @instance.expects(:should).returns "myvalue"
      @class.expects(:value_option).with('myvalue', :event).returns :event_name

      @instance.event_name
    end

    it "should return any event defined with the specified value" do
      @instance.expects(:should).returns :myval
      @class.expects(:value_option).with(:myval, :event).returns :event_name

      @instance.event_name.should == :event_name
    end

    describe "and the property is 'ensure'" do
      before do
        @instance.stubs(:name).returns :ensure
        @resource.expects(:type).returns :mytype
      end

      it "should use <type>_created if the 'should' value is 'present'" do
        @instance.expects(:should).returns :present
        @instance.event_name.should == :mytype_created
      end

      it "should use <type>_removed if the 'should' value is 'absent'" do
        @instance.expects(:should).returns :absent
        @instance.event_name.should == :mytype_removed
      end

      it "should use <type>_changed if the 'should' value is not 'absent' or 'present'" do
        @instance.expects(:should).returns :foo
        @instance.event_name.should == :mytype_changed
      end

      it "should use <type>_changed if the 'should value is nil" do
        @instance.expects(:should).returns nil
        @instance.event_name.should == :mytype_changed
      end
    end

    it "should use <property>_changed if the property is not 'ensure'" do
      @instance.stubs(:name).returns :myparam
      @instance.expects(:should).returns :foo
      @instance.event_name.should == :myparam_changed
    end

    it "should use <property>_changed if no 'should' value is set" do
      @instance.stubs(:name).returns :myparam
      @instance.expects(:should).returns nil
      @instance.event_name.should == :myparam_changed
    end
  end

  describe "when creating an event" do
    before do
      @event = Puppet::Transaction::Event.new

      # Use a real resource so we can test the event creation integration
      @resource = Puppet::Type.type(:mount).new :name => "foo"
      @instance = @class.new(:resource => @resource)
      @instance.stubs(:should).returns "myval"
    end

    it "should use an event from the resource as the base event" do
      event = Puppet::Transaction::Event.new
      @resource.expects(:event).returns event

      @instance.event.should equal(event)
    end

    it "should have the default event name" do
      @instance.expects(:event_name).returns :my_event
      @instance.event.name.should == :my_event
    end

    it "should have the property's name" do
      @instance.event.property.should == @instance.name.to_s
    end

    it "should have the 'should' value set" do
      @instance.stubs(:should).returns "foo"
      @instance.event.desired_value.should == "foo"
    end

    it "should provide its path as the source description" do
      @instance.stubs(:path).returns "/my/param"
      @instance.event.source_description.should == "/my/param"
    end
  end

  describe "when shadowing metaparameters" do
    before do
      @shadow_class = Class.new(Puppet::Property) do
        @name = :alias
      end
      @shadow_class.initvars
    end

    it "should create an instance of the metaparameter at initialization" do
      Puppet::Type.metaparamclass(:alias).expects(:new).with(:resource => @resource)

      @shadow_class.new :resource => @resource
    end

    it "should munge values using the shadow's munge method" do
      shadow = mock 'shadow'
      Puppet::Type.metaparamclass(:alias).expects(:new).returns shadow

      shadow.expects(:munge).with "foo"

      property = @shadow_class.new :resource => @resource
      property.munge("foo")
    end
  end

  describe "when defining new values" do
    it "should define a method for each value created with a block that's not a regex" do
      @class.newvalue(:foo) { }
      @property.must respond_to(:set_foo)
    end
  end

  describe "when assigning the value" do
    it "should just set the 'should' value" do
      @property.value = "foo"
      @property.should.must == "foo"
    end

    it "should validate each value separately" do
      @property.expects(:validate).with("one")
      @property.expects(:validate).with("two")

      @property.value = %w{one two}
    end

    it "should munge each value separately and use any result as the actual value" do
      @property.expects(:munge).with("one").returns :one
      @property.expects(:munge).with("two").returns :two

      # Do this so we get the whole array back.
      @class.array_matching = :all

      @property.value = %w{one two}
      @property.should.must == [:one, :two]
    end

    it "should return any set value" do
      (@property.value = :one).should == :one
    end
  end

  describe "when returning the value" do
    it "should return nil if no value is set" do
      @property.should.must be_nil
    end

    it "should return the first set 'should' value if :array_matching is set to :first" do
      @class.array_matching = :first
      @property.should = %w{one two}
      @property.should.must == "one"
    end

    it "should return all set 'should' values as an array if :array_matching is set to :all" do
      @class.array_matching = :all
      @property.should = %w{one two}
      @property.should.must == %w{one two}
    end

    it "should default to :first array_matching" do
      @class.array_matching.should == :first
    end

    it "should unmunge the returned value if :array_matching is set to :first" do
      @property.class.unmunge do |v| v.to_sym end
      @class.array_matching = :first
      @property.should = %w{one two}

      @property.should.must == :one
    end

    it "should unmunge all the returned values if :array_matching is set to :all" do
      @property.class.unmunge do |v| v.to_sym end
      @class.array_matching = :all
      @property.should = %w{one two}

      @property.should.must == [:one, :two]
    end
  end

  describe "when validating values" do
    it "should do nothing if no values or regexes have been defined" do
      lambda { @property.should = "foo" }.should_not raise_error
    end

    it "should fail if the value is not a defined value or alias and does not match a regex" do
      @class.newvalue(:foo)

      lambda { @property.should = "bar" }.should raise_error
    end

    it "should succeeed if the value is one of the defined values" do
      @class.newvalue(:foo)

      lambda { @property.should = :foo }.should_not raise_error
    end

    it "should succeeed if the value is one of the defined values even if the definition uses a symbol and the validation uses a string" do
      @class.newvalue(:foo)

      lambda { @property.should = "foo" }.should_not raise_error
    end

    it "should succeeed if the value is one of the defined values even if the definition uses a string and the validation uses a symbol" do
      @class.newvalue("foo")

      lambda { @property.should = :foo }.should_not raise_error
    end

    it "should succeed if the value is one of the defined aliases" do
      @class.newvalue("foo")
      @class.aliasvalue("bar", "foo")

      lambda { @property.should = :bar }.should_not raise_error
    end

    it "should succeed if the value matches one of the regexes" do
      @class.newvalue(/./)

      lambda { @property.should = "bar" }.should_not raise_error
    end

    it "should validate that all required features are present" do
      @class.newvalue(:foo, :required_features => [:a, :b])

      @provider.expects(:satisfies?).with([:a, :b]).returns true

      @property.should = :foo
    end

    it "should fail if required features are missing" do
      @class.newvalue(:foo, :required_features => [:a, :b])

      @provider.expects(:satisfies?).with([:a, :b]).returns false

      lambda { @property.should = :foo }.should raise_error(Puppet::Error)
    end

    it "should internally raise an ArgumentError if required features are missing" do
      @class.newvalue(:foo, :required_features => [:a, :b])

      @provider.expects(:satisfies?).with([:a, :b]).returns false

      lambda { @property.validate_features_per_value :foo }.should raise_error(ArgumentError)
    end

    it "should validate that all required features are present for regexes" do
      value = @class.newvalue(/./, :required_features => [:a, :b])

      @provider.expects(:satisfies?).with([:a, :b]).returns true

      @property.should = "foo"
    end

    it "should support specifying an individual required feature" do
      value = @class.newvalue(/./, :required_features => :a)

      @provider.expects(:satisfies?).returns true

      @property.should = "foo"
    end
  end

  describe "when munging values" do
    it "should do nothing if no values or regexes have been defined" do
      @property.munge("foo").should == "foo"
    end

    it "should return return any matching defined values" do
      @class.newvalue(:foo)
      @property.munge("foo").should == :foo
    end

    it "should return any matching aliases" do
      @class.newvalue(:foo)
      @class.aliasvalue(:bar, :foo)
      @property.munge("bar").should == :foo
    end

    it "should return the value if it matches a regex" do
      @class.newvalue(/./)
      @property.munge("bar").should == "bar"
    end

    it "should return the value if no other option is matched" do
      @class.newvalue(:foo)
      @property.munge("bar").should == "bar"
    end
  end

  describe "when syncing the 'should' value" do
    it "should set the value" do
      @class.newvalue(:foo)
      @property.should = :foo
      @property.expects(:set).with(:foo)
      @property.sync
    end
  end

  describe "when setting a value" do
    it "should catch exceptions and raise Puppet::Error" do
      @class.newvalue(:foo) { raise "eh" }
      lambda { @property.set(:foo) }.should raise_error(Puppet::Error)
    end

    describe "that was defined without a block" do
      it "should call the settor on the provider" do
        @class.newvalue(:bar)
        @provider.expects(:foo=).with :bar
        @property.set(:bar)
      end
    end

    describe "that was defined with a block" do
      it "should call the method created for the value if the value is not a regex" do
        @class.newvalue(:bar) {}
        @property.expects(:set_bar)
        @property.set(:bar)
      end

      it "should call the provided block if the value is a regex" do
        @class.newvalue(/./) { self.test }
        @property.expects(:test)
        @property.set("foo")
      end
    end
  end

  describe "when producing a change log" do
    it "should say 'defined' when the current value is 'absent'" do
      @property.change_to_s(:absent, "foo").should =~ /^defined/
    end

    it "should say 'undefined' when the new value is 'absent'" do
      @property.change_to_s("foo", :absent).should =~ /^undefined/
    end

    it "should say 'changed' when neither value is 'absent'" do
      @property.change_to_s("foo", "bar").should =~ /changed/
    end
  end
end