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-03 12:06:43 +00:00
|
|
|
# test all images
|
2015-09-10 11:01:12 +00:00
|
|
|
promises = @conf['images'].map { |n|
|
2015-09-11 09:24:22 +00:00
|
|
|
Concurrent::Promise.new {
|
|
|
|
container = prepare_image(n)
|
|
|
|
runner = test_container(container.id)
|
|
|
|
[container, runner]
|
|
|
|
}.execute
|
2015-09-07 16:25:10 +00:00
|
|
|
}
|
|
|
|
|
2015-09-10 11:01:12 +00:00
|
|
|
sleep(0.1) until promises.all?(&:fulfilled?)
|
2015-09-07 16:25:10 +00:00
|
|
|
|
2015-09-11 09:24:22 +00:00
|
|
|
runner = promises[0].value[1]
|
|
|
|
ok = runner.run
|
2015-09-07 16:25:10 +00:00
|
|
|
|
|
|
|
done = promises.map do |promise|
|
2015-09-11 09:24:22 +00:00
|
|
|
promise.then { |c, _| stop_container(c) }
|
2015-09-07 16:25:10 +00:00
|
|
|
end
|
2015-09-10 11:01:12 +00:00
|
|
|
sleep(0.1) until done.all?(&:fulfilled?)
|
2015-09-07 16:25:10 +00:00
|
|
|
|
2015-09-11 09:24:22 +00:00
|
|
|
ok or fail 'Test failures'
|
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
|
|
|
|
|
|
|
|
def test_container(container_id)
|
2015-09-10 11:01:12 +00:00
|
|
|
puts "--> run test on docker #{container_id}"
|
2015-09-07 16:25:10 +00:00
|
|
|
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 09:24:22 +00:00
|
|
|
runner
|
2015-09-03 12:06:43 +00:00
|
|
|
end
|
|
|
|
|
2015-09-10 11:01:12 +00:00
|
|
|
def prepare_image(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
|