Module: PuppetlabsSpec::PuppetInternals

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

Class Method Summary collapse

Class Method Details

.compiler(parts = {}) ⇒ Object



41
42
43
44
# File 'lib/puppetlabs_spec_helper/puppetlabs_spec/puppet_internals.rb', line 41

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



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

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



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

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

.resource(parts = {}) ⇒ Object



34
35
36
37
38
# File 'lib/puppetlabs_spec_helper/puppetlabs_spec/puppet_internals.rb', line 34

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
# File 'lib/puppetlabs_spec_helper/puppetlabs_spec/puppet_internals.rb', line 10

def scope(parts = {})
  if Puppet.version =~ /^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)

  if Puppet.version =~ /^2\.[67]/
    scope = Puppet::Parser::Scope.new(:compiler => scope_compiler)
  else
    scope = Puppet::Parser::Scope.new(scope_compiler)
  end

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