Module: PuppetlabsSpec::PuppetInternals

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

Class Method Summary collapse

Class Method Details

.compiler(parts = {}) ⇒ Object



45
46
47
48
# File 'lib/puppetlabs_spec_helper/puppetlabs_spec/puppet_internals.rb', line 45

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



66
67
68
69
70
71
72
73
74
# File 'lib/puppetlabs_spec_helper/puppetlabs_spec/puppet_internals.rb', line 66

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



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

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



38
39
40
41
42
# File 'lib/puppetlabs_spec_helper/puppetlabs_spec/puppet_internals.rb', line 38

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.



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

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

  if %r{^2\.[67]}.match?(Puppet.version)
    # 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 %r{^2\.[67]}.match?(Puppet.version)
            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