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'
12
+ require 'super_diff/rspec'
3
13
require 'simp/rspec-puppet-facts'
4
14
include Simp ::RspecPuppetFacts
5
15
6
16
require 'pathname'
7
17
8
18
# RSpec Material
9
19
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
20
+ module_name = File . basename ( File . expand_path ( File . join ( __FILE__ , '../..' ) ) )
18
21
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'
22
+ if ENV [ 'PUPPET_DEBUG' ]
23
+ Puppet ::Util ::Log . level = :debug
24
+ Puppet ::Util ::Log . newdestination ( :console )
23
25
end
24
26
25
- default_hiera_config = <<-EOM
27
+ default_hiera_config = <<~HIERA_CONFIG
26
28
---
27
- :backends:
28
- - "rspec"
29
- - "yaml"
30
- :yaml:
31
- :datadir: "stub"
32
- :hierarchy:
33
- - "%{custom_hiera}"
34
- - "%{spec_title}"
35
- - "%{module_name}"
36
- - "default"
37
- EOM
29
+ version: 5
30
+ hierarchy:
31
+ - name: SIMP Compliance Engine
32
+ lookup_key: compliance_markup::enforcement
33
+ options:
34
+ enabled_sce_versions: [2]
35
+ - name: Custom Test Hiera
36
+ path: "%{custom_hiera}.yaml"
37
+ - name: "%{module_name}"
38
+ path: "%{module_name}.yaml"
39
+ - name: Common
40
+ path: default.yaml
41
+ defaults:
42
+ data_hash: yaml_data
43
+ datadir: "stub"
44
+ HIERA_CONFIG
38
45
39
46
# This can be used from inside your spec tests to set the testable environment.
40
47
# You can use this to stub out an ENC.
47
54
# end
48
55
#
49
56
def set_environment ( environment = :production )
50
- RSpec . configure { |c | c . default_facts [ 'environment' ] = environment . to_s }
57
+ RSpec . configure { |c | c . default_facts [ 'environment' ] = environment . to_s }
51
58
end
52
59
53
60
# This can be used from inside your spec tests to load custom hieradata within
@@ -69,39 +76,39 @@ def set_environment(environment = :production)
69
76
#
70
77
# Note: Any colons (:) are replaced with underscores (_) in the class name.
71
78
def set_hieradata ( hieradata )
72
- RSpec . configure { |c | c . default_facts [ 'custom_hiera' ] = hieradata }
79
+ RSpec . configure { |c | c . default_facts [ 'custom_hiera' ] = hieradata }
73
80
end
74
81
75
- if not File . directory? ( File . join ( fixture_path , 'hieradata' ) ) then
76
- FileUtils . mkdir_p ( File . join ( fixture_path , 'hieradata' ) )
82
+ unless File . directory? ( File . join ( fixture_path , 'hieradata' ) )
83
+ FileUtils . mkdir_p ( File . join ( fixture_path , 'hieradata' ) )
77
84
end
78
85
79
- if not File . directory? ( File . join ( fixture_path , 'modules' , module_name ) ) then
80
- FileUtils . mkdir_p ( File . join ( fixture_path , 'modules' , module_name ) )
86
+ unless File . directory? ( File . join ( fixture_path , 'modules' , module_name ) )
87
+ FileUtils . mkdir_p ( File . join ( fixture_path , 'modules' , module_name ) )
81
88
end
82
89
83
90
RSpec . configure do |c |
84
91
# If nothing else...
85
92
c . default_facts = {
86
- : production => {
93
+ production : {
87
94
#:fqdn => 'production.rspec.test.localdomain',
88
- : path => '/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin' ,
89
- : concat_basedir => '/tmp'
95
+ path : '/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin' ,
96
+ concat_basedir : '/tmp'
90
97
}
91
98
}
92
99
93
100
c . mock_framework = :rspec
94
- c . mock_with :mocha
101
+ c . mock_with :rspec
95
102
96
103
c . module_path = File . join ( fixture_path , 'modules' )
97
104
c . manifest_dir = File . join ( fixture_path , 'manifests' ) if c . respond_to? ( :manifest_dir )
98
105
99
- c . hiera_config = File . join ( fixture_path , 'hieradata' , 'hiera.yaml' )
106
+ c . hiera_config = File . join ( fixture_path , 'hieradata' , 'hiera.yaml' )
100
107
101
108
# Useless backtrace noise
102
109
backtrace_exclusion_patterns = [
103
- / spec_helper/ ,
104
- / gems/
110
+ %r{ spec_helper} ,
111
+ %r{ gems} ,
105
112
]
106
113
107
114
if c . respond_to? ( :backtrace_exclusion_patterns )
@@ -110,21 +117,31 @@ def set_hieradata(hieradata)
110
117
c . backtrace_clean_patterns = backtrace_exclusion_patterns
111
118
end
112
119
120
+ # rubocop:disable RSpec/BeforeAfterAll
113
121
c . before ( :all ) do
114
- data = YAML . load ( default_hiera_config )
115
- data [ :yaml ] [ :datadir ] = File . join ( fixture_path , 'hieradata' )
122
+ data = YAML . safe_load ( default_hiera_config )
123
+ data . each_key do |key |
124
+ next unless data [ key ] . is_a? ( Hash )
125
+
126
+ if data [ key ] [ :datadir ] == 'stub'
127
+ data [ key ] [ :datadir ] = File . join ( fixture_path , 'hieradata' )
128
+ elsif data [ key ] [ 'datadir' ] == 'stub'
129
+ data [ key ] [ 'datadir' ] = File . join ( fixture_path , 'hieradata' )
130
+ end
131
+ end
116
132
117
133
File . open ( c . hiera_config , 'w' ) do |f |
118
134
f . write data . to_yaml
119
135
end
120
136
end
137
+ # rubocop:enable RSpec/BeforeAfterAll
121
138
122
139
c . before ( :each ) do
123
140
@spec_global_env_temp = Dir . mktmpdir ( 'simpspec' )
124
141
125
142
if defined? ( environment )
126
143
set_environment ( environment )
127
- FileUtils . mkdir_p ( File . join ( @spec_global_env_temp , environment . to_s ) )
144
+ FileUtils . mkdir_p ( File . join ( @spec_global_env_temp , environment . to_s ) )
128
145
end
129
146
130
147
# ensure the user running these tests has an accessible environmentpath
@@ -135,9 +152,9 @@ def set_hieradata(hieradata)
135
152
136
153
# sanitize hieradata
137
154
if defined? ( hieradata )
138
- set_hieradata ( hieradata . gsub ( ':' , '_' ) )
155
+ set_hieradata ( hieradata . gsub ( ':' , '_' ) )
139
156
elsif defined? ( class_name )
140
- set_hieradata ( class_name . gsub ( ':' , '_' ) )
157
+ set_hieradata ( class_name . gsub ( ':' , '_' ) )
141
158
end
142
159
end
143
160
@@ -151,7 +168,7 @@ def set_hieradata(hieradata)
151
168
Dir . glob ( "#{ RSpec . configuration . module_path } /*" ) . each do |dir |
152
169
begin
153
170
Pathname . new ( dir ) . realpath
154
- rescue
155
- fail "ERROR: The module '#{ dir } ' is not installed. Tests cannot continue."
171
+ rescue StandardError
172
+ raise "ERROR: The module '#{ dir } ' is not installed. Tests cannot continue."
156
173
end
157
174
end
0 commit comments