Class: RSpec::Puppet::Adapters::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec-puppet/adapters.rb

Instance Method Summary collapse

Instance Method Details

#catalog(node, exported) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/rspec-puppet/adapters.rb', line 118

def catalog(node, exported)
  node.environment = current_environment
  # Override $::environment to workaround PUP-5835, where Puppet otherwise
  # stores a symbol for the parameter
  if node.parameters['environment'] != node.parameters['environment'].to_s
    node.parameters['environment'] = current_environment.name.to_s
  end

  catalog = if exported
              # Use the compiler directly to skip the filtering done by the indirector
              Puppet::Parser::Compiler.compile(node).filter { |r| !r.exported? }
            else
              Puppet::Resource::Catalog.indirection.find(node.name, use_node: node)
            end

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

  catalog
end

#current_environmentObject



138
139
140
# File 'lib/rspec-puppet/adapters.rb', line 138

def current_environment
  Puppet::Node::Environment.new(@environment_name)
end

#get_setting(example_group, rspec_setting) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/rspec-puppet/adapters.rb', line 110

def get_setting(example_group, rspec_setting)
  if example_group.respond_to?(rspec_setting)
    example_group.send(rspec_setting)
  else
    RSpec.configuration.send(rspec_setting)
  end
end

#manifestString?

Returns The path to the Puppet manifest if it is present and set, nil otherwise.

Returns:

  • (String, nil)

    The path to the Puppet manifest if it is present and set, nil otherwise.



164
165
166
167
168
169
170
171
# File 'lib/rspec-puppet/adapters.rb', line 164

def manifest
  m = current_environment.manifest
  if m == Puppet::Node::Environment::NO_MANIFEST
    nil
  else
    m
  end
end

#modulepathObject



159
160
161
# File 'lib/rspec-puppet/adapters.rb', line 159

def modulepath
  current_environment.modulepath
end

#set_facter_impl(impl) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Set the FacterImpl constant to the given Facter implementation or noop if the constant is already set. If a proc is given, it will only be called if FacterImpl is not defined.

Parameters:

  • impl (Object, Proc)

    An object or a proc that implements the Facter API



180
181
182
183
184
185
# File 'lib/rspec-puppet/adapters.rb', line 180

def set_facter_impl(impl)
  return if defined?(FacterImpl)

  impl = impl.call if impl.is_a?(Proc)
  Object.send(:const_set, :FacterImpl, impl)
end

#settings_mapObject



142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/rspec-puppet/adapters.rb', line 142

def settings_map
  [
    %i[modulepath module_path],
    %i[basemodulepath basemodulepath],
    %i[config config],
    %i[confdir confdir],
    %i[environmentpath environmentpath],
    %i[hiera_config hiera_config],
    %i[strict_variables strict_variables],
    %i[vendormoduledir vendormoduledir]
  ]
end

#setup_puppet(example_group) ⇒ void

This method returns an undefined value.

Set up all Puppet settings applicable for this Puppet version as application defaults.

Puppet setting values can be taken from the global RSpec configuration, or from the currently executing RSpec context. When a setting is specified both in the global configuration and in the example group, the setting in the example group is preferred.

Examples:

Configuring a Puppet setting from within an RSpec example group

RSpec.describe 'my_module::my_class', :type => :class do
  let(:module_path) { "/Users/luke/modules" }
  #=> Puppet[:modulepath] will be "/Users/luke/modules"
end

Configuring a Puppet setting with both a global RSpec configuration and local context

RSpec.configure do |config|
  config.confdir = "/etc/puppet"
end
RSpec.describe 'my_module', :type => :class do
  # Puppet[:confdir] will be "/etc/puppet"
end
RSpec.describe 'my_module::my_class', :type => :class do
  let(:confdir) { "/etc/puppetlabs/puppet" }
  # => Puppet[:confdir] will be "/etc/puppetlabs/puppet" in this example group
end
RSpec.describe 'my_module::my_define', :type => :define do
  # Puppet[:confdir] will be "/etc/puppet" again
end

Parameters:

  • example_group (RSpec::Core::ExampleGroup)

    The RSpec context to use for local settings



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
100
101
102
103
104
105
106
107
108
# File 'lib/rspec-puppet/adapters.rb', line 38

def setup_puppet(example_group)
  case RSpec.configuration.facter_implementation.to_sym
  when :rspec
    # Lazily instantiate FacterTestImpl here to optimize memory
    # allocation, as the proc will only be called if FacterImpl is unset
    set_facter_impl(proc { RSpec::Puppet::FacterTestImpl.new })
  when :facter
    set_facter_impl(Facter)
  else
    raise "Unsupported facter_implementation '#{RSpec.configuration.facter_implementation}'"
  end

  Puppet.runtime[:facter] = FacterImpl

  settings = settings_map.map do |puppet_setting, rspec_setting|
    [puppet_setting, get_setting(example_group, rspec_setting)]
  end.flatten
  default_hash = { confdir: '/dev/null', vardir: '/dev/null' }
  if defined?(Puppet::Test::TestHelper) && Puppet::Test::TestHelper.respond_to?(:app_defaults_for_tests, true)
    default_hash.merge!(Puppet::Test::TestHelper.send(:app_defaults_for_tests))
  end
  settings_hash = default_hash.merge(Hash[*settings])
  if Gem.win_platform?
    settings_hash.each_with_object(settings_hash) do |(k, v), h|
      h[k] = v == '/dev/null' ? 'c:/nul/' : v
    end
  end

  Puppet.settings.initialize_app_defaults(settings_hash)

  # Forcefully apply the environmentpath setting instead of relying on
  # the application defaults as Puppet::Test::TestHelper automatically
  # sets this value as well, overriding our application default
  Puppet.settings[:environmentpath] = settings_hash[:environmentpath] if settings_hash.key?(:environmentpath)

  @environment_name = example_group.environment

  modulepath = if (rspec_modulepath = RSpec.configuration.module_path)
                 rspec_modulepath.split(File::PATH_SEPARATOR)
               else
                 Puppet[:environmentpath].split(File::PATH_SEPARATOR).map do |path|
                   File.join(path, 'fixtures', 'modules')
                 end
               end

  if (rspec_manifest = RSpec.configuration.manifest)
    manifest = rspec_manifest
  else
    manifest_paths = Puppet[:environmentpath].split(File::PATH_SEPARATOR).map do |path|
      File.join(path, 'fixtures', 'manifests')
    end

    manifest = manifest_paths.find do |path|
      File.exist?(path)
    end

    manifest ||= Puppet::Node::Environment::NO_MANIFEST
  end

  env = Puppet::Node::Environment.create(@environment_name, modulepath, manifest)
  loader = Puppet::Environments::Static.new(env)

  Puppet.push_context(
    {
      environments: loader,
      current_environment: env,
      loaders: Puppet::Pops::Loaders.new(env)
    },
    'Setup rspec-puppet environments'
  )
end