test/test_process.rb in god-0.4.3 vs test/test_process.rb in god-0.5.0

- old
+ new

@@ -1,12 +1,12 @@ require File.dirname(__FILE__) + '/helper' module God class Process - def fork - raise "You forgot to stub fork" - end + # def fork + # raise "You forgot to stub fork" + # end def exec(*args) raise "You forgot to stub exec" end end @@ -17,10 +17,13 @@ God.internal_init @p = God::Process.new @p.name = 'foo' @p.stubs(:test).returns true # so we don't try to mkdir_p Process.stubs(:detach) # because we stub fork + + ::Process::Sys.stubs(:setuid).returns(true) + ::Process::Sys.stubs(:setgid).returns(true) end # valid? def test_valid_should_return_true_if_auto_daemonized_and_log @@ -37,45 +40,70 @@ assert @p.valid? end def test_valid_should_return_true_if_uid_exists @p.start = 'qux' - @p.log = 'bar' + @p.log = '/tmp/foo.log' @p.uid = 'root' assert @p.valid? end def test_valid_should_return_true_if_uid_does_not_exists @p.start = 'qux' - @p.log = 'bar' + @p.log = '/tmp/foo.log' @p.uid = 'foobarbaz' no_stdout do - assert !@p.valid? + no_stderr do + assert !@p.valid? + end end end def test_valid_should_return_true_if_gid_exists @p.start = 'qux' - @p.log = 'bar' + @p.log = '/tmp/foo.log' @p.gid = 'wheel' assert @p.valid? end def test_valid_should_return_true_if_gid_does_not_exists @p.start = 'qux' - @p.log = 'bar' + @p.log = '/tmp/foo.log' @p.gid = 'foobarbaz' no_stdout do - assert !@p.valid? + no_stderr do + assert !@p.valid? + end end end + + # call_action + + def test_call_action_should_write_pid + # Only for start, restart + [:start, :restart].each do |action| + @p.stubs(:test).returns true + IO.expects(:pipe).returns([StringIO.new('1234'), StringIO.new]) + @p.expects(:fork) + Process.expects(:waitpid) + File.expects(:open).with(@p.default_pid_file, 'w') + @p.send("#{action}=", "run") + @p.call_action(action) + end + end end +############################################################################### +# +# Daemon +# +############################################################################### + class TestProcessDaemon < Test::Unit::TestCase def setup God.internal_init @p = God::Process.new @p.name = 'foo' @@ -90,21 +118,17 @@ File.expects(:read).with('blah.pid').returns('1234') System::Process.any_instance.expects(:exists?).returns(false) assert !@p.alive? end - # valid? - - def test_valid_should_return_false_if_no_name - @p.name = nil - @p.start = 'bar' - @p.stop = 'baz' - no_stdout do - assert !@p.valid? - end + def test_alive_should_return_false_if_no_such_file + File.expects(:read).with('blah.pid').raises(Errno::ENOENT) + assert !@p.alive? end + # valid? + def test_valid_should_return_false_if_no_start @p.name = 'foo' @p.stop = 'baz' no_stdout do assert !@p.valid? @@ -118,67 +142,30 @@ no_stdout do assert !@p.valid? end end - def test_valid_should_return_false_if_self_daemonized_and_log - @p.pid_file = 'foo' - @p.start = 'baz' - @p.stop = 'qux' - @p.log = 'bar' - - no_stdout do - assert !@p.valid? - end - end - # defaul_pid_file def test_default_pid_file assert_equal File.join(God.pid_file_directory, 'foo.pid'), @p.default_pid_file end # call_action # These actually excercise call_action in the back at this point - Kev - def test_call_action_with_string_should_fork_exec + def test_call_action_with_string_should_call_system @p.start = "do something" - IO.expects(:pipe).returns([StringIO.new('1234'), StringIO.new]) @p.expects(:fork) Process.expects(:waitpid) @p.call_action(:start) end def test_call_action_with_lambda_should_call cmd = lambda { puts "Hi" } cmd.expects(:call) @p.start = cmd @p.call_action(:start) - end - - def test_call_action_without_pid_should_write_pid - # Only for start, restart - [:start, :restart].each do |action| - @p = God::Process.new - @p.name = 'foo' - @p.stubs(:test).returns true - IO.expects(:pipe).returns([StringIO.new('1234'), StringIO.new]) - @p.expects(:fork) - Process.expects(:waitpid) - File.expects(:open).with(@p.default_pid_file, 'w') - @p.send("#{action}=", "run") - @p.call_action(action) - end - end - - def test_call_action_should_not_write_pid_for_stop - @p.pid_file = nil - IO.expects(:pipe).returns([StringIO.new('1234'), StringIO.new]) - @p.expects(:fork) - Process.expects(:waitpid) - File.expects(:open).times(0) - @p.stop = "stopping" - @p.call_action(:stop) end def test_call_action_with_invalid_command_class_should_raise @p.start = 5 @p.stop = 'baz' \ No newline at end of file