Skip to content

Commit

Permalink
(CAT-1226) - Remove Compatibility Puppet 6.x and below
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanbreen28 committed Oct 2, 2023
1 parent 057a248 commit e0a2c3f
Show file tree
Hide file tree
Showing 39 changed files with 256 additions and 363 deletions.
2 changes: 1 addition & 1 deletion lib/rspec-puppet/adapters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def setup_puppet(example_group)
{
environments: loader,
current_environment: env,
loaders: (Puppet::Pops::Loaders.new(env) if Gem::Version.new(Puppet.version) >= Gem::Version.new('6.0.0'))
loaders: (Puppet::Pops::Loaders.new(env))
},
'Setup rspec-puppet environments'
)
Expand Down
68 changes: 24 additions & 44 deletions lib/rspec-puppet/example/function_example_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,17 @@ def find_function(function_name = self.class.top_level_description)
with_vardir do
env = adapter.current_environment

if Puppet.version.to_f >= 4.0
context_overrides = compiler.context_overrides
func = nil
loaders = Puppet.lookup(:loaders)
Puppet.override(context_overrides, 'rspec-test scope') do
func = V4FunctionWrapper.new(function_name,
loaders.private_environment_loader.load(:function, function_name), context_overrides)
@scope = context_overrides[:global_scope]
end

return func if func.func
context_overrides = compiler.context_overrides
func = nil
loaders = Puppet.lookup(:loaders)
Puppet.override(context_overrides, 'rspec-test scope') do
func = V4FunctionWrapper.new(function_name,
loaders.private_environment_loader.load(:function, function_name), context_overrides)
@scope = context_overrides[:global_scope]
end

return func if func.func

if Puppet::Parser::Functions.function(function_name)
V3FunctionWrapper.new(function_name, scope.method("function_#{function_name}".intern))
end
Expand Down Expand Up @@ -157,46 +155,28 @@ def build_compiler

node = build_node(node_name, node_options)

if Puppet::Util::Package.versioncmp(Puppet.version, '4.3.0') >= 0
Puppet.push_context(
{
trusted_information: Puppet::Context::TrustedInformation.new('remote', node_name, trusted_values)
},
'Context for spec trusted hash'
)
end
Puppet.push_context(
{
trusted_information: Puppet::Context::TrustedInformation.new('remote', node_name, trusted_values)
},
'Context for spec trusted hash'
)

compiler = Puppet::Parser::Compiler.new(node)
compiler.compile
if Puppet::Util::Package.versioncmp(Puppet.version, '4.0.0') >= 0
loaders = Puppet::Pops::Loaders.new(adapter.current_environment)
Puppet.push_context(
{
loaders: loaders,
global_scope: compiler.context_overrides[:global_scope]
},
'set globals'
)
end
loaders = Puppet::Pops::Loaders.new(adapter.current_environment)
Puppet.push_context(
{
loaders: loaders,
global_scope: compiler.context_overrides[:global_scope]
},
'set globals'
)
compiler
end

def build_scope(compiler, node_name)
if Puppet.version.to_f >= 4.0
return compiler.context_overrides[:global_scope]
elsif /^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
scope = Puppet::Parser::Scope.new(compiler: compiler)
else
scope = Puppet::Parser::Scope.new(compiler)
end

scope.source = Puppet::Resource::Type.new(:node, node_name)
scope.parent = compiler.topscope
scope
compiler.context_overrides[:global_scope]
end

def build_node(name, opts = {})
Expand Down
58 changes: 28 additions & 30 deletions lib/rspec-puppet/monkey_patches.rb
Original file line number Diff line number Diff line change
Expand Up @@ -256,42 +256,40 @@ class Provider::Confine::Exists < Puppet::Provider::Confine
end
end

if Puppet::Util::Package.versioncmp(Puppet.version, '4.9.0') >= 0
class Module
old_hiera_conf_file = instance_method(:hiera_conf_file)
define_method(:hiera_conf_file) do
if RSpec::Puppet.rspec_puppet_example?
if RSpec.configuration.disable_module_hiera
return nil
elsif RSpec.configuration.fixture_hiera_configs.key?(name)
config = RSpec.configuration.fixture_hiera_configs[name]
config = File.absolute_path(config, path) unless config.nil?
return config
elsif RSpec.configuration.use_fixture_spec_hiera
config = RSpec::Puppet.current_example.fixture_spec_hiera_conf(self)
return config unless config.nil? && RSpec.configuration.fallback_to_default_hiera
end
class Module
old_hiera_conf_file = instance_method(:hiera_conf_file)
define_method(:hiera_conf_file) do
if RSpec::Puppet.rspec_puppet_example?
if RSpec.configuration.disable_module_hiera
return nil
elsif RSpec.configuration.fixture_hiera_configs.key?(name)
config = RSpec.configuration.fixture_hiera_configs[name]
config = File.absolute_path(config, path) unless config.nil?
return config
elsif RSpec.configuration.use_fixture_spec_hiera
config = RSpec::Puppet.current_example.fixture_spec_hiera_conf(self)
return config unless config.nil? && RSpec.configuration.fallback_to_default_hiera
end
old_hiera_conf_file.bind_call(self)
end
old_hiera_conf_file.bind_call(self)
end
end

