2015-09-03 16:04:13 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
|
2015-09-03 12:06:43 +00:00
|
|
|
require 'docker'
|
|
|
|
require 'yaml'
|
2015-09-10 11:01:12 +00:00
|
|
|
require 'concurrent'
|
2015-09-03 11:22:15 +00:00
|
|
|
require_relative '../lib/vulcano'
|
2015-09-03 12:06:43 +00:00
|
|
|
|
2015-09-03 12:14:57 +00:00
|
|
|
tests = ARGV
|
|
|
|
if tests.empty?
|
|
|
|
puts 'Nothing to do.'
|
|
|
|
exit 0
|
|
|
|
end
|
2015-09-03 11:22:15 +00:00
|
|
|
|
2015-09-03 12:06:43 +00:00
|
|
|
class DockerTester
|
|
|
|
def initialize(tests)
|
|
|
|
@tests = tests
|
|
|
|
@images = docker_images_by_tag
|
|
|
|
@conf = tests_conf
|
|
|
|
end
|
|
|
|
|
|
|
|
def run
|
2015-09-10 11:01:12 +00:00
|
|
|
puts ['Running tests:', @tests].flatten.join("\n- ")
|
2015-09-03 16:09:41 +00:00
|
|
|
puts ''
|
2015-09-11 10:42:21 +00:00
|
|
|
|
|
|
|
conf = RSpec.configuration
|
|
|
|
reporter = conf.reporter
|
|
|
|
promises = nil
|
|
|
|
|
|
|
|
# start reporting loop
|
|
|
|
reporter.report(0) do |report|
|
|
|
|
# test all images
|
2015-09-11 11:49:42 +00:00
|
|
|
promises = @conf['images'].map do |name|
|
|
|
|
run_on_target(name, report, conf)
|
|
|
|
end
|
2015-09-11 10:42:21 +00:00
|
|
|
# wait for all tests to be finished
|
|
|
|
sleep(0.1) until promises.all?(&:fulfilled?)
|
|
|
|
end
|
|
|
|
|
|
|
|
# check if we were successful
|
2015-09-11 11:49:42 +00:00
|
|
|
failures = promises.map(&:value).find_all { |x| x!=0 }
|
|
|
|
failures.empty? or fail 'Test failures'
|
|
|
|
end
|
|
|
|
|
|
|
|
def run_on_target(name, report, conf)
|
|
|
|
pr = Concurrent::Promise.new {
|
|
|
|
container = prepare_container(name)
|
|
|
|
status = test_container(container, report)
|
|
|
|
stop_container(container)
|
|
|
|
# Report either 0 for all ok or an exit code
|
|
|
|
status.all? ? 0 : conf.failure_exit_code
|
|
|
|
}.execute
|
2015-09-11 11:50:07 +00:00
|
|
|
pr.rescue do |err|
|
|
|
|
puts err.message
|
|
|
|
puts err.backtrace.join("\n")
|
|
|
|
conf.failure_exit_code
|
|
|
|
end
|
2015-09-03 12:06:43 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def docker_images_by_tag
|
|
|
|
# get all docker image tags
|
|
|
|
images = {}
|
|
|
|
Docker::Image.all.map do |img|
|
|
|
|
Array(img.info['RepoTags']).each do |tag|
|
|
|
|
images[tag] = img
|
|
|
|
end
|
|
|
|
end
|
|
|
|
images
|
|
|
|
end
|
|
|
|
|
|
|
|
def tests_conf
|
|
|
|
# get the test configuration
|
2015-09-10 11:01:12 +00:00
|
|
|
conf_path = File.join(File.dirname(__FILE__), '..', '.tests.yaml')
|
|
|
|
fail "Can't find tests config in #{conf_path}" unless File.file?(conf_path)
|
|
|
|
YAML.load(File.read(conf_path))
|
2015-09-03 12:06:43 +00:00
|
|
|
end
|
|
|
|
|
2015-09-11 11:49:42 +00:00
|
|
|
def test_container(container, report)
|
|
|
|
puts "--> run test on docker #{container.id}"
|
|
|
|
opts = { 'target' => "docker://#{container.id}" }
|
2015-09-03 12:06:43 +00:00
|
|
|
runner = Vulcano::Runner.new(nil, opts)
|
|
|
|
runner.add_tests(@tests)
|
2015-09-11 11:49:42 +00:00
|
|
|
tests = runner.tests.ordered_example_groups
|
|
|
|
tests.map { |g| g.run(report) }
|
2015-09-03 12:06:43 +00:00
|
|
|
end
|
|
|
|
|
2015-09-11 11:49:42 +00:00
|
|
|
def prepare_container(name)
|
2015-09-03 12:06:43 +00:00
|
|
|
dname = "docker-#{name}:latest"
|
|
|
|
image = @images[dname]
|
2015-09-10 11:01:12 +00:00
|
|
|
fail "Can't find docker image #{dname}" if image.nil?
|
2015-09-03 12:06:43 +00:00
|
|
|
|
2015-09-03 15:24:45 +00:00
|
|
|
puts "--> start docker #{name}"
|
2015-09-03 12:06:43 +00:00
|
|
|
container = Docker::Container.create(
|
2015-09-10 11:01:12 +00:00
|
|
|
'Cmd' => ['/bin/bash'],
|
2015-09-03 12:06:43 +00:00
|
|
|
'Image' => image.id,
|
2015-09-07 16:25:10 +00:00
|
|
|
'OpenStdin' => true,
|
2015-09-03 12:06:43 +00:00
|
|
|
)
|
|
|
|
container.start
|
2015-09-10 11:01:12 +00:00
|
|
|
container
|
|
|
|
end
|
2015-09-03 12:06:43 +00:00
|
|
|
|
2015-09-10 11:01:12 +00:00
|
|
|
def stop_container(container)
|
|
|
|
puts "--> killrm docker #{container.id}"
|
2015-09-03 12:06:43 +00:00
|
|
|
container.kill
|
|
|
|
container.delete(force: true)
|
|
|
|
end
|
|
|
|
end
|
2015-09-03 11:22:15 +00:00
|
|
|
|
2015-09-03 12:06:43 +00:00
|
|
|
DockerTester.new(tests).run
|