2015-08-12 19:03:41 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
# copyright: 2015, Dominik Richter
|
|
|
|
# license: All rights reserved
|
|
|
|
|
2015-08-13 00:05:32 +00:00
|
|
|
require 'uri'
|
2015-08-13 01:48:17 +00:00
|
|
|
require 'vulcano/backend'
|
|
|
|
require 'vulcano/targets'
|
2015-08-27 20:59:58 +00:00
|
|
|
require 'vulcano/profile_context'
|
2015-08-13 01:48:17 +00:00
|
|
|
# spec requirements
|
2015-08-12 19:03:41 +00:00
|
|
|
require 'rspec'
|
|
|
|
require 'rspec/its'
|
|
|
|
require 'specinfra'
|
|
|
|
require 'specinfra/helper'
|
|
|
|
require 'specinfra/helper/set'
|
|
|
|
require 'vulcano/rspec_json_formatter'
|
|
|
|
|
|
|
|
module Vulcano
|
|
|
|
|
|
|
|
class Runner
|
2015-08-13 01:48:17 +00:00
|
|
|
|
2015-08-12 21:19:44 +00:00
|
|
|
def initialize(profile_id, conf)
|
2015-08-12 19:03:41 +00:00
|
|
|
@rules = []
|
|
|
|
@profile_id = profile_id
|
2015-09-03 10:56:40 +00:00
|
|
|
@conf = Vulcano::Backend.target_config(normalize_map(conf))
|
2015-08-29 23:31:36 +00:00
|
|
|
configure_output
|
|
|
|
configure_backend
|
|
|
|
end
|
2015-08-13 00:05:32 +00:00
|
|
|
|
2015-09-03 10:56:40 +00:00
|
|
|
def normalize_map(hm)
|
|
|
|
res = {}
|
2015-09-03 11:43:32 +00:00
|
|
|
hm.each{|k,v|
|
|
|
|
res[k.to_s] = v
|
|
|
|
}
|
2015-09-03 10:56:40 +00:00
|
|
|
res
|
|
|
|
end
|
|
|
|
|
2015-08-29 23:31:36 +00:00
|
|
|
def configure_output
|
2015-08-12 19:03:41 +00:00
|
|
|
# RSpec.configuration.output_stream = $stdout
|
|
|
|
# RSpec.configuration.error_stream = $stderr
|
2015-09-03 11:43:32 +00:00
|
|
|
RSpec.configuration.add_formatter(@conf['format'] || 'progress')
|
2015-08-29 23:31:36 +00:00
|
|
|
end
|
2015-08-12 19:03:41 +00:00
|
|
|
|
2015-08-29 23:31:36 +00:00
|
|
|
def configure_backend
|
2015-09-03 11:43:32 +00:00
|
|
|
backend_name = ( @conf['backend'] ||= 'exec' )
|
2015-08-30 02:33:15 +00:00
|
|
|
# @TODO all backends except for mock revert to specinfra for now
|
2015-08-30 00:13:07 +00:00
|
|
|
unless %w{ mock }.include? backend_name
|
|
|
|
backend_class = Vulcano::Backend.registry['specinfra']
|
|
|
|
else
|
|
|
|
backend_class = Vulcano::Backend.registry[backend_name]
|
|
|
|
end
|
|
|
|
|
2015-08-30 02:33:15 +00:00
|
|
|
# Return on failure
|
2015-08-29 23:11:23 +00:00
|
|
|
if backend_class.nil?
|
2015-08-29 23:31:36 +00:00
|
|
|
raise "Can't find command backend '#{backend_name}'."
|
2015-08-29 23:11:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# create the backend based on the config
|
2015-08-30 00:36:02 +00:00
|
|
|
@backend = backend_class.new(@conf)
|
2015-08-29 23:11:23 +00:00
|
|
|
end
|
|
|
|
|
2015-08-28 17:13:05 +00:00
|
|
|
def add_tests(tests)
|
2015-08-30 02:33:15 +00:00
|
|
|
# retrieve the raw ruby code of all tests
|
2015-08-28 17:13:05 +00:00
|
|
|
items = tests.map do |test|
|
|
|
|
Vulcano::Targets.resolve(test)
|
2015-08-12 21:19:44 +00:00
|
|
|
end
|
2015-08-30 02:33:15 +00:00
|
|
|
|
|
|
|
# add all tests (raw) to the runtime
|
2015-08-13 03:47:02 +00:00
|
|
|
items.flatten.each do |item|
|
|
|
|
add_content(item[:content], item[:ref], item[:line])
|
2015-08-12 21:19:44 +00:00
|
|
|
end
|
2015-08-12 19:03:41 +00:00
|
|
|
end
|
|
|
|
|
2015-08-13 03:47:02 +00:00
|
|
|
def add_content(content, source, line = nil)
|
2015-08-29 23:11:23 +00:00
|
|
|
ctx = Vulcano::ProfileContext.new(@profile_id, @backend)
|
2015-08-12 19:03:41 +00:00
|
|
|
|
|
|
|
# evaluate all tests
|
2015-08-27 20:59:58 +00:00
|
|
|
ctx.load(content, source, line || 1)
|
2015-08-12 19:03:41 +00:00
|
|
|
|
|
|
|
# process the resulting rules
|
2015-08-27 20:59:58 +00:00
|
|
|
ctx.rules.each do |rule_id, rule|
|
2015-08-12 19:03:41 +00:00
|
|
|
#::Vulcano::DSL.execute_rule(rule, profile_id)
|
|
|
|
checks = rule.instance_variable_get(:@checks)
|
|
|
|
checks.each do |m,a,b|
|
2015-08-14 06:22:56 +00:00
|
|
|
# resource skipping
|
|
|
|
if !a.empty? &&
|
|
|
|
a[0].respond_to?(:resource_skipped) &&
|
|
|
|
!a[0].resource_skipped.nil?
|
|
|
|
example = RSpec::Core::ExampleGroup.describe(*a) do
|
|
|
|
it a[0].resource_skipped
|
|
|
|
end
|
|
|
|
else
|
|
|
|
# add the resource
|
|
|
|
example = RSpec::Core::ExampleGroup.describe(*a, &b)
|
|
|
|
end
|
|
|
|
|
2015-08-12 19:03:41 +00:00
|
|
|
set_rspec_ids(example, rule_id)
|
|
|
|
RSpec.world.register(example)
|
|
|
|
end
|
|
|
|
end
|
2015-08-12 22:16:50 +00:00
|
|
|
end
|
2015-08-12 19:03:41 +00:00
|
|
|
|
2015-08-12 22:16:50 +00:00
|
|
|
def run
|
2015-08-12 19:03:41 +00:00
|
|
|
rspec_runner = RSpec::Core::Runner.new(nil)
|
|
|
|
rspec_runner.run_specs(RSpec.world.ordered_example_groups)
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_rspec_ids(example, id)
|
|
|
|
example.metadata[:id] = id
|
|
|
|
example.filtered_examples.each do |e|
|
|
|
|
e.metadata[:id] = id
|
|
|
|
end
|
|
|
|
example.children.each do |child|
|
|
|
|
set_rspec_ids(child, id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|