-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
150 lines (126 loc) · 4.67 KB
/
Rakefile
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
require 'rubygems'
require 'bundler'
require 'bundler/setup'
require 'tmpdir'
require 'rake/clean'
require 'puppet-strings/tasks'
require 'puppetlabs_spec_helper/rake_tasks'
require 'puppet/version'
require 'semantic_puppet'
require 'puppet-lint/tasks/puppet-lint'
require 'puppet-syntax/tasks/puppet-syntax'
require 'metadata-json-lint/rake_task'
require 'yaml'
require 'puppet_blacksmith/rake_tasks'
# Allow acceptances tests to set their own value for the BEAKER_set variable.
beaker_set = {}
exclude_paths = [
"bundle/**/*",
"pkg/**/*",
"vendor/**/*",
"spec/**/*",
]
all_acc_tasks = []
Rake::Task[:lint].clear
PuppetLint.configuration.send('disable_relative')
PuppetLint::RakeTask.new :lint do |config|
config.ignore_paths = exclude_paths
end
PuppetSyntax.exclude_paths = exclude_paths
# Create rake tasks for individual acceptance tests
# Need to cater for nested structure
# Enables the ability to run tests on different hiera configurations
acc_tests = Dir.glob(['spec/acceptance/**/*']).grep(/_spec\.rb/i)
acc_tests.each do |item|
target = item.sub('spec/', '').gsub('/', ':').sub(/_spec\.rb/, '') # Generate task name
hiera_target = target.gsub('acceptance', '').gsub(':', '/').gsub('.yaml', '')
hiera_tests = Dir.glob(["spec/fixtures/hiera/data/#{hiera_target}/*"]).grep(/acceptance/)
if hiera_tests.empty?
hiera_tests.push('acceptance')
else
hiera_tests.map! {|hiera_test| File.basename hiera_test}
end
hiera_tests.each do |testcase|
target_task = target + testcase.gsub('acceptance','').gsub('.yaml', '')
all_acc_tasks.push(target_task)
RSpec::Core::RakeTask.new(target_task) do |task|
if beaker_set[target_task]
ENV['BEAKER_set'] = beaker_set[target_task]
end
handle_beaker_provision
# second argument is the hiera file assigned to this test case
task.rspec_opts = ['--color' , "--options #{testcase.gsub('.yaml', '')}"]
task.pattern = item
end
end
end
# Create rake tasks for individual spec tests
# Need to cater for nested structure
spec_tests = Dir.glob(['spec/classes/**/*', 'spec/defines/**/*'])
spec_tests.each do |item|
if item =~ /_spec\.rb/i
target = item.sub('spec/', '').gsub('classes', 'spec/classes').gsub('/', ':').sub(/_spec\.rb/, '') # Generate task name
RSpec::Core::RakeTask.new(target) do |task|
Rake::Task[:syntax].invoke
Rake::Task[:lint].invoke
Rake::Task[:spec_prep].invoke
task.rspec_opts = ['--color']
task.pattern = item
end
end
end
# *** Important Note: we need to cater for nested structure
# where the profiles/roles are nested the tests, metadata and hiera data will conform to the same structure
# Create tasks for running all role/profile acceptance tests in parallel
# Get array of all acceptance tasks
namespace 'acceptance' do
desc "Run acceptance tests in parallel"
multitask :all_p => all_acc_tasks
desc "Run acceptance tests"
task :all do
all_acc_tasks.each do |task|
Rake::Task[task].invoke()
end
end
end
desc "Run syntax, lint, and spec tests."
task :test => [
:metadata_lint,
:syntax,
:lint,
:spec,
]
# Set the BEAKER_setfile env variable to use a temporary nodefile crafted
# with the machine names from the previous run when BEAKER_provision=no
def handle_beaker_provision
bp = ENV['BEAKER_provision'] ? ENV['BEAKER_provision'] : 'yes'
p "handling BEAKER_provision ('#{bp}')"
if bp.casecmp('no') == 0
# Find the logfile for the last run
nodeset_name = ENV['BEAKER_set'] ? ENV['BEAKER_set'] : 'default'
logdir = File.join(Dir.pwd, "log", nodeset_name)
last_logfile = Dir.glob("#{logdir}/**/*.*").max_by { |f| File.mtime(f) }
p "Extracting node names from #{File.expand_path(last_logfile)}"
# Extract node name to role mapping
nodenames = Hash.new
File.open(last_logfile).each do |line|
hostname, role = line.split.values_at(5, 6).map { |v| v.gsub(/[()]/, '') }
nodenames[role] = hostname
end
p "Node mapping: #{nodenames.to_s}"
# Generate temporary nodeset file
nodeset_dir = File.join(Dir.pwd, 'spec', 'acceptance', 'nodesets')
nodeset = YAML::load(File.open(File.join(nodeset_dir, "#{nodeset_name}.yml")))
nodeset['HOSTS'] = nodeset['HOSTS'].inject({}) { |result, node|
result.merge({nodenames[node.first] => node.last})
}
File.open(Dir::Tmpname.create(['tmp_nodeset', '.yaml'], nodeset_dir) {}, 'w') { |nodefile|
nodefile.write nodeset.to_yaml
p "Nodeset YAML written to #{File.expand_path(nodefile)}"
# Set env to use temp nodeset file
ENV['BEAKER_setfile'] = File.expand_path(nodefile)
ENV.delete('BEAKER_set')
}
end
CLEAN.include(FileList["#{nodeset_dir}/tmp_nodeset*.yaml"])
end