diff options
author | elijah <elijah@riseup.net> | 2014-12-14 16:17:14 -0800 |
---|---|---|
committer | elijah <elijah@riseup.net> | 2014-12-14 16:17:14 -0800 |
commit | 92db2c6e6210a860a4de4baf8033428fbe72b7cc (patch) | |
tree | 19cb97b35ed094d1a5f844cc4cebdf56dfed91e2 /test/support | |
parent | 0eedbfa5ce7a8ed4799408b591e655070336f0cc (diff) |
added TapiceroProcess, to handle starting and stopping tapicero during tests.
Diffstat (limited to 'test/support')
-rw-r--r-- | test/support/integration_test.rb | 30 | ||||
-rw-r--r-- | test/support/tapicero_process.rb | 126 |
2 files changed, 126 insertions, 30 deletions
diff --git a/test/support/integration_test.rb b/test/support/integration_test.rb index 117fc3f..552b0a1 100644 --- a/test/support/integration_test.rb +++ b/test/support/integration_test.rb @@ -48,35 +48,5 @@ module Tapicero assert false, "Database #{db} should exist." end - def assert_tapicero_running - return if $tapicero_running - pid_file = '/tmp/tapicero.pid' - unless File.exists?(pid_file) - puts 'Tapicero must be running. Run `bin/tapicero run -- test/config.yaml`' - exit(1) - end - pid = File.read(pid_file).strip - if pid !~ /^\d+/ - puts "Bad #{pid_file}: Remove the file and try again."; - exit(1) - else - pid = pid.to_i - end - begin - Process.kill(0, pid) - puts "OK, tapicero is running with process id #{pid}." - $tapicero_running = true - rescue Errno::EPERM - puts "Failed to test tapicero pid: No permission to query #{pid}!" - exit(1) - rescue Errno::ESRCH - puts "Bad #{pid_file}: #{pid} is NOT running. Remove the file and try again."; - exit(1) - rescue - puts "Unable to determine status for tapicero process #{pid} : #{$!}" - exit(1) - end - end - end end diff --git a/test/support/tapicero_process.rb b/test/support/tapicero_process.rb new file mode 100644 index 0000000..fdfb37d --- /dev/null +++ b/test/support/tapicero_process.rb @@ -0,0 +1,126 @@ +## +## TAPICERO PROCESS MANIPULATION +## + +module TapiceroProcess + + public + + def self.run_with_config(config_path) + if running? + if config_path != config_of_running_tapicero() + kill + start(config_path) + end + else + start(config_path) + end + end + + def self.start(config_path) + Dir.chdir(base_dir) do + other_process = fork do + if DEBUG + puts "bin/tapicero run -- '#{config_path}'" + exec "bin/tapicero run -- '#{config_path}'" + else + exec "bin/tapicero run -- '#{config_path}' > /dev/null 2>&1" + end + end + Process.detach(other_process) + 10.times do + sleep 0.1 + break if get_pid + end + if !running? + puts 'Tapicero should be running' + exit 1 + end + end + end + + # kill everything, not just the ones we started + def self.kill! + kill # first try the pid file method + pids = `pgrep -f 'bin/tapicero'`.strip + if !pids.empty? + pids.gsub!("\n", " ") + puts "Killing all bin/tapicero #{pids}" if DEBUG + `pkill -f 'bin/tapicero'` + end + end + + def self.kill + pid = get_pid + if pid + puts "Killing bin/tapicero #{pid}" if DEBUG + Process.kill("TERM", pid) + 10.times do + sleep 0.1 + break if get_pid.nil? + end + if running? + puts 'Tapicero could not be killed' + exit 1 + end + end + end + + private + + def self.base_dir + File.expand_path('../../..', __FILE__) + end + + def self.pid_file + '/tmp/tapicero.pid' + end + + def self.get_pid + if File.exists?(pid_file) + pid = File.read(pid_file).strip + if pid !~ /^\d+/ + puts "Bad #{pid_file}: Remove the file and try again."; + exit(1) + else + return pid.to_i + end + else + return nil + end + end + + def self.running? + return false if !File.exists?(pid_file) + pid = get_pid() + begin + Process.kill(0, pid) + return pid + rescue Errno::EPERM + puts "Failed to test tapicero pid: No permission to query #{_id}!" + exit(1) + rescue Errno::ESRCH + puts "Bad #{pid_file}: #{pid} is NOT running. Remove the file and try again."; + exit(1) + rescue + puts "Unable to determine status for tapicero process #{pid} : #{$!}" + exit(1) + end + end + + # + # returns the path of the currently running tapicero. + # + def self.config_of_running_tapicero + config = `ps -o cmd= --pid #{get_pid}`.split('--')[1] + if config.nil? + puts "Could not determine config file of currently running tapicero. Please kill it and rerun the tests." + else + return config.strip.gsub("'", '') + end + rescue StandardError => ex + puts "Could not parse running tapicero options: #{ex}" + exit 1 + end + +end |