-
Notifications
You must be signed in to change notification settings - Fork 37
/
Rakefile
118 lines (87 loc) · 2.93 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
# see https://github.com/vincentbernat/serverspec-example/blob/master/Rakefile
require 'rake'
require 'rbconfig'
require 'rspec/core/rake_task'
require 'json'
require 'yaml'
# set JSON format as default one
ENV['FORMAT'] ||= 'json'
$results = []
class ServerspecTask < RSpec::Core::RakeTask
attr_accessor :target_host
attr_accessor :target_host_vars
attr_accessor :target_play_role
def run_task(verbose)
command = "TARGET_HOST=#{target_host_vars['ansible_ssh_host']} "
command << "TARGET_HOST_NAME=#{target_host} "
command << "TARGET_PORT=#{target_host_vars['ansible_ssh_port']} "
command << "TARGET_USER=#{target_host_vars['ansible_ssh_user']} "
command << "#{spec_command}"
if ENV['FORMAT'] == 'json'
output = `#{command}`
$results << {
:name => self.name,
:exit_code => $?.to_i,
:output => JSON.parse(output)
}
else
puts "Running \"#{target_play_role}\" tests on #{target_host} [#{target_host_vars['ansible_ssh_host']}]"
succeed = system "#{command}"
raise "Failed!" if not succeed
end
end
end
playbook = YAML.load_file File.join('.', 'setup.yml')
inventory = JSON.parse `./plugins/inventory/terraform.py --list`
hosts = inventory['_meta']['hostvars']
namespace :spec do
all_tasks = []
playbook.each do |play|
checkable_hosts = hosts.select { |host|
play['hosts'] == 'all' or
play['hosts'].include? hosts[host]['role']
}
play_tasks = []
play['roles'].each do |play_role|
checkable_hosts.each do |hostname, vars|
task_name = "#{play_role}::#{hostname}".to_sym
desc "Run serverspec for \"#{play_role}\" role at #{hostname}"
ServerspecTask.new(task_name) do |t|
t.target_host = hostname
t.target_host_vars = vars
t.target_play_role = play_role
t.pattern = File.join('.', 'roles', play_role, 'spec', '*_spec.rb')
end
play_tasks << task_name
all_tasks << task_name
end
end
namespace :play do
desc "Run serverspec to play \"#{play['name']}\""
task play['name'].to_sym => play_tasks
end
end
desc "Run all serverspecs at once"
task ':all' => all_tasks
task :aggregate_results do
summary = {
:succeed => true,
:example_count => 0,
:failure_count => 0
}
$results.each do |result|
summary[:succeed] &&= result[:exit_code] == 0
summary[:example_count] += result[:output]['summary']['example_count']
summary[:failure_count] += result[:output]['summary']['failure_count']
end
# save tonns of output to file
File.write File.join(__dir__, 'serverspec_results.json'), JSON.pretty_generate($results)
puts JSON.pretty_generate(summary)
exit 1 if not summary[:succeed]
end
end
task :default => ['spec::all']
# add JSON result aggregation as last task
if ENV['FORMAT'] == 'json'
at_exit { Rake::Task['spec:aggregate_results'].invoke if $!.nil? }
end