Module: PuppetSpec::Compiler

Defined in:
core_modules/sshkeys_core/spec/lib/puppet_spec/compiler.rb

Class Method Summary collapse

Class Method Details

.apply_compiled_manifest(manifest, prioritizer = Puppet::Graph::SequentialPrioritizer.new) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'core_modules/sshkeys_core/spec/lib/puppet_spec/compiler.rb', line 31

def apply_compiled_manifest(manifest, prioritizer = Puppet::Graph::SequentialPrioritizer.new)
  args = []
  if Puppet.version.to_f < 5.0
    args << 'apply'
  end
  catalog = compile_to_ral(manifest)
  if block_given?
    catalog.resources.each { |res| yield res }
  end
  transaction = Puppet::Transaction.new(catalog,
                                        Puppet::Transaction::Report.new(*args),
                                        prioritizer)
  transaction.evaluate
  transaction.report.finalize_report

  transaction
end

.apply_with_error_check(manifest) ⇒ Object



49
50
51
52
53
# File 'core_modules/sshkeys_core/spec/lib/puppet_spec/compiler.rb', line 49

def apply_with_error_check(manifest)
  apply_compiled_manifest(manifest) do |res|
    expect(res).to receive(:err).never
  end
end

.collect_notices(code, node = Puppet::Node.new('foonode')) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'core_modules/sshkeys_core/spec/lib/puppet_spec/compiler.rb', line 61

def collect_notices(code, node = Puppet::Node.new('foonode'))
  Puppet[:code] = code
  compiler = Puppet::Parser::Compiler.new(node)
  node.environment.check_for_reparse
  logs = []
  Puppet::Util::Log.with_destination(Puppet::Test::LogCollector.new(logs)) do
    yield(compiler)
  end
  logs = logs.select { |log| log.level == :notice }.map { |log| log.message }
  logs
end

.compile_to_catalog(string, node = Puppet::Node.new('test')) ⇒ Object



4
5
6
7
8
# File 'core_modules/sshkeys_core/spec/lib/puppet_spec/compiler.rb', line 4

def compile_to_catalog(string, node = Puppet::Node.new('test'))
  Puppet[:code] = string
  # see lib/puppet/indirector/catalog/compiler.rb#filter
  Puppet::Parser::Compiler.compile(node).filter { |r| r.virtual? }
end

.compile_to_catalog_unfiltered(string, node = Puppet::Node.new('test')) ⇒ Object

Does not removed virtual resources in compiled catalog (i.e. keeps unrealized)



11
12
13
14
15
# File 'core_modules/sshkeys_core/spec/lib/puppet_spec/compiler.rb', line 11

def compile_to_catalog_unfiltered(string, node = Puppet::Node.new('test'))
  Puppet[:code] = string
  # see lib/puppet/indirector/catalog/compiler.rb#filter
  Puppet::Parser::Compiler.compile(node)
end

.compile_to_ral(manifest, node = Puppet::Node.new('test')) ⇒ Object



17
18
19
20
21
22
# File 'core_modules/sshkeys_core/spec/lib/puppet_spec/compiler.rb', line 17

def compile_to_ral(manifest, node = Puppet::Node.new('test'))
  catalog = compile_to_catalog(manifest, node)
  ral = catalog.to_ral
  ral.finalize
  ral
end

.compile_to_relationship_graph(manifest, prioritizer = Puppet::Graph::SequentialPrioritizer.new) ⇒ Object



24
25
26
27
28
29
# File 'core_modules/sshkeys_core/spec/lib/puppet_spec/compiler.rb', line 24

def compile_to_relationship_graph(manifest, prioritizer = Puppet::Graph::SequentialPrioritizer.new)
  ral = compile_to_ral(manifest)
  graph = Puppet::Graph::RelationshipGraph.new(prioritizer)
  graph.populate_from(ral)
  graph
end

.eval_and_collect_notices(code, node = Puppet::Node.new('foonode'), topscope_vars = {}) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'core_modules/sshkeys_core/spec/lib/puppet_spec/compiler.rb', line 73

def eval_and_collect_notices(code, node = Puppet::Node.new('foonode'), topscope_vars = {})
  collect_notices(code, node) do |compiler|
    unless topscope_vars.empty?
      scope = compiler.topscope
      topscope_vars.each { |k, v| scope.setvar(k, v) }
    end
    if block_given?
      compiler.compile do |catalog|
        yield(compiler.topscope, catalog)
        catalog
      end
    else
      compiler.compile
    end
  end
end

.evaluate(code: 'undef', source: nil, node: Puppet::Node.new('testnode'), variables: {}) ⇒ Object

Compiles a catalog, and if source is given evaluates it and returns its result. The catalog is returned if no source is given. Topscope variables are set before compilation Uses a created node 'testnode' if none is given. (Parameters given by name)



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'core_modules/sshkeys_core/spec/lib/puppet_spec/compiler.rb', line 96

def evaluate(code: 'undef', source: nil, node: Puppet::Node.new('testnode'), variables: {})
  source_location = caller(0..0).first
  Puppet[:code] = code
  compiler = Puppet::Parser::Compiler.new(node)
  unless variables.empty?
    scope = compiler.topscope
    variables.each { |k, v| scope.setvar(k, v) }
  end

  if source.nil?
    compiler.compile
    # see lib/puppet/indirector/catalog/compiler.rb#filter
    return compiler.filter { |r| r.virtual? }
  end

  # evaluate given source is the context of the compiled state and return its result
  compiler.compile do |_catalog|
    Puppet::Pops::Parser::EvaluatingParser.singleton.evaluate_string(compiler.topscope, source, source_location)
  end
end

.order_resources_traversed_in(relationships) ⇒ Object



55
56
57
58
59
# File 'core_modules/sshkeys_core/spec/lib/puppet_spec/compiler.rb', line 55

def order_resources_traversed_in(relationships)
  order_seen = []
  relationships.traverse { |resource| order_seen << resource.ref }
  order_seen
end