class Pops::Lookup::ModuleDataProvider
old_configuration_path = instance_method(:configuration_path)
define_method(:configuration_path) do |lookup_invocation|
if RSpec::Puppet.rspec_puppet_example?
env = lookup_invocation.scope.environment
mod = env.module(module_name)
unless mod
raise Puppet::DataBinding::LookupError,
format(_("Environment '%<env>s', cannot find module '%<module_name>s'"), env: env.name,
module_name: module_name)
end

return Pathname.new(mod.hiera_conf_file)
class Pops::Lookup::ModuleDataProvider
old_configuration_path = instance_method(:configuration_path)
define_method(:configuration_path) do |lookup_invocation|
if RSpec::Puppet.rspec_puppet_example?
env = lookup_invocation.scope.environment
mod = env.module(module_name)
unless mod
raise Puppet::DataBinding::LookupError,
format(_("Environment '%<env>s', cannot find module '%<module_name>s'"), env: env.name,
module_name: module_name)
end
old_configuration_path.bind_call(self, lookup_invocation)

return Pathname.new(mod.hiera_conf_file)
end
old_configuration_path.bind_call(self, lookup_invocation)
end
end
end
Expand Down
8 changes: 2 additions & 6 deletions lib/rspec-puppet/setup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,8 @@ def self.get_module_name
def self.get_module_name_from_file(file)
# FIXME: see discussion at
# https://github.com/rodjek/rspec-puppet/issues/290
if Puppet.version.to_f >= 4.0 || RSpec.configuration.parser == 'future'
require 'puppet/pops'
p = Puppet::Pops::Parser::Lexer2.new
else
p = Puppet::Parser::Lexer.new
end
require 'puppet/pops'
p = Puppet::Pops::Parser::Lexer2.new
module_name = nil
p.string = File.read(file)
tokens = p.fullscan
Expand Down
35 changes: 10 additions & 25 deletions lib/rspec-puppet/support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@ def environment
end

def build_code(type, manifest_opts)
if (Puppet.version.to_f >= 4.0) || (Puppet[:parser] == 'future')
[site_pp_str, pre_cond, test_manifest(type, manifest_opts), post_cond].compact.join("\n")
else
[import_str, pre_cond, test_manifest(type, manifest_opts), post_cond].compact.join("\n")
end
[site_pp_str, pre_cond, test_manifest(type, manifest_opts), post_cond].compact.join("\n")
end

def guess_type_from_path(path)
Expand Down Expand Up @@ -302,8 +298,6 @@ def param_str(params)
end

def trusted_facts_hash(_node_name)
return {} unless Puppet::Util::Package.versioncmp(Puppet.version, '4.3.0') >= 0

extensions = {}

if RSpec.configuration.default_trusted_facts.any?
Expand All @@ -315,8 +309,6 @@ def trusted_facts_hash(_node_name)
end

def trusted_external_data_hash
return {} unless Puppet::Util::Package.versioncmp(Puppet.version, '6.14.0') >= 0

external_data = {}

if RSpec.configuration.default_trusted_external_data.any?
Expand Down Expand Up @@ -389,11 +381,6 @@ def setup_puppet
vardir = Dir.mktmpdir
Puppet[:vardir] = vardir

# Enable app_management by default for Puppet versions that support it
if Puppet::Util::Package.versioncmp(Puppet.version, '4.3.0') >= 0 && Puppet.version.to_i < 5
Puppet[:app_management] = !ENV.include?('PUPPET_NOAPP_MANAGMENT')
end

adapter.modulepath.map do |d|
Dir["#{d}/*/lib"].entries
end.flatten.each do |lib|
Expand Down Expand Up @@ -457,17 +444,15 @@ def build_catalog_without_cache_v2(
node_obj = Puppet::Node.new(nodename, { parameters: node_params, facts: node_facts })

trusted_info = ['remote', nodename, trusted_facts_val]
trusted_info.push(trusted_external_data) if Puppet::Util::Package.versioncmp(Puppet.version, '6.14.0') >= 0
if Puppet::Util::Package.versioncmp(Puppet.version, '4.3.0') >= 0
Puppet.push_context(
{
trusted_information: Puppet::Context::TrustedInformation.new(*trusted_info)
},
'Context for spec trusted hash'
)

node_obj.add_server_facts(server_facts_hash) if RSpec.configuration.trusted_server_facts
end
trusted_info.push(trusted_external_data)
Puppet.push_context(
{
trusted_information: Puppet::Context::TrustedInformation.new(*trusted_info)
},
'Context for spec trusted hash'
)

node_obj.add_server_facts(server_facts_hash) if RSpec.configuration.trusted_server_facts

adapter.catalog(node_obj, exported)
end
Expand Down
20 changes: 1 addition & 19 deletions spec/classes/array_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,7 @@
] }
end

