File: undef_param_spec.rb

package info (click to toggle)
puppet 5.5.22-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 21,316 kB
  • sloc: ruby: 254,925; sh: 1,608; xml: 219; makefile: 153; sql: 103
file content (95 lines) | stat: -rw-r--r-- 3,058 bytes parent folder | download | duplicates (3)
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
require 'spec_helper'
require 'puppet_spec/compiler'

describe "Parameter passing" do
  include PuppetSpec::Compiler

  before :each do
    # DataBinding will be consulted before falling back to a default value,
    # but we aren't testing that here
    allow(Puppet::DataBinding.indirection).to receive(:find)
  end

  def expect_the_message_to_be(message, node = Puppet::Node.new('the node'))
    catalog = compile_to_catalog(yield, node)
    expect(catalog.resource('Notify', 'something')[:message]).to eq(message)
  end

  def expect_puppet_error(message, node = Puppet::Node.new('the node'))
    expect { compile_to_catalog(yield, node) }.to raise_error(Puppet::Error, message)
  end

  it "overrides the default when a value is given" do
    expect_the_message_to_be('2') do <<-MANIFEST
      define a($x='1') { notify { 'something': message => $x }}
      a {'a': x => '2'}
      MANIFEST
    end
  end

  it "shadows an inherited variable with the default value when undef is passed" do
    expect_the_message_to_be('default') do <<-MANIFEST
      class a { $x = 'inherited' }
      class b($x='default') inherits a { notify { 'something': message => $x }}
      class { 'b': x => undef}
      MANIFEST
    end
  end

  it "uses a default value that comes from an inherited class when the parameter is undef" do
    expect_the_message_to_be('inherited') do <<-MANIFEST
      class a { $x = 'inherited' }
      class b($y=$x) inherits a { notify { 'something': message => $y }}
      class { 'b': y => undef}
      MANIFEST
    end
  end

  it "uses a default value that references another variable when the parameter is passed as undef" do
    expect_the_message_to_be('a') do <<-MANIFEST
        define a($a = $title) { notify { 'something': message => $a }}
        a {'a': a => undef}
      MANIFEST
    end
  end

  it "uses the default when 'undef' is given'" do
    expect_the_message_to_be('1') do <<-MANIFEST
        define a($x='1') { notify { 'something': message => $x }}
        a {'a': x => undef}
      MANIFEST
    end
  end

  it "uses the default when no parameter is provided" do
    expect_the_message_to_be('1') do <<-MANIFEST
        define a($x='1') { notify { 'something': message => $x }}
        a {'a': }
      MANIFEST
    end
  end

  it "uses a value of undef when the default is undef and no parameter is provided" do
    expect_the_message_to_be(true) do <<-MANIFEST
        define a($x=undef) { notify { 'something': message => $x == undef}}
        a {'a': }
    MANIFEST
    end
  end

  it "errors when no parameter is provided and there is no default" do
    expect_puppet_error(/A\[a\]: expects a value for parameter 'x'/) do <<-MANIFEST
        define a($x) { notify { 'something': message => $x }}
        a {'a': }
    MANIFEST
    end
  end

  it "uses a given undef and do not require a default expression" do
    expect_the_message_to_be(true) do <<-MANIFEST
        define a(Optional[Integer] $x) { notify { 'something': message => $x == undef}}
        a {'a': x => undef }
    MANIFEST
    end
  end
end