Class: PuppetUnitTests::TestEnvironment

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet_unit_tests/test_environment.rb

Overview

Puppet environment for tests.

Constant Summary collapse

SETTINGS_DEFAULT =
{
  confdir: "/dev/null",
  vardir: "/dev/null",
  logdir: "/dev/null",
  publicdir: "/dev/null",
  codedir: "/dev/null",
  rundir: "/dev/null",
  hiera_config: "/dev/null",
  modulepath: "/dev/null",
  basemodulepath: "/dev/null",
  config: nil,
  environmentpath: "/dev/null",
  strict_variables: true,
  vendormoduledir: "/dev/null"
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ TestEnvironment

Returns a new instance of TestEnvironment.

Yields:

  • (_self)

Yield Parameters:



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/puppet_unit_tests/test_environment.rb', line 28

def initialize
  @nodename = "testnode"
  @stubsdir = Pathname.new("stubs")
  @settings = SETTINGS_DEFAULT.dup
  yield self if block_given?

  @puppet_env = global_setup(@settings)
  Puppet.push_context(
    {
      loaders: Puppet::Pops::Loaders.new(@puppet_env),
      current_environment: @puppet_env
    },
    "Environment-level test context"
  )
end

Instance Attribute Details

#nodename ⇒ Object

Returns the value of attribute nodename.



25
26
27
# File 'lib/puppet_unit_tests/test_environment.rb', line 25

def nodename
  @nodename
end

#stubs_dir ⇒ Object

Returns the value of attribute stubs_dir.



26
27
28
# File 'lib/puppet_unit_tests/test_environment.rb', line 26

def stubs_dir
  @stubs_dir
end

Instance Method Details

#clear_compiler ⇒ Object



56
57
58
59
60
# File 'lib/puppet_unit_tests/test_environment.rb', line 56

def clear_compiler
  Puppet.runtime[:facter].clear
  Puppet.pop_context
  Puppet.pop_context
end

#environmentpath=(path) ⇒ Object



52
53
54
# File 'lib/puppet_unit_tests/test_environment.rb', line 52

def environmentpath=(path)
  @settings[:environmentpath] = Pathname.new(path).realpath
end

#hiera_config=(path) ⇒ Object



44
45
46
# File 'lib/puppet_unit_tests/test_environment.rb', line 44

def hiera_config=(path)
  @settings[:hiera_config] = Pathname.new(path).realpath
end

#modulepath=(path) ⇒ Object



48
49
50
# File 'lib/puppet_unit_tests/test_environment.rb', line 48

def modulepath=(path)
  @settings[:modulepath] = Pathname.new(path).realpath
end

#with_compiler(code, facts:, server_facts:, trusted_facts:, stubs: []) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/puppet_unit_tests/test_environment.rb', line 62

def with_compiler(code, facts:, server_facts:, trusted_facts:, stubs: [])
  # TODO: Puppet.features.add(:posix) { true } if facts match UNIX

  code = format_stubs(stubs) + $RS + code

  name = trusted_facts["certname"]
  node_facts = Puppet::Node::Facts.new(name, facts)
  node = Puppet::Node.new(name, parameters: {}, facts: node_facts)
  trusted_info = ["remote", name, trusted_facts]
  Puppet.push_context(
    {
      trusted_information: Puppet::Context::TrustedInformation.new(*trusted_info)
    },
    "Context for test code"
  )
  node.environment = @puppet_env
  node.add_server_facts(server_facts)

  Puppet[:code] = code
  compiler = Puppet::Parser::Compiler.new(node)
  compiler.compile
  loaders = Puppet::Pops::Loaders.new(@puppet_env)
  Puppet.push_context(
    {
      loaders: loaders,
      global_scope: compiler.context_overrides[:global_scope]
    },
    "set globals"
  )

  Puppet::Pops::Evaluator::DeferredResolver.resolve_and_replace(
    node.facts, compiler.catalog
  )

  yield compiler
ensure
  Puppet.runtime[:facter].clear
end