Skip to content

Commit 6489399

Browse files
committed
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
1 parent 50cdfb2 commit 6489399

File tree

4 files changed

+83
-65
lines changed

4 files changed

+83
-65
lines changed

Gemfile

+19-20
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,28 @@
1-
gem_sources = ENV.fetch('GEM_SERVERS','https://rubygems.org').split(/[, ]+/)
1+
gem_sources = ENV.fetch('GEM_SERVERS', 'https://rubygems.org').split(%r{[, ]+})
22

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

55
gem_sources.each { |gem_source| source gem_source }
66

77
group :test do
8-
puppet_version = ENV['PUPPET_VERSION'] || '~> 6.22'
9-
major_puppet_version = puppet_version.scan(/(\d+)(?:\.|\Z)/).flatten.first.to_i
10-
gem 'rake'
11-
gem 'terminal-table'
8+
puppet_version = ENV.fetch('PUPPET_VERSION', ['>= 7', '< 9'])
9+
major_puppet_version = Array(puppet_version).first.scan(%r{(\d+)(?:\.|\Z)}).flatten.first.to_i
10+
gem 'hiera-puppet-helper'
11+
gem 'metadata-json-lint'
1212
gem 'naturally'
13+
gem 'pathspec', '~> 0.2' if Gem::Requirement.create('< 2.6').satisfied_by?(Gem::Version.new(RUBY_VERSION.dup))
14+
gem('pdk', ENV.fetch('PDK_VERSION', ['>= 2.0', '< 4.0']), require: false) if major_puppet_version > 5
1315
gem 'puppet', puppet_version
16+
gem 'puppet-lint-trailing_comma-check', :require => false
17+
gem 'puppet-strings'
18+
gem 'puppetlabs_spec_helper'
19+
gem 'rake'
1420
gem 'rspec'
1521
gem 'rspec-puppet'
16-
gem 'hiera-puppet-helper'
17-
gem 'puppetlabs_spec_helper'
18-
gem 'metadata-json-lint'
19-
gem 'puppet-strings'
20-
gem 'puppet-lint-empty_string-check', :require => false
21-
gem 'puppet-lint-trailing_comma-check', :require => false
22-
gem 'simp-rspec-puppet-facts', ENV['SIMP_RSPEC_PUPPET_FACTS_VERSION'] || '~> 3.1'
23-
gem 'simp-rake-helpers', ENV['SIMP_RAKE_HELPERS_VERSION'] || ['>= 5.17.1', '< 6']
24-
gem( 'pdk', ENV['PDK_VERSION'] || '~> 2.0', :require => false) if major_puppet_version > 5
25-
gem 'pathspec', '~> 0.2' if Gem::Requirement.create('< 2.6').satisfied_by?(Gem::Version.new(RUBY_VERSION.dup))
26-
gem 'simp-build-helpers', ENV['SIMP_BUILD_HELPERS_VERSION'] || ['> 0.1', '< 2.0']
27-
22+
gem 'simp-build-helpers', ENV.fetch('SIMP_BUILD_HELPERS_VERSION', ['> 0.1', '< 2.0'])
23+
gem 'simp-rake-helpers', ENV.fetch('SIMP_RAKE_HELPERS_VERSION', ['>= 5.21.0', '< 6'])
24+
gem 'simp-rspec-puppet-facts', ENV.fetch('SIMP_RSPEC_PUPPET_FACTS_VERSION', '~> 3.7')
25+
gem 'terminal-table'
2826
end
2927

3028
group :development do
@@ -34,20 +32,21 @@ group :development do
3432
end
3533

3634
group :system_tests do
35+
gem 'bcrypt_pbkdf'
3736
gem 'beaker'
3837
gem 'beaker-rspec'
39-
gem 'simp-beaker-helpers', ENV['SIMP_BEAKER_HELPERS_VERSION'] || ['>= 1.23.2', '< 2']
38+
gem 'simp-beaker-helpers', ENV.fetch('SIMP_BEAKER_HELPERS_VERSION', ['>= 1.32.1', '< 2'])
4039
end
4140

