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
+
1
10
require 'puppetlabs_spec_helper/module_spec_helper'
2
11
require 'rspec-puppet'
3
12
require 'simp/rspec-puppet-facts'
7
16
8
17
# RSpec Material
9
18
fixture_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__ , '../..' ) ) )
11
20
12
- if ! ENV . key? ( 'TRUSTED_NODE_DATA' )
13
- warn '== WARNING: TRUSTED_NODE_DATA is unset, using TRUSTED_NODE_DATA=yes'
14
- ENV [ 'TRUSTED_NODE_DATA' ] = 'yes'
21
+ if ENV [ 'PUPPET_DEBUG' ]
22
+ Puppet :: Util :: Log . level = :debug
23
+ Puppet :: Util :: Log . newdestination ( :console )
15
24
end
16
25
17
- default_hiera_config = <<-EOM
26
+ default_hiera_config = <<~HIERA_CONFIG
18
27
---
19
28
version: 5
20
29
hierarchy:
21
30
- name: SIMP Compliance Engine
22
31
lookup_key: compliance_markup::enforcement
32
+ options:
33
+ enabled_sce_versions: [2]
23
34
- name: Custom Test Hiera
24
35
path: "%{custom_hiera}.yaml"
25
36
- name: "%{module_name}"
29
40
defaults:
30
41
data_hash: yaml_data
31
42
datadir: "stub"
32
- EOM
43
+ HIERA_CONFIG
33
44
34
45
# This can be used from inside your spec tests to set the testable environment.
35
46
# You can use this to stub out an ENC.
@@ -67,38 +78,36 @@ def set_hieradata(hieradata)
67
78
RSpec . configure { |c | c . default_facts [ 'custom_hiera' ] = hieradata }
68
79
end
69
80
70
- if not File . directory? ( File . join ( fixture_path , 'hieradata' ) ) then
71
- 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' ) )
72
83
end
73
84
74
- if not File . directory? ( File . join ( fixture_path , 'modules' , module_name ) ) then
75
- 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 ) )
76
87
end
77
88
78
89
RSpec . configure do |c |
79
90
# If nothing else...
80
91
c . default_facts = {
81
- : production => {
92
+ production : {
82
93
#:fqdn => 'production.rspec.test.localdomain',
83
- : path => '/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin' ,
84
- : concat_basedir => '/tmp'
94
+ path : '/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin' ,
95
+ concat_basedir : '/tmp'
85
96
}
86
97
}
87
98
88
- c . trusted_server_facts = true if c . respond_to? ( :trusted_server_facts )
89
-
90
99
c . mock_framework = :rspec
91
- c . mock_with :mocha
100
+ c . mock_with :rspec
92
101
93
102
c . module_path = File . join ( fixture_path , 'modules' )
94
103
c . manifest_dir = File . join ( fixture_path , 'manifests' ) if c . respond_to? ( :manifest_dir )
95
104
96
- c . hiera_config = File . join ( fixture_path , 'hieradata' , 'hiera.yaml' )
105
+ c . hiera_config = File . join ( fixture_path , 'hieradata' , 'hiera.yaml' )
97
106
98
107
# Useless backtrace noise
99
108
backtrace_exclusion_patterns = [
100
- / spec_helper/ ,
101
- / gems/
109
+ %r{ spec_helper} ,
110
+ %r{ gems} ,
102
111
]
103
112
104
113
if c . respond_to? ( :backtrace_exclusion_patterns )
@@ -107,9 +116,10 @@ def set_hieradata(hieradata)
107
116
c . backtrace_clean_patterns = backtrace_exclusion_patterns
108
117
end
109
118
119
+ # rubocop:disable RSpec/BeforeAfterAll
110
120
c . before ( :all ) do
111
- data = YAML . load ( default_hiera_config )
112
- data . keys . each do |key |
121
+ data = YAML . safe_load ( default_hiera_config )
122
+ data . each_key do |key |
113
123
next unless data [ key ] . is_a? ( Hash )
114
124
115
125
if data [ key ] [ :datadir ] == 'stub'
@@ -123,25 +133,27 @@ def set_hieradata(hieradata)
123
133
f . write data . to_yaml
124
134
end
125
135
end
136
+ # rubocop:enable RSpec/BeforeAfterAll
126
137
127
138
c . before ( :each ) do
128
139
@spec_global_env_temp = Dir . mktmpdir ( 'simpspec' )
129
140
130
141
if defined? ( environment )
131
142
set_environment ( environment )
132
- 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 ) )
133
144
end
134
145
135
146
# ensure the user running these tests has an accessible environmentpath
147
+ Puppet [ :digest_algorithm ] = 'sha256'
136
148
Puppet [ :environmentpath ] = @spec_global_env_temp
137
149
Puppet [ :user ] = Etc . getpwuid ( Process . uid ) . name
138
150
Puppet [ :group ] = Etc . getgrgid ( Process . gid ) . name
139
151
140
152
# sanitize hieradata
141
153
if defined? ( hieradata )
142
- set_hieradata ( hieradata . gsub ( ':' , '_' ) )
154
+ set_hieradata ( hieradata . gsub ( ':' , '_' ) )
143
155
elsif defined? ( class_name )
144
- set_hieradata ( class_name . gsub ( ':' , '_' ) )
156
+ set_hieradata ( class_name . gsub ( ':' , '_' ) )
145
157
end
146
158
end
147
159
@@ -150,34 +162,12 @@ def set_hieradata(hieradata)
150
162
FileUtils . rm_rf ( @spec_global_env_temp )
151
163
@spec_global_env_temp = nil
152
164
end
153
-
154
- if ENV [ 'RSPEC_TIME' ]
155
- c . before ( :all ) do
156
- @suite_start_time = Time . now
157
- end
158
-
159
- c . before ( :context ) do
160
- @context_start_time = Time . now
161
- end
162
-
163
- c . before ( :example ) do
164
- @example_start_time = Time . now
165
- end
166
-
167
- c . after ( :all ) do
168
- puts ( "TIME FOR SUITE '#{ self . class . description } ': #{ Time . now - @suite_start_time } " )
169
- end
170
-
171
- c . after ( :context ) do
172
- puts ( "TIME FOR CONTEXT '#{ self . class . description } ': #{ Time . now - @context_start_time } " )
173
- end
174
- end
175
165
end
176
166
177
167
Dir . glob ( "#{ RSpec . configuration . module_path } /*" ) . each do |dir |
178
168
begin
179
169
Pathname . new ( dir ) . realpath
180
- rescue
181
- 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."
182
172
end
183
173
end
0 commit comments