File: language.rb

package info (click to toggle)
puppet 4.8.2-5
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 20,736 kB
  • ctags: 14,616
  • sloc: ruby: 236,754; xml: 1,586; sh: 1,178; lisp: 299; sql: 103; yacc: 72; makefile: 52
file content (74 lines) | stat: -rw-r--r-- 2,529 bytes parent folder | download | duplicates (5)
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
require 'puppet_spec/compiler'
require 'matchers/resource'

module PuppetSpec::Language
  extend RSpec::Matchers::DSL

  def produces(expectations)
    calledFrom = caller
    expectations.each do |manifest, resources|
      it "evaluates #{manifest} to produce #{resources}" do
        begin
        case resources
        when String
          node = Puppet::Node.new('specification')
          Puppet[:code] = manifest
          compiler = Puppet::Parser::Compiler.new(node)
          evaluator = Puppet::Pops::Parser::EvaluatingParser.new()

          # see lib/puppet/indirector/catalog/compiler.rb#filter
          catalog = compiler.compile.filter { |r| r.virtual? }

          compiler.send(:instance_variable_set, :@catalog, catalog)

          expect(evaluator.evaluate_string(compiler.topscope, resources)).to eq(true)
        when Array
          catalog = PuppetSpec::Compiler.compile_to_catalog(manifest)

          if resources.empty?
            base_resources = ["Class[Settings]", "Class[main]", "Stage[main]"]
            expect(catalog.resources.collect(&:ref) - base_resources).to eq([])
          else
            resources.each do |reference|
              if reference.is_a?(Array)
                matcher = Matchers::Resource.have_resource(reference[0])
                reference[1].each do |name, value|
                  matcher = matcher.with_parameter(name, value)
                end
              else
                matcher = Matchers::Resource.have_resource(reference)
              end

              expect(catalog).to matcher
            end
          end
        else
          raise "Unsupported creates specification: #{resources.inspect}"
        end
      rescue  Puppet::Error, RSpec::Expectations::ExpectationNotMetError => e
        # provide the backtrace from the caller, or it is close to impossible to find some originators
        e.set_backtrace(calledFrom)
        raise
      end

      end
    end
  end

  def fails(expectations)
    calledFrom = caller
    expectations.each do |manifest, pattern|
      it "fails to evaluate #{manifest} with message #{pattern}" do
        begin
        expect do
          PuppetSpec::Compiler.compile_to_catalog(manifest)
        end.to raise_error(Puppet::Error, pattern)
        rescue  RSpec::Expectations::ExpectationNotMetError => e
          # provide the backgrace from the caller, or it is close to impossible to find some originators
          e.set_backtrace(calledFrom)
          raise
        end
      end
    end
  end
end