1 require File.dirname(__FILE__) + '/helper'
3 class TestGod < Test::Unit::TestCase
5 God::Socket.stubs(:new).returns(true)
6 God.stubs(:setup).returns(true)
7 God.stubs(:validater).returns(true)
18 LOG.expects(:log).with(nil, :debug, 'foo')
19 applog(nil, :debug, 'foo')
24 def test_init_should_initialize_watches_to_empty_array
26 assert_equal Hash.new, God.watches
31 def test_pid_file_directory_should_abort_if_called_after_watch
32 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
35 God.pid_file_directory = 'foo'
41 def test_pid_file_directory_should_return_default_if_not_set_explicitly
43 assert_equal '/var/run/god', God.pid_file_directory
46 def test_pid_file_directory_equals_should_set
47 God.pid_file_directory = '/foo'
49 assert_equal '/foo', God.pid_file_directory
54 def test_watch_should_get_stored
62 assert_equal 1, God.watches.size
63 assert_equal watch, God.watches.values.first
65 assert_equal 0, God.groups.size
68 def test_watch_should_get_stored_in_pending_watches
76 assert_equal 1, God.pending_watches.size
77 assert_equal watch, God.pending_watches.first
80 def test_watch_should_register_processes
81 assert_nil God.registry['foo']
86 assert_kind_of God::Process, God.registry['foo']
89 def test_watch_should_get_stored_by_group
99 assert_equal({'test' => [a]}, God.groups)
102 def test_watches_should_get_stored_by_group
120 assert_equal({'test' => [a, b]}, God.groups)
123 def test_watch_should_allow_multiple_watches
124 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
126 assert_nothing_raised do
127 God.watch { |w| w.name = 'bar'; w.start = 'bar' }
131 def test_watch_should_disallow_duplicate_watch_names
132 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
135 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
139 def test_watch_should_disallow_identical_watch_and_group_names
140 God.watch { |w| w.name = 'foo'; w.group = 'bar'; w.start = 'bar' }
143 God.watch { |w| w.name = 'bar'; w.start = 'bar' }
147 def test_watch_should_disallow_identical_watch_and_group_names_other_way
148 God.watch { |w| w.name = 'bar'; w.start = 'bar' }
151 God.watch { |w| w.name = 'foo'; w.group = 'bar'; w.start = 'bar' }
155 def test_watch_should_unwatch_new_watch_if_running_and_duplicate_watch
156 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
159 assert_nothing_raised do
161 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
168 def test_unwatch_should_unmonitor_watch
169 God.watch { |w| w.name = 'bar'; w.start = 'bar' }
170 w = God.watches['bar']
172 w.expects(:unmonitor)
178 def test_unwatch_should_unregister_watch
179 God.watch { |w| w.name = 'bar'; w.start = 'bar' }
180 w = God.watches['bar']
181 w.expects(:unregister!)
187 def test_unwatch_should_remove_same_name_watches
188 God.watch { |w| w.name = 'bar'; w.start = 'bar' }
189 w = God.watches['bar']
193 assert_equal 0, God.watches.size
196 def test_unwatch_should_remove_from_group
202 w = God.watches['bar']
206 assert !God.groups[w.group].include?(w)
211 def test_contact_should_ensure_init_is_called
212 God.contact(:fake_contact) { |c| c.name = 'tom' }
216 def test_contact_should_abort_on_invalid_contact_kind
218 God.contact(:foo) { |c| c.name = 'tom' }
222 def test_contact_should_create_and_store_contact
224 God.contact(:fake_contact) { |c| c.name = 'tom'; contact = c }
225 assert [contact], God.contacts
228 def test_contact_should_add_to_group
229 God.contact(:fake_contact) { |c| c.name = 'tom'; c.group = 'devs' }
230 God.contact(:fake_contact) { |c| c.name = 'chris'; c.group = 'devs' }
231 assert 2, God.contact_groups.size
234 def test_contact_should_abort_on_no_name
237 God.contact(:fake_contact) { |c| }
242 def test_contact_should_abort_on_duplicate_contact_name
243 God.contact(:fake_contact) { |c| c.name = 'tom' }
245 God.contact(:fake_contact) { |c| c.name = 'tom' }
249 def test_contact_should_abort_on_contact_with_same_name_as_group
250 God.contact(:fake_contact) { |c| c.name = 'tom'; c.group = 'devs' }
252 God.contact(:fake_contact) { |c| c.name = 'devs' }
256 def test_contact_should_abort_on_contact_with_same_group_as_name
257 God.contact(:fake_contact) { |c| c.name = 'tom' }
259 God.contact(:fake_contact) { |c| c.name = 'chris'; c.group = 'tom' }
263 def test_contact_should_abort_if_contact_is_invalid
265 God.contact(:fake_contact) do |c|
267 c.stubs(:valid?).returns(false)
274 def test_control_should_monitor_on_start
275 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
277 w = God.watches['foo']
279 God.control('foo', 'start')
282 def test_control_should_move_to_restart_on_restart
283 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
285 w = God.watches['foo']
286 w.expects(:move).with(:restart)
287 God.control('foo', 'restart')
290 def test_control_should_unmonitor_and_stop_on_stop
291 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
293 w = God.watches['foo']
295 w.expects(:unmonitor).returns(w)
296 w.expects(:action).with(:stop)
297 God.control('foo', 'stop')
300 def test_control_should_unmonitor_on_unmonitor
301 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
303 w = God.watches['foo']
305 w.expects(:unmonitor).returns(w)
306 God.control('foo', 'unmonitor')
309 def test_control_should_unwatch_on_remove
310 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
312 w = God.watches['foo']
314 God.expects(:unwatch)
315 God.control('foo', 'remove')
318 def test_control_should_raise_on_invalid_command
319 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
321 assert_raise InvalidCommandError do
322 God.control('foo', 'invalid')
326 def test_control_should_operate_on_each_watch_in_group
339 God.watches['foo1'].expects(:monitor)
340 God.watches['foo2'].expects(:monitor)
342 God.control('bar', 'start')
349 def test_terminate_should_exit
351 FileUtils.expects(:rm_f).never
356 def test_terminate_should_delete_pid
358 FileUtils.expects(:rm_f).with("/foo/bar")
365 def test_status_should_show_state
366 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
368 w = God.watches['foo']
370 assert_equal({'foo' => {:state => :up}}, God.status)
373 def test_status_should_show_unmonitored_for_nil_state
374 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
376 w = God.watches['foo']
377 assert_equal({'foo' => {:state => :unmonitored}}, God.status)
382 def test_running_log_should_call_watch_log_since_on_main_log
383 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
385 LOG.expects(:watch_log_since).with('foo', t)
386 God.running_log('foo', t)
389 def test_running_log_should_raise_on_unknown_watch
391 assert_raise(NoSuchWatchError) do
392 God.running_log('foo', Time.now)
398 def test_running_load_should_eval_code
407 God.running_load(code, '/foo/bar.god')
410 assert_equal 1, God.watches.size
413 def test_running_load_should_monitor_new_watches
421 Watch.any_instance.expects(:monitor)
423 God.running_load(code, '/foo/bar.god')
427 def test_running_load_should_not_monitor_new_watches_with_autostart_false
436 Watch.any_instance.expects(:monitor).never
438 God.running_load(code, '/foo/bar.god')
442 def test_running_load_should_return_array_of_affected_watches
452 w, e = *God.running_load(code, '/foo/bar.god')
454 assert_equal 1, w.size
455 assert_equal 'foo', w.first
458 def test_running_load_should_clear_pending_watches
467 God.running_load(code, '/foo/bar.god')
469 assert_equal 0, God.pending_watches.size
474 def test_load_should_collect_and_load_globbed_path
475 Dir.expects(:[]).with('/path/to/*.thing').returns(['a', 'b'])
476 Kernel.expects(:load).with('a').once
477 Kernel.expects(:load).with('b').once
478 God.load('/path/to/*.thing')
483 def test_start_should_kick_off_a_server_instance
484 God::Socket.expects(:new).returns(true)
488 def test_start_should_start_event_handler
489 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
490 Timer.any_instance.expects(:join)
491 EventHandler.expects(:start).once
497 def test_start_should_begin_monitoring_autostart_watches
503 Timer.any_instance.expects(:join)
504 Watch.any_instance.expects(:monitor).once
508 def test_start_should_not_begin_monitoring_non_autostart_watches
515 Timer.any_instance.expects(:join)
516 Watch.any_instance.expects(:monitor).never
520 def test_start_should_get_and_join_timer
521 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
522 Timer.any_instance.expects(:join)
530 def test_at_exit_should_call_start
531 God.expects(:start).once
537 def test_pattern_match
538 list = %w{ mongrel-3000 mongrel-3001 fuzed fuzed2 apache mysql}
540 assert_equal %w{ mongrel-3000 }, God.pattern_match('m3000', list)
541 assert_equal %w{ mongrel-3001 }, God.pattern_match('m31', list)
542 assert_equal %w{ fuzed fuzed2 }, God.pattern_match('fu', list)
543 assert_equal %w{ mysql }, God.pattern_match('sql', list)
548 class TestGodOther < Test::Unit::TestCase
550 God::Socket.stubs(:new).returns(true)
561 def test_setup_should_create_pid_file_directory_if_it_doesnt_exist
562 God.expects(:test).returns(false)
563 FileUtils.expects(:mkdir_p).with(God.pid_file_directory)
567 def test_setup_should_raise_if_no_permissions_to_create_pid_file_directory
568 God.expects(:test).returns(false)
569 FileUtils.expects(:mkdir_p).raises(Errno::EACCES)
578 def test_validate_should_abort_if_pid_file_directory_is_unwriteable
579 God.expects(:test).returns(false)
585 def test_validate_should_not_abort_if_pid_file_directory_is_writeable
586 God.expects(:test).returns(true)
587 assert_nothing_raised do