File: create_resources_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 (344 lines) | stat: -rw-r--r-- 13,375 bytes parent folder | download
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
require 'puppet'
require 'spec_helper'
require 'puppet_spec/compiler'
require 'puppet_spec/files'

describe 'function for dynamically creating resources' do
  include PuppetSpec::Compiler
  include PuppetSpec::Files

  before :each do
    node      = Puppet::Node.new("floppy", :environment => 'production')
    @compiler = Puppet::Parser::Compiler.new(node)
    @scope    = Puppet::Parser::Scope.new(@compiler)
    @topscope = @scope.compiler.topscope
    @scope.parent = @topscope
    Puppet::Parser::Functions.function(:create_resources)
  end

  it "should exist" do
    expect(Puppet::Parser::Functions.function(:create_resources)).to eq("function_create_resources")
  end

  it 'should require two or three arguments' do
    expect { @scope.function_create_resources(['foo']) }.to raise_error(ArgumentError, 'create_resources(): Wrong number of arguments given (1 for minimum 2)')
    expect { @scope.function_create_resources(['foo', 'bar', 'blah', 'baz']) }.to raise_error(ArgumentError, 'create_resources(): wrong number of arguments (4; must be 2 or 3)')
  end

  it 'should require second argument to be a hash' do
    expect { @scope.function_create_resources(['foo','bar']) }.to raise_error(ArgumentError, 'create_resources(): second argument must be a hash')
  end

  it 'should require optional third argument to be a hash' do
    expect { @scope.function_create_resources(['foo',{},'foo']) }.to raise_error(ArgumentError, 'create_resources(): third argument, if provided, must be a hash')
  end

  context 'when being called from a manifest in a file' do
    let(:dir) do
      dir_containing('manifests', {
              'site.pp' => <<-EOF
                # comment here to make the call be on a particular
                # source line (3)
                create_resources('notify', {
                  'a'  => { 'message'=>'message a'},
                  'b'  => { 'message'=>'message b'},
                  }
                )
              EOF
          }
      )
    end

    it 'file and line information where call originates is written to all resources created in one call' do
      node = Puppet::Node.new('test')
      file = File.join(dir, 'site.pp')
      Puppet[:manifest] = file
      catalog = Puppet::Parser::Compiler.compile(node).filter { |r| r.virtual? }

      expect(catalog.resource(:notify, 'a').file).to eq(file)
      expect(catalog.resource(:notify, 'a').line).to eq(3)
      expect(catalog.resource(:notify, 'b').file).to eq(file)
      expect(catalog.resource(:notify, 'b').line).to eq(3)
    end

  end
  describe 'when creating native types' do
    it 'empty hash should not cause resources to be added' do
      noop_catalog = compile_to_catalog("create_resources('file', {})")
      empty_catalog = compile_to_catalog("")
      expect(noop_catalog.resources.size).to eq(empty_catalog.resources.size)
    end

    it 'should be able to add' do
      catalog = compile_to_catalog("create_resources('file', {'/etc/foo'=>{'ensure'=>'present'}})")
      expect(catalog.resource(:file, "/etc/foo")['ensure']).to eq('present')
    end

    it 'should pick up and pass on file and line information' do
      # mock location as the compile_to_catalog sets Puppet[:code} which does not
      # have file/line support.
      expect(Puppet::Pops::PuppetStack).to receive(:top_of_stack).once.and_return(['test.pp', 1234])
      catalog = compile_to_catalog("create_resources('file', {'/etc/foo'=>{'ensure'=>'present'}})")
      r = catalog.resource(:file, "/etc/foo")
      expect(r.file).to eq('test.pp')
      expect(r.line).to eq(1234)
    end

    it 'should be able to add virtual resources' do
      catalog = compile_to_catalog("create_resources('@file', {'/etc/foo'=>{'ensure'=>'present'}})\nrealize(File['/etc/foo'])")
      expect(catalog.resource(:file, "/etc/foo")['ensure']).to eq('present')
    end

    it 'unrealized exported resources should not be added' do
      # a compiled catalog is normally filtered on virtual resources
      # here the compilation is performed unfiltered to be able to find the exported resource
      # it is then asserted that the exported resource is also virtual (and therefore filtered out by a real compilation).
      catalog = compile_to_catalog_unfiltered("create_resources('@@file', {'/etc/foo'=>{'ensure'=>'present'}})")
      expect(catalog.resource(:file, "/etc/foo").exported).to eq(true)
      expect(catalog.resource(:file, "/etc/foo").virtual).to eq(true)
    end

    it 'should be able to add exported resources' do
      catalog = compile_to_catalog("create_resources('@@file', {'/etc/foo'=>{'ensure'=>'present'}}) realize(File['/etc/foo'])")
      expect(catalog.resource(:file, "/etc/foo")['ensure']).to eq('present')
      expect(catalog.resource(:file, "/etc/foo").exported).to eq(true)
    end

    it 'should accept multiple resources' do
      catalog = compile_to_catalog("create_resources('notify', {'foo'=>{'message'=>'one'}, 'bar'=>{'message'=>'two'}})")
      expect(catalog.resource(:notify, "foo")['message']).to eq('one')
      expect(catalog.resource(:notify, "bar")['message']).to eq('two')
    end

    it 'should fail to add non-existing resource type' do
      expect do
        @scope.function_create_resources(['create-resource-foo', { 'foo' => {} }])
      end.to raise_error(/Unknown resource type: 'create-resource-foo'/)
    end

    it 'should be able to add edges' do
      rg = compile_to_relationship_graph("notify { test: }\n create_resources('notify', {'foo'=>{'require'=>'Notify[test]'}})")
      test  = rg.vertices.find { |v| v.title == 'test' }
      foo   = rg.vertices.find { |v| v.title == 'foo' }
      expect(test).to be
      expect(foo).to be
      expect(rg.path_between(test,foo)).to be
    end

    it 'should filter out undefined edges as they cause errors' do
      rg = compile_to_relationship_graph("notify { test: }\n create_resources('notify', {'foo'=>{'require'=>undef}})")
      test  = rg.vertices.find { |v| v.title == 'test' }
      foo   = rg.vertices.find { |v| v.title == 'foo' }
      expect(test).to be
      expect(foo).to be
      expect(rg.path_between(foo,nil)).to_not be
    end

    it 'should filter out undefined edges in an array as they cause errors' do
      rg = compile_to_relationship_graph("notify { test: }\n create_resources('notify', {'foo'=>{'require'=>[undef]}})")
      test  = rg.vertices.find { |v| v.title == 'test' }
      foo   = rg.vertices.find { |v| v.title == 'foo' }
      expect(test).to be
      expect(foo).to be
      expect(rg.path_between(foo,nil)).to_not be
    end

    it 'should account for default values' do
      catalog = compile_to_catalog("create_resources('file', {'/etc/foo'=>{'ensure'=>'present'}, '/etc/baz'=>{'group'=>'food'}}, {'group' => 'bar'})")
      expect(catalog.resource(:file, "/etc/foo")['group']).to eq('bar')
      expect(catalog.resource(:file, "/etc/baz")['group']).to eq('food')
    end
  end

  describe 'when dynamically creating resource types' do
    it 'should be able to create defined resource types' do
      catalog = compile_to_catalog(<<-MANIFEST)
        define foocreateresource($one) {
          notify { $name: message => $one }
        }

        create_resources('foocreateresource', {'blah'=>{'one'=>'two'}})
      MANIFEST
      expect(catalog.resource(:notify, "blah")['message']).to eq('two')
    end

    it 'should fail if defines are missing params' do
      expect {
        compile_to_catalog(<<-MANIFEST)
          define foocreateresource($one) {
            notify { $name: message => $one }
          }

          create_resources('foocreateresource', {'blah'=>{}})
        MANIFEST
      }.to raise_error(Puppet::Error, /Foocreateresource\[blah\]: expects a value for parameter 'one'/)
    end

    it 'should accept undef as explicit value when parameter has no default value' do
        catalog = compile_to_catalog(<<-MANIFEST)
          define foocreateresource($one) {
            notify { $name: message => "aaa${one}bbb" }
          }

          create_resources('foocreateresource', {'blah'=>{ one => undef}})
        MANIFEST
      expect(catalog.resource(:notify, "blah")['message']).to eq('aaabbb')
    end

    it 'should use default value expression if given value is undef' do
        catalog = compile_to_catalog(<<-MANIFEST)
          define foocreateresource($one = 'xx') {
            notify { $name: message => "aaa${one}bbb" }
          }

          create_resources('foocreateresource', {'blah'=>{ one => undef}})
        MANIFEST
      expect(catalog.resource(:notify, "blah")['message']).to eq('aaaxxbbb')
    end

    it 'should be able to add multiple defines' do
      catalog = compile_to_catalog(<<-MANIFEST)
        define foocreateresource($one) {
          notify { $name: message => $one }
        }

        create_resources('foocreateresource', {'blah'=>{'one'=>'two'}, 'blaz'=>{'one'=>'three'}})
      MANIFEST

      expect(catalog.resource(:notify, "blah")['message']).to eq('two')
      expect(catalog.resource(:notify, "blaz")['message']).to eq('three')
    end

    it 'should be able to add edges' do
      rg = compile_to_relationship_graph(<<-MANIFEST)
        define foocreateresource($one) {
          notify { $name: message => $one }
        }

        notify { test: }

        create_resources('foocreateresource', {'blah'=>{'one'=>'two', 'require' => 'Notify[test]'}})
      MANIFEST

      test = rg.vertices.find { |v| v.title == 'test' }
      blah = rg.vertices.find { |v| v.title == 'blah' }
      expect(test).to be
      expect(blah).to be
      expect(rg.path_between(test,blah)).to be
    end

    it 'should account for default values' do
      catalog = compile_to_catalog(<<-MANIFEST)
        define foocreateresource($one) {
          notify { $name: message => $one }
        }

        create_resources('foocreateresource', {'blah'=>{}}, {'one' => 'two'})
      MANIFEST

      expect(catalog.resource(:notify, "blah")['message']).to eq('two')
    end
  end

  describe 'when creating classes' do
    let(:logs) { [] }
    let(:warnings) { logs.select { |log| log.level == :warning }.map { |log| log.message } }

    it 'should be able to create classes' do
      catalog = compile_to_catalog(<<-MANIFEST)
        class bar($one) {
          notify { test: message => $one }
        }

        create_resources('class', {'bar'=>{'one'=>'two'}})
      MANIFEST

      expect(catalog.resource(:notify, "test")['message']).to eq('two')
      expect(catalog.resource(:class, "bar")).not_to be_nil
    end

    [:off, :warning].each do | strictness |
      it "should warn if strict = #{strictness} and class is exported" do
        Puppet[:strict] = strictness
        collect_notices('class test{} create_resources("@@class", {test => {}})')
        expect(warnings).to include(/Classes are not virtualizable/)
      end
    end

    it 'should error if strict = error and class is exported' do
      Puppet[:strict] = :error
      expect{
        compile_to_catalog('class test{} create_resources("@@class", {test => {}})')
      }.to raise_error(/Classes are not virtualizable/)
    end

    [:off, :warning].each do | strictness |
      it "should warn if strict = #{strictness} and class is virtual" do
        Puppet[:strict] = strictness
        collect_notices('class test{} create_resources("@class", {test => {}})')
        expect(warnings).to include(/Classes are not virtualizable/)
      end
    end

    it 'should error if strict = error and class is virtual' do
      Puppet[:strict] = :error
      expect{
        compile_to_catalog('class test{} create_resources("@class", {test => {}})')
      }.to raise_error(/Classes are not virtualizable/)
    end

    it 'should be able to add edges' do
      rg = compile_to_relationship_graph(<<-MANIFEST)
        class bar($one) {
          notify { test: message => $one }
        }

        notify { tester: }

        create_resources('class', {'bar'=>{'one'=>'two', 'require' => 'Notify[tester]'}})
      MANIFEST

      test   = rg.vertices.find { |v| v.title == 'test' }
      tester = rg.vertices.find { |v| v.title == 'tester' }
      expect(test).to be
      expect(tester).to be
      expect(rg.path_between(tester,test)).to be
    end

    it 'should account for default values' do
      catalog = compile_to_catalog(<<-MANIFEST)
        class bar($one) {
          notify { test: message => $one }
        }

        create_resources('class', {'bar'=>{}}, {'one' => 'two'})
      MANIFEST

      expect(catalog.resource(:notify, "test")['message']).to eq('two')
      expect(catalog.resource(:class, "bar")).not_to be_nil
    end

    it 'should fail with a correct error message if the syntax of an imported file is incorrect' do
      expect{
        Puppet[:modulepath] = my_fixture_dir
        compile_to_catalog('include foo')
      }.to raise_error(Puppet::Error, /Syntax error at.*/)
    end

    it 'is not available when --tasks is on' do
      Puppet[:tasks] = true
      expect do
        compile_to_catalog(<<-MANIFEST)
          create_resources('class', {'bar'=>{}}, {'one' => 'two'})
        MANIFEST
      end.to raise_error(Puppet::ParseError, /is only available when compiling a catalog/)
    end
  end

  def collect_notices(code)
    Puppet::Util::Log.with_destination(Puppet::Test::LogCollector.new(logs)) do
      compile_to_catalog(code)
    end
  end

end