From b81a28000b31b2637499d2ae1225ab10fbfab51f Mon Sep 17 00:00:00 2001 From: Steven Pritchard Date: Thu, 1 Aug 2024 16:04:01 -0500 Subject: [PATCH] Fixes for Puppet 8/Ruby 3 * 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 --- Gemfile | 39 +++++++++---------- spec/acceptance/helpers/repo_helper.rb | 6 +-- .../default/90_compliance_enforcement_spec.rb | 2 +- spec/spec_helper.rb | 35 ++++++++++++----- 4 files changed, 48 insertions(+), 34 deletions(-) diff --git a/Gemfile b/Gemfile index d812a4e56..44d2e92a4 100644 --- a/Gemfile +++ b/Gemfile @@ -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 @@ -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 diff --git a/spec/acceptance/helpers/repo_helper.rb b/spec/acceptance/helpers/repo_helper.rb index 20737516c..58dcd52ca 100644 --- a/spec/acceptance/helpers/repo_helper.rb +++ b/spec/acceptance/helpers/repo_helper.rb @@ -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) @@ -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") diff --git a/spec/acceptance/suites/default/90_compliance_enforcement_spec.rb b/spec/acceptance/suites/default/90_compliance_enforcement_spec.rb index 0d405c747..72a048615 100644 --- a/spec/acceptance/suites/default/90_compliance_enforcement_spec.rb +++ b/spec/acceptance/suites/default/90_compliance_enforcement_spec.rb @@ -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') } diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index f2014d05c..67ef92149 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -24,14 +24,21 @@ default_hiera_config =<<-EOM --- -:backends: - - "yaml" -:yaml: - :datadir: "stub" -:hierarchy: - - "%{custom_hiera}" - - "%{module_name}" - - "default" +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" EOM # This can be used from inside your spec tests to set the testable environment. @@ -92,7 +99,7 @@ def set_hieradata(hieradata) c.mock_with :mocha 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') @@ -110,7 +117,15 @@ def set_hieradata(hieradata) c.before(:all) do data = YAML.load(default_hiera_config) - data[:yaml][:datadir] = File.join(fixture_path, 'hieradata') + data.keys.each 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