summaryrefslogtreecommitdiff
path: root/spec/unit/property/validation_spec.rb
blob: a54a38eb855fdae31fb0131e92a188fb08e9e8bb (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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
require 'support/shared/integration/integration_helper'

describe "Chef::Resource.property validation" do
  include IntegrationSupport

  module Namer
    @i = 0
    def self.next_resource_name
      "chef_resource_property_spec_#{@i += 1}"
    end
    def self.reset_index
      @current_index = 0
    end
    def self.current_index
      @current_index
    end
    def self.next_index
      @current_index += 1
    end
  end

  def lazy(&block)
    Chef::DelayedEvaluator.new(&block)
  end

  before do
    Namer.reset_index
  end

  def self.new_resource_name
    Namer.next_resource_name
  end

  let(:resource_class) do
    new_resource_name = self.class.new_resource_name
    Class.new(Chef::Resource) do
      resource_name new_resource_name
      def blah
        Namer.next_index
      end
      def self.blah
        "class#{Namer.next_index}"
      end
    end
  end

  let(:resource) do
    resource_class.new("blah")
  end

  def self.english_join(values)
    return '<nothing>' if values.size == 0
    return values[0].inspect if values.size == 1
    "#{values[0..-2].map { |v| v.inspect }.join(", ")} and #{values[-1].inspect}"
  end

  def self.with_property(*properties, &block)
    tags_index = properties.find_index { |p| !p.is_a?(String)}
    if tags_index
      properties, tags = properties[0..tags_index-1], properties[tags_index..-1]
    else
      tags = []
    end
    properties = properties.map { |property| "property #{property}" }
    context "With properties #{english_join(properties)}", *tags do
      before do
        properties.each do |property_str|
          resource_class.class_eval(property_str, __FILE__, __LINE__)
        end
      end
      instance_eval(&block)
    end
  end

  def self.validation_test(validation, success_values, failure_values, getter_values=[], *tags)
    with_property ":x, #{validation}", *tags do
      it "gets nil when retrieving the initial (non-set) value" do
        expect(resource.x).to be_nil
      end
      success_values.each do |v|
        it "value #{v.inspect} is valid" do
          resource.instance_eval { @x = 'default' }
          expect(resource.x v).to eq v
          expect(resource.x).to eq v
        end
      end
      failure_values.each do |v|
        it "value #{v.inspect} is invalid" do
          expect { resource.x v }.to raise_error Chef::Exceptions::ValidationFailed
          resource.instance_eval { @x = 'default' }
          expect { resource.x v }.to raise_error Chef::Exceptions::ValidationFailed
        end
      end
      getter_values.each do |v|
        it "setting value to #{v.inspect} does not change the value" do
          Chef::Config[:treat_deprecation_warnings_as_errors] = false
          resource.instance_eval { @x = 'default' }
          expect(resource.x v).to eq 'default'
          expect(resource.x).to eq 'default'
        end
      end
    end
  end

  context "basic get, set, and nil set" do
    with_property ":x, kind_of: String" do
      context "when the variable already has a value" do
        before do
          resource.instance_eval { @x = 'default' }
        end
        it "get succeeds" do
          expect(resource.x).to eq 'default'
        end
        it "set to valid value succeeds" do
          expect(resource.x 'str').to eq 'str'
          expect(resource.x).to eq 'str'
        end
        it "set to invalid value raises ValidationFailed" do
          expect { resource.x 10 }.to raise_error Chef::Exceptions::ValidationFailed
        end
        it "set to nil emits a deprecation warning and does a get" do
          expect { resource.x nil }.to raise_error Chef::Exceptions::DeprecatedFeatureError
          Chef::Config[:treat_deprecation_warnings_as_errors] = false
          resource.x 'str'
          expect(resource.x nil).to eq 'str'
          expect(resource.x).to eq 'str'
        end
      end
      context "when the variable does not have an initial value" do
        it "get succeeds" do
          expect(resource.x).to be_nil
        end
        it "set to valid value succeeds" do
          expect(resource.x 'str').to eq 'str'
          expect(resource.x).to eq 'str'
        end
        it "set to invalid value raises ValidationFailed" do
          expect { resource.x 10 }.to raise_error Chef::Exceptions::ValidationFailed
        end
        it "set to nil emits no warning because the value would not change" do
          expect(resource.x nil).to be_nil
        end
      end
    end
    with_property ":x, [ String, nil ]" do
      context "when the variable already has a value" do
        before do
          resource.instance_eval { @x = 'default' }
        end
        it "get succeeds" do
          expect(resource.x).to eq 'default'
        end
        it "set(nil) sets the value" do
          expect(resource.x nil).to be_nil
          expect(resource.x).to be_nil
        end
        it "set to valid value succeeds" do
          expect(resource.x 'str').to eq 'str'
          expect(resource.x).to eq 'str'
        end
        it "set to invalid value raises ValidationFailed" do
          expect { resource.x 10 }.to raise_error Chef::Exceptions::ValidationFailed
        end
      end
      context "when the variable does not have an initial value" do
        it "get succeeds" do
          expect(resource.x).to be_nil
        end
        it "set(nil) sets the value" do
          expect(resource.x nil).to be_nil
          expect(resource.x).to be_nil
        end
        it "set to valid value succeeds" do
          expect(resource.x 'str').to eq 'str'
          expect(resource.x).to eq 'str'
        end
        it "set to invalid value raises ValidationFailed" do
          expect { resource.x 10 }.to raise_error Chef::Exceptions::ValidationFailed
        end
      end
    end
  end

  # Bare types
  context "bare types" do
    validation_test 'String',
      [ 'hi' ],
      [ 10 ],
      [ nil ]

    validation_test ':a',
      [ :a ],
      [ :b ],
      [ nil ]

    validation_test ':a, is: :b',
      [ :a, :b ],
      [ :c ],
      [ nil ]

    validation_test ':a, is: [ :b, :c ]',
      [ :a, :b, :c ],
      [ :d ],
      [ nil ]

    validation_test '[ :a, :b ], is: :c',
      [ :a, :b, :c ],
      [ :d ],
      [ nil ]

    validation_test '[ :a, :b ], is: [ :c, :d ]',
      [ :a, :b, :c, :d ],
      [ :e ],
      [ nil ]

    validation_test 'nil',
      [ nil ],
      [ :a ]

    validation_test '[ nil ]',
      [ nil ],
      [ :a ]

    validation_test '[]',
      [],
      [ :a ],
      [ nil ]
  end

  # is
  context "is" do
    # Class
    validation_test 'is: String',
      [ 'a', '' ],
      [ :a, 1 ],
      [ nil ]

    # Value
    validation_test 'is: :a',
      [ :a ],
      [ :b ],
      [ nil ]

    validation_test 'is: [ :a, :b ]',
      [ :a, :b ],
      [ [ :a, :b ] ],
      [ nil ]

    validation_test 'is: [ [ :a, :b ] ]',
      [ [ :a, :b ] ],
      [ :a, :b ],
      [ nil ]

    # Regex
    validation_test 'is: /abc/',
      [ 'abc', 'wowabcwow' ],
      [ '', 'abac' ],
      [ nil ]

    # Property
    validation_test 'is: Chef::Property.new(is: :a)',
      [ :a ],
      [ :b, nil ]

    # RSpec Matcher
    class Globalses
      extend RSpec::Matchers
    end

    validation_test "is: Globalses.eq(10)",
      [ 10 ],
      [ 1 ],
      [ nil ]

    # Proc
    validation_test 'is: proc { |x| x }',
      [ true, 1 ],
      [ false ],
      [ nil ]

    validation_test 'is: proc { |x| x > blah }',
      [ 10 ],
      [ -1 ]

    validation_test 'is: nil',
      [ nil ],
      [ 'a' ]

    validation_test 'is: [ String, nil ]',
      [ 'a', nil ],
      [ :b ]

    validation_test 'is: []',
      [],
      [ :a ],
      [ nil ]
  end

  # Combination
  context "combination" do
    validation_test 'kind_of: String, equal_to: "a"',
      [ 'a' ],
      [ 'b' ],
      [ nil ]
  end

  # equal_to
  context "equal_to" do
    # Value
    validation_test 'equal_to: :a',
      [ :a ],
      [ :b ],
      [ nil ]

    validation_test 'equal_to: [ :a, :b ]',
      [ :a, :b ],
      [ [ :a, :b ] ],
      [ nil ]

    validation_test 'equal_to: [ [ :a, :b ] ]',
      [ [ :a, :b ] ],
      [ :a, :b ],
      [ nil ]

    validation_test 'equal_to: nil',
      [ ],
      [ 'a' ],
      [ nil ]

    validation_test 'equal_to: [ "a", nil ]',
      [ 'a' ],
      [ 'b' ],
      [ nil ]

    validation_test 'equal_to: [ nil, "a" ]',
      [ 'a' ],
      [ 'b' ],
      [ nil ]

    validation_test 'equal_to: []',
      [],
      [ :a ],
      [ nil ]
  end

  # kind_of
  context "kind_of" do
    validation_test 'kind_of: String',
      [ 'a' ],
      [ :b ],
      [ nil ]

    validation_test 'kind_of: [ String, Symbol ]',
      [ 'a', :b ],
      [ 1 ],
      [ nil ]

    validation_test 'kind_of: [ Symbol, String ]',
      [ 'a', :b ],
      [ 1 ],
      [ nil ]

    validation_test 'kind_of: NilClass',
      [ ],
      [ 'a' ],
      [ nil ]

    validation_test 'kind_of: [ NilClass, String ]',
      [ 'a' ],
      [ :a ],
      [ nil ]

    validation_test 'kind_of: []',
      [],
      [ :a ],
      [ nil ]

    validation_test 'kind_of: nil',
      [],
      [ :a ],
      [ nil ]
  end

  # regex
  context "regex" do
    validation_test 'regex: /abc/',
      [ 'xabcy' ],
      [ 'gbh', 123 ],
      [ nil ]

    validation_test 'regex: [ /abc/, /z/ ]',
      [ 'xabcy', 'aza' ],
      [ 'gbh', 123 ],
      [ nil ]

    validation_test 'regex: [ /z/, /abc/ ]',
      [ 'xabcy', 'aza' ],
      [ 'gbh', 123 ],
      [ nil ]

    validation_test 'regex: [ [ /z/, /abc/ ], [ /n/ ] ]',
      [ 'xabcy', 'aza', 'ana' ],
      [ 'gbh', 123 ],
      [ nil ]

    validation_test 'regex: []',
      [],
      [ :a ],
      [ nil ]

    validation_test 'regex: nil',
      [],
      [ :a ],
      [ nil ]
  end

  # callbacks
  context "callbacks" do
    validation_test 'callbacks: { "a" => proc { |x| x > 10 }, "b" => proc { |x| x%2 == 0 } }',
      [ 12 ],
      [ 11, 4 ]

    validation_test 'callbacks: { "a" => proc { |x| x%2 == 0 }, "b" => proc { |x| x > 10 } }',
      [ 12 ],
      [ 11, 4 ]

    validation_test 'callbacks: { "a" => proc { |x| x.nil? } }',
      [ ],
      [ 'a' ],
      [ nil ]

    validation_test 'callbacks: {}',
      [ :a ],
      [],
      [ nil ]
  end

  # respond_to
  context "respond_to" do
    validation_test 'respond_to: :split',
      [ 'hi' ],
      [ 1 ],
      [ nil ]

    validation_test 'respond_to: "split"',
      [ 'hi' ],
      [ 1 ],
      [ nil ]

    validation_test 'respond_to: :to_s',
      [ :a ],
      [],
      [ nil ]

    validation_test 'respond_to: [ :split, :to_s ]',
      [ 'hi' ],
      [ 1 ],
      [ nil ]

    validation_test 'respond_to: %w(split to_s)',
      [ 'hi' ],
      [ 1 ],
      [ nil ]

    validation_test 'respond_to: [ :to_s, :split ]',
      [ 'hi' ],
      [ 1, ],
      [ nil ]

    validation_test 'respond_to: []',
      [ :a ],
      [],
      [ nil ]

    validation_test 'respond_to: nil',
      [ :a ],
      [],
      [ nil ]
  end

  context "cannot_be" do
    validation_test 'cannot_be: :empty',
      [ 1, [1,2], { a: 10 } ],
      [ [] ],
      [ nil ]

    validation_test 'cannot_be: "empty"',
      [ 1, [1,2], { a: 10 } ],
      [ [] ],
      [ nil ]

    validation_test 'cannot_be: [ :empty, :nil ]',
      [ 1, [1,2], { a: 10 } ],
      [ [] ],
      [ nil ]

    validation_test 'cannot_be: [ "empty", "nil" ]',
      [ 1, [1,2], { a: 10 } ],
      [ [] ],
      [ nil ]

    validation_test 'cannot_be: [ :nil, :empty ]',
      [ 1, [1,2], { a: 10 } ],
      [ [] ],
      [ nil ]

    validation_test 'cannot_be: [ :empty, :nil, :blahblah ]',
      [ 1, [1,2], { a: 10 } ],
      [ [] ],
      [ nil ]

    validation_test 'cannot_be: []',
      [ :a ],
      [],
      [ nil ]

    validation_test 'cannot_be: nil',
      [ :a ],
      [],
      [ nil ]

  end

  context "required" do
    with_property ':x, required: true' do
      it "if x is not specified, retrieval fails" do
        expect { resource.x }.to raise_error Chef::Exceptions::ValidationFailed
      end
      it "value 1 is valid" do
        expect(resource.x 1).to eq 1
        expect(resource.x).to eq 1
      end
      it "value nil emits a validation failed error because it must have a value" do
        expect { resource.x nil }.to raise_error Chef::Exceptions::ValidationFailed
      end
      context "and value is set to something other than nil" do
        before { resource.x 10 }
        it "value nil emits a deprecation warning and does a get" do
          expect { resource.x nil }.to raise_error Chef::Exceptions::DeprecatedFeatureError
          Chef::Config[:treat_deprecation_warnings_as_errors] = false
          resource.x 1
          expect(resource.x nil).to eq 1
          expect(resource.x).to eq 1
        end
      end
    end

    with_property ':x, [String, nil], required: true' do
      it "if x is not specified, retrieval fails" do
        expect { resource.x }.to raise_error Chef::Exceptions::ValidationFailed
      end
      it "value nil is valid" do
        expect(resource.x nil).to be_nil
        expect(resource.x).to be_nil
      end
      it "value '1' is valid" do
        expect(resource.x '1').to eq '1'
        expect(resource.x).to eq '1'
      end
      it "value 1 is invalid" do
        expect { resource.x 1 }.to raise_error Chef::Exceptions::ValidationFailed
      end
    end

    with_property ':x, name_property: true, required: true' do
      it "if x is not specified, the name property is returned" do
        expect(resource.x).to eq 'blah'
      end
      it "value 1 is valid" do
        expect(resource.x 1).to eq 1
        expect(resource.x).to eq 1
      end
      it "value nil emits a deprecation warning and does a get" do
        expect { resource.x nil }.to raise_error Chef::Exceptions::DeprecatedFeatureError
        Chef::Config[:treat_deprecation_warnings_as_errors] = false
        resource.x 1
        expect(resource.x nil).to eq 1
        expect(resource.x).to eq 1
      end
    end

    with_property ':x, default: 10, required: true' do
      it "if x is not specified, the default is returned" do
        expect(resource.x).to eq 10
      end
      it "value 1 is valid" do
        expect(resource.x 1).to eq 1
        expect(resource.x).to eq 1
      end
      it "value nil is invalid" do
        expect { resource.x nil }.to raise_error Chef::Exceptions::DeprecatedFeatureError
        Chef::Config[:treat_deprecation_warnings_as_errors] = false
        resource.x 1
        expect(resource.x nil).to eq 1
        expect(resource.x).to eq 1
      end
    end
  end

  context "custom validators (def _pv_blarghle)" do
    before do
      Chef::Config[:treat_deprecation_warnings_as_errors] = false
    end

    with_property ':x, blarghle: 1' do
      context "and a class that implements _pv_blarghle" do
        before do
          resource_class.class_eval do
            def _pv_blarghle(opts, key, value)
              if _pv_opts_lookup(opts, key) != value
                raise Chef::Exceptions::ValidationFailed, "ouch"
              end
            end
          end
        end

        it "value 1 is valid" do
          expect(resource.x 1).to eq 1
          expect(resource.x).to eq 1
        end

        it "value '1' is invalid" do
          Chef::Config[:treat_deprecation_warnings_as_errors] = false
          expect { resource.x '1' }.to raise_error Chef::Exceptions::ValidationFailed
        end

        it "value nil does a get" do
          Chef::Config[:treat_deprecation_warnings_as_errors] = false
          resource.x 1
          resource.x nil
          expect(resource.x).to eq 1
        end
      end
    end

    with_property ':x, blarghle: 1' do
      context "and a class that implements _pv_blarghle" do
        before do
          resource_class.class_eval do
            def _pv_blarghle(opts, key, value)
              if _pv_opts_lookup(opts, key) != value
                raise Chef::Exceptions::ValidationFailed, "ouch"
              end
            end
          end
        end

        it "value 1 is valid" do
          expect(resource.x 1).to eq 1
          expect(resource.x).to eq 1
        end

        it "value '1' is invalid" do
          expect { resource.x '1' }.to raise_error Chef::Exceptions::ValidationFailed
        end

        it "value nil does a get" do
          resource.x 1
          resource.x nil
          expect(resource.x).to eq 1
        end
      end
    end
  end
end