Module: PuppetlabsSpec::PuppetInternals

Defined in:
lib/puppetlabs_spec_helper/puppetlabs_spec/puppet_internals.rb

Class Method Summary collapse

Class Method Details

.compiler(parts = {}) ⇒ Object



43
44
45
46
# File 'lib/puppetlabs_spec_helper/puppetlabs_spec/puppet_internals.rb', line 43

def compiler(parts = {})
  compiler_node = parts[:node] || node
  Puppet::Parser::Compiler.new(compiler_node)
end

.function_method(name, parts = {}) ⇒ Object

Return a method instance for a given function. This is primarily useful for rspec-puppet



64
65
66
67
68
69
70
71
# File 'lib/puppetlabs_spec_helper/puppetlabs_spec/puppet_internals.rb', line 64

def function_method(name, parts = {})
  scope = parts[:scope] || scope()
  # Ensure the method instance is defined by side-effect of checking if it
  # exists.  This is a hack, but at least it's a hidden hack and not an
  # exposed hack.
  return nil unless Puppet::Parser::Functions.function(name)
  scope.method("function_#{name}".intern)
end

.node(parts = {}) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/puppetlabs_spec_helper/puppetlabs_spec/puppet_internals.rb', line 49

def node(parts = {})
  node_name = parts[:name] || 'testinghost'
  options = parts[:options] || {}
  node_environment = if Puppet.version.to_f >= 4.0
                       Puppet::Node::Environment.create(parts[:environment] || 'test', [])
                     else
                       Puppet::Node::Environment.new(parts[:environment] || 'test')
                     end
  options[:environment] = node_environment
  Puppet::Node.new(node_name, options)
end

.resource(parts = {}) ⇒ Object



36
37
38
39
40
# File 'lib/puppetlabs_spec_helper/puppetlabs_spec/puppet_internals.rb', line 36

def resource(parts = {})
  resource_type = parts[:type] || :hostclass
  resource_name = parts[:name] || 'testing'
  Puppet::Resource::Type.new(resource_type, resource_name)
end

.scope(parts = {}) ⇒ Object

parser_scope is intended to return a Puppet::Parser::Scope instance suitable for placing in a test harness with the intent of testing parser functions from modules.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/puppetlabs_spec_helper/puppetlabs_spec/puppet_internals.rb', line 10

def scope(parts = {})
  RSpec.deprecate('scope', replacement: 'rspec-puppet 2.2.0 provides a scope property')

  if Puppet.version =~ %r{^2\.[67]}
    # loadall should only be necessary prior to 3.x
    # Please note, loadall needs to happen first when creating a scope, otherwise
    # you might receive undefined method `function_*' errors
    Puppet::Parser::Functions.autoloader.loadall
  end

  scope_compiler = parts[:compiler] || compiler
  scope_parent = parts[:parent] || scope_compiler.topscope
  scope_resource = parts[:resource] || resource(type: :node, title: scope_compiler.node.name)

  scope = if Puppet.version =~ %r{^2\.[67]}
            Puppet::Parser::Scope.new(compiler: scope_compiler)
          else
            Puppet::Parser::Scope.new(scope_compiler)
          end

  scope.source = Puppet::Resource::Type.new(:node, 'foo')
  scope.parent = scope_parent
  scope
end