From 92db2c6e6210a860a4de4baf8033428fbe72b7cc Mon Sep 17 00:00:00 2001 From: elijah Date: Sun, 14 Dec 2014 16:17:14 -0800 Subject: added TapiceroProcess, to handle starting and stopping tapicero during tests. --- test/badconfig.yaml | 20 +++++++ test/config.yaml | 2 +- test/integration/actions_test.rb | 2 +- test/integration/failure_test.rb | 24 ++++++++ test/support/integration_test.rb | 30 ---------- test/support/tapicero_process.rb | 126 +++++++++++++++++++++++++++++++++++++++ test/test_helper.rb | 11 ++++ 7 files changed, 183 insertions(+), 32 deletions(-) create mode 100644 test/badconfig.yaml create mode 100644 test/integration/failure_test.rb create mode 100644 test/support/tapicero_process.rb (limited to 'test') diff --git a/test/badconfig.yaml b/test/badconfig.yaml new file mode 100644 index 0000000..4941806 --- /dev/null +++ b/test/badconfig.yaml @@ -0,0 +1,20 @@ +# +# A bad config, used to test errors +# + +# couch connection configurationq +connection: + protocol: "http" + host: "localhost" + port: 59840 + ## ^^ wrong port :) + username: ~ + password: ~ + prefix: "tapicero_test" + suffix: "" + +# file to store the last processed user record in so we can resume after +# a restart: +seq_file: "/tmp/tapicero_test.seq" +log_file: "/tmp/tapicero_test.log" +log_level: debug diff --git a/test/config.yaml b/test/config.yaml index c1502db..601c104 100644 --- a/test/config.yaml +++ b/test/config.yaml @@ -1,5 +1,5 @@ # -# Default configuration options for Tapicero +# Default configuration options for Tapicero tests # # couch connection configuration diff --git a/test/integration/actions_test.rb b/test/integration/actions_test.rb index 52c1083..f3d3faa 100644 --- a/test/integration/actions_test.rb +++ b/test/integration/actions_test.rb @@ -3,7 +3,7 @@ require_relative '../test_helper.rb' class ActionsTest < Tapicero::IntegrationTest def setup - assert_tapicero_running + TapiceroProcess.run_with_config("test/config.yaml") create_user end diff --git a/test/integration/failure_test.rb b/test/integration/failure_test.rb new file mode 100644 index 0000000..ac84d05 --- /dev/null +++ b/test/integration/failure_test.rb @@ -0,0 +1,24 @@ +require_relative '../test_helper.rb' + +class FailureTest < Tapicero::IntegrationTest + + def setup + end + + def teardown + end + + def test_couchdb_not_running_and_then_running_again + TapiceroProcess.run_with_config('test/badconfig.yaml') + create_user + assert_raises RestClient::ResourceNotFound do + user_database.info + end + TapiceroProcess.run_with_config('test/config.yaml') + # it would be nice if we could signal tapicero to ask if it is idle. + # instead, we wait. + sleep 0.5 + assert_database_exists user_database + end + +end 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 diff --git a/test/test_helper.rb b/test/test_helper.rb index 4fe81df..ec42f41 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -12,6 +12,10 @@ begin rescue LoadError end +unless defined? DEBUG + DEBUG = ENV["DEBUG"] +end + $:.unshift BASE_DIR + 'lib' require 'mocha/setup' @@ -22,3 +26,10 @@ Tapicero::RERAISE = true require 'tapicero' require_relative 'support/integration_test' + +require_relative 'support/tapicero_process' +TapiceroProcess.kill! +MiniTest.after_run { + TapiceroProcess.kill! +} + -- cgit v1.2.3