File: include_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 (186 lines) | stat: -rw-r--r-- 4,780 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
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
require 'spec_helper'
require 'puppet_spec/compiler'
require 'puppet/parser/functions'
require 'matchers/containment_matchers'
require 'matchers/resource'
require 'matchers/include_in_order'
require 'unit/functions/shared'

describe 'The "include" function' do
  include PuppetSpec::Compiler
  include ContainmentMatchers
  include Matchers::Resource

  before(:each) do
    compiler  = Puppet::Parser::Compiler.new(Puppet::Node.new("foo"))
    @scope = Puppet::Parser::Scope.new(compiler)
  end

  it "includes a class" do
    catalog = compile_to_catalog(<<-MANIFEST)
      class included {
        notify { "included": }
      }

      include included
    MANIFEST

    expect(catalog.classes).to include("included")
  end

  it "includes a class when using a fully qualified anchored name" do
    catalog = compile_to_catalog(<<-MANIFEST)
      class included {
        notify { "included": }
      }

      include ::included
    MANIFEST

    expect(catalog.classes).to include("included")
  end

  it "includes multiple classes" do
    catalog = compile_to_catalog(<<-MANIFEST)
      class included {
        notify { "included": }
      }
      class included_too {
        notify { "included_too": }
      }

      include included, included_too
    MANIFEST

    expect(catalog.classes).to include("included")
    expect(catalog.classes).to include("included_too")
  end

  it "includes multiple classes given as an array" do
    catalog = compile_to_catalog(<<-MANIFEST)
      class included {
        notify { "included": }
      }
      class included_too {
        notify { "included_too": }
      }

      include [included, included_too]
    MANIFEST

    expect(catalog.classes).to include("included")
    expect(catalog.classes).to include("included_too")
  end

  it "flattens nested arrays" do
    catalog = compile_to_catalog(<<-MANIFEST)
      class included {
        notify { "included": }
      }
      class included_too {
        notify { "included_too": }
      }

      include [[[included], [[[included_too]]]]]
    MANIFEST

    expect(catalog.classes).to include("included")
    expect(catalog.classes).to include("included_too")
  end

  it "raises an error if class does not exist" do
    expect {
      compile_to_catalog(<<-MANIFEST)
        include the_god_in_your_religion
      MANIFEST
    }.to raise_error(Puppet::Error)
  end

    { "''"      => 'empty string', 
      'undef'   => 'undef',
      "['']"    => 'empty string',
      "[undef]" => 'undef'
    }.each_pair do |value, name_kind|
      it "raises an error if class is #{name_kind}" do
        expect {
          compile_to_catalog(<<-MANIFEST)
            include #{value}
          MANIFEST
        }.to raise_error(/Cannot use #{name_kind}/)
      end
    end

  it "does not contained the included class in the current class" do
    catalog = compile_to_catalog(<<-MANIFEST)
      class not_contained {
        notify { "not_contained": }
      }

      class container {
        include not_contained
      }

      include container
    MANIFEST

    expect(catalog).to_not contain_class("not_contained").in("container")
  end

  it 'produces an array with a single class references given a single argument' do
    catalog = compile_to_catalog(<<-MANIFEST)
      class a {
        notify { "a": }
      }

      $x = include(a)
      Array[Type[Class], 1, 1].assert_type($x)
      notify { 'feedback': message => "$x" }
    MANIFEST

    feedback = catalog.resource("Notify", "feedback")
    expect(feedback[:message]).to eql("[Class[a]]")
  end

  it 'produces an array with class references given multiple arguments' do
    catalog = compile_to_catalog(<<-MANIFEST)
      class a {
        notify { "a": }
      }

      class b {
        notify { "b": }
      }

      $x = include(a, b)
      Array[Type[Class], 2, 2].assert_type($x)
      notify { 'feedback': message => "$x" }
    MANIFEST

    feedback = catalog.resource("Notify", "feedback")
    expect(feedback[:message]).to eql("[Class[a], Class[b]]")
  end

  it 'allows the result to be used in a relationship operation' do
    catalog = compile_to_catalog(<<-MANIFEST)
      class a {
        notify { "a": }
      }

      class b {
        notify { "b": }
      }

      notify { 'c': }

      include(a, b) -> Notify[c]
    MANIFEST

    # Assert relationships are formed
    expect(catalog.resource("Class", "a")[:before][0]).to eql('Notify[c]')
    expect(catalog.resource("Class", "b")[:before][0]).to eql('Notify[c]')
  end

  it_should_behave_like 'all functions transforming relative to absolute names', :include
  it_should_behave_like 'an inclusion function, regardless of the type of class reference,', :include
  it_should_behave_like 'an inclusion function, when --tasks is on,', :include
end