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
- # 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 )
17
24
end
18
25
19
- default_hiera_config = <<-EOM
26
+ default_hiera_config = <<~HIERA_CONFIG
20
27
---
21
- :backends:
22
- - "yaml"
23
- :yaml:
24
- :datadir: "stub"
25
- :hierarchy:
26
- - "%{custom_hiera}"
27
- - "%{module_name}"
28
- - "default"
29
- 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
30
40
31
41
# This can be used from inside your spec tests to set the testable environment.
32
42
# You can use this to stub out an ENC.
@@ -64,36 +74,36 @@ def set_hieradata(hieradata)
64
74
RSpec . configure { |c | c . default_facts [ 'custom_hiera' ] = hieradata }
65
75
end
66
76
67
- if not File . directory? ( File . join ( fixture_path , 'hieradata' ) ) then
68
- 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' ) )
69
79
end
70
80
71
- if not File . directory? ( File . join ( fixture_path , 'modules' , module_name ) ) then
72
- 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 ) )
73
83
end
74
84
75
85
RSpec . configure do |c |
76
86
# If nothing else...
77
87
c . default_facts = {
78
- : production => {
88
+ production : {
79
89
#:fqdn => 'production.rspec.test.localdomain',
80
- : path => '/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin' ,
81
- : concat_basedir => '/tmp'
90
+ path : '/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin' ,
91
+ concat_basedir : '/tmp'
82
92
}
83
93
}
84
94
85
95
c . mock_framework = :rspec
86
- c . mock_with :mocha
96
+ c . mock_with :rspec
87
97
88
98
c . module_path = File . join ( fixture_path , 'modules' )
89
99
c . manifest_dir = File . join ( fixture_path , 'manifests' ) if c . respond_to? ( :manifest_dir )
90
100
91
- c . hiera_config = File . join ( fixture_path , 'hieradata' , 'hiera.yaml' )
101
+ c . hiera_config = File . join ( fixture_path , 'hieradata' , 'hiera.yaml' )
92
102
93
103
# Useless backtrace noise
94
104
backtrace_exclusion_patterns = [
95
- / spec_helper/ ,
96
- / gems/
105
+ %r{ spec_helper} ,
106
+ %r{ gems} ,
97
107
]
98
108
99
109
if c . respond_to? ( :backtrace_exclusion_patterns )
@@ -102,33 +112,44 @@ def set_hieradata(hieradata)
102
112
c . backtrace_clean_patterns = backtrace_exclusion_patterns
103
113
end
104
114
115
+ # rubocop:disable RSpec/BeforeAfterAll
105
116
c . before ( :all ) do
106
- data = YAML . load ( default_hiera_config )
107
- 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
108
127
109
128
File . open ( c . hiera_config , 'w' ) do |f |
110
129
f . write data . to_yaml
111
130
end
112
131
end
132
+ # rubocop:enable RSpec/BeforeAfterAll
113
133
114
134
c . before ( :each ) do
115
135
@spec_global_env_temp = Dir . mktmpdir ( 'simpspec' )
116
136
117
137
if defined? ( environment )
118
138
set_environment ( environment )
119
- 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 ) )
120
140
end
121
141
122
142
# ensure the user running these tests has an accessible environmentpath
143
+ Puppet [ :digest_algorithm ] = 'sha256'
123
144
Puppet [ :environmentpath ] = @spec_global_env_temp
124
145
Puppet [ :user ] = Etc . getpwuid ( Process . uid ) . name
125
146
Puppet [ :group ] = Etc . getgrgid ( Process . gid ) . name
126
147
127
148
# sanitize hieradata
128
149
if defined? ( hieradata )
129
- set_hieradata ( hieradata . gsub ( ':' , '_' ) )
150
+ set_hieradata ( hieradata . gsub ( ':' , '_' ) )
130
151
elsif defined? ( class_name )
131
- set_hieradata ( class_name . gsub ( ':' , '_' ) )
152
+ set_hieradata ( class_name . gsub ( ':' , '_' ) )
132
153
end
133
154
end
134
155
@@ -142,7 +163,7 @@ def set_hieradata(hieradata)
142
163
Dir . glob ( "#{ RSpec . configuration . module_path } /*" ) . each do |dir |
143
164
begin
144
165
Pathname . new ( dir ) . realpath
145
- rescue
146
- 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."
147
168
end
148
169
end
0 commit comments