-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathRakefile.concurrency
69 lines (63 loc) · 2.52 KB
/
Rakefile.concurrency
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
require 'bundler/setup'
desc 'Run Test Kitchen integration tests'
namespace :integration do
# Gets a collection of instances.
#
# @param regexp [String] regular expression to match against instance names.
# @param config [Hash] configuration values for the `Kitchen::Config` class.
# @return [Collection<Instance>] all instances.
def kitchen_instances(regexp, config)
instances = Kitchen::Config.new(config).instances
instances = instances.get_all(Regexp.new(regexp)) unless regexp.nil? || regexp == 'all'
raise Kitchen::UserError, "regexp '#{regexp}' matched 0 instances" if instances.empty?
instances
end
# Runs a test kitchen action against some instances.
#
# @param action [String] kitchen action to run (defaults to `'test'`).
# @param regexp [String] regular expression to match against instance names.
# @param concurrency [#to_i] number of instances to run the action against concurrently.
# @param loader_config [Hash] loader configuration options.
# @return void
def run_kitchen(action, regexp, concurrency, loader_config = {})
require 'kitchen'
Kitchen.logger = Kitchen.default_file_logger
config = { loader: Kitchen::Loader::YAML.new(loader_config) }
call_threaded(
kitchen_instances(regexp, config),
action,
concurrency
)
end
# Calls a method on a list of objects in concurrent threads.
#
# @param objects [Array] list of objects.
# @param method_name [#to_s] method to call on the objects.
# @param concurrency [Integer] number of objects to call the method on concurrently.
# @return void
def call_threaded(objects, method_name, concurrency)
puts "method_name: #{method_name}, concurrency: #{concurrency}"
threads = []
raise 'concurrency must be > 0' if concurrency < 1
objects.each do |obj|
sleep 3 until threads.map(&:alive?).count(true) < concurrency
threads << Thread.new { obj.method(method_name).call }
end
threads.map(&:join)
end
desc 'Run integration tests with kitchen-vagrant'
task :vagrant, [:regexp, :action, :concurrency] do |_t, args|
args.with_defaults(regexp: 'all', action: 'test', concurrency: 1)
run_kitchen(args.action, args.regexp, args.concurrency.to_i)
end
desc 'Run integration tests with kitchen-docker'
task :docker, [:regexp, :action, :concurrency] do |_t, args|
args.with_defaults(regexp: 'all', action: 'test', concurrency: 1)
run_kitchen(
args.action,
args.regexp,
args.concurrency.to_i,
local_config: '.kitchen.docker.yml'
)
end
end