Skip to content
This repository was archived by the owner on Jul 24, 2024. It is now read-only.

Commit 1c225e0

Browse files
SPECHELPER
- got spec_helpers.rb from pupmod-simp-pupmod - moved normalize function from pupmod tests to spec_helper - create global_spec_helper(dir, modulename) function to call from spec_helper.rb.
1 parent 7062b41 commit 1c225e0

File tree

3 files changed

+270
-6
lines changed

3 files changed

+270
-6
lines changed

lib/simp-spec-helpers.rb

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require 'simp/spec_helpers'

lib/simp/spec_helpers.rb

+192-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,200 @@
1-
require 'bundler'
2-
require 'rspec'
31

42
module Simp; end
53

64
module Simp::SpecHelpers
75

8-
# Stealing this from the Ruby 2.5 Dir::Tmpname workaround from Rails
9-
def self.tmpname
10-
t = Time.new.strftime("%Y%m%d")
11-
"simp-spec-helpers-#{t}-#{$$}-#{rand(0x100000000).to_s(36)}.tmp"
6+
def global_spec_helper(fixture_path, module_name)
7+
8+
9+
if ENV['PUPPET_DEBUG']
10+
Puppet::Util::Log.level = :debug
11+
Puppet::Util::Log.newdestination(:console)
12+
end
13+
14+
15+
default_hiera_config =<<-EOM
16+
---
17+
version: 5
18+
hierarchy:
19+
- name: SIMP Compliance Engine
20+
lookup_key: compliance_markup::enforcement
21+
options:
22+
enabled_sce_versions: [2]
23+
- name: Custom Test Hiera
24+
path: "%{custom_hiera}.yaml"
25+
- name: "%{module_name}"
26+
path: "%{module_name}.yaml"
27+
- name: Common
28+
path: default.yaml
29+
defaults:
30+
data_hash: yaml_data
31+
datadir: "stub"
32+
EOM
33+
34+
if not File.directory?(File.join(fixture_path,'hieradata')) then
35+
FileUtils.mkdir_p(File.join(fixture_path,'hieradata'))
36+
end
37+
38+
if not File.directory?(File.join(fixture_path,'modules',module_name)) then
39+
FileUtils.mkdir_p(File.join(fixture_path,'modules',module_name))
1240
end
1341

42+
RSpec.configure do |c|
43+
# If nothing else...
44+
c.default_facts = {
45+
:production => {
46+
#:fqdn => 'production.rspec.test.localdomain',
47+
:path => '/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin',
48+
:concat_basedir => '/tmp'
49+
}
50+
}
51+
52+
c.mock_framework = :rspec
53+
c.mock_with :mocha
54+
55+
c.module_path = File.join(fixture_path, 'modules')
56+
c.manifest_dir = File.join(fixture_path, 'manifests')
57+
58+
c.hiera_config = File.join(fixture_path,'hieradata','hiera.yaml')
59+
60+
# Useless backtrace noise
61+
backtrace_exclusion_patterns = [
62+
/spec_helper/,
63+
/gems/
64+
]
65+
66+
if c.respond_to?(:backtrace_exclusion_patterns)
67+
c.backtrace_exclusion_patterns = backtrace_exclusion_patterns
68+
elsif c.respond_to?(:backtrace_clean_patterns)
69+
c.backtrace_clean_patterns = backtrace_exclusion_patterns
70+
end
71+
72+
c.before(:all) do
73+
data = YAML.load(default_hiera_config)
74+
data.keys.each do |key|
75+
next unless data[key].is_a?(Hash)
76+
77+
if data[key][:datadir] == 'stub'
78+
data[key][:datadir] = File.join(fixture_path, 'hieradata')
79+
elsif data[key]['datadir'] == 'stub'
80+
data[key]['datadir'] = File.join(fixture_path, 'hieradata')
81+
end
82+
end
83+
84+
File.open(c.hiera_config, 'w') do |f|
85+
f.write data.to_yaml
86+
end
87+
end
88+
89+
c.before(:each) do
90+
@spec_global_env_temp = Dir.mktmpdir('simpspec')
91+
92+
if defined?(environment)
93+
set_environment(environment)
94+
FileUtils.mkdir_p(File.join(@spec_global_env_temp,environment.to_s))
95+
end
96+
97+
# ensure the user running these tests has an accessible environmentpath
98+
Puppet[:environmentpath] = @spec_global_env_temp
99+
Puppet[:user] = Etc.getpwuid(Process.uid).name
100+
Puppet[:group] = Etc.getgrgid(Process.gid).name
101+
Puppet[:digest_algorithm] = 'sha256'
102+
103+
# sanitize hieradata
104+
if defined?(hieradata)
105+
set_hieradata(hieradata.gsub(':','_'))
106+
elsif defined?(class_name)
107+
set_hieradata(class_name.gsub(':','_'))
108+
end
109+
end
110+
111+
c.after(:each) do
112+
# clean up the mocked environmentpath
113+
FileUtils.rm_rf(@spec_global_env_temp)
114+
@spec_global_env_temp = nil
115+
end
116+
end
117+
118+
Dir.glob("#{RSpec.configuration.module_path}/*").each do |dir|
119+
begin
120+
Pathname.new(dir).realpath
121+
rescue
122+
fail "ERROR: The module '#{dir}' is not installed. Tests cannot continue."
123+
end
124+
end
125+
end
126+
127+
# This can be used from inside your spec tests to set the testable environment.
128+
# You can use this to stub out an ENC.
129+
#
130+
131+
# Example:
132+
#
133+
# context 'in the :foo environment' do
134+
# let(:environment){:foo}
135+
# ...
136+
# end
137+
#
138+
def set_environment(environment = :production)
139+
RSpec.configure { |c| c.default_facts['environment'] = environment.to_s }
140+
end
141+
142+
# This can be used from inside your spec tests to load custom hieradata within
143+
# any context.
144+
#
145+
# Example:
146+
#
147+
# describe 'some::class' do
148+
# context 'with version 10' do
149+
# let(:hieradata){ "#{class_name}_v10" }
150+
# ...
151+
# end
152+
# end
153+
#
154+
# Then, create a YAML file at spec/fixtures/hieradata/some__class_v10.yaml.
155+
#
156+
# Hiera will use this file as it's base of information stacked on top of
157+
# 'default.yaml' and <module_name>.yaml per the defaults above.
158+
#
159+
# Note: Any colons (:) are replaced with underscores (_) in the class name.
160+
161+
def set_hieradata(hieradata)
162+
RSpec.configure { |c| c.default_facts['custom_hiera'] = hieradata }
14163
end
164+
165+
def normalize_compliance_results(compliance_profile_data, section, exceptions)
166+
normalized = Marshal.load(Marshal.dump(compliance_profile_data))
167+
if section == 'non_compliant'
168+
exceptions['non_compliant'].each do |resource,params|
169+
params.each do |param|
170+
if normalized['non_compliant'].key?(resource) &&
171+
normalized['non_compliant'][resource]['parameters'].key?(param)
172+
normalized['non_compliant'][resource]['parameters'].delete(param)
173+
if normalized['non_compliant'][resource]['parameters'].empty?
174+
normalized['non_compliant'].delete(resource)
175+
end
176+
end
177+
end
178+
end
179+
else
180+
normalized[section].delete_if do |item|
181+
rm = false
182+
Array(exceptions[section]).each do |allowed|
183+
if allowed.is_a?(Regexp)
184+
if allowed.match?(item)
185+
rm = true
186+
break
187+
end
188+
else
189+
rm = (allowed == item)
190+
end
191+
end
192+
rm
193+
end
194+
end
195+
196+
normalized
197+
end
198+
199+
end
200+

