Skip to content

Commit

Permalink
[puppetsync] Don't ignore Puppet 8 failures
Browse files Browse the repository at this point in the history
Puppet 8 failures should not be ignored.

Also
* Use the correct Ruby version for Puppet 8 tests
* Manage spec/spec_helper.rb in Puppet modules
  • Loading branch information
silug committed Jul 3, 2024
1 parent af3cc3c commit cbf1c01
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 40 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/pr_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ jobs:
experimental: false
- label: 'Puppet 8.x'
puppet_version: '~> 8.0'
ruby_version: 3.1
experimental: true
ruby_version: '3.2'
experimental: false
fail-fast: false
env:
PUPPET_VERSION: ${{matrix.puppet.puppet_version}}
Expand Down
75 changes: 37 additions & 38 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,29 +16,17 @@

# RSpec Material
fixture_path = File.expand_path(File.join(__FILE__, '..', 'fixtures'))
module_name = File.basename(File.expand_path(File.join(__FILE__,'../..')))
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
if ENV['PUPPET_DEBUG']
Puppet::Util::Log.level = :debug
Puppet::Util::Log.newdestination(:console)
end


if !ENV.key?( 'TRUSTED_NODE_DATA' )
warn '== WARNING: TRUSTED_NODE_DATA is unset, using TRUSTED_NODE_DATA=yes'
ENV['TRUSTED_NODE_DATA']='yes'
end

default_hiera_config =<<-EOM
default_hiera_config = <<~HIERA_CONFIG
---
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}"
Expand All @@ -39,7 +36,7 @@
defaults:
data_hash: yaml_data
datadir: "stub"
EOM
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 All @@ -52,7 +49,7 @@
# end
#
def set_environment(environment = :production)
RSpec.configure { |c| c.default_facts['environment'] = environment.to_s }
RSpec.configure { |c| c.default_facts['environment'] = environment.to_s }
end

# This can be used from inside your spec tests to load custom hieradata within
Expand All @@ -74,24 +71,24 @@ def set_environment(environment = :production)
#
# Note: Any colons (:) are replaced with underscores (_) in the class name.
def set_hieradata(hieradata)
RSpec.configure { |c| c.default_facts['custom_hiera'] = 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'
}
}

Expand All @@ -101,12 +98,12 @@ def set_hieradata(hieradata)
c.module_path = File.join(fixture_path, 'modules')
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 @@ -115,9 +112,10 @@ 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.keys.each do |key|
data = YAML.safe_load(default_hiera_config)
data.each_key do |key|
next unless data[key].is_a?(Hash)

if data[key][:datadir] == 'stub'
Expand All @@ -131,26 +129,27 @@ def set_hieradata(hieradata)
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
Puppet[:digest_algorithm] = 'sha256'

# 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 @@ -164,7 +163,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 cbf1c01

Please sign in to comment.