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'
|
|
|
|
# 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 'serverspec/helper'
|
|
|
|
require 'serverspec/matcher'
|
|
|
|
require 'serverspec/subject'
|
|
|
|
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-08-13 00:05:32 +00:00
|
|
|
@conf = conf.dup
|
|
|
|
|
2015-08-12 19:03:41 +00:00
|
|
|
# RSpec.configuration.output_stream = $stdout
|
|
|
|
# RSpec.configuration.error_stream = $stderr
|
|
|
|
RSpec.configuration.add_formatter(:json)
|
|
|
|
|
|
|
|
# specinfra
|
2015-08-13 02:07:01 +00:00
|
|
|
backend = Vulcano::Backend.new(@conf)
|
|
|
|
backend.resolve_target_options
|
|
|
|
backend.configure_shared_options
|
|
|
|
backend.configure_target
|
2015-08-12 21:19:44 +00:00
|
|
|
end
|
|
|
|
|
2015-08-13 01:48:17 +00:00
|
|
|
def add_resources(resources)
|
|
|
|
files = resources.each do |resource|
|
|
|
|
Vulcano::Targets.resolve(resource)
|
2015-08-12 21:19:44 +00:00
|
|
|
end
|
2015-08-13 01:48:17 +00:00
|
|
|
files.flatten.each do |file|
|
|
|
|
add_file(file)
|
2015-08-12 21:19:44 +00:00
|
|
|
end
|
2015-08-12 19:03:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def add_file(path)
|
|
|
|
ctx = Vulcano::ProfileContext.new(@profile_id, {}, [])
|
|
|
|
|
|
|
|
# read the test file
|
|
|
|
apath = File::expand_path(path)
|
|
|
|
raw = File::read(apath)
|
|
|
|
|
|
|
|
# evaluate all tests
|
|
|
|
ctx.instance_eval(raw, apath, 0)
|
|
|
|
|
|
|
|
# process the resulting rules
|
|
|
|
rules = ctx.instance_variable_get(:@rules)
|
|
|
|
rules.each do |rule_id, rule|
|
|
|
|
#::Vulcano::DSL.execute_rule(rule, profile_id)
|
|
|
|
checks = rule.instance_variable_get(:@checks)
|
|
|
|
checks.each do |m,a,b|
|
|
|
|
example = RSpec::Core::ExampleGroup.describe(*a, &b)
|
|
|
|
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
|