Skip to content

Commit

Permalink
Fixes for Puppet 8/Ruby 3
Browse files Browse the repository at this point in the history
* Fix use of deprecated methods (#893)
* Update gem dependencies
* Use common `spec/spec_helper.rb` from SIMP Puppet modules
* Fix use of legacy `fqdn` fact

Fixes #893
  • Loading branch information
silug committed Aug 1, 2024
1 parent 50cdfb2 commit 6489399
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 65 deletions.
39 changes: 19 additions & 20 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
gem_sources = ENV.fetch('GEM_SERVERS','https://rubygems.org').split(/[, ]+/)
gem_sources = ENV.fetch('GEM_SERVERS', 'https://rubygems.org').split(%r{[, ]+})

ENV['PDK_DISABLE_ANALYTICS'] ||= 'true'

gem_sources.each { |gem_source| source gem_source }

group :test do
puppet_version = ENV['PUPPET_VERSION'] || '~> 6.22'
major_puppet_version = puppet_version.scan(/(\d+)(?:\.|\Z)/).flatten.first.to_i
gem 'rake'
gem 'terminal-table'
puppet_version = ENV.fetch('PUPPET_VERSION', ['>= 7', '< 9'])
major_puppet_version = Array(puppet_version).first.scan(%r{(\d+)(?:\.|\Z)}).flatten.first.to_i
gem 'hiera-puppet-helper'
gem 'metadata-json-lint'
gem 'naturally'
gem 'pathspec', '~> 0.2' if Gem::Requirement.create('< 2.6').satisfied_by?(Gem::Version.new(RUBY_VERSION.dup))
gem('pdk', ENV.fetch('PDK_VERSION', ['>= 2.0', '< 4.0']), require: false) if major_puppet_version > 5
gem 'puppet', puppet_version
gem 'puppet-lint-trailing_comma-check', :require => false
gem 'puppet-strings'
gem 'puppetlabs_spec_helper'
gem 'rake'
gem 'rspec'
gem 'rspec-puppet'
gem 'hiera-puppet-helper'
gem 'puppetlabs_spec_helper'
gem 'metadata-json-lint'
gem 'puppet-strings'
gem 'puppet-lint-empty_string-check', :require => false
gem 'puppet-lint-trailing_comma-check', :require => false
gem 'simp-rspec-puppet-facts', ENV['SIMP_RSPEC_PUPPET_FACTS_VERSION'] || '~> 3.1'
gem 'simp-rake-helpers', ENV['SIMP_RAKE_HELPERS_VERSION'] || ['>= 5.17.1', '< 6']
gem( 'pdk', ENV['PDK_VERSION'] || '~> 2.0', :require => false) if major_puppet_version > 5
gem 'pathspec', '~> 0.2' if Gem::Requirement.create('< 2.6').satisfied_by?(Gem::Version.new(RUBY_VERSION.dup))
gem 'simp-build-helpers', ENV['SIMP_BUILD_HELPERS_VERSION'] || ['> 0.1', '< 2.0']

gem 'simp-build-helpers', ENV.fetch('SIMP_BUILD_HELPERS_VERSION', ['> 0.1', '< 2.0'])
gem 'simp-rake-helpers', ENV.fetch('SIMP_RAKE_HELPERS_VERSION', ['>= 5.21.0', '< 6'])
gem 'simp-rspec-puppet-facts', ENV.fetch('SIMP_RSPEC_PUPPET_FACTS_VERSION', '~> 3.7')
gem 'terminal-table'
end

group :development do
Expand All @@ -34,20 +32,21 @@ group :development do
end

group :system_tests do
gem 'bcrypt_pbkdf'
gem 'beaker'
gem 'beaker-rspec'
gem 'simp-beaker-helpers', ENV['SIMP_BEAKER_HELPERS_VERSION'] || ['>= 1.23.2', '< 2']
gem 'simp-beaker-helpers', ENV.fetch('SIMP_BEAKER_HELPERS_VERSION', ['>= 1.32.1', '< 2'])
end

# Evaluate extra gemfiles if they exist
extra_gemfiles = [
ENV['EXTRA_GEMFILE'] || '',
ENV.fetch('EXTRA_GEMFILE', ''),
"#{__FILE__}.project",
"#{__FILE__}.local",
File.join(Dir.home, '.gemfile'),
]
extra_gemfiles.each do |gemfile|
if File.file?(gemfile) && File.readable?(gemfile)
eval(File.read(gemfile), binding)
eval(File.read(gemfile), binding) # rubocop:disable Security/Eval
end
end
6 changes: 3 additions & 3 deletions spec/acceptance/helpers/repo_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module RepoHelper
#
# @fails if the specified repo file cannot be installed on host
def copy_repo(host, repo_filename, repo_name = 'simp_manual.repo')
if File.exists?(repo_filename)
if File.exist?(repo_filename)
puts('='*72)
puts("Using repos defined in #{repo_filename}")
puts('='*72)
Expand Down Expand Up @@ -131,13 +131,13 @@ def find_simp_release_tarball(relver, osname)
filename = "SIMP-downloaded-#{osname}-#{relver}-x86_64.tar.gz"
url = "#{tarball}"
require 'net/http'
Dir.exists?("spec/fixtures") || Dir.mkdir("spec/fixtures")
Dir.exist?("spec/fixtures") || Dir.mkdir("spec/fixtures")
File.write("spec/fixtures/#{filename}", Net::HTTP.get(URI.parse(url)))
tarball = "spec/fixtures/#{filename}"
puts("Downloaded SIMP release tarball from #{url} to #{tarball}")
else
unless tarball.nil?
if File.exists?(tarball)
if File.exist?(tarball)
puts("Found SIMP release tarball: #{tarball}")
else
warn("SIMP release tarball '#{tarball}' not found")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@

agents.each do |host|
context "a valid report for #{host}" do
let(:fqdn) { fact_on(host, 'fqdn') }
let(:fqdn) { fact_on(host, 'networking.fqdn') }
let(:host_sec_results_dir) { File.join(sec_results_dir, fqdn) }
let(:report_file) { File.join(host_sec_results_dir, 'compliance_report.json') }

Expand Down
101 changes: 60 additions & 41 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# frozen_string_literal: true
#
# ------------------------------------------------------------------------------
# NOTICE: **This file is maintained with puppetsync**
#
# This file is automatically updated as part of a puppet module baseline.
# The next baseline sync will overwrite any local changes made to this file.
# ------------------------------------------------------------------------------

require 'puppetlabs_spec_helper/module_spec_helper'
require 'rspec-puppet'
require 'simp/rspec-puppet-facts'
Expand All @@ -7,32 +16,31 @@

# RSpec Material
fixture_path = File.expand_path(File.join(__FILE__, '..', 'fixtures'))
module_name = File.basename(File.expand_path(File.join(__FILE__,'../..')))

# Add fixture lib dirs to LOAD_PATH. Work-around for PUP-3336
if Puppet.version < "4.0.0"
Dir["#{fixture_path}/modules/*/lib"].entries.each do |lib_dir|
$LOAD_PATH << lib_dir
end
end
module_name = File.basename(File.expand_path(File.join(__FILE__, '../..')))


if !ENV.key?( 'TRUSTED_NODE_DATA' )
warn '== WARNING: TRUSTED_NODE_DATA is unset, using TRUSTED_NODE_DATA=yes'
ENV['TRUSTED_NODE_DATA']='yes'
if ENV['PUPPET_DEBUG']
Puppet::Util::Log.level = :debug
Puppet::Util::Log.newdestination(:console)
end

default_hiera_config =<<-EOM
default_hiera_config = <<~HIERA_CONFIG
---
:backends:
- "yaml"
:yaml:
:datadir: "stub"
:hierarchy:
- "%{custom_hiera}"
- "%{module_name}"
- "default"
EOM
version: 5
hierarchy:
- name: SIMP Compliance Engine
lookup_key: compliance_markup::enforcement
options:
enabled_sce_versions: [2]
- name: Custom Test Hiera
path: "%{custom_hiera}.yaml"
- name: "%{module_name}"
path: "%{module_name}.yaml"
- name: Common
path: default.yaml
defaults:
data_hash: yaml_data
datadir: "stub"
HIERA_CONFIG

# This can be used from inside your spec tests to set the testable environment.
# You can use this to stub out an ENC.
Expand Down Expand Up @@ -70,36 +78,36 @@ def set_hieradata(hieradata)
RSpec.configure { |c| c.default_facts['custom_hiera'] = hieradata }
end

if not File.directory?(File.join(fixture_path,'hieradata')) then
FileUtils.mkdir_p(File.join(fixture_path,'hieradata'))
unless File.directory?(File.join(fixture_path, 'hieradata'))
FileUtils.mkdir_p(File.join(fixture_path, 'hieradata'))
end

if not File.directory?(File.join(fixture_path,'modules',module_name)) then
FileUtils.mkdir_p(File.join(fixture_path,'modules',module_name))
unless File.directory?(File.join(fixture_path, 'modules', module_name))
FileUtils.mkdir_p(File.join(fixture_path, 'modules', module_name))
end

RSpec.configure do |c|
# If nothing else...
c.default_facts = {
:production => {
production: {
#:fqdn => 'production.rspec.test.localdomain',
:path => '/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin',
:concat_basedir => '/tmp'
path: '/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin',
concat_basedir: '/tmp'
}
}

c.mock_framework = :rspec
c.mock_with :mocha
c.mock_with :rspec

c.module_path = File.join(fixture_path, 'modules')
c.manifest_dir = File.join(fixture_path, 'manifests')
c.manifest_dir = File.join(fixture_path, 'manifests') if c.respond_to?(:manifest_dir)

c.hiera_config = File.join(fixture_path,'hieradata','hiera.yaml')
c.hiera_config = File.join(fixture_path, 'hieradata', 'hiera.yaml')

# Useless backtrace noise
backtrace_exclusion_patterns = [
/spec_helper/,
/gems/
%r{spec_helper},
%r{gems},
]

if c.respond_to?(:backtrace_exclusion_patterns)
Expand All @@ -108,33 +116,44 @@ def set_hieradata(hieradata)
c.backtrace_clean_patterns = backtrace_exclusion_patterns
end

# rubocop:disable RSpec/BeforeAfterAll
c.before(:all) do
data = YAML.load(default_hiera_config)
data[:yaml][:datadir] = File.join(fixture_path, 'hieradata')
data = YAML.safe_load(default_hiera_config)
data.each_key do |key|
next unless data[key].is_a?(Hash)

if data[key][:datadir] == 'stub'
data[key][:datadir] = File.join(fixture_path, 'hieradata')
elsif data[key]['datadir'] == 'stub'
data[key]['datadir'] = File.join(fixture_path, 'hieradata')
end
end

File.open(c.hiera_config, 'w') do |f|
f.write data.to_yaml
end
end
# rubocop:enable RSpec/BeforeAfterAll

c.before(:each) do
@spec_global_env_temp = Dir.mktmpdir('simpspec')

if defined?(environment)
set_environment(environment)
FileUtils.mkdir_p(File.join(@spec_global_env_temp,environment.to_s))
FileUtils.mkdir_p(File.join(@spec_global_env_temp, environment.to_s))
end

# ensure the user running these tests has an accessible environmentpath
Puppet[:digest_algorithm] = 'sha256'
Puppet[:environmentpath] = @spec_global_env_temp
Puppet[:user] = Etc.getpwuid(Process.uid).name
Puppet[:group] = Etc.getgrgid(Process.gid).name

# sanitize hieradata
if defined?(hieradata)
set_hieradata(hieradata.gsub(':','_'))
set_hieradata(hieradata.gsub(':', '_'))
elsif defined?(class_name)
set_hieradata(class_name.gsub(':','_'))
set_hieradata(class_name.gsub(':', '_'))
end
end

Expand All @@ -148,7 +167,7 @@ def set_hieradata(hieradata)
Dir.glob("#{RSpec.configuration.module_path}/*").each do |dir|
begin
Pathname.new(dir).realpath
rescue
fail "ERROR: The module '#{dir}' is not installed. Tests cannot continue."
rescue StandardError
raise "ERROR: The module '#{dir}' is not installed. Tests cannot continue."
end
end

0 comments on commit 6489399

Please sign in to comment.