4241
# Evaluate extra gemfiles if they exist
4342
extra_gemfiles = [
44-
ENV['EXTRA_GEMFILE'] || '',
43+
ENV.fetch('EXTRA_GEMFILE', ''),
4544
"#{__FILE__}.project",
4645
"#{__FILE__}.local",
4746
File.join(Dir.home, '.gemfile'),
4847
]
4948
extra_gemfiles.each do |gemfile|
5049
if File.file?(gemfile) && File.readable?(gemfile)
51-
eval(File.read(gemfile), binding)
50+
eval(File.read(gemfile), binding) # rubocop:disable Security/Eval
5251
end
5352
end

spec/acceptance/helpers/repo_helper.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module RepoHelper
99
#
1010
# @fails if the specified repo file cannot be installed on host
1111
def copy_repo(host, repo_filename, repo_name = 'simp_manual.repo')
12-
if File.exists?(repo_filename)
12+
if File.exist?(repo_filename)
1313
puts('='*72)
1414
puts("Using repos defined in #{repo_filename}")
1515
puts('='*72)
@@ -131,13 +131,13 @@ def find_simp_release_tarball(relver, osname)
131131
filename = "SIMP-downloaded-#{osname}-#{relver}-x86_64.tar.gz"
132132
url = "#{tarball}"
133133
require 'net/http'
134-
Dir.exists?("spec/fixtures") || Dir.mkdir("spec/fixtures")
134+
Dir.exist?("spec/fixtures") || Dir.mkdir("spec/fixtures")
135135
File.write("spec/fixtures/#{filename}", Net::HTTP.get(URI.parse(url)))
136136
tarball = "spec/fixtures/#{filename}"
137137
puts("Downloaded SIMP release tarball from #{url} to #{tarball}")
138138
else
139139
unless tarball.nil?
140-
if File.exists?(tarball)
140+
if File.exist?(tarball)
141141
puts("Found SIMP release tarball: #{tarball}")
142142
else
143143
warn("SIMP release tarball '#{tarball}' not found")

spec/acceptance/suites/default/90_compliance_enforcement_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@
148148

149149
agents.each do |host|
150150
context "a valid report for #{host}" do
151-
let(:fqdn) { fact_on(host, 'fqdn') }
151+
let(:fqdn) { fact_on(host, 'networking.fqdn') }
152152
let(:host_sec_results_dir) { File.join(sec_results_dir, fqdn) }
153153
let(:report_file) { File.join(host_sec_results_dir, 'compliance_report.json') }
154154

spec/spec_helper.rb

+60-41
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
# frozen_string_literal: true
2+
#
3+
# ------------------------------------------------------------------------------
4+
# NOTICE: **This file is maintained with puppetsync**
5+
#
6+
# This file is automatically updated as part of a puppet module baseline.
7+
# The next baseline sync will overwrite any local changes made to this file.
8+
# ------------------------------------------------------------------------------
9+
110
require 'puppetlabs_spec_helper/module_spec_helper'
211
require 'rspec-puppet'
312
require 'simp/rspec-puppet-facts'
@@ -7,32 +16,31 @@
716

817
# RSpec Material
918
fixture_path = File.expand_path(File.join(__FILE__, '..', 'fixtures'))
10-
module_name = File.basename(File.expand_path(File.join(__FILE__,'../..')))
11-
12-
# Add fixture lib dirs to LOAD_PATH. Work-around for PUP-3336
13-
if Puppet.version < "4.0.0"
14-
Dir["#{fixture_path}/modules/*/lib"].entries.each do |lib_dir|
15-
$LOAD_PATH << lib_dir
16-
end
17-
end
19+
module_name = File.basename(File.expand_path(File.join(__FILE__, '../..')))
1820

19-
20-
if !ENV.key?( 'TRUSTED_NODE_DATA' )
21-
warn '== WARNING: TRUSTED_NODE_DATA is unset, using TRUSTED_NODE_DATA=yes'
22-
ENV['TRUSTED_NODE_DATA']='yes'
21+
if ENV['PUPPET_DEBUG']
22+
Puppet::Util::Log.level = :debug
23+
Puppet::Util::Log.newdestination(:console)
2324
end
2425

