-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathspec_helper.rb
129 lines (109 loc) · 3.62 KB
/
spec_helper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
require 'rspec-puppet'
require 'simp/rspec-puppet-facts'
include Simp::RspecPuppetFacts
require 'pathname'
# RSpec Material
fixture_path = File.expand_path(File.join(__FILE__, '..', 'fixtures'))
module_name = File.basename(File.expand_path(File.join(__FILE__,'../..')))
# This can be used from inside your spec tests to set the testable environment.
# You can use this to stub out an ENC.
#
# Example:
#
# context 'in the :foo environment' do
# let(:environment){:foo}
# ...
# end
#
def set_environment(environment = :production)
RSpec.configure { |c| c.default_facts['environment'] = environment.to_s }
end
# This can be used from inside your spec tests to load custom hieradata within
# any context.
#
# Example:
#
# describe 'some::class' do
# context 'with version 10' do
# let(:hieradata){ "#{class_name}_v10" }
# ...
# end
# end
#
# Then, create a YAML file at spec/fixtures/hieradata/some__class_v10.yaml.
#
# Hiera will use this file as it's base of information stacked on top of
# 'default.yaml' and <module_name>.yaml per the defaults above.
#
# Note: Any colons (:) are replaced with underscores (_) in the class name.
def set_hieradata(hieradata)
RSpec.configure { |c| c.default_facts['custom_hiera'] = hieradata }
end
if not File.directory?(File.join(fixture_path,'hieradata')) then
FileUtils.mkdir_p(File.join(fixture_path,'hieradata'))
end
if not File.directory?(File.join(fixture_path,'modules',module_name)) then
FileUtils.mkdir_p(File.join(fixture_path,'modules',module_name))
end
RSpec.configure do |c|
# If nothing else...
c.default_facts = {
:production => {
#:fqdn => 'production.rspec.test.localdomain',
:path => '/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin',
:concat_basedir => '/tmp',
}
}
c.mock_framework = :rspec
c.mock_with :rspec
c.module_path = File.join(fixture_path, 'modules')
c.manifest_dir = File.join(fixture_path, 'manifests')
c.hiera_config = File.join(fixture_path,'hieradata','hiera.yaml')
# Useless backtrace noise
backtrace_exclusion_patterns = [
/spec_helper/,
/gems/
]
if c.respond_to?(:backtrace_exclusion_patterns)
c.backtrace_exclusion_patterns = backtrace_exclusion_patterns
elsif c.respond_to?(:backtrace_clean_patterns)
c.backtrace_clean_patterns = backtrace_exclusion_patterns
end
c.before(:each) do
# For working on FIPS enabled systems
Puppet[:digest_algorithm] = 'sha256'
@spec_global_env_temp = Dir.mktmpdir('simpspec')
if defined?(environment)
set_environment(environment)
FileUtils.mkdir_p(File.join(@spec_global_env_temp,environment.to_s))
end
# ensure the user running these tests has an accessible environmentpath
Puppet[:environmentpath] = @spec_global_env_temp
Puppet[:user] = Etc.getpwuid(Process.uid).name
Puppet[:group] = Etc.getgrgid(Process.gid).name
Puppet[:digest_algorithm] = 'sha256'
if ENV.fetch('DEBUG','no') == 'yes'
Puppet.debug=true
Puppet::Util::Log.level = :debug
Puppet::Util::Log.newdestination(:console)
end
# sanitize hieradata
if defined?(hieradata)
set_hieradata(hieradata.gsub(':','_'))
elsif defined?(class_name)
set_hieradata(class_name.gsub(':','_'))
end
end
c.after(:each) do
FileUtils.rm_rf(@spec_global_env_temp) # clean up the mocked environmentpath
@spec_global_env_temp = nil
end
end
require 'puppetlabs_spec_helper/module_spec_helper'
Dir.glob("#{RSpec.configuration.module_path}/*").each do |dir|
begin
Pathname.new(dir).realpath
rescue
fail "ERROR: The module '#{dir}' is not installed. Tests cannot continue."
end
end