feature: run tests from cli

Signed-off-by: Dominik Richter <dominik.richter@gmail.com>
This commit is contained in:
Dominik Richter 2015-08-12 12:03:41 -07:00
parent f6509b7f81
commit 33043dd6a1
8 changed files with 103 additions and 102 deletions

View file

@ -4,7 +4,6 @@
require 'thor'
require 'json'
require_relative '../lib/vulcano'
require_relative '../lib/verify'
class VulcanoCLI < Thor
@ -13,6 +12,7 @@ class VulcanoCLI < Thor
option :print, aliases: :p, type: :boolean
option :id, type: :string
def json(*paths)
require_relative '../lib/verify'
paths.each do |path|
Vulcano::Profiles.new.valid_folder? path unless options[:print]
vc = Vulcano::Profiles.new({ quiet: options[:print], id: options[:id] })
@ -34,6 +34,7 @@ class VulcanoCLI < Thor
desc "check PATH", "check all tests in PATH"
def check(*paths)
require_relative '../lib/verify'
paths.each do |path|
puts "#{path}"
Vulcano::Profiles.new.valid_folder? path
@ -41,5 +42,14 @@ class VulcanoCLI < Thor
end
end
desc "exec PATHS", "run all test files"
option :id, type: :string
def exec(*files)
runner = Vulcano::Runner.new(options[:id])
files.each do |file|
runner.add_file(file)
end
end
end
VulcanoCLI.start(ARGV)

View file

@ -1,94 +0,0 @@
# encoding: utf-8
# copyright: 2015, Dominik Richter
# license: All rights reserved
require 'serverspec'
# Run spec on an ssh target
if ENV['SSH_SPEC']
require 'pathname'
require 'net/ssh'
set :backend, :ssh
if ENV['USE_SUDO'] == "false"
set :disable_sudo, true
end
if ENV['SUDO_OPTIONS'].to_s != ""
set :sudo_options, ENV['SUDO_OPTIONS']
end
set :request_pty, true
RSpec.configure do |c|
options = {}
c.sudo_password = ENV['SUDO_PASSWORD'] || ENV['sudo_password']
c.host = ENV['TARGET_HOST']
options[:port] = ( ENV['LOGIN_PORT'] || 22 ).to_i
options[:auth_methods] = ["none"]
options[:user_known_hosts_file ] = "/dev/null"
options[:global_known_hosts_file ] = "/dev/null"
options[:number_of_password_prompts] = 0
options[:user] = ENV['LOGIN_USERNAME'] || ENV['user'] || Etc.getlogin
if options[:user].nil?
raise 'specify a user for login via env LOGIN_USERNAME= or by adding user='
end
options[:password] = ENV['LOGIN_PASSWORD'] || ENV['password']
if !options[:password].nil?
options[:auth_methods].push("password")
end
if !ENV['LOGIN_KEY'].nil?
options[:keys] = [ENV['LOGIN_KEY']]
options[:keys_only] = true
options[:auth_methods].push("publickey")
end
# Local alternative: use configuration from .ssh/config
# for the given host.
# ssh_conf = Net::SSH::Config.for(c.host)
# options.merge(ssh_conf)
c.ssh_options = options
end
# Run spec on Windows via WinRM
elsif ENV['WINRM_SPEC']
require 'winrm'
set :backend, :winrm
set :os, :family => 'windows'
user = ENV['LOGIN_USERNAME']
pass = ENV['LOGIN_PASSWORD']
host = ENV['TARGET_HOST']
port = ENV['LOGIN_PORT']
# check if we should use ssl
ssl = ENV['WINRM_SSL']
if (ssl)
scheme = "https"
port = port || '5986'
else
scheme = "http"
port = port || '5985'
end
# deactivate certificate check if requested, default is on
accept_self_signed = false
if (ENV['WINRM_SELF_SIGNED_CERT'])
accept_self_signed = true
end
endpoint = "#{scheme}://#{host}:#{port}/wsman"
winrm = ::WinRM::WinRMWebService.new(endpoint, :ssl, :user => user, :pass => pass, :basic_auth_only => true, :no_ssl_peer_verification => accept_self_signed)
Specinfra.configuration.winrm = winrm
# Run spec on local machine
else
require 'serverspec'
set :backend, :exec
end

View file

@ -32,8 +32,12 @@ module Vulcano
end
# DSL methods
def __register_rule r
def __register_rule r, &block
if block_given?
src = __get_block_source(&block)
else
src = __get_block_source(&r.instance_variable_get(:@__block))
end
r.instance_variable_set(:@__code, src)
fid = VulcanoBaseRule.full_id r, @profile_id
if @rules[fid].nil?

View file

@ -8,12 +8,11 @@ Encoding.default_internal = Encoding::UTF_8
libdir = File.dirname(__FILE__)
$LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir)
require 'utils/spec_helper'
require 'vulcano/version'
require 'vulcano/resource'
require 'vulcano/rule'
require 'vulcano/rspec_json_formatter'
require 'vulcano/rule'
require 'vulcano/runner'
require 'resources/resources'
require 'matchers/matchers'

View file

@ -2,6 +2,7 @@
# copyright: 2015, Dominik Richter
# license: All rights reserved
require 'vulcano/base_rule'
require 'serverspec'
module Vulcano
class Rule < VulcanoBaseRule
@ -31,6 +32,15 @@ module Vulcano::DSL
__register_rule Vulcano::Rule.new(id, opts, &block)
end
def describe *args, &block
@auto_id_cnt ||= 0
@auto_id_cnt += 1
rule = Vulcano::Rule.new(@auto_id_cnt.to_s, {}) do
describe *args, &block
end
__register_rule rule, &block
end
def skip_rule id
__unregister_rule id
end
@ -165,7 +175,10 @@ end
module Vulcano
class ProfileContext
include Serverspec::Helper::Type
extend Serverspec::Helper::Type
include Vulcano::DSL
def initialize profile_id, profile_registry, only_ifs
@profile_id = profile_id
@rules = profile_registry

69
lib/vulcano/runner.rb Normal file
View file

@ -0,0 +1,69 @@
# encoding: utf-8
# copyright: 2015, Dominik Richter
# license: All rights reserved
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
def initialize(profile_id)
@rules = []
@profile_id = profile_id
# RSpec.configuration.output_stream = $stdout
# RSpec.configuration.error_stream = $stderr
RSpec.configuration.add_formatter(:json)
# specinfra
Specinfra::Backend::Cmd.send(:include, Specinfra::Helper::Set)
Specinfra.configuration.backend = :exec
Specinfra.configuration.os = nil
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
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