Skip to content

Instantly share code, notes, and snippets.

@rex
Created August 4, 2015 22:58
Show Gist options
  • Save rex/9daeaff9b827002d907b to your computer and use it in GitHub Desktop.
Save rex/9daeaff9b827002d907b to your computer and use it in GitHub Desktop.
Run some or all kitchen tests using converge and verify
#!/usr/bin/env ruby
require 'colorize'
require 'open3'
require 'benchmark'
require 'optparse'
class TestRunner
attr_accessor :converge_enabled, :verify_enabled
def initialize(params = {})
@tests = `kitchen list | grep - | awk '{print $1}'`.split("\n")
@max_length = 0
@max_length_test = nil
@prefix = " > "
@postfix = "..."
@execution_plan = []
@converge_enabled = true
@verify_enabled = true
@filter = params[:filter]
@verbose = params[:verbose] || false
create_execution_plan
determine_longest_name
end
def determine_longest_name
addl_length = "#{@prefix}#{@postfix}".length
@max_length_test = @execution_plan.max_by(&:length)
@max_length = @max_length_test.length + addl_length
end
def start(cmd)
print "#{@prefix}#{cmd}#{@postfix}".ljust(@max_length, '.').light_blue
end
def complete(success)
msg = success ? " [ OK ]".light_green : " [ FAIL ]".red
print msg
end
def append_time(seconds)
m = ( seconds / 60 ).to_f.round(2)
puts "[ #{m} minutes ]".light_yellow
end
def execute_step(cmd, dry = false)
time = Benchmark.realtime do
start cmd
if dry
complete true
else
Open3.popen3(cmd) do |stdin, stdout, stderr, wait_thr|
# while line = stdout.gets
# puts "[STDOUT #{cmd}] #{line}"
# end
# while line = stderr.gets
# puts "[STDERR #{cmd}] #{line}"
# end
result = wait_thr.value
# print " ((( RESULT: #{result} ))) "
complete result.success?
end
end
end
append_time time
end
def add_to_execution_plan(test_name)
@execution_plan << "kitchen converge #{test_name};"
@execution_plan << "kitchen verify #{test_name};"
end
def create_execution_plan
@tests.each do |test_name|
if @filter.nil?
add_to_execution_plan(test_name)
else
if test_name.include?(@filter)
add_to_execution_plan(test_name)
else
puts "[SKIPPED VIA FILTER] #{test_name}" if @verbose
end
end
end
end
def debug_execution_plan
@execution_plan.each do |test_name|
puts "[TO EXECUTE]> #{test_name}"
end
end
def list_available
@tests.each {|t| puts t}
end
def run!
@execution_plan.each do |cmd|
execute_step(cmd)
end
end
end
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: run-tests.rb [options]"
opts.on("-fFILTER", "--filter=FILTER", "Filter Tests to Run") do |f|
options[:filter] = f
end
opts.on("-v", "--verbose", "Verbose Output") do |v|
options[:verbose] = v
end
end.parse!
p options
p ARGV
runner = TestRunner.new(options)
# runner.debug_execution_plan
# runner.list_available
runner.run!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment