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(:validate).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_control_should_monitor_on_start
200 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
202 w = God.watches['foo']
204 God.control('foo', 'start')
207 def test_control_should_move_to_restart_on_restart
208 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
210 w = God.watches['foo']
211 w.expects(:move).with(:restart)
212 God.control('foo', 'restart')
215 def test_control_should_unmonitor_and_stop_on_stop
216 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
218 w = God.watches['foo']
219 w.expects(:unmonitor).returns(w)
220 w.expects(:action).with(:stop)
221 God.control('foo', 'stop')
224 def test_control_should_unmonitor_on_unmonitor
225 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
227 w = God.watches['foo']
228 w.expects(:unmonitor).returns(w)
229 God.control('foo', 'unmonitor')
232 def test_control_should_raise_on_invalid_command
233 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
235 assert_raise InvalidCommandError do
236 God.control('foo', 'invalid')
240 def test_control_should_operate_on_each_watch_in_group
253 God.watches['foo1'].expects(:monitor)
254 God.watches['foo2'].expects(:monitor)
256 God.control('bar', 'start')
263 def test_terminate_should_exit
264 God.expects(:exit!).with(0)
270 def test_status_should_show_state
271 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
273 w = God.watches['foo']
275 assert_equal "foo: up", God.status
278 def test_status_should_show_unmonitored_for_nil_state
279 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
281 w = God.watches['foo']
282 assert_equal "foo: unmonitored", God.status
287 def test_running_load_should_eval_code
296 God.running_load(code)
299 assert_equal 1, God.watches.size
302 def test_running_load_should_monitor_new_watches
310 Watch.any_instance.expects(:monitor)
312 God.running_load(code)
316 def test_running_load_should_not_monitor_new_watches_with_autostart_false
325 Watch.any_instance.expects(:monitor).never
327 God.running_load(code)
331 def test_running_load_should_return_array_of_affected_watches
341 w = God.running_load(code)
343 assert_equal 1, w.size
344 assert_equal 'foo', w.first.name
347 def test_running_load_should_clear_pending_watches
356 God.running_load(code)
358 assert_equal 0, God.pending_watches.size
363 def test_load_should_collect_and_load_globbed_path
364 Dir.expects(:[]).with('/path/to/*.thing').returns(['a', 'b'])
365 Kernel.expects(:load).with('a').once
366 Kernel.expects(:load).with('b').once
367 God.load('/path/to/*.thing')
372 def test_start_should_kick_off_a_server_instance
373 Server.expects(:new).returns(true)
377 def test_start_should_start_event_handler
378 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
379 Timer.any_instance.expects(:join)
380 EventHandler.expects(:start).once
386 def test_start_should_begin_monitoring_autostart_watches
392 Timer.any_instance.expects(:join)
393 Watch.any_instance.expects(:monitor).once
397 def test_start_should_not_begin_monitoring_non_autostart_watches
404 Timer.any_instance.expects(:join)
405 Watch.any_instance.expects(:monitor).never
409 def test_start_should_get_and_join_timer
410 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
411 Timer.any_instance.expects(:join)
419 def test_at_exit_should_call_start
420 God.expects(:start).once
426 class TestGodOther < Test::Unit::TestCase
428 Server.stubs(:new).returns(true)
439 def test_setup_should_create_pid_file_directory_if_it_doesnt_exist
440 God.expects(:test).returns(false)
441 FileUtils.expects(:mkdir_p).with(God.pid_file_directory)
445 def test_setup_should_raise_if_no_permissions_to_create_pid_file_directory
446 God.expects(:test).returns(false)
447 FileUtils.expects(:mkdir_p).raises(Errno::EACCES)