# Puppet 2.6 will automatically flatten nested arrays. If we're going
# to be testing recursive data structures, we might as well ensure that
# we're still handling numeric values correctly.
describe 'on Puppet 2.6', if: Puppet.version =~ /^2\.6/ do
it {
expect(subject).to contain_structured_data__def('thing').with(
{ 'data' => [
'first',
'second',
'third',
'fourth',
5,
6
] }
)
}
end

describe 'on Puppet 2.7 and later', unless: Puppet.version =~ /^2\.6/ do
describe 'on Puppet 2.7 and later' do
it {
expect(subject).to contain_structured_data__def('thing').with(
{ 'data' => [
Expand Down
2 changes: 1 addition & 1 deletion spec/classes/default_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require 'spec_helper'

describe 'default_test', if: Puppet.version.to_f >= 4.0 do
describe 'default_test' do
let(:params) { { value: :default } }

it { is_expected.to compile.with_all_deps }
Expand Down
2 changes: 1 addition & 1 deletion spec/classes/deferred_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

require 'spec_helper'

describe 'deferred', if: Puppet::Util::Package.versioncmp(Puppet.version, '6.0.0') >= 0 do
describe 'deferred' do
it { is_expected.to contain_notify('deferred msg').with_message('A STRING') }
end
12 changes: 6 additions & 6 deletions spec/classes/facts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# with the description of "Facter should not downcast fact names". The "mixed case in facts" tests this functionality.

describe 'structured_facts::hash' do
context 'symbols and strings in facts', if: Puppet.version.to_f >= 4.0 do
context 'symbols and strings in facts' do
let(:facts) do
{
os: {
Expand All @@ -23,7 +23,7 @@
it { is_expected.to contain_notify(family) }
end

context 'only symbols in facts', if: Puppet.version.to_f >= 4.0 do
context 'only symbols in facts' do
let(:facts) do
{
os: {
Expand All @@ -39,7 +39,7 @@
end

# See note concerning mixed case in facts at the beginning of the file
context 'mixed case symbols in facts', if: Puppet.version.to_f >= 4.0 do
context 'mixed case symbols in facts' do
let(:facts) do
{
oS: {
Expand All @@ -54,7 +54,7 @@
it { is_expected.to contain_notify(family) }
end

context 'only strings in facts', if: Puppet.version.to_f >= 4.0 do
context 'only strings in facts' do
let(:facts) do
{
'os' => {
Expand All @@ -70,7 +70,7 @@
end

# See note concerning mixed case in facts at the beginning of the file
context 'mixed case strings in facts', if: Puppet.version.to_f >= 4.0 do
context 'mixed case strings in facts' do
let(:facts) do
{
'oS' => {
Expand Down Expand Up @@ -166,7 +166,7 @@
end

describe 'structured_facts::case_check' do
context 'mixed case in structure fact nested keys', if: Puppet.version.to_f >= 4.0 do
context 'mixed case in structure fact nested keys' do
let(:facts) do
{
'custom_fact' => {
Expand Down
10 changes: 1 addition & 9 deletions spec/classes/hash_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,7 @@
{ 'data' => { 1 => 'uno', 2 => 'dos' } }
end

context 'puppet less than 4', unless: Puppet.version.to_f >= 4.0 do
it {
expect(subject).to contain_structured_data__def('thing').with(
{ 'data' => { '1' => 'uno', '2' => 'dos' } }
)
}
end

context 'puppet 4 or greater', if: Puppet.version.to_f >= 4.0 do
context 'puppet 4 or greater' do
it {
expect(subject).to contain_structured_data__def('thing').with(
{ 'data' => { 1 => 'uno', 2 => 'dos' } }
Expand Down
6 changes: 3 additions & 3 deletions spec/classes/hiera_integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require 'spec_helper'

# hiera is not supported before 2.7
describe 'test::hiera', if: Puppet.version.to_f >= 3.0 do
describe 'test::hiera' do
context 'with :hiera_config set' do
let(:hiera_config) { 'spec/fixtures/hiera.yaml' }

Expand All @@ -15,7 +15,7 @@
end
end

describe 'hiera_test', if: Puppet::Util::Package.versioncmp(Puppet.version, '4.9.0') >= 0 do
describe 'hiera_test' do
before do
RSpec.configuration.disable_module_hiera = false
RSpec.configuration.use_fixture_spec_hiera = false
Expand Down Expand Up @@ -106,7 +106,7 @@
end
end

describe 'hiera_test2', if: Puppet::Util::Package.versioncmp(Puppet.version, '4.9.0') >= 0 do
describe 'hiera_test2' do
before do
RSpec.configuration.disable_module_hiera = false
RSpec.configuration.use_fixture_spec_hiera = false
Expand Down
Loading

0 comments on commit e0a2c3f

Please sign in to comment.