Sha256: 3b07cd1952e9a096a499a40d52457066fdc80255d467f6648a92956d3adc08bc

Contents?: true

Size: 1.47 KB

Versions: 2

Compression:

Stored size: 1.47 KB

Contents

# frozen_string_literal: true

# Initialize puppet for testing by loading the
# 'puppetlabs_spec_helper/puppet_spec_helper' library
require 'puppetlabs_spec_helper/puppet_spec_helper'

# PuppetInternals provides a set of methods that interface
# with internal puppet implementations.
module PuppetlabsSpec::PuppetInternals
  def resource(parts = {})
    resource_type = parts[:type] || :hostclass
    resource_name = parts[:name] || 'testing'
    Puppet::Resource::Type.new(resource_type, resource_name)
  end
  module_function :resource

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

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

  # Return a method instance for a given function.  This is primarily useful
  # for rspec-puppet
  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}".to_sym)
  end
  module_function :function_method
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
puppetlabs_spec_helper-5.0.1 lib/puppetlabs_spec_helper/puppetlabs_spec/puppet_internals.rb
puppetlabs_spec_helper-5.0.0 lib/puppetlabs_spec_helper/puppetlabs_spec/puppet_internals.rb