test/test_process.rb in god-0.3.0 vs test/test_process.rb in god-0.4.0
- old
+ new
@@ -10,18 +10,138 @@
raise "You forgot to stub exec"
end
end
end
-class TestProcess < Test::Unit::TestCase
+class TestProcessChild < Test::Unit::TestCase
def setup
- @p = God::Process.new(:name => 'foo', :pid_file => 'blah.pid')
+ 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
end
+ # valid?
+
+ def test_valid_should_return_true_if_auto_daemonized_and_log
+ @p.start = 'qux'
+ @p.log = 'bar'
+
+ assert @p.valid?
+ end
+
+ def test_valid_should_return_true_if_auto_daemonized_and_no_stop
+ @p.start = 'qux'
+ @p.log = 'bar'
+
+ assert @p.valid?
+ end
+
+ def test_valid_should_return_true_if_uid_exists
+ @p.start = 'qux'
+ @p.log = 'bar'
+ @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.uid = 'foobarbaz'
+
+ no_stdout do
+ assert !@p.valid?
+ end
+ end
+
+ def test_valid_should_return_true_if_gid_exists
+ @p.start = 'qux'
+ @p.log = 'bar'
+ @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.gid = 'foobarbaz'
+
+ no_stdout do
+ assert !@p.valid?
+ end
+ end
+end
+
+class TestProcessDaemon < Test::Unit::TestCase
+ def setup
+ God.internal_init
+ @p = God::Process.new
+ @p.name = 'foo'
+ @p.pid_file = 'blah.pid'
+ @p.stubs(:test).returns true # so we don't try to mkdir_p
+ Process.stubs(:detach) # because we stub fork
+ end
+
+ # alive?
+
+ def test_alive_should_call_system_process_exists
+ 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
+ end
+
+ def test_valid_should_return_false_if_no_start
+ @p.name = 'foo'
+ @p.stop = 'baz'
+ no_stdout do
+ assert !@p.valid?
+ end
+ end
+
+ def test_valid_should_return_false_if_self_daemonized_and_no_stop
+ @p.start = 'bar'
+ @p.pid_file = 'foo'
+
+ 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
@p.start = "do something"
IO.expects(:pipe).returns([StringIO.new('1234'), StringIO.new])
@p.expects(:fork)
Process.expects(:waitpid)
@@ -32,19 +152,16 @@
cmd = lambda { puts "Hi" }
cmd.expects(:call)
@p.start = cmd
@p.call_action(:start)
end
-
- def test_default_pid_file
- assert_equal File.join(God.pid_file_directory, 'foo.pid'), @p.default_pid_file
- end
def test_call_action_without_pid_should_write_pid
# Only for start, restart
[:start, :restart].each do |action|
- @p = God::Process.new(:name => 'foo')
+ @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')
@@ -61,20 +178,21 @@
File.expects(:open).times(0)
@p.stop = "stopping"
@p.call_action(:stop)
end
- def test_call_action_should_mkdir_p_if_pid_file_dir_existence_test_fails
- @p.pid_file = nil
- IO.expects(:pipe).returns([StringIO.new('1234'), StringIO.new])
- @p.expects(:fork)
- Process.expects(:waitpid)
- @p.expects(:test).returns(false, true)
- FileUtils.expects(:mkdir_p).with(God.pid_file_directory)
- File.expects(:open)
- @p.start = "starting"
- @p.call_action(:start)
+ def test_call_action_with_invalid_command_class_should_raise
+ @p.start = 5
+ @p.stop = 'baz'
+
+ assert @p.valid?
+
+ assert_raise NotImplementedError do
+ @p.call_action(:start)
+ end
end
+
+ # start!/stop!/restart!
def test_start_stop_restart_bang
[:start, :stop, :restart].each do |x|
@p.expects(:call_action).with(x)
@p.send("#{x}!")
\ No newline at end of file