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__ , '../..' ) ) )
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__ , '../..' ) ) )
18
20
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 )
23
24
end
24
25
25
- default_hiera_config = <<-EOM
26
+ default_hiera_config = <<~HIERA_CONFIG
26
27
---
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
36
44
37
45
# This can be used from inside your spec tests to set the testable environment.
38
46
# You can use this to stub out an ENC.
@@ -70,36 +78,36 @@ def set_hieradata(hieradata)
70
78
RSpec . configure { |c | c . default_facts [ 'custom_hiera' ] = hieradata }
71
79
end
72
80
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' ) )
75
83
end
76
84
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 ) )
79
87
end
80
88
81
89
RSpec . configure do |c |
82
90
# If nothing else...
83
91
c . default_facts = {
84
- : production => {
92
+ production : {
85
93
#: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'
88
96
}
89
97
}
90
98
91
99
c . mock_framework = :rspec
92
- c . mock_with :mocha
100
+ c . mock_with :rspec
93
101
94
102
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 )
96
104
97
- c . hiera_config = File . join ( fixture_path , 'hieradata' , 'hiera.yaml' )
105
+ c . hiera_config = File . join ( fixture_path , 'hieradata' , 'hiera.yaml' )
98
106
99
107
# Useless backtrace noise
100
108
backtrace_exclusion_patterns = [
101
- / spec_helper/ ,
102
- / gems/
109
+ %r{ spec_helper} ,
110
+ %r{ gems} ,
103
111
]
104
112
105
113
if c . respond_to? ( :backtrace_exclusion_patterns )
@@ -108,33 +116,44 @@ def set_hieradata(hieradata)
108
116
c . backtrace_clean_patterns = backtrace_exclusion_patterns
109
117
end
110
118
119
+ # rubocop:disable RSpec/BeforeAfterAll
111
120
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
114
131
115
132
File . open ( c . hiera_config , 'w' ) do |f |
116
133
f . write data . to_yaml
117
134
end
118
135
end
136
+ # rubocop:enable RSpec/BeforeAfterAll
119
137
120
138
c . before ( :each ) do
121
139
@spec_global_env_temp = Dir . mktmpdir ( 'simpspec' )
122
140
123
141
if defined? ( environment )
124
142
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 ) )
126
144
end
127
145
128
146
# ensure the user running these tests has an accessible environmentpath
147
+ Puppet [ :digest_algorithm ] = 'sha256'
129
148
Puppet [ :environmentpath ] = @spec_global_env_temp
130
149
Puppet [ :user ] = Etc . getpwuid ( Process . uid ) . name
131
150
Puppet [ :group ] = Etc . getgrgid ( Process . gid ) . name
132
151
133
152
# sanitize hieradata
134
153
if defined? ( hieradata )
135
- set_hieradata ( hieradata . gsub ( ':' , '_' ) )
154
+ set_hieradata ( hieradata . gsub ( ':' , '_' ) )
136
155
elsif defined? ( class_name )
137
- set_hieradata ( class_name . gsub ( ':' , '_' ) )
156
+ set_hieradata ( class_name . gsub ( ':' , '_' ) )
138
157
end
139
158
end
140
159
@@ -148,7 +167,7 @@ def set_hieradata(hieradata)
148
167
Dir . glob ( "#{ RSpec . configuration . module_path } /*" ) . each do |dir |
149
168
begin
150
169
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."
153
172
end
154
173
end
0 commit comments