forked from sous-chefs/kafka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile
193 lines (172 loc) · 5.23 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# encoding: utf-8
require 'rspec/core/rake_task'
require 'foodcritic'
require 'rubocop/rake_task'
require 'stove'
require 'stove/rake_task'
require 'logger'
$logger = Logger.new($stdout)
$logger.formatter = proc do |_, datetime, _, message|
format(%(%s :: %s\n), datetime.strftime('%Y-%m-%d %H:%M:%S'), message)
end
RSpec::Core::RakeTask.new(:spec)
FoodCritic::Rake::LintTask.new do |t|
t.options = {
fail_tags: %w[any],
}
end
RuboCop::RakeTask.new(:rubocop)
desc 'Run FoodCritic, RuboCop and ChefSpec'
task :test do
Rake::Task['foodcritic'].execute
Rake::Task['rubocop'].execute
Rake::Task['spec'].execute
end
desc 'Package the latest version as a .tar.gz archive'
task package: :test do
cookbook = Stove::Cookbook.new(Dir.pwd)
version = cookbook.tag_version
release_name = %(kafka-cookbook-#{version})
archive_path = ::File.join('pkg', format('%s.tar.gz', release_name))
if File.exist?(archive_path)
puts %(#{archive_path} already exist, exiting...)
exit(1)
else
packager = Stove::Packager.new(cookbook, false)
File.open(archive_path, 'wb') do |f|
f.write(packager.tarball.read)
end
puts %(Created archive of #{version} as #{archive_path})
end
end
Stove::RakeTask.new do |t|
t.stove_opts = %w[--no-git]
t.log_level = :debug
end
class KitchenTask
attr_reader :output, :duration
def initialize(version)
@output = []
@env = {
'KAFKA_VERSION' => version,
'CHEF_VERSION' => ENV['CHEF_VERSION'],
}
end
def run
start_timestamp = Time.now
if @concurrency > 1
status = run_and_wait(format('bundle exec kitchen setup --concurrency=%d', @concurrency))
if status.success?
status = run_and_wait('bundle exec kitchen verify')
end
run_and_wait(format('bundle exec kitchen destroy --concurrency=%d', @concurrency))
else
status = run_and_wait('bundle exec kitchen test')
end
@duration = Time.now - start_timestamp
status
end
private
def run_and_wait(command)
start_time = Time.now
rd, wr = IO.pipe
pid = Process.fork do
$stdout.reopen(wr)
rd.close
exec(@env, command)
end
wr.close
rd.each do |line|
@output << line
end
_, status = Process.waitpid2(pid)
duration = Time.now - start_time
$logger.info('Ran %p, in %d seconds', command, duration)
status
end
end
class DockerTask < KitchenTask
def initialize(version)
super(version)
@env['KITCHEN_YAML'] = '.kitchen.docker.yml'
@concurrency = ENV.fetch('concurrency', 3).to_i
end
end
class VagrantTask < KitchenTask
def initialize(version)
super(version)
@env['KITCHEN_YAML'] = '.kitchen.yml'
@concurrency = ENV.fetch('concurrency', 4).to_i
end
end
namespace :test do
default_versions = %w[0.8.1.1 0.8.2.2 0.9.0.1 0.10.0.1]
def run_tests_for(versions, task_class)
$logger.info(format('Running tests for versions: %s', versions.join(', ')))
failed_versions = []
done = false
until done
versions.each do |version|
$logger.info('Starting tests for v%s', version)
task = task_class.new(version)
if task.run.success?
$logger.info('Done testing v%s, run took %d seconds', version, task.duration)
else
$logger.info(task.output)
$logger.info('v%s failed, run took %d seconds, see output above ^', version, task.duration)
if ENV.key?('yes')
answer = ''
else
print format('%s :: Continue with the remaining versions? [Y/n]: ', Time.now.strftime('%Y-%m-%d %H:%M:%S'))
answer = $stdin.gets.strip.downcase
end
break unless answer.empty? || answer == 'y'
failed_versions << version
next
end
end
if failed_versions.any?
print format('%s :: The following versions failed: %s, would you like to retry them? [Y/n]: ', Time.now.strftime('%Y-%m-%d %H:%M:%S'), failed_versions.join(', '))
answer = $stdin.gets.strip.downcase
if answer.empty? || answer == 'y'
versions = failed_versions
failed_versions = []
else
done = true
end
else
done = true
end
end
end
desc 'Run test-kitchen with kitchen-docker'
task docker: 'docker:running' do
versions = ENV.fetch('versions', default_versions.join(',')).split(',')
run_tests_for(versions, DockerTask)
end
namespace :docker do
task :running do
docker_binary = `which docker 2> /dev/null`.strip
if docker_binary.empty?
abort 'Unable to find Docker binary. Docker needs to be installed on your system for running these tests.'
else
boot2docker_binary = `which boot2docker 2> /dev/null`.strip
unless boot2docker_binary.empty?
boot2docker_status = `boot2docker status`.strip
if boot2docker_status != 'running'
`boot2docker stop && boot2docker start`
unless $?.success?
abort 'Failed to start boot2docker'
end
end
end
end
end
end
desc 'Run test-kitchen with kitchen-vagrant'
task :vagrant do
versions = ENV.fetch('versions', default_versions.join(',')).split(',')
run_tests_for(versions, VagrantTask)
end
end
at_exit { $logger.close if $logger }