1 require File.dirname(__FILE__) + '/helper'
3 class TestGod < Test::Unit::TestCase
5 Server.stubs(:new).returns(true)
6 God.stubs(:setup).returns(true)
7 God.stubs(:validater).returns(true)
17 def test_init_should_initialize_watches_to_empty_array
19 assert_equal Hash.new, God.watches
22 def test_init_should_abort_if_called_after_watch
23 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
32 def test_pid_file_directory_should_return_default_if_not_set_explicitly
34 assert_equal '/var/run/god', God.pid_file_directory
37 def test_pid_file_directory_equals_should_set
39 God.pid_file_directory = '/foo'
40 assert_equal '/foo', God.pid_file_directory
45 def test_watch_should_get_stored
53 assert_equal 1, God.watches.size
54 assert_equal watch, God.watches.values.first
56 assert_equal 0, God.groups.size
59 def test_watch_should_get_stored_in_pending_watches
67 assert_equal 1, God.pending_watches.size
68 assert_equal watch, God.pending_watches.first
71 def test_watch_should_register_processes
72 assert_nil God.registry['foo']
77 assert_kind_of God::Process, God.registry['foo']
80 def test_watch_should_get_stored_by_group
90 assert_equal({'test' => [a]}, God.groups)
93 def test_watches_should_get_stored_by_group
111 assert_equal({'test' => [a, b]}, God.groups)
114 def test_watch_should_allow_multiple_watches
115 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
117 assert_nothing_raised do
118 God.watch { |w| w.name = 'bar'; w.start = 'bar' }
122 def test_watch_should_disallow_duplicate_watch_names
123 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
126 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
130 def test_watch_should_disallow_identical_watch_and_group_names
131 God.watch { |w| w.name = 'foo'; w.group = 'bar'; w.start = 'bar' }
134 God.watch { |w| w.name = 'bar'; w.start = 'bar' }
138 def test_watch_should_disallow_identical_watch_and_group_names_other_way
139 God.watch { |w| w.name = 'bar'; w.start = 'bar' }
142 God.watch { |w| w.name = 'foo'; w.group = 'bar'; w.start = 'bar' }
146 def test_watch_should_unwatch_new_watch_if_running_and_duplicate_watch
147 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
150 assert_nothing_raised do
152 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
159 def test_unwatch_should_unmonitor_watch
160 God.watch { |w| w.name = 'bar'; w.start = 'bar' }
161 w = God.watches['bar']
162 w.expects(:unmonitor)
166 def test_unwatch_should_unregister_watch
167 God.watch { |w| w.name = 'bar'; w.start = 'bar' }
168 w = God.watches['bar']
169 w.expects(:unregister!)
175 def test_unwatch_should_remove_same_name_watches
176 God.watch { |w| w.name = 'bar'; w.start = 'bar' }
177 w = God.watches['bar']
181 assert_equal 0, God.watches.size
184 def test_unwatch_should_remove_from_group
190 w = God.watches['bar']
194 assert !God.groups[w.group].include?(w)
199 def test_ping_should_return_true
205 def test_control_should_monitor_on_start
206 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
208 w = God.watches['foo']
210 God.control('foo', 'start')
213 def test_control_should_move_to_restart_on_restart
214 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
216 w = God.watches['foo']
217 w.expects(:move).with(:restart)
218 God.control('foo', 'restart')
221 def test_control_should_unmonitor_and_stop_on_stop
222 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
224 w = God.watches['foo']
225 w.expects(:unmonitor).returns(w)
226 w.expects(:action).with(:stop)
227 God.control('foo', 'stop')
230 def test_control_should_unmonitor_on_unmonitor
231 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
233 w = God.watches['foo']
234 w.expects(:unmonitor).returns(w)
235 God.control('foo', 'unmonitor')
238 def test_control_should_raise_on_invalid_command
239 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
241 assert_raise InvalidCommandError do
242 God.control('foo', 'invalid')
246 def test_control_should_operate_on_each_watch_in_group
259 God.watches['foo1'].expects(:monitor)
260 God.watches['foo2'].expects(:monitor)
262 God.control('bar', 'start')
269 def test_terminate_should_exit
270 God.expects(:exit!).with(0)
276 def test_status_should_show_state
277 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
279 w = God.watches['foo']
281 assert_equal "foo: up", God.status
284 def test_status_should_show_unmonitored_for_nil_state
285 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
287 w = God.watches['foo']
288 assert_equal "foo: unmonitored", God.status
293 def test_running_load_should_eval_code
302 God.running_load(code)
305 assert_equal 1, God.watches.size
308 def test_running_load_should_monitor_new_watches
316 Watch.any_instance.expects(:monitor)
318 God.running_load(code)
322 def test_running_load_should_not_monitor_new_watches_with_autostart_false
331 Watch.any_instance.expects(:monitor).never
333 God.running_load(code)
337 def test_running_load_should_return_array_of_affected_watches
347 w = God.running_load(code)
349 assert_equal 1, w.size
350 assert_equal 'foo', w.first.name
353 def test_running_load_should_clear_pending_watches
362 God.running_load(code)
364 assert_equal 0, God.pending_watches.size
369 def test_load_should_collect_and_load_globbed_path
370 Dir.expects(:[]).with('/path/to/*.thing').returns(['a', 'b'])
371 Kernel.expects(:load).with('a').once
372 Kernel.expects(:load).with('b').once
373 God.load('/path/to/*.thing')
378 def test_start_should_kick_off_a_server_instance
379 Server.expects(:new).returns(true)
383 def test_start_should_start_event_handler
384 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
385 Timer.any_instance.expects(:join)
386 EventHandler.expects(:start).once
392 def test_start_should_begin_monitoring_autostart_watches
398 Timer.any_instance.expects(:join)
399 Watch.any_instance.expects(:monitor).once
403 def test_start_should_not_begin_monitoring_non_autostart_watches
410 Timer.any_instance.expects(:join)
411 Watch.any_instance.expects(:monitor).never
415 def test_start_should_get_and_join_timer
416 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
417 Timer.any_instance.expects(:join)
425 def test_at_exit_should_call_start
426 God.expects(:start).once
432 class TestGodOther < Test::Unit::TestCase
434 Server.stubs(:new).returns(true)
445 def test_setup_should_create_pid_file_directory_if_it_doesnt_exist
446 God.expects(:test).returns(false)
447 FileUtils.expects(:mkdir_p).with(God.pid_file_directory)
451 def test_setup_should_raise_if_no_permissions_to_create_pid_file_directory
452 God.expects(:test).returns(false)
453 FileUtils.expects(:mkdir_p).raises(Errno::EACCES)
462 def test_validate_should_abort_if_pid_file_directory_is_unwriteable
463 God.expects(:test).returns(false)
469 def test_validate_should_not_abort_if_pid_file_directory_is_writeable
470 God.expects(:test).returns(true)
471 assert_nothing_raised do