lib/simp/spec_helpers/helpers.rb

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
module Simp; end
2+
3+
module Simp::SpecHelpers::Helpers
4+
5+
# This can be used from inside your spec tests to set the testable environment.
6+
# You can use this to stub out an ENC.
7+
#
8+
9+
# Example:
10+
#
11+
# context 'in the :foo environment' do
12+
# let(:environment){:foo}
13+
# ...
14+
# end
15+
#
16+
def set_environment(environment = :production)
17+
RSpec.configure { |c| c.default_facts['environment'] = environment.to_s }
18+
end
19+
20+
# This can be used from inside your spec tests to load custom hieradata within
21+
# any context.
22+
#
23+
# Example:
24+
#
25+
# describe 'some::class' do
26+
# context 'with version 10' do
27+
# let(:hieradata){ "#{class_name}_v10" }
28+
# ...
29+
# end
30+
# end
31+
#
32+
# Then, create a YAML file at spec/fixtures/hieradata/some__class_v10.yaml.
33+
#
34+
# Hiera will use this file as it's base of information stacked on top of
35+
# 'default.yaml' and <module_name>.yaml per the defaults above.
36+
#
37+
# Note: Any colons (:) are replaced with underscores (_) in the class name.
38+
39+
def set_hieradata(hieradata)
40+
RSpec.configure { |c| c.default_facts['custom_hiera'] = hieradata }
41+
end
42+
43+
def normalize_compliance_results(compliance_profile_data, section, exceptions)
44+
normalized = Marshal.load(Marshal.dump(compliance_profile_data))
45+
if section == 'non_compliant'
46+
exceptions['non_compliant'].each do |resource,params|
47+
params.each do |param|
48+
if normalized['non_compliant'].key?(resource) &&
49+
normalized['non_compliant'][resource]['parameters'].key?(param)
50+
normalized['non_compliant'][resource]['parameters'].delete(param)
51+
if normalized['non_compliant'][resource]['parameters'].empty?
52+
normalized['non_compliant'].delete(resource)
53+
end
54+
end
55+
end
56+
end
57+
else
58+
normalized[section].delete_if do |item|
59+
rm = false
60+
Array(exceptions[section]).each do |allowed|
61+
if allowed.is_a?(Regexp)
62+
if allowed.match?(item)
63+
rm = true
64+
break
65+
end
66+
else
67+
rm = (allowed == item)
68+
end
69+
end
70+
rm
71+
end
72+
end
73+
74+
normalized
75+
end
76+
77+
end

0 commit comments

Comments
 (0)