File: script_compiler_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 (113 lines) | stat: -rw-r--r-- 3,422 bytes parent folder | download | duplicates (4)
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
require 'puppet'
require 'spec_helper'
require 'puppet_spec/compiler'
require 'matchers/resource'
require 'puppet/parser/script_compiler'

describe 'the script compiler' do
  include PuppetSpec::Compiler
  include PuppetSpec::Files
  include Matchers::Resource
  before(:each) do
    Puppet[:tasks] = true
  end

  context "when used" do
    let(:env_name) { 'testenv' }
    let(:environments_dir) { Puppet[:environmentpath] }
    let(:env_dir) { File.join(environments_dir, env_name) }
    let(:manifest) { Puppet::Node::Environment::NO_MANIFEST }
    let(:env) { Puppet::Node::Environment.create(env_name.to_sym, [File.join(populated_env_dir, 'modules')], manifest) }
    let(:node) { Puppet::Node.new("test", :environment => env) }

    let(:env_dir_files) {
      {
        'manifests' => {
          'good.pp' => "'good'\n"
        },
        'modules' => {
          'test' => {
            'plans' => {
               'run_me.pp' => 'plan test::run_me() { "worked2" }'
            }
          }
        }
      }
    }

    let(:populated_env_dir) do
      dir_contained_in(environments_dir, env_name => env_dir_files)
      PuppetSpec::Files.record_tmp(env_dir)
      env_dir
    end

    let(:script_compiler) do
      Puppet::Parser::ScriptCompiler.new(env, node.name)
    end

    context 'is configured such that' do
      it 'returns what the script_compiler returns' do
        Puppet[:code] = <<-CODE
            42
          CODE
        expect(script_compiler.compile).to eql(42)
      end

      it 'referencing undefined variables raises an error' do
        expect do
          Puppet[:code] = <<-CODE
              notice $rubyversion
            CODE
            Puppet::Parser::ScriptCompiler.new(env, 'test_node_name').compile

        end.to raise_error(/Unknown variable: 'rubyversion'/)
      end

      it 'has strict=error behavior' do
        expect do
          Puppet[:code] = <<-CODE
              notice({a => 10, a => 20})
            CODE
            Puppet::Parser::ScriptCompiler.new(env, 'test_node_name').compile

        end.to raise_error(/The key 'a' is declared more than once/)
      end

      it 'performing a multi assign from a class reference raises an error' do
        expect do
          Puppet[:code] = <<-CODE
              [$a] = Class[the_dalit]
            CODE
            Puppet::Parser::ScriptCompiler.new(env, 'test_node_name').compile

        end.to raise_error(/The catalog operation 'multi var assignment from class' is only available when compiling a catalog/)
      end
    end

    context 'when using environment manifest' do
      context 'set to single file' do
        let (:manifest) { "#{env_dir}/manifests/good.pp" }

        it 'loads and evaluates' do
          expect(script_compiler.compile).to eql('good')
        end
      end

      context 'set to directory' do
        let (:manifest) { "#{env_dir}/manifests" }

        it 'fails with an error' do
          expect{script_compiler.compile}.to raise_error(/manifest of environment 'testenv' appoints directory '.*\/manifests'. It must be a file/)
        end
      end

      context 'set to non existing path' do
        let (:manifest) { "#{env_dir}/manyfiests/good.pp" }

        it 'fails with an error' do
          expect{script_compiler.compile}.to raise_error(/manifest of environment 'testenv' appoints '.*\/good.pp'. It does not exist/)
        end
      end
    end
  end
end