summaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
Diffstat (limited to 'bin')
-rwxr-xr-xbin/debug.sh29
-rw-r--r--bin/node_init86
-rwxr-xr-xbin/puppet_command313
-rwxr-xr-xbin/run_tests515
4 files changed, 943 insertions, 0 deletions
diff --git a/bin/debug.sh b/bin/debug.sh
new file mode 100755
index 00000000..d6f37542
--- /dev/null
+++ b/bin/debug.sh
@@ -0,0 +1,29 @@
+#!/bin/bash
+# debug script to be run on remote servers
+# called from leap_cli with the 'leap debug' cmd
+
+apps='(leap|pixelated|stunnel|couch|soledad|haproxy)'
+
+facts='(apt_running |^architecture |^augeasversion |^couchdb_.* |^debian_.* |^dhcp_enabled |^domain |^facterversion |^filesystems |^fqdn |^hardwaremodel |^hostname |^interface.* |^ipaddress.* |^is_pe |^is_virtual |^kernel.* |^lib |^lsb.* |^memory.* |^mtu_.* |^netmask.* |^network_.* |^operatingsystem |^os.* |^path |^physicalprocessorcount |^processor.* |^ps |^puppetversion |^root_home |^rsyslog_version |^rubysitedir |^rubyversion |^selinux |^ssh_version |^swapfree.* |^swapsize.* |^type |^virtual)'
+
+
+# query facts and filter out private stuff
+export FACTERLIB="/srv/leap/puppet/modules/apache/lib/facter:/srv/leap/puppet/modules/apt/lib/facter:/srv/leap/puppet/modules/concat/lib/facter:/srv/leap/puppet/modules/couchdb/lib/facter:/srv/leap/puppet/modules/rsyslog/lib/facter:/srv/leap/puppet/modules/site_config/lib/facter:/srv/leap/puppet/modules/sshd/lib/facter:/srv/leap/puppet/modules/stdlib/lib/facter"
+
+facter 2>/dev/null | egrep -i "$facts"
+
+# query installed versions
+echo -e '\n\n'
+dpkg -l | egrep "$apps"
+
+
+# query running procs
+echo -e '\n\n'
+ps aux|egrep "$apps"
+
+echo -e '\n\n'
+echo -e "Last deploy:\n"
+tail -2 /var/log/leap/deploy-summary.log
+
+
+
diff --git a/bin/node_init b/bin/node_init
new file mode 100644
index 00000000..da250012
--- /dev/null
+++ b/bin/node_init
@@ -0,0 +1,86 @@
+#!/bin/bash
+#
+# LEAP Platform node initialization.
+# This script is run on the target server when `leap node init` is run.
+#
+
+DEBIAN_VERSION="^(jessie|8\.)"
+LEAP_DIR="/srv/leap"
+HIERA_DIR="/etc/leap"
+INIT_FILE="/srv/leap/initialized"
+REQUIRED_PACKAGES="puppet rsync lsb-release locales"
+
+PATH="/bin:/sbin:/usr/sbin:/usr/bin"
+APT_GET="apt-get -q -y -o DPkg::Options::=--force-confold"
+APT_GET_UPDATE="apt-get update -o Acquire::Languages=none"
+BAD_APT_RESPONSE="(BADSIG|NO_PUBKEY|KEYEXPIRED|REVKEYSIG|NODATA|Could not resolve|failed to fetch)"
+export DEBIAN_FRONTEND=noninteractive
+
+test -f $INIT_FILE && rm $INIT_FILE
+if ! egrep -q "$DEBIAN_VERSION" /etc/debian_version; then
+ echo "ERROR: This operating system is not supported. The file /etc/debian_version must match /$DEBIAN_VERSION/ but is: `cat /etc/debian_version`"
+ exit 1
+fi
+mkdir -p $LEAP_DIR
+echo "en_US.UTF-8 UTF-8" > /etc/locale.gen
+
+#
+# UPDATE PACKAGES
+# (exit code is not reliable, sadly)
+#
+echo "updating package list"
+
+error_count=0
+while read line; do
+ error=$(echo $line | egrep "$BAD_APT_RESPONSE")
+ if [[ $error ]]; then
+ errors[error_count]=$error
+ ((error_count++))
+ break # should we halt on first error?
+ fi
+ echo $line
+done < <($APT_GET_UPDATE 2>&1)
+
+if [[ $error_count > 0 ]]; then
+ echo "ERROR: fatal error in 'apt-get update', bailing out."
+ for e in "${errors[@]}"; do
+ echo " $e"
+ done
+ exit 1
+fi
+
+#
+# UPDATE TIME
+#
+if [[ ! $(which ntpd) ]]; then
+ echo "installing ntpd"
+ $APT_GET install ntp
+ exit_code=$?
+ if [[ $exit_code -ne 0 ]]; then
+ echo "ERROR: bailing out."
+ exit $exit_code
+ fi
+fi
+
+echo "updating server time"
+systemctl -q is-active ntp.service && systemctl stop ntp.service
+ntpd -gxq
+systemctl -q is-active ntp.service || systemctl start ntp.service
+
+#
+# INSTALL PACKAGES
+#
+echo "installing required packages"
+$APT_GET install $REQUIRED_PACKAGES
+exit_code=$?
+if [[ $exit_code -ne 0 ]]; then
+ echo "ERROR: bailing out."
+ exit $exit_code
+fi
+
+#
+# FINALIZE
+#
+mkdir -p $HIERA_DIR
+chmod 0755 $HIERA_DIR
+touch $INIT_FILE
diff --git a/bin/puppet_command b/bin/puppet_command
new file mode 100755
index 00000000..eb3cd0b9
--- /dev/null
+++ b/bin/puppet_command
@@ -0,0 +1,313 @@
+#!/usr/bin/ruby
+
+#
+# This is a wrapper script around the puppet command used by the LEAP platform.
+#
+# We do this in order to make it faster and easier to control puppet remotely
+# (exit codes, logging, lockfile, version check, etc)
+#
+
+require 'pty'
+require 'yaml'
+require 'logger'
+require 'socket'
+require 'fileutils'
+
+DEBIAN_VERSION = /^(jessie|8\.)/
+PUPPET_BIN = '/usr/bin/puppet'
+PUPPET_DIRECTORY = '/srv/leap'
+PUPPET_PARAMETERS = '--color=false --detailed-exitcodes --libdir=puppet/lib --confdir=puppet'
+SITE_MANIFEST = 'puppet/manifests/site.pp'
+SITE_MODULES = 'puppet/modules'
+CUSTOM_MODULES = ':files/puppet/modules'
+DEFAULT_TAGS = 'leap_base,leap_service'
+HIERA_FILE = '/etc/leap/hiera.yaml'
+LOG_DIR = '/var/log/leap'
+DEPLOY_LOG = '/var/log/leap/deploy.log'
+SUMMARY_LOG = '/var/log/leap/deploy-summary.log'
+SUMMARY_LOG_1 = '/var/log/leap/deploy-summary.log.1'
+APPLY_START_STR = "STARTING APPLY"
+APPLY_FINISH_STR = "APPLY COMPLETE"
+
+
+def main
+ if File.read('/etc/debian_version') !~ DEBIAN_VERSION
+ log "ERROR: This operating system is not supported. The file /etc/debian_version must match #{DEBIAN_VERSION}."
+ exit 1
+ end
+ process_command_line_arguments
+ with_lockfile do
+ @commands.each do |command|
+ self.send(command)
+ end
+ end
+end
+
+def open_log_files
+ FileUtils.mkdir_p(LOG_DIR)
+ $logger = Logger.new(DEPLOY_LOG)
+ $summary_logger = Logger.new(SUMMARY_LOG)
+ [$logger, $summary_logger].each do |logger|
+ logger.level = Logger::INFO
+ logger.formatter = proc do |severity, datetime, progname, msg|
+ "%s %s: %s\n" % [datetime.strftime("%b %d %H:%M:%S"), Socket.gethostname, msg]
+ end
+ end
+end
+
+def close_log_files
+ $logger.close
+ $summary_logger.close
+end
+
+def log(str, *args)
+ str = str.strip
+ $stdout.puts str
+ $stdout.flush
+ if $logger
+ $logger.info(str)
+ if args.include? :summary
+ $summary_logger.info(str)
+ end
+ end
+end
+
+def process_command_line_arguments
+ @commands = []
+ @verbosity = 1
+ @tags = DEFAULT_TAGS
+ @info = {}
+ @downgrade = false
+ loop do
+ case ARGV[0]
+ when 'apply' then ARGV.shift; @commands << 'apply'
+ when 'set_hostname' then ARGV.shift; @commands << 'set_hostname'
+ when '--verbosity' then ARGV.shift; @verbosity = ARGV.shift.to_i
+ when '--force' then ARGV.shift; remove_lockfile
+ when '--tags' then ARGV.shift; @tags = ARGV.shift
+ when '--info' then ARGV.shift; @info = parse_info(ARGV.shift)
+ when '--downgrade' then ARGV.shift; @downgrade = true
+ when /^-/ then usage("Unknown option: #{ARGV[0].inspect}")
+ else break
+ end
+ end
+ usage("No command given") unless @commands.any?
+end
+
+def apply
+ platform_version_check! unless @downgrade
+ log "#{APPLY_START_STR} {#{format_info(@info)}}", :summary
+ exit_code = puppet_apply do |line|
+ log line
+ end
+ log "#{APPLY_FINISH_STR} (#{exitcode_description(exit_code)}) {#{format_info(@info)}}", :summary
+end
+
+def set_hostname
+ hostname = hiera_file['name']
+ if hostname.nil? || hostname.empty?
+ log('ERROR: "name" missing from hiera file')
+ exit(1)
+ end
+ current_hostname_file = File.read('/etc/hostname') rescue nil
+ current_hostname = `/bin/hostname`.strip
+
+ # set /etc/hostname
+ if current_hostname_file != hostname
+ File.open('/etc/hostname', 'w', 0611, :encoding => 'ascii') do |f|
+ f.write hostname
+ end
+ if File.read('/etc/hostname') == hostname
+ log "Changed /etc/hostname to #{hostname}"
+ else
+ log "ERROR: failed to update /etc/hostname"
+ end
+ end
+
+ # call /bin/hostname
+ if current_hostname != hostname
+ if run("/bin/hostname #{hostname}") == 0
+ log "Changed hostname to #{hostname}"
+ else
+ log "ERROR: call to `/bin/hostname #{hostname}` returned an error."
+ end
+ end
+end
+
+#
+# each line of output is yielded. the exit code is returned.
+#
+def puppet_apply(options={}, &block)
+ options = {:verbosity => @verbosity, :tags => @tags}.merge(options)
+ manifest = options[:manifest] || SITE_MANIFEST
+ modulepath = options[:module_path] || SITE_MODULES + CUSTOM_MODULES
+ fqdn = hiera_file['domain']['full']
+ domain = hiera_file['domain']['full_suffix']
+ Dir.chdir(PUPPET_DIRECTORY) do
+ return run("FACTER_fqdn='#{fqdn}' FACTER_domain='#{domain}' #{PUPPET_BIN} apply #{custom_parameters(options)} --modulepath='#{modulepath}' #{PUPPET_PARAMETERS} #{manifest}", &block)
+ end
+end
+
+#
+# parse the --info flag. example str: "key1: value1, key2: value2, ..."
+#
+def parse_info(str)
+ str.split(', ').
+ map {|i| i.split(': ')}.
+ inject({}) {|h,i| h[i[0]] = i[1]; h}
+rescue Exception => exc
+ {"platform" => "INVALID_FORMAT"}
+end
+
+def format_info(info)
+ info.to_a.map{|i|i.join(': ')}.join(', ')
+end
+
+#
+# exits with a warning message if the last successful deployed
+# platform was newer than the one we are currently attempting to
+# deploy.
+#
+PLATFORM_RE = /\{.*platform: ([0-9\.]+)[ ,\}].*[\}$]/
+def platform_version_check!
+ return unless @info["platform"]
+ new_version = @info["platform"].split(' ').first
+ return unless new_version
+ if File.exists?(SUMMARY_LOG) && File.size(SUMMARY_LOG) != 0
+ file = SUMMARY_LOG
+ elsif File.exists?(SUMMARY_LOG_1) && File.size(SUMMARY_LOG_1) != 0
+ file = SUMMARY_LOG_1
+ else
+ return
+ end
+ most_recent_line = `tail '#{file}'`.split("\n").grep(PLATFORM_RE).last
+ if most_recent_line
+ prior_version = most_recent_line.match(PLATFORM_RE)[1]
+ if Gem::Version.new(prior_version) > Gem::Version.new(new_version)
+ log("ERROR: You are attempting to deploy platform v#{new_version} but this node uses v#{prior_version}.")
+ log(" Run with --downgrade if you really want to deploy an older platform version.")
+ exit(0)
+ end
+ end
+end
+
+#
+# Return a ruby object representing the contents of the hiera yaml file.
+#
+def hiera_file
+ unless File.exists?(HIERA_FILE)
+ log("ERROR: hiera file '#{HIERA_FILE}' does not exist.")
+ exit(1)
+ end
+ $hiera_contents ||= YAML.load_file(HIERA_FILE)
+ return $hiera_contents
+rescue Exception => exc
+ log("ERROR: problem reading hiera file '#{HIERA_FILE}' (#{exc})")
+ exit(1)
+end
+
+def custom_parameters(options)
+ params = []
+ if options[:tags] && options[:tags].chars.any?
+ params << "--tags #{options[:tags]}"
+ end
+ if options[:verbosity]
+ case options[:verbosity]
+ when 3 then params << '--verbose'
+ when 4 then params << '--verbose --debug'
+ when 5 then params << '--verbose --debug --trace'
+ end
+ end
+ params.join(' ')
+end
+
+def exitcode_description(code)
+ case code
+ when 0 then "no changes"
+ when 1 then "failed"
+ when 2 then "changes made"
+ when 4 then "failed"
+ when 6 then "changes and failures"
+ else code
+ end
+end
+
+def usage(s)
+ $stderr.puts(s)
+ $stderr.puts
+ $stderr.puts("Usage: #{File.basename($0)} COMMAND [OPTIONS]")
+ $stderr.puts
+ $stderr.puts("COMMAND may be one or more of:
+ set_hostname -- set the hostname of this server.
+ apply -- apply puppet manifests.")
+ $stderr.puts
+ $stderr.puts("OPTIONS may be one or more of:
+ --verbosity VERB -- set the verbosity level 0..5.
+ --tags TAGS -- set the tags to pass through to puppet.
+ --force -- run even when lockfile is present.
+ --info -- additional info to include in logs (e.g. 'user: alice, platform: 0.6.1')
+ --downgrade -- allow a deploy even if the platform version is older than previous deploy.
+ ")
+ exit(2)
+end
+
+##
+## Simple lock file
+##
+
+require 'fileutils'
+DEFAULT_LOCKFILE = '/tmp/puppet.lock'
+
+def remove_lockfile(lock_file_path=DEFAULT_LOCKFILE)
+ FileUtils.remove_file(lock_file_path, true)
+end
+
+def with_lockfile(lock_file_path=DEFAULT_LOCKFILE)
+ begin
+ File.open(lock_file_path, File::CREAT | File::EXCL | File::WRONLY) do |o|
+ o.write(Process.pid)
+ end
+ open_log_files
+ yield
+ remove_lockfile
+ close_log_files
+ rescue Errno::EEXIST
+ log("ERROR: the lock file '#{lock_file_path}' already exists. Wait a minute for the process to die, or run with --force to ignore. Bailing out.")
+ exit(1)
+ rescue IOError => exc
+ log("ERROR: problem with lock file '#{lock_file_path}' (#{exc}). Bailing out.")
+ exit(1)
+ end
+end
+
+##
+## simple pass through process runner (to ensure output is not buffered and return exit code)
+## this only works under ruby 1.9
+##
+
+def run(cmd)
+ log(cmd) if @verbosity >= 3
+ PTY.spawn("#{cmd}") do |output, input, pid|
+ begin
+ while line = output.gets do
+ yield line
+ end
+ rescue Errno::EIO
+ end
+ Process.wait(pid) # only works in ruby 1.9, required to capture the exit status.
+ end
+ return $?.exitstatus
+rescue PTY::ChildExited
+end
+
+##
+## RUN MAIN
+##
+
+Signal.trap("EXIT") do
+ remove_lockfile # clean up the lockfile when process is terminated.
+ # this will remove the lockfile if ^C killed the process
+ # but only after the child puppet process is also dead (I think).
+end
+
+main()
diff --git a/bin/run_tests b/bin/run_tests
new file mode 100755
index 00000000..b6784ed5
--- /dev/null
+++ b/bin/run_tests
@@ -0,0 +1,515 @@
+#!/usr/bin/ruby
+
+#
+# this script will run the unit tests in ../tests/*.rb.
+#
+# Tests for the platform differ from traditional ruby unit tests in a few ways:
+#
+# (1) at the end of every test function, you should call 'pass()'
+# (2) you can specify test dependencies by calling depends_on("TestFirst") in the test class definition.
+# (3) test functions are always run in alphabetical order.
+# (4) any halt or error will stop the testing unless --continue is specified.
+#
+
+require 'minitest/unit'
+require 'yaml'
+require 'tsort'
+require 'timeout'
+
+##
+## CONSTANTS
+##
+
+EXIT_CODES = {
+ :success => 0,
+ :warning => 1,
+ :failure => 2,
+ :error => 3
+}
+
+HIERA_FILE = '/etc/leap/hiera.yaml'
+HELPER_PATHS = [
+ '../../tests/helpers/*.rb',
+ '/srv/leap/files/tests/helpers/*.rb'
+]
+TEST_PATHS = [
+ '../../tests/white-box/*.rb',
+ '/srv/leap/files/tests/white-box/*.rb',
+ '/srv/leap/tests_custom/*.rb'
+]
+
+##
+## UTILITY
+##
+
+def bail(code, msg=nil)
+ puts msg if msg
+ if code.is_a? Symbol
+ exit(EXIT_CODES[code])
+ else
+ exit(code)
+ end
+end
+
+def service?(service)
+ $node["services"].include?(service.to_s)
+end
+
+##
+## EXCEPTIONS
+##
+
+# this class is raised if a test file wants to be skipped entirely.
+# (to skip an individual test, MiniTest::Skip is used instead)
+class SkipTest < StandardError
+end
+
+# raised if --no-continue and there is an error
+class TestError < StandardError
+end
+
+# raised if --no-continue and there is a failure
+class TestFailure < StandardError
+end
+
+##
+## CUSTOM UNIT TEST CLASS
+##
+
+#
+# Our custom unit test class. All tests should be subclasses of this.
+#
+class LeapTest < MiniTest::Unit::TestCase
+ class Pass < MiniTest::Assertion
+ end
+ class SilentPass < Pass
+ end
+ class Ignore < MiniTest::Assertion
+ end
+
+ def initialize(name)
+ super(name)
+ io # << calling this will suppress the marching ants
+ end
+
+ #
+ # Test class dependencies
+ #
+ def self.depends_on(*class_names)
+ @dependencies ||= []
+ @dependencies += class_names
+ end
+ def self.dependencies
+ @dependencies || []
+ end
+
+ #
+ # returns all the test classes, sorted in dependency order.
+ #
+ def self.test_classes
+ classes = ObjectSpace.each_object(Class).select {|test_class|
+ test_class.ancestors.include?(self)
+ }
+ return TestDependencyGraph.new(classes).sorted
+ end
+
+ def self.tests
+ self.instance_methods.grep(/^test_/).sort
+ end
+
+ #
+ # thrown Timeout::Error if test run
+ # takes longer than $timeout
+ #
+ def run(*args)
+ Timeout::timeout($timeout, Timeout::Error) do
+ super(*args)
+ end
+ end
+
+ #
+ # The default pass just does an `assert true`. In our case, we want to make the passes more explicit.
+ #
+ def pass
+ raise LeapTest::Pass
+ end
+
+ #
+ # This is just like pass(), but the result is normally silent, unless `run_tests --test TEST`
+ def silent_pass
+ raise LeapTest::SilentPass
+ end
+
+ #
+ # Called when the test should be silently ignored.
+ #
+ def ignore
+ raise LeapTest::Ignore
+ end
+
+ #
+ # the default fail() is part of the kernel and it just throws a runtime exception. for tests,
+ # we want the same behavior as assert(false)
+ #
+ def fail(msg=nil, exception=nil)
+ if DEBUG && exception && exception.respond_to?(:backtrace)
+ msg += MiniTest::filter_backtrace(exception.backtrace).join "\n"
+ end
+ assert(false, msg)
+ end
+
+ def warn(*msg)
+ method_name = caller.first.split('`').last.gsub(/(block in |')/,'')
+ MiniTest::Unit.runner.warn(self.class, method_name, msg.join("\n"))
+ end
+
+ #
+ # Always runs test methods within a test class in alphanumeric order
+ #
+ def self.test_order
+ :alpha
+ end
+
+end
+
+#
+# Custom test runner in order to modify the output.
+#
+class LeapRunner < MiniTest::Unit
+
+ attr_accessor :passes, :warnings
+
+ def initialize
+ @passes = 0
+ @warnings = 0
+ @ignores = 0
+ super
+ end
+
+ #
+ # call stack:
+ # MiniTest::Unit.new.run
+ # MiniTest::Unit.runner
+ # LeapTest._run
+ #
+ def _run args = []
+ if $pinned_test_class
+ suites = [$pinned_test_class]
+ if $pinned_test_method
+ options.merge!(:filter => $pinned_test_method.to_s)
+ end
+ else
+ suites = LeapTest.send "test_suites"
+ suites = TestDependencyGraph.new(suites).sorted
+ end
+ output.sync = true
+ results = _run_suites(suites, :test)
+ @test_count = results.inject(0) { |sum, (tc, _)| sum + tc }
+ @assertion_count = results.inject(0) { |sum, (_, ac)| sum + ac }
+ status
+ return exit_code()
+ rescue Interrupt
+ bail :error, 'Tests halted on interrupt.'
+ rescue TestFailure
+ bail :failure, 'Tests halted on failure (because of --no-continue).'
+ rescue TestError
+ bail :error, 'Tests halted on error (because of --no-continue).'
+ end
+
+ #
+ # override puke to change what prints out.
+ #
+ def puke(klass, meth, e)
+ case e
+ when MiniTest::Skip then
+ @skips += 1
+ report_line("SKIP", klass, meth, e, e.message)
+ when LeapTest::Ignore then
+ @ignores += 1
+ if @verbose
+ report_line("IGNORE", klass, meth, e, e.message)
+ end
+ when LeapTest::SilentPass then
+ if $pinned_test_method || $output_format == :checkmk
+ report_line("PASS", klass, meth)
+ end
+ when LeapTest::Pass then
+ @passes += 1
+ report_line("PASS", klass, meth)
+ when MiniTest::Assertion then
+ @failures += 1
+ report_line("FAIL", klass, meth, e, e.message)
+ if $halt_on_failure
+ raise TestFailure.new
+ end
+ when Timeout::Error then
+ @failures += 1
+ report_line("TIMEOUT", klass, meth, nil, "Test stopped because timeout exceeded (#{$timeout} seconds).")
+ if $halt_on_failure
+ raise TestFailure.new
+ end
+ else
+ @errors += 1
+ bt = MiniTest::filter_backtrace(e.backtrace).join "\n"
+ report_line("ERROR", klass, meth, e, "#{e.class}: #{e.message}\n#{bt}")
+ if $halt_on_failure
+ raise TestError.new
+ end
+ end
+ return "" # disable the marching ants
+ end
+
+ #
+ # override default status summary
+ #
+ def status(io = self.output)
+ if $output_format == :human
+ format = "%d tests: %d passes, %d skips, %d warnings, %d failures, %d errors"
+ output.puts format % [test_count, passes, skips, warnings, failures, errors]
+ end
+ end
+
+ #
+ # return an appropriate exit_code symbol
+ #
+ def exit_code
+ if @errors > 0
+ :error
+ elsif @failures > 0
+ :failure
+ elsif @warnings > 0
+ # :warning << warnings don't warrant a non-zero exit code.
+ :success
+ else
+ :success
+ end
+ end
+
+ #
+ # returns a string for a PASS, SKIP, or FAIL error
+ #
+ def report_line(prefix, klass, meth, e=nil, message=nil)
+ msg_txt = nil
+ if message
+ message = message.gsub(/http:\/\/([a-z_]+):([a-zA-Z0-9_]+)@/, "http://\\1:REDACTED@")
+ if $output_format == :human
+ indent = "\n "
+ msg_txt = indent + message.split("\n").join(indent)
+ else
+ msg_txt = message.gsub("\n", ' ')
+ end
+ end
+
+ if $output_format == :human
+ if e && msg_txt
+ output.puts "#{prefix}: #{readable(klass.name)} > #{readable(meth)} [#{File.basename(location(e))}]:#{msg_txt}"
+ elsif msg_txt
+ output.puts "#{prefix}: #{readable(klass.name)} > #{readable(meth)}:#{msg_txt}"
+ else
+ output.puts "#{prefix}: #{readable(klass.name)} > #{readable(meth)}"
+ end
+ # I don't understand at all why, but adding a very tiny sleep here will
+ sleep(0.0001) # keep lines from being joined together by the logger. output.flush doesn't.
+ elsif $output_format == :checkmk
+ code = CHECKMK_CODES[prefix]
+ msg_txt ||= "Success" if prefix == "PASS"
+ if e && msg_txt
+ output.puts "#{code} #{klass.name}/#{machine_readable(meth)} - [#{File.basename(location(e))}]:#{msg_txt}"
+ elsif msg_txt
+ output.puts "#{code} #{klass.name}/#{machine_readable(meth)} - #{msg_txt}"
+ else
+ output.puts "#{code} #{klass.name}/#{machine_readable(meth)} - no message"
+ end
+ end
+ end
+
+ #
+ # a new function used by TestCase to report warnings.
+ #
+ def warn(klass, method_name, msg)
+ @warnings += 1
+ report_line("WARN", klass, method_name, nil, msg)
+ end
+
+ private
+
+ CHECKMK_CODES = {"PASS" => 0, "SKIP" => 1, "FAIL" => 2, "ERROR" => 3}
+
+ #
+ # Converts snake_case and CamelCase to something more pleasant for humans to read.
+ #
+ def readable(str)
+ str.
+ gsub(/_/, ' ').
+ sub(/^test (\d* )?/i, '')
+ end
+
+ def machine_readable(str)
+ str.sub(/^test_(\d+_)?/i, '')
+ end
+
+end
+
+##
+## Dependency resolution
+## Use a topographical sort to manage test dependencies
+##
+
+class TestDependencyGraph
+ include TSort
+
+ def initialize(test_classes)
+ @dependencies = {} # each key is a test class name, and the values
+ # are arrays of test class names that the key depends on.
+ test_classes.each do |test_class|
+ @dependencies[test_class.name] = test_class.dependencies
+ end
+ end
+
+ def tsort_each_node(&block)
+ @dependencies.each_key(&block)
+ end
+
+ def tsort_each_child(test_class_name, &block)
+ if @dependencies[test_class_name]
+ @dependencies[test_class_name].each(&block)
+ else
+ puts "ERROR: bad dependency, no such class `#{test_class_name}`"
+ bail :error
+ end
+ end
+
+ def sorted
+ self.tsort.collect {|class_name|
+ Kernel.const_get(class_name)
+ }
+ end
+end
+
+##
+## COMMAND LINE ACTIONS
+##
+
+def die(test, msg)
+ if $output_format == :human
+ puts "ERROR in test `#{test}`: #{msg}"
+ elsif $output_format == :checkmk
+ puts "3 #{test} - #{msg}"
+ end
+ bail :error
+end
+
+def print_help
+ puts ["USAGE: run_tests [OPTIONS]",
+ " --continue Don't halt on an error, but continue to the next test.",
+ " --checkmk Print test results in checkmk format (must come before --test).",
+ " --test TEST Run only the test with name TEST.",
+ " --list-tests Prints the names of all available tests and exit.",
+ " --retry COUNT If the tests don't pass, retry COUNT additional times (default is zero).",
+ " --timeout SECONDS Halt a test if it exceed SECONDS (default is 30).",
+ " --wait SECONDS Wait for SECONDS between retries (default is 5).",
+ " --debug Print out full stack trace on errors."].join("\n")
+ exit(0)
+end
+
+def list_tests
+ LeapTest.test_classes.each do |test_class|
+ test_class.tests.each do |test|
+ puts test_class.name + "/" + test.to_s.sub(/^test_(\d+_)?/, '')
+ end
+ end
+ exit(0)
+end
+
+def pin_test_name(name)
+ test_class, test_name = name.split('/')
+ $pinned_test_class = LeapTest.test_classes.detect{|c| c.name == test_class}
+ unless $pinned_test_class
+ die name, "there is no test class `#{test_class}`"
+ end
+ if test_name
+ $pinned_test_method = $pinned_test_class.tests.detect{|m| m.to_s =~ /^test_(\d+_)?#{Regexp.escape(test_name)}$/}
+ unless $pinned_test_method
+ die name, "there is no test `#{test_name}` in class `#{test_class}`"
+ end
+ end
+end
+
+#
+# run the tests, multiple times if `--retry` and not all tests were successful.
+#
+def run_tests
+ exit_code = nil
+ run_count = $retry ? $retry + 1 : 1
+ run_count.times do |i|
+ MiniTest::Unit.runner = LeapRunner.new
+ exit_code = MiniTest::Unit.new.run
+ if !$retry || exit_code == :success
+ break
+ elsif i != run_count-1
+ sleep $wait
+ end
+ end
+ bail exit_code
+end
+
+##
+## MAIN
+##
+
+def main
+ # load node data from hiera file
+ if File.exists?(HIERA_FILE)
+ $node = YAML.load_file(HIERA_FILE)
+ else
+ $node = {"services" => [], "dummy" => true}
+ end
+
+ # load all test classes
+ this_file = File.symlink?(__FILE__) ? File.readlink(__FILE__) : __FILE__
+ HELPER_PATHS.each do |path|
+ Dir[File.expand_path(path, this_file)].each do |helper|
+ require helper
+ end
+ end
+ TEST_PATHS.each do |path|
+ Dir[File.expand_path(path, this_file)].each do |test_file|
+ begin
+ require test_file
+ rescue SkipTest
+ end
+ end
+ end
+
+ # parse command line options
+ $halt_on_failure = true
+ $output_format = :human
+ $retry = false
+ $wait = 5
+ $timeout = 30
+ loop do
+ case ARGV[0]
+ when '--continue' then ARGV.shift; $halt_on_failure = false;
+ when '--checkmk' then ARGV.shift; $output_format = :checkmk; $halt_on_failure = false
+ when '--help' then print_help
+ when '--test' then ARGV.shift; pin_test_name(ARGV.shift)
+ when '--list-tests' then list_tests
+ when '--retry' then ARGV.shift; $retry = ARGV.shift.to_i
+ when '--timeout' then ARGV.shift; $timeout = ARGV.shift.to_i;
+ when '--wait' then ARGV.shift; $wait = ARGV.shift.to_i
+ when '--debug' then ARGV.shift
+ when '-d' then ARGV.shift
+ else break
+ end
+ end
+ run_tests
+end
+
+if ARGV.include?('--debug') || ARGV.include?('-d')
+ DEBUG=true
+ require 'debugger'
+else
+ DEBUG=false
+end
+
+main()