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

- old
+ new

@@ -1,44 +1,46 @@ require File.dirname(__FILE__) + '/helper' class TestGod < Test::Unit::TestCase def setup - Server.stubs(:new).returns(true) + God::Socket.stubs(:new).returns(true) God.stubs(:setup).returns(true) God.stubs(:validater).returns(true) God.reset end def teardown Timer.get.timer.kill end - # init + # internal_init def test_init_should_initialize_watches_to_empty_array - God.init { } + God.internal_init { } assert_equal Hash.new, God.watches end - def test_init_should_abort_if_called_after_watch + # init + + def test_pid_file_directory_should_abort_if_called_after_watch God.watch { |w| w.name = 'foo'; w.start = 'bar' } assert_abort do - God.init { } + God.pid_file_directory = 'foo' end end # pid_file_directory def test_pid_file_directory_should_return_default_if_not_set_explicitly - God.init + God.internal_init assert_equal '/var/run/god', God.pid_file_directory end def test_pid_file_directory_equals_should_set - God.init God.pid_file_directory = '/foo' + God.internal_init assert_equal '/foo', God.pid_file_directory end # watch @@ -157,10 +159,11 @@ # unwatch def test_unwatch_should_unmonitor_watch God.watch { |w| w.name = 'bar'; w.start = 'bar' } w = God.watches['bar'] + w.state = :up w.expects(:unmonitor) God.unwatch(w) end def test_unwatch_should_unregister_watch @@ -192,10 +195,73 @@ God.unwatch(w) end assert !God.groups[w.group].include?(w) end + # contact + + def test_contact_should_ensure_init_is_called + God.contact(:fake_contact) { |c| c.name = 'tom' } + assert God.inited + end + + def test_contact_should_abort_on_invalid_contact_kind + assert_abort do + God.contact(:foo) { |c| c.name = 'tom' } + end + end + + def test_contact_should_create_and_store_contact + contact = nil + God.contact(:fake_contact) { |c| c.name = 'tom'; contact = c } + assert [contact], God.contacts + end + + def test_contact_should_add_to_group + God.contact(:fake_contact) { |c| c.name = 'tom'; c.group = 'devs' } + God.contact(:fake_contact) { |c| c.name = 'chris'; c.group = 'devs' } + assert 2, God.contact_groups.size + end + + def test_contact_should_abort_on_no_name + no_stdout do + assert_abort do + God.contact(:fake_contact) { |c| } + end + end + end + + def test_contact_should_abort_on_duplicate_contact_name + God.contact(:fake_contact) { |c| c.name = 'tom' } + assert_abort do + God.contact(:fake_contact) { |c| c.name = 'tom' } + end + end + + def test_contact_should_abort_on_contact_with_same_name_as_group + God.contact(:fake_contact) { |c| c.name = 'tom'; c.group = 'devs' } + assert_abort do + God.contact(:fake_contact) { |c| c.name = 'devs' } + end + end + + def test_contact_should_abort_on_contact_with_same_group_as_name + God.contact(:fake_contact) { |c| c.name = 'tom' } + assert_abort do + God.contact(:fake_contact) { |c| c.name = 'chris'; c.group = 'tom' } + end + end + + def test_contact_should_abort_if_contact_is_invalid + assert_abort do + God.contact(:fake_contact) do |c| + c.name = 'tom' + c.stubs(:valid?).returns(false) + end + end + end + # control def test_control_should_monitor_on_start God.watch { |w| w.name = 'foo'; w.start = 'bar' } @@ -214,19 +280,21 @@ def test_control_should_unmonitor_and_stop_on_stop God.watch { |w| w.name = 'foo'; w.start = 'bar' } w = God.watches['foo'] + w.state = :up w.expects(:unmonitor).returns(w) w.expects(:action).with(:stop) God.control('foo', 'stop') end def test_control_should_unmonitor_on_unmonitor God.watch { |w| w.name = 'foo'; w.start = 'bar' } w = God.watches['foo'] + w.state = :up w.expects(:unmonitor).returns(w) God.control('foo', 'unmonitor') end def test_control_should_raise_on_invalid_command @@ -259,11 +327,11 @@ # stop_all # terminate def test_terminate_should_exit - God.expects(:exit!).with(0) + God.expects(:exit!) God.terminate end # status @@ -280,10 +348,26 @@ w = God.watches['foo'] assert_equal({'foo' => {:state => :unmonitored}}, God.status) end + # running_log + + def test_running_log_should_call_watch_log_since_on_main_log + God.watch { |w| w.name = 'foo'; w.start = 'bar' } + t = Time.now + LOG.expects(:watch_log_since).with('foo', t) + God.running_log('foo', t) + end + + def test_running_log_should_raise_on_unknown_watch + God.internal_init + assert_raise(NoSuchWatchError) do + God.running_log('foo', Time.now) + end + end + # running_load def test_running_load_should_eval_code code = <<-EOF God.watch do |w| @@ -291,11 +375,11 @@ w.start = 'go' end EOF no_stdout do - God.running_load(code) + God.running_load(code, '/foo/bar.god') end assert_equal 1, God.watches.size end @@ -307,11 +391,11 @@ end EOF Watch.any_instance.expects(:monitor) no_stdout do - God.running_load(code) + God.running_load(code, '/foo/bar.god') end end def test_running_load_should_not_monitor_new_watches_with_autostart_false code = <<-EOF @@ -322,11 +406,11 @@ end EOF Watch.any_instance.expects(:monitor).never no_stdout do - God.running_load(code) + God.running_load(code, '/foo/bar.god') end end def test_running_load_should_return_array_of_affected_watches code = <<-EOF @@ -336,14 +420,14 @@ end EOF w = nil no_stdout do - w = God.running_load(code) + w, e = *God.running_load(code, '/foo/bar.god') end assert_equal 1, w.size - assert_equal 'foo', w.first.name + assert_equal 'foo', w.first end def test_running_load_should_clear_pending_watches code = <<-EOF God.watch do |w| @@ -351,11 +435,11 @@ w.start = 'go' end EOF no_stdout do - God.running_load(code) + God.running_load(code, '/foo/bar.god') end assert_equal 0, God.pending_watches.size end # load @@ -368,11 +452,11 @@ end # start def test_start_should_kick_off_a_server_instance - Server.expects(:new).returns(true) + God::Socket.expects(:new).returns(true) God.start end def test_start_should_start_event_handler God.watch { |w| w.name = 'foo'; w.start = 'bar' } @@ -416,17 +500,17 @@ # at_exit def test_at_exit_should_call_start God.expects(:start).once - God.at_exit_orig + God.at_exit end end class TestGodOther < Test::Unit::TestCase def setup - Server.stubs(:new).returns(true) + God::Socket.stubs(:new).returns(true) God.internal_init God.reset end def teardown \ No newline at end of file