forked from wikimedia/operations-puppet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
157 lines (140 loc) · 5 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
151
152
153
154
155
156
157
# This rakefile is meant to run linters and tests
# tailored to a specific changeset.
# You will need 'bundler' to install dependencies:
#
# $ apt-get install bundler
# $ bundle install
#
# Then run all the tests, in parallel, that are pertinent to the current changeset
#
# $ bundle exec rake test
#
# If you just want to check which tests would be ran, run
#
# $ bundle exec rake debug
#
# Based on the contents of the change, this rakefile will define and run
# all or just some of the following tests:
#
# * puppet_lint - runs puppet lint on the changed puppet files
# * typos - checks the changed files against a predefined list of typos defined
# in ./typos
# * syntax - run syntax checks for puppet files, hiera files, and templates
# changed in the current changeset
# * rubocop - run rubocop style checks on ruby files changed in this changeset
# * spec - run the spec tests on the modules where files are changed, or whose
# tests depend on modules that have been modified.
# * tox - run the tox tests if needed.
#
require 'git'
require 'parallel_tests'
require 'set'
require 'rake'
require 'rake/tasklib'
require 'shellwords'
# Needed by docs
require 'puppet-strings/tasks/generate'
$LOAD_PATH.unshift File.expand_path('.')
require 'rake_modules/taskgen'
require_relative 'rake_modules/module_rake_tasks.rb'
pattern_end = 'spec/{aliases,classes,defines,functions,hosts,integration,plans,tasks,type_aliases,types,unit}/**/*_spec.rb'
private_repo = 'https://gerrit.wikimedia.org/r/labs/private'
fixture_path = File.join(__dir__, 'spec', 'fixtures')
private_modules_path = File.join(fixture_path, 'private')
if File.exist?(File.join(private_modules_path, '.git'))
system('git', '-C', private_modules_path, 'pull', out: File::NULL)
else
system('git', 'clone', private_repo, private_modules_path, out: File::NULL)
end
ENV['SPEC_PREP_DONE'] = 'DONE'
# We should probably set this in the dockerfile
ENV['HOME'] = '/srv/workspace/puppet' if ENV['HOME'] == '/nonexistent'
t = TaskGen.new('.')
multitask :parallel => t.tasks
desc 'Run all actual tests in parallel for changes in HEAD'
task :test => [:parallel, :wmf_styleguide_delta]
task :static => t.tasks - [:spec, :tox, :per_module_tox, :dhcp]
task :unit => [:spec, :tox, :per_module_tox]
# Show what we would run
task :debug do
puts "Tasks that would be run: "
puts t.tasks
end
# Global tasks. Only the ones deemed useful are added here.
namespace :global do
desc "Build documentation"
task :doc do
Rake::Task['strings:generate'].invoke(
'**/*.pp **/*.rb', # patterns
'false', # debug
'false', # backtrace
'rdoc', # markup format
)
end
spec_failed = []
spec_tasks = []
namespace :spec do
FileList['modules/*/spec'].each do |path|
next unless path.match('modules/(.+)/')
module_name = Regexp.last_match(1)
task module_name do
if File.exist?("modules/#{module_name}/Rakefile")
spec_result = system("cd 'modules/#{module_name}' && rake parallel_spec")
spec_failed << module_name unless spec_result
else
spec_failed << "#{module_name}: Missing Rakefile"
end
end
spec_tasks << "spec:#{module_name}"
end
end
desc "Run all spec tests found in modules"
multitask :spec => spec_tasks do
raise "Modules that failed to pass the spec tests: #{spec_failed.join ', '}" unless spec_failed.empty?
end
desc 'run Global rspec using parralel_spec (this is experimental prefer spec)'
task :parallel_spec do
spec_modules = Set.new
FileList['modules/**/spec'].each do |path|
next unless path.match('modules/([^\/]+)/')
module_name = Regexp.last_match(1)
spec_modules << module_name
end
pattern = "modules/{#{spec_modules.to_a.join(',')}}/#{pattern_end}"
args = ['-t', 'rspec', '--']
args.concat(Rake::FileList[pattern].to_a)
ParallelTests::CLI.new.run(args)
end
desc 'Run the wmf style guide check on all files, or on a single module (with module=<module-name>)'
task :wmf_style do
if ENV['module']
pattern = "modules/#{ENV['module']}/**/*.pp"
else
pattern = '**/*.pp'
end
t.setup_wmf_lint_check
linter = PuppetLint.new
FileList[pattern].to_a.each do |puppet_file|
linter.file = puppet_file
linter.run
next if linter.problems.empty?
if ENV.key?('JENKINS_URL')
t.print_wmf_style_violations linter.problems, nil, '%{path}:%{line}:%{check}:%{KIND}:%{message}'
else
t.print_wmf_style_violations linter.problems
end
end
end
end
desc 'Show the help'
task :help do
puts "Puppet helper for operations/puppet.git
Welcome #{ENV['USER']} to WMFs wonderful rake helper to play with puppet.
---[Command line options]----------------------------------------------
`rake -T` : list available tasks
`rake -P` : shows tasks dependencies
---[Available rake tasks]----------------------------------------------"
# Show our tasks list.
system "rake -T"
puts "-----------------------------------------------------------------------"
end