25-
default_hiera_config =<<-EOM
26+
default_hiera_config = <<~HIERA_CONFIG
2627
---
27-
:backends:
28-
- "yaml"
29-
:yaml:
30-
:datadir: "stub"
31-
:hierarchy:
32-
- "%{custom_hiera}"
33-
- "%{module_name}"
34-
- "default"
35-
EOM
28+
version: 5
29+
hierarchy:
30+
- name: SIMP Compliance Engine
31+
lookup_key: compliance_markup::enforcement
32+
options:
33+
enabled_sce_versions: [2]
34+
- name: Custom Test Hiera
35+
path: "%{custom_hiera}.yaml"
36+
- name: "%{module_name}"
37+
path: "%{module_name}.yaml"
38+
- name: Common
39+
path: default.yaml
40+
defaults:
41+
data_hash: yaml_data
42+
datadir: "stub"
43+
HIERA_CONFIG
3644

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

73-
if not File.directory?(File.join(fixture_path,'hieradata')) then
74-
FileUtils.mkdir_p(File.join(fixture_path,'hieradata'))
81+
unless File.directory?(File.join(fixture_path, 'hieradata'))
82+
FileUtils.mkdir_p(File.join(fixture_path, 'hieradata'))
7583
end
7684

77-
if not File.directory?(File.join(fixture_path,'modules',module_name)) then
78-
FileUtils.mkdir_p(File.join(fixture_path,'modules',module_name))
85+
unless File.directory?(File.join(fixture_path, 'modules', module_name))
86+
FileUtils.mkdir_p(File.join(fixture_path, 'modules', module_name))
7987
end
8088

8189
RSpec.configure do |c|
8290
# If nothing else...
8391
c.default_facts = {
84-
:production => {
92+
production: {
8593
#:fqdn => 'production.rspec.test.localdomain',
86-
:path => '/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin',
87-
:concat_basedir => '/tmp'
94+
path: '/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin',
95+
concat_basedir: '/tmp'
8896
}
8997
}
9098

9199
c.mock_framework = :rspec
92-
c.mock_with :mocha
100+
c.mock_with :rspec
93101

94102
c.module_path = File.join(fixture_path, 'modules')
95-
c.manifest_dir = File.join(fixture_path, 'manifests')
103+
c.manifest_dir = File.join(fixture_path, 'manifests') if c.respond_to?(:manifest_dir)
96104

97-
c.hiera_config = File.join(fixture_path,'hieradata','hiera.yaml')
105+
c.hiera_config = File.join(fixture_path, 'hieradata', 'hiera.yaml')
98106

99107
# Useless backtrace noise
100108
backtrace_exclusion_patterns = [
101-
/spec_helper/,
102-
/gems/
109+
%r{spec_helper},
110+
%r{gems},
103111
]
104112

105113
if c.respond_to?(:backtrace_exclusion_patterns)
@@ -108,33 +116,44 @@ def set_hieradata(hieradata)
108116
c.backtrace_clean_patterns = backtrace_exclusion_patterns
109117
end
110118

119+
# rubocop:disable RSpec/BeforeAfterAll
111120
c.before(:all) do
112-
data = YAML.load(default_hiera_config)
113-
data[:yaml][:datadir] = File.join(fixture_path, 'hieradata')
121+
data = YAML.safe_load(default_hiera_config)
122+
data.each_key do |key|
123+
next unless data[key].is_a?(Hash)
124+
125+
if data[key][:datadir] == 'stub'
126+
data[key][:datadir] = File.join(fixture_path, 'hieradata')
127+
elsif data[key]['datadir'] == 'stub'
128+
data[key]['datadir'] = File.join(fixture_path, 'hieradata')
129+
end
130+
end
114131

115132
File.open(c.hiera_config, 'w') do |f|
116133
f.write data.to_yaml
117134
end
118135
end
136+
# rubocop:enable RSpec/BeforeAfterAll
119137

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

123141
if defined?(environment)
124142
set_environment(environment)
125-
FileUtils.mkdir_p(File.join(@spec_global_env_temp,environment.to_s))
143+
FileUtils.mkdir_p(File.join(@spec_global_env_temp, environment.to_s))
126144
end
127145

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

133152
# sanitize hieradata
134153
if defined?(hieradata)
135-
set_hieradata(hieradata.gsub(':','_'))
154+
set_hieradata(hieradata.gsub(':', '_'))
136155
elsif defined?(class_name)
137-
set_hieradata(class_name.gsub(':','_'))
156+
set_hieradata(class_name.gsub(':', '_'))
138157
end
139158
end
140159

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

0 commit comments

Comments
 (0)