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
- - "rspec"
29
- - "yaml"
30
- :yaml:
31
- :datadir: "stub"
32
- :hierarchy:
33
- - "%{custom_hiera}"
34
- - "%{spec_title}"
35
- - "%{module_name}"
36
- - "default"
37
- EOM
28
+ version: 5
29
+ hierarchy:
30
+ - name: Custom Test Hiera
31
+ path: "%{custom_hiera}.yaml"
32
+ - name: "%{module_name}"
33
+ path: "%{module_name}.yaml"
34
+ - name: Common
35
+ path: default.yaml
36
+ defaults:
37
+ data_hash: yaml_data
38
+ datadir: "stub"
39
+ HIERA_CONFIG
38
40
39
41
# This can be used from inside your spec tests to set the testable environment.
40
42
# You can use this to stub out an ENC.
47
49
# end
48
50
#
49
51
def set_environment ( environment = :production )
50
- RSpec . configure { |c | c . default_facts [ 'environment' ] = environment . to_s }
52
+ RSpec . configure { |c | c . default_facts [ 'environment' ] = environment . to_s }
51
53
end
52
54
53
55
# This can be used from inside your spec tests to load custom hieradata within
@@ -69,39 +71,39 @@ def set_environment(environment = :production)
69
71
#
70
72
# Note: Any colons (:) are replaced with underscores (_) in the class name.
71
73
def set_hieradata ( hieradata )
72
- RSpec . configure { |c | c . default_facts [ 'custom_hiera' ] = hieradata }
74
+ RSpec . configure { |c | c . default_facts [ 'custom_hiera' ] = hieradata }
73
75
end
74
76
75
- if not File . directory? ( File . join ( fixture_path , 'hieradata' ) ) then
76
- FileUtils . mkdir_p ( File . join ( fixture_path , 'hieradata' ) )
77
+ unless File . directory? ( File . join ( fixture_path , 'hieradata' ) )
78
+ FileUtils . mkdir_p ( File . join ( fixture_path , 'hieradata' ) )
77
79
end
78
80
79
- if not File . directory? ( File . join ( fixture_path , 'modules' , module_name ) ) then
80
- FileUtils . mkdir_p ( File . join ( fixture_path , 'modules' , module_name ) )
81
+ unless File . directory? ( File . join ( fixture_path , 'modules' , module_name ) )
82
+ FileUtils . mkdir_p ( File . join ( fixture_path , 'modules' , module_name ) )
81
83
end
82
84
83
85
RSpec . configure do |c |
84
86
# If nothing else...
85
87
c . default_facts = {
86
- : production => {
88
+ production : {
87
89
#:fqdn => 'production.rspec.test.localdomain',
88
- : path => '/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin' ,
89
- : concat_basedir => '/tmp'
90
+ path : '/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin' ,
91
+ concat_basedir : '/tmp'
90
92
}
91
93
}
92
94
93
95
c . mock_framework = :rspec
94
- c . mock_with :mocha
96
+ c . mock_with :rspec
95
97
96
98
c . module_path = File . join ( fixture_path , 'modules' )
97
99
c . manifest_dir = File . join ( fixture_path , 'manifests' ) if c . respond_to? ( :manifest_dir )
98
100
99
- c . hiera_config = File . join ( fixture_path , 'hieradata' , 'hiera.yaml' )
101
+ c . hiera_config = File . join ( fixture_path , 'hieradata' , 'hiera.yaml' )
100
102
101
103
# Useless backtrace noise
102
104
backtrace_exclusion_patterns = [
103
- / spec_helper/ ,
104
- / gems/
105
+ %r{ spec_helper} ,
106
+ %r{ gems} ,
105
107
]
106
108
107
109
if c . respond_to? ( :backtrace_exclusion_patterns )
@@ -110,21 +112,31 @@ def set_hieradata(hieradata)
110
112
c . backtrace_clean_patterns = backtrace_exclusion_patterns
111
113
end
112
114
115
+ # rubocop:disable RSpec/BeforeAfterAll
113
116
c . before ( :all ) do
114
- data = YAML . load ( default_hiera_config )
115
- data [ :yaml ] [ :datadir ] = File . join ( fixture_path , 'hieradata' )
117
+ data = YAML . safe_load ( default_hiera_config )
118
+ data . each_key do |key |
119
+ next unless data [ key ] . is_a? ( Hash )
120
+
121
+ if data [ key ] [ :datadir ] == 'stub'
122
+ data [ key ] [ :datadir ] = File . join ( fixture_path , 'hieradata' )
123
+ elsif data [ key ] [ 'datadir' ] == 'stub'
124
+ data [ key ] [ 'datadir' ] = File . join ( fixture_path , 'hieradata' )
125
+ end
126
+ end
116
127
117
128
File . open ( c . hiera_config , 'w' ) do |f |
118
129
f . write data . to_yaml
119
130
end
120
131
end
132
+ # rubocop:enable RSpec/BeforeAfterAll
121
133
122
134
c . before ( :each ) do
123
135
@spec_global_env_temp = Dir . mktmpdir ( 'simpspec' )
124
136
125
137
if defined? ( environment )
126
138
set_environment ( environment )
127
- FileUtils . mkdir_p ( File . join ( @spec_global_env_temp , environment . to_s ) )
139
+ FileUtils . mkdir_p ( File . join ( @spec_global_env_temp , environment . to_s ) )
128
140
end
129
141
130
142
# ensure the user running these tests has an accessible environmentpath
@@ -135,9 +147,9 @@ def set_hieradata(hieradata)
135
147
136
148
# sanitize hieradata
137
149
if defined? ( hieradata )
138
- set_hieradata ( hieradata . gsub ( ':' , '_' ) )
150
+ set_hieradata ( hieradata . gsub ( ':' , '_' ) )
139
151
elsif defined? ( class_name )
140
- set_hieradata ( class_name . gsub ( ':' , '_' ) )
152
+ set_hieradata ( class_name . gsub ( ':' , '_' ) )
141
153
end
142
154
end
143
155
@@ -151,7 +163,7 @@ def set_hieradata(hieradata)
151
163
Dir . glob ( "#{ RSpec . configuration . module_path } /*" ) . each do |dir |
152
164
begin
153
165
Pathname . new ( dir ) . realpath
154
- rescue
155
- fail "ERROR: The module '#{ dir } ' is not installed. Tests cannot continue."
166
+ rescue StandardError
167
+ raise "ERROR: The module '#{ dir } ' is not installed. Tests cannot continue."
156
168
end
157
169
end
0 commit comments