hydra/test/runner_test.rb

145 lines
4.4 KiB
Ruby
Raw Normal View History

require File.join(File.dirname(__FILE__), 'test_helper')
class RunnerTest < Test::Unit::TestCase
context "with a file to test and a destination to verify" do
setup do
2010-02-17 21:28:32 +00:00
sleep(0.2)
FileUtils.rm_f(target_file)
FileUtils.rm_f(alternate_target_file)
end
teardown do
FileUtils.rm_f(target_file)
FileUtils.rm_f(alternate_target_file)
end
2010-02-03 21:03:16 +00:00
should "run a test in the foreground" do
# flip it around to the parent is in the fork, this gives
# us more direct control over the runner and proper test
# coverage output
pipe = Hydra::Pipe.new
parent = Process.fork do
2010-03-30 17:45:10 +00:00
request_a_file_and_verify_completion(pipe, test_file)
end
run_the_runner(pipe)
Process.wait(parent)
end
# this flips the above test, so that the main process runs a bit of the parent
# code, but only with minimal assertion
2010-02-03 21:03:16 +00:00
should "run a test in the background" do
pipe = Hydra::Pipe.new
child = Process.fork do
run_the_runner(pipe)
end
2010-03-30 17:45:10 +00:00
request_a_file_and_verify_completion(pipe, test_file)
Process.wait(child)
end
2010-02-03 21:03:16 +00:00
2010-04-04 00:32:00 +00:00
should "run two rspec tests" do
runner = Hydra::Runner.new(:io => File.new('/dev/null', 'w'))
2010-04-04 00:32:00 +00:00
runner.run_file(rspec_file)
assert File.exists?(target_file)
assert_equal "HYDRA", File.read(target_file)
2010-04-04 00:32:00 +00:00
FileUtils.rm_f(target_file)
2010-04-04 00:32:00 +00:00
runner.run_file(alternate_rspec_file)
assert File.exists?(alternate_target_file)
assert_equal "HYDRA", File.read(alternate_target_file)
2010-04-04 00:32:00 +00:00
assert !File.exists?(target_file)
2010-03-30 17:45:10 +00:00
end
should "run rspec tests with pending examples" do
runner = Hydra::Runner.new(:io => File.new('/dev/null', 'w'))
assert File.exists?(rspec_file_with_pending)
runner.run_file(rspec_file_with_pending)
assert File.exists?(target_file)
assert_equal "HYDRA", File.read(target_file)
FileUtils.rm_f(target_file)
end
2010-04-04 00:32:00 +00:00
should "run two cucumber tests" do
# because of all the crap cucumber pulls in
# we run this in a fork to not contaminate
# the main test environment
pid = Process.fork do
puts "THE FOLLOWING WARNINGS CAN BE IGNORED"
puts "It is caused by Cucumber loading all rb files near its features"
runner = Hydra::Runner.new(:io => File.new('/dev/null', 'w'))
runner.run_file(cucumber_feature_file)
assert File.exists?(target_file)
assert_equal "HYDRA", File.read(target_file)
FileUtils.rm_f(target_file)
runner.run_file(alternate_cucumber_feature_file)
assert File.exists?(alternate_target_file)
assert_equal "HYDRA", File.read(alternate_target_file)
assert !File.exists?(target_file)
puts "END IGNORABLE OUTPUT"
end
Process.wait pid
end
2010-02-03 21:03:16 +00:00
should "be able to run a runner over ssh" do
ssh = Hydra::SSH.new(
'localhost',
File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib')),
"ruby -e \"require 'rubygems'; require 'hydra'; Hydra::Runner.new(:io => Hydra::Stdio.new, :verbose => true);\""
2010-02-03 21:03:16 +00:00
)
assert ssh.gets.is_a?(Hydra::Messages::Runner::RequestFile)
ssh.write(Hydra::Messages::Worker::RunFile.new(:file => test_file))
# grab its response. This makes us wait for it to finish
echo = ssh.gets # get the ssh echo
2010-02-03 21:03:16 +00:00
response = ssh.gets
assert_equal Hydra::Messages::Runner::Results, response.class
2010-02-03 21:03:16 +00:00
# tell it to shut down
ssh.write(Hydra::Messages::Worker::Shutdown.new)
ssh.close
2010-02-03 21:03:16 +00:00
# ensure it ran
assert File.exists?(target_file)
assert_equal "HYDRA", File.read(target_file)
end
end
module RunnerTestHelper
2010-03-30 17:45:10 +00:00
def request_a_file_and_verify_completion(pipe, file)
pipe.identify_as_parent
# make sure it asks for a file, then give it one
assert pipe.gets.is_a?(Hydra::Messages::Runner::RequestFile)
2010-03-30 17:45:10 +00:00
pipe.write(Hydra::Messages::Worker::RunFile.new(:file => file))
# grab its response. This makes us wait for it to finish
response = pipe.gets
puts response.output
# tell it to shut down
2010-01-29 18:30:25 +00:00
pipe.write(Hydra::Messages::Worker::Shutdown.new)
# ensure it ran
assert File.exists?(target_file)
assert_equal "HYDRA", File.read(target_file)
end
def run_the_runner(pipe)
pipe.identify_as_child
2010-02-03 21:03:16 +00:00
Hydra::Runner.new(:io => pipe)
end
end
include RunnerTestHelper
end