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+
110require 'puppetlabs_spec_helper/module_spec_helper'
211require 'rspec-puppet'
312require 'simp/rspec-puppet-facts'
716
817# RSpec Material
918fixture_path = File . expand_path ( File . join ( __FILE__ , '..' , 'fixtures' ) )
10- module_name = File . basename ( File . expand_path ( File . join ( __FILE__ , '../..' ) ) )
19+ module_name = File . basename ( File . expand_path ( File . join ( __FILE__ , '../..' ) ) )
1120
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
21+ if ENV [ 'PUPPET_DEBUG' ]
22+ Puppet ::Util ::Log . level = :debug
23+ Puppet ::Util ::Log . newdestination ( :console )
1724end
1825
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'
23- end
24-
25- default_hiera_config = <<-EOM
26+ default_hiera_config = <<~HIERA_CONFIG
2627---
2728version: 5
2829hierarchy:
3940defaults:
4041 data_hash: yaml_data
4142 datadir: "stub"
42- EOM
43+ HIERA_CONFIG
4344
4445# This can be used from inside your spec tests to set the testable environment.
4546# You can use this to stub out an ENC.
@@ -77,35 +78,36 @@ def set_hieradata(hieradata)
7778 RSpec . configure { |c | c . default_facts [ 'custom_hiera' ] = hieradata }
7879end
7980
80- if not File . directory? ( File . join ( fixture_path , 'hieradata' ) ) then
81- 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' ) )
8283end
8384
84- if not File . directory? ( File . join ( fixture_path , 'modules' , module_name ) ) then
85- 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 ) )
8687end
8788
8889RSpec . configure do |c |
8990 # If nothing else...
9091 c . default_facts = {
91- : production => {
92+ production : {
9293 #:fqdn => 'production.rspec.test.localdomain',
93- : path => '/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin' ,
94- : concat_basedir => '/tmp'
94+ path : '/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin' ,
95+ concat_basedir : '/tmp'
9596 }
9697 }
9798
9899 c . mock_framework = :rspec
100+ c . mock_with :rspec
99101
100102 c . module_path = File . join ( fixture_path , 'modules' )
101103 c . manifest_dir = File . join ( fixture_path , 'manifests' ) if c . respond_to? ( :manifest_dir )
102104
103- c . hiera_config = File . join ( fixture_path , 'hieradata' , 'hiera.yaml' )
105+ c . hiera_config = File . join ( fixture_path , 'hieradata' , 'hiera.yaml' )
104106
105107 # Useless backtrace noise
106108 backtrace_exclusion_patterns = [
107- / spec_helper/ ,
108- / gems/
109+ %r{ spec_helper} ,
110+ %r{ gems} ,
109111 ]
110112
111113 if c . respond_to? ( :backtrace_exclusion_patterns )
@@ -114,9 +116,10 @@ def set_hieradata(hieradata)
114116 c . backtrace_clean_patterns = backtrace_exclusion_patterns
115117 end
116118
119+ # rubocop:disable RSpec/BeforeAfterAll
117120 c . before ( :all ) do
118- data = YAML . load ( default_hiera_config )
119- data . keys . each do |key |
121+ data = YAML . safe_load ( default_hiera_config )
122+ data . each_key do |key |
120123 next unless data [ key ] . is_a? ( Hash )
121124
122125 if data [ key ] [ :datadir ] == 'stub'
@@ -130,26 +133,27 @@ def set_hieradata(hieradata)
130133 f . write data . to_yaml
131134 end
132135 end
136+ # rubocop:enable RSpec/BeforeAfterAll
133137
134138 c . before ( :each ) do
135139 @spec_global_env_temp = Dir . mktmpdir ( 'simpspec' )
136140
137141 if defined? ( environment )
138142 set_environment ( environment )
139- 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 ) )
140144 end
141145
142146 # ensure the user running these tests has an accessible environmentpath
147+ Puppet [ :digest_algorithm ] = 'sha256'
143148 Puppet [ :environmentpath ] = @spec_global_env_temp
144149 Puppet [ :user ] = Etc . getpwuid ( Process . uid ) . name
145150 Puppet [ :group ] = Etc . getgrgid ( Process . gid ) . name
146- Puppet [ :digest_algorithm ] = 'sha256'
147151
148152 # sanitize hieradata
149153 if defined? ( hieradata )
150- set_hieradata ( hieradata . gsub ( ':' , '_' ) )
154+ set_hieradata ( hieradata . gsub ( ':' , '_' ) )
151155 elsif defined? ( class_name )
152- set_hieradata ( class_name . gsub ( ':' , '_' ) )
156+ set_hieradata ( class_name . gsub ( ':' , '_' ) )
153157 end
154158 end
155159
@@ -163,7 +167,7 @@ def set_hieradata(hieradata)
163167Dir . glob ( "#{ RSpec . configuration . module_path } /*" ) . each do |dir |
164168 begin
165169 Pathname . new ( dir ) . realpath
166- rescue
167- 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."
168172 end
169173end
0 commit comments