2019-06-11 22:24:35 +00:00
|
|
|
require_relative "docker_run"
|
|
|
|
require_relative "../lib/inspec"
|
2016-08-05 16:58:20 +00:00
|
|
|
#
|
|
|
|
# BUGON: These requires are to get around concurrency issues with
|
|
|
|
# autoloading in Ruby
|
|
|
|
#
|
2019-06-11 22:24:35 +00:00
|
|
|
require "train"
|
|
|
|
require "train/plugins"
|
|
|
|
require "train/plugins/transport"
|
|
|
|
require "train/transports/docker"
|
2015-09-22 15:12:14 +00:00
|
|
|
|
|
|
|
tests = ARGV
|
|
|
|
if tests.empty?
|
2019-06-11 22:24:35 +00:00
|
|
|
puts "Nothing to do."
|
2015-09-22 15:12:14 +00:00
|
|
|
exit 0
|
|
|
|
end
|
|
|
|
|
|
|
|
class DockerTester
|
|
|
|
def initialize(tests)
|
|
|
|
@tests = tests
|
|
|
|
@docker = DockerRunner.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def run
|
2019-06-11 22:24:35 +00:00
|
|
|
puts ["Running tests:", @tests].flatten.join("\n- ")
|
|
|
|
puts ""
|
2015-09-22 15:12:14 +00:00
|
|
|
|
|
|
|
conf = RSpec.configuration
|
|
|
|
reporter = conf.reporter
|
|
|
|
results = nil
|
|
|
|
|
|
|
|
# start reporting loop
|
|
|
|
reporter.report(0) do |report|
|
2015-09-22 15:21:07 +00:00
|
|
|
results = @docker.run_all do |name, container|
|
2015-09-22 15:12:14 +00:00
|
|
|
status = test_container(container, report)
|
2015-09-22 15:21:07 +00:00
|
|
|
status.all? ? nil : "Failed to run tests on #{name}"
|
2015-09-22 15:12:14 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# check if we were successful
|
2015-09-22 15:21:07 +00:00
|
|
|
failures = results.compact
|
|
|
|
failures.each { |f| puts "\033[31;1m#{f}\033[0m\n\n" }
|
2019-06-11 22:24:35 +00:00
|
|
|
failures.empty? || raise("Test failures")
|
2015-09-22 15:12:14 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_container(container, report)
|
|
|
|
puts "--> run test on docker #{container.id}"
|
2019-06-11 22:24:35 +00:00
|
|
|
opts = { "target" => "docker://#{container.id}" }
|
2015-10-26 03:04:18 +00:00
|
|
|
runner = Inspec::Runner.new(opts)
|
2016-08-31 11:17:44 +00:00
|
|
|
@tests.each { |test| runner.add_target(test) }
|
2016-01-16 17:59:44 +00:00
|
|
|
runner.tests.map { |g| g.run(report) }
|
2015-09-22 15:12:14 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
DockerTester.new(tests).run
|