File: loader_spec.rb

package info (click to toggle)
puppet-agent 7.23.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 19,092 kB
  • sloc: ruby: 245,074; sh: 456; makefile: 38; xml: 33
file content (516 lines) | stat: -rw-r--r-- 18,217 bytes parent folder | download | duplicates (2)
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
require 'spec_helper'
require 'puppet_spec/files'
require 'puppet_spec/compiler'

require 'puppet/pops'
require 'puppet/loaders'

module Puppet::Pops
module Loader
describe 'The Loader' do
  include PuppetSpec::Compiler
  include PuppetSpec::Files

  before(:each) do
    Puppet[:tasks] = true
  end

  let(:testing_env) do
    {
      'testing' => {
        'functions' => functions,
        'lib' => { 'puppet' => lib_puppet },
        'manifests' => manifests,
        'modules' => modules,
        'plans' => plans,
        'tasks' => tasks,
        'types' => types,
      }
    }
  end

  let(:functions) { {} }
  let(:manifests) { {} }
  let(:modules) { {} }
  let(:plans) { {} }
  let(:lib_puppet) { {} }
  let(:tasks) { {} }
  let(:types) { {} }

  let(:environments_dir) { Puppet[:environmentpath] }

  let(:testing_env_dir) do
    dir_contained_in(environments_dir, testing_env)
    env_dir = File.join(environments_dir, 'testing')
    PuppetSpec::Files.record_tmp(env_dir)
    env_dir
  end

  let(:modules_dir) { File.join(testing_env_dir, 'modules') }
  let(:env) { Puppet::Node::Environment.create(:testing, [modules_dir]) }
  let(:node) { Puppet::Node.new('test', :environment => env) }
  let(:loader) { Loaders.find_loader(nil) }
  let(:tasks_feature) { false }

  before(:each) do
    Puppet[:tasks] = tasks_feature
    loaders = Loaders.new(env)
    Puppet.push_context(:loaders => loaders)
    loaders.pre_load
  end

  after(:each) { Puppet.pop_context }

  context 'when doing discovery' do
    context 'of things' do
      it 'finds statically basic types' do
        expect(loader.discover(:type)).to include(tn(:type, 'integer'))
      end

      it 'finds statically loaded types' do
        expect(loader.discover(:type)).to include(tn(:type, 'file'))
      end

      it 'finds statically loaded Object types' do
        expect(loader.discover(:type)).to include(tn(:type, 'puppet::ast::accessexpression'))
      end

      context 'in environment' do
        let(:types) {
          {
            'global.pp' => <<-PUPPET.unindent,
              type Global = Integer
              PUPPET
            'environment' => {
              'env.pp' => <<-PUPPET.unindent,
                type Environment::Env = String
                PUPPET
            }
          }
        }

        let(:functions) {
          {
            'globfunc.pp' => 'function globfunc() {}',
            'environment' => {
              'envfunc.pp' => 'function environment::envfunc() {}'
            }
          }
        }

        let(:lib_puppet) {
          {
            'functions' => {
              'globrubyfunc.rb' => 'Puppet::Functions.create_function(:globrubyfunc) { def globrubyfunc; end }',
              'environment' => {
                'envrubyfunc.rb' => "Puppet::Functions.create_function(:'environment::envrubyfunc') { def envrubyfunc; end }",
              }
            }
          }
        }

        it 'finds global types in environment' do
          expect(loader.discover(:type)).to include(tn(:type, 'global'))
        end

        it 'finds global functions in environment' do
          expect(loader.discover(:function)).to include(tn(:function, 'lookup'))
        end

        it 'finds types prefixed with Environment in environment' do
          expect(loader.discover(:type)).to include(tn(:type, 'environment::env'))
        end

        it 'finds global functions in environment' do
          expect(loader.discover(:function)).to include(tn(:function, 'globfunc'))
        end

        it 'finds functions prefixed with Environment in environment' do
          expect(loader.discover(:function)).to include(tn(:function, 'environment::envfunc'))
        end

        it 'finds global ruby functions in environment' do
          expect(loader.discover(:function)).to include(tn(:function, 'globrubyfunc'))
        end

        it 'finds ruby functions prefixed with Environment in environment' do
          expect(loader.discover(:function)).to include(tn(:function, 'environment::envrubyfunc'))
        end

        it 'can filter the list of discovered entries using a block' do
          expect(loader.discover(:function) { |t| t.name =~ /rubyfunc\z/ }).to contain_exactly(
            tn(:function, 'environment::envrubyfunc'),
            tn(:function, 'globrubyfunc')
          )
        end

        context 'with multiple modules' do
          let(:metadata_json_a) {
              {
                'name' => 'example/a',
                'version' => '0.1.0',
                'source' => 'git@github.com/example/example-a.git',
                'dependencies' => [{'name' => 'c', 'version_range' => '>=0.1.0'}],
                'author' => 'Bob the Builder',
                'license' => 'Apache-2.0'
              }
          }

          let(:metadata_json_b) {
            {
              'name' => 'example/b',
              'version' => '0.1.0',
              'source' => 'git@github.com/example/example-b.git',
              'dependencies' => [{'name' => 'c', 'version_range' => '>=0.1.0'}],
              'author' => 'Bob the Builder',
              'license' => 'Apache-2.0'
            }
          }

          let(:metadata_json_c) {
            {
              'name' => 'example/c',
              'version' => '0.1.0',
              'source' => 'git@github.com/example/example-c.git',
              'dependencies' => [],
              'author' => 'Bob the Builder',
              'license' => 'Apache-2.0'
            }
          }

          let(:modules) {
            {
              'a' => {
                'functions' => a_functions,
                'lib' => { 'puppet' => a_lib_puppet },
                'plans' => a_plans,
                'tasks' => a_tasks,
                'types' => a_types,
                'metadata.json' => metadata_json_a.to_json
              },
              'b' => {
                'functions' => b_functions,
                'lib' => { 'puppet' => b_lib_puppet },
                'plans' => b_plans,
                'tasks' => b_tasks,
                'types' => b_types,
                'metadata.json' => metadata_json_b.to_json
              },
              'c' => {
                'types' => c_types,
                'tasks' => c_tasks,
                'metadata.json' => metadata_json_c.to_json
              },
            }
          }

          let(:a_plans) {
            {
              'aplan.pp' => <<-PUPPET.unindent,
                plan a::aplan() {}
                PUPPET
            }
          }

          let(:a_types) {
            {
              'atype.pp' => <<-PUPPET.unindent,
                type A::Atype = Integer
                PUPPET
            }
          }

          let(:a_tasks) {
            {
              'atask' => '',
            }
          }

          let(:a_functions) {
            {
              'afunc.pp' => 'function a::afunc() {}',
            }
          }

          let(:a_lib_puppet) {
            {
              'functions' => {
                'a' => {
                  'arubyfunc.rb' => "Puppet::Functions.create_function(:'a::arubyfunc') { def arubyfunc; end }",
                }
              }
            }
          }

          let(:b_plans) {
            {
              'init.pp' => <<-PUPPET.unindent,
                plan b() {}
                PUPPET
              'aplan.pp' => <<-PUPPET.unindent,
                plan b::aplan() {}
                PUPPET
              'yamlplan.yaml' => '{}',
              'conflict.yaml' => '{}',
              'conflict.pp' => <<-PUPPET.unindent,
                plan b::conflict() {}
                PUPPET
            }
          }

          let(:b_types) {
            {
              'atype.pp' => <<-PUPPET.unindent,
                type B::Atype = Integer
                PUPPET
            }
          }

          let(:b_tasks) {
            {
              'init.json' => <<-JSON.unindent,
                {
                  "description": "test task b",
                  "parameters": {}
                }
                JSON
              'init.sh' => "# doing exactly nothing\n",
              'atask' => "# doing exactly nothing\n",
              'atask.json' => <<-JSON.unindent,
                {
                  "description": "test task b::atask",
                  "input_method": "stdin",
                  "parameters": {
                    "string_param": {
                      "description": "A string parameter",
                      "type": "String[1]"
                    },
                    "int_param": {
                      "description": "An integer parameter",
                      "type": "Integer"
                    }
                  }
                }
                JSON
            }
          }

          let(:b_functions) {
            {
              'afunc.pp' => 'function b::afunc() {}',
            }
          }

          let(:b_lib_puppet) {
            {
              'functions' => {
                'b' => {
                  'arubyfunc.rb' => "Puppet::Functions.create_function(:'b::arubyfunc') { def arubyfunc; end }",
                }
              }
            }
          }

          let(:c_types) {
            {
              'atype.pp' => <<-PUPPET.unindent,
                type C::Atype = Integer
            PUPPET
            }
          }

          let(:c_tasks) {
            {
              'foo.sh' => <<-SH.unindent,
                # This is a task that does nothing
                SH
              'fee.md' => <<-MD.unindent,
                This is not a task because it has .md extension
                MD
              'fum.conf' => <<-CONF.unindent,
                text=This is not a task because it has .conf extension
                CONF
              'bad_syntax.sh' => '',
              'bad_syntax.json' => <<-TXT.unindent,
                text => This is not a task because JSON is unparsable
                TXT
              'missing_adjacent.json' => <<-JSON.unindent,
                {
                  "description": "This is not a task because there is no adjacent file with the same base name",
                  "parameters": { "string_param": { "type": "String[1]" } }
                }
                JSON
            }
          }

          it 'private loader finds types in all modules' do
            expect(loader.private_loader.discover(:type) { |t| t.name =~ /^.::.*\z/ }).to(
              contain_exactly(tn(:type, 'a::atype'), tn(:type, 'b::atype'), tn(:type, 'c::atype')))
          end

          it 'module loader finds types only in itself' do
            expect(Loaders.find_loader('a').discover(:type) { |t| t.name =~ /^.::.*\z/ }).to(
              contain_exactly(tn(:type, 'a::atype')))
          end

          it 'private loader finds functions in all modules' do
            expect(loader.private_loader.discover(:function) { |t| t.name =~ /^.::.*\z/ }).to(
              contain_exactly(tn(:function, 'a::afunc'), tn(:function, 'b::afunc'), tn(:function, 'a::arubyfunc'), tn(:function, 'b::arubyfunc')))
          end

          it 'module loader finds functions only in itself' do
            expect(Loaders.find_loader('a').discover(:function) { |t| t.name =~ /^.::.*\z/ }).to(
              contain_exactly(tn(:function, 'a::afunc'), tn(:function, 'a::arubyfunc')))
          end

          it 'discover is only called once on dependent loader' do
            times_called = 0
            allow_any_instance_of(ModuleLoaders::FileBased).to receive(:discover).with(:type, nil, Pcore::RUNTIME_NAME_AUTHORITY) { times_called += 1 }.and_return([])
            expect(loader.private_loader.discover(:type) { |t| t.name =~ /^.::.*\z/ }).to(contain_exactly())
            expect(times_called).to eq(4)
          end

          context 'with tasks enabled' do
            let(:tasks_feature) { true }

            it 'private loader finds plans in all modules' do
              expect(loader.private_loader.discover(:plan) { |t| t.name =~ /^.(?:::.*)?\z/ }).to(
                contain_exactly(tn(:plan, 'b'), tn(:plan, 'a::aplan'), tn(:plan, 'b::aplan'), tn(:plan, 'b::conflict')))
            end

            it 'module loader finds plans only in itself' do
              expect(Loaders.find_loader('a').discover(:plan)).to(
                contain_exactly(tn(:plan, 'a::aplan')))
            end

            context 'with a yaml plan instantiator defined' do
              before :each do
                Puppet.push_context(:yaml_plan_instantiator => double(:create => double('plan')))
              end

              after :each do
                Puppet.pop_context
              end

              it 'module loader finds yaml plans' do
                expect(Loaders.find_loader('b').discover(:plan)).to(
                  include(tn(:plan, 'b::yamlplan')))
              end

              it 'module loader excludes plans with both .pp and .yaml versions' do
                expect(Loaders.find_loader('b').discover(:plan)).not_to(
                  include(tn(:plan, 'b::conflict')))
              end
            end

            it 'private loader finds types in all modules' do
              expect(loader.private_loader.discover(:type) { |t| t.name =~ /^.::.*\z/ }).to(
                contain_exactly(tn(:type, 'a::atype'), tn(:type, 'b::atype'), tn(:type, 'c::atype')))
            end

            it 'private loader finds tasks in all modules' do
              expect(loader.private_loader.discover(:task) { |t| t.name =~ /^.(?:::.*)?\z/ }).to(
                contain_exactly(tn(:task, 'a::atask'), tn(:task, 'b::atask'), tn(:task, 'b'), tn(:task, 'c::foo')))
            end

            it 'module loader finds types only in itself' do
              expect(Loaders.find_loader('a').discover(:type) { |t| t.name =~ /^.::.*\z/ }).to(
                contain_exactly(tn(:type, 'a::atype')))
            end

            it 'module loader finds tasks only in itself' do
              expect(Loaders.find_loader('a').discover(:task) { |t| t.name =~ /^.::.*\z/ }).to(
                contain_exactly(tn(:task, 'a::atask')))
            end

            it 'module loader does not consider files with .md and .conf extension to be tasks' do
              expect(Loaders.find_loader('c').discover(:task) { |t| t.name =~ /(?:foo|fee|fum)\z/ }).to(
                contain_exactly(tn(:task, 'c::foo')))
            end

            it 'without error_collector, invalid task metadata results in warnings' do
              logs = []
              Puppet::Util::Log.with_destination(Puppet::Test::LogCollector.new(logs)) do
                expect(Loaders.find_loader('c').discover(:task)).to(
                  contain_exactly(tn(:task, 'c::foo')))
              end
              expect(logs.select { |log| log.level == :warning }.map { |log| log.message }).to(
                contain_exactly(/unexpected token/, /No source besides task metadata was found/)
              )
            end

            it 'with error_collector, errors are collected and no warnings are logged' do
              logs = []
              error_collector = []
              Puppet::Util::Log.with_destination(Puppet::Test::LogCollector.new(logs)) do
                expect(Loaders.find_loader('c').discover(:task, error_collector)).to(
                  contain_exactly(tn(:task, 'c::foo')))
              end
              expect(logs.select { |log| log.level == :warning }.map { |log| log.message }).to be_empty
              expect(error_collector.size).to eql(2)
              expect(error_collector.all? { |e| e.is_a?(Puppet::DataTypes::Error) })
              expect(error_collector.all? { |e| e.issue_code == Puppet::Pops::Issues::LOADER_FAILURE.issue_code })
              expect(error_collector.map { |e| e.details['original_error'] }).to(
                contain_exactly(/unexpected token/, /No source besides task metadata was found/)
              )
            end

            context 'and an environment without directory' do
              let(:environments_dir) { tmpdir('loader_spec') }
              let(:env) { Puppet::Node::Environment.create(:none_such, [modules_dir]) }

              it 'an EmptyLoader is used and module loader finds types' do
                expect_any_instance_of(Puppet::Pops::Loader::ModuleLoaders::EmptyLoader).to receive(:find).and_return(nil)
                expect(Loaders.find_loader('a').discover(:type) { |t| t.name =~ /^.::.*\z/ }).to(
                  contain_exactly(tn(:type, 'a::atype')))
              end

              it 'an EmptyLoader is used and module loader finds tasks' do
                expect_any_instance_of(Puppet::Pops::Loader::ModuleLoaders::EmptyLoader).to receive(:find).and_return(nil)
                expect(Loaders.find_loader('a').discover(:task) { |t| t.name =~ /^.::.*\z/ }).to(
                  contain_exactly(tn(:task, 'a::atask')))
              end
            end
          end

          context 'with no explicit dependencies' do
            let(:modules) do
              {
                'a' => {
                  'functions' => a_functions,
                  'lib' => { 'puppet' => a_lib_puppet },
                  'plans' => a_plans,
                  'tasks' => a_tasks,
                  'types' => a_types,
                },
                'b' => {
                  'functions' => b_functions,
                  'lib' => { 'puppet' => b_lib_puppet },
                  'plans' => b_plans,
                  'tasks' => b_tasks,
                  'types' => b_types,
                },
                'c' => {
                  'types' => c_types,
                },
              }
            end

            it 'discover is only called once on dependent loader' do
              times_called = 0
              allow_any_instance_of(ModuleLoaders::FileBased).to receive(:discover).with(:type, nil, Pcore::RUNTIME_NAME_AUTHORITY) { times_called += 1 }.and_return([])
              expect(loader.private_loader.discover(:type) { |t| t.name =~ /^.::.*\z/ }).to(contain_exactly())
              expect(times_called).to eq(4)
            end
          end
        end
      end
    end
  end

  def tn(type, name)
    TypedName.new(type, name)
  end
end
end
end