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
24 def test_init_should_abort_if_called_after_watch
25 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
28 God.init { |g| g.pid_file_directory = 'foo' }
34 def test_pid_file_directory_should_return_default_if_not_set_explicitly
36 assert_equal '/var/run/god', God.pid_file_directory
39 def test_pid_file_directory_equals_should_set
41 God.pid_file_directory = '/foo'
42 assert_equal '/foo', God.pid_file_directory
47 def test_watch_should_get_stored
55 assert_equal 1, God.watches.size
56 assert_equal watch, God.watches.values.first
58 assert_equal 0, God.groups.size
61 def test_watch_should_get_stored_in_pending_watches
69 assert_equal 1, God.pending_watches.size
70 assert_equal watch, God.pending_watches.first
73 def test_watch_should_register_processes
74 assert_nil God.registry['foo']
79 assert_kind_of God::Process, God.registry['foo']
82 def test_watch_should_get_stored_by_group
92 assert_equal({'test' => [a]}, God.groups)
95 def test_watches_should_get_stored_by_group
113 assert_equal({'test' => [a, b]}, God.groups)
116 def test_watch_should_allow_multiple_watches
117 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
119 assert_nothing_raised do
120 God.watch { |w| w.name = 'bar'; w.start = 'bar' }
124 def test_watch_should_disallow_duplicate_watch_names
125 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
128 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
132 def test_watch_should_disallow_identical_watch_and_group_names
133 God.watch { |w| w.name = 'foo'; w.group = 'bar'; w.start = 'bar' }
136 God.watch { |w| w.name = 'bar'; w.start = 'bar' }
140 def test_watch_should_disallow_identical_watch_and_group_names_other_way
141 God.watch { |w| w.name = 'bar'; w.start = 'bar' }
144 God.watch { |w| w.name = 'foo'; w.group = 'bar'; w.start = 'bar' }
148 def test_watch_should_unwatch_new_watch_if_running_and_duplicate_watch
149 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
152 assert_nothing_raised do
154 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
161 def test_unwatch_should_unmonitor_watch
162 God.watch { |w| w.name = 'bar'; w.start = 'bar' }
163 w = God.watches['bar']
165 w.expects(:unmonitor)
169 def test_unwatch_should_unregister_watch
170 God.watch { |w| w.name = 'bar'; w.start = 'bar' }
171 w = God.watches['bar']
172 w.expects(:unregister!)
178 def test_unwatch_should_remove_same_name_watches
179 God.watch { |w| w.name = 'bar'; w.start = 'bar' }
180 w = God.watches['bar']
184 assert_equal 0, God.watches.size
187 def test_unwatch_should_remove_from_group
193 w = God.watches['bar']
197 assert !God.groups[w.group].include?(w)
202 def test_contact_should_ensure_init_is_called
203 God.contact(:fake_contact) { |c| c.name = 'tom' }
207 def test_contact_should_abort_on_invalid_contact_kind
209 God.contact(:foo) { |c| c.name = 'tom' }
213 def test_contact_should_create_and_store_contact
215 God.contact(:fake_contact) { |c| c.name = 'tom'; contact = c }
216 assert [contact], God.contacts
219 def test_contact_should_add_to_group
220 God.contact(:fake_contact) { |c| c.name = 'tom'; c.group = 'devs' }
221 God.contact(:fake_contact) { |c| c.name = 'chris'; c.group = 'devs' }
222 assert 2, God.contact_groups.size
225 def test_contact_should_abort_on_no_name
228 God.contact(:fake_contact) { |c| }
233 def test_contact_should_abort_on_duplicate_contact_name
234 God.contact(:fake_contact) { |c| c.name = 'tom' }
236 God.contact(:fake_contact) { |c| c.name = 'tom' }
240 def test_contact_should_abort_on_contact_with_same_name_as_group
241 God.contact(:fake_contact) { |c| c.name = 'tom'; c.group = 'devs' }
243 God.contact(:fake_contact) { |c| c.name = 'devs' }
247 def test_contact_should_abort_on_contact_with_same_group_as_name
248 God.contact(:fake_contact) { |c| c.name = 'tom' }
250 God.contact(:fake_contact) { |c| c.name = 'chris'; c.group = 'tom' }
254 def test_contact_should_abort_if_contact_is_invalid
256 God.contact(:fake_contact) do |c|
258 c.stubs(:valid?).returns(false)
265 def test_control_should_monitor_on_start
266 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
268 w = God.watches['foo']
270 God.control('foo', 'start')
273 def test_control_should_move_to_restart_on_restart
274 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
276 w = God.watches['foo']
277 w.expects(:move).with(:restart)
278 God.control('foo', 'restart')
281 def test_control_should_unmonitor_and_stop_on_stop
282 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
284 w = God.watches['foo']
286 w.expects(:unmonitor).returns(w)
287 w.expects(:action).with(:stop)
288 God.control('foo', 'stop')
291 def test_control_should_unmonitor_on_unmonitor
292 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
294 w = God.watches['foo']
296 w.expects(:unmonitor).returns(w)
297 God.control('foo', 'unmonitor')
300 def test_control_should_raise_on_invalid_command
301 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
303 assert_raise InvalidCommandError do
304 God.control('foo', 'invalid')
308 def test_control_should_operate_on_each_watch_in_group
321 God.watches['foo1'].expects(:monitor)
322 God.watches['foo2'].expects(:monitor)
324 God.control('bar', 'start')
331 def test_terminate_should_exit
338 def test_status_should_show_state
339 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
341 w = God.watches['foo']
343 assert_equal({'foo' => {:state => :up}}, God.status)
346 def test_status_should_show_unmonitored_for_nil_state
347 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
349 w = God.watches['foo']
350 assert_equal({'foo' => {:state => :unmonitored}}, God.status)
355 def test_running_log_should_call_watch_log_since_on_main_log
356 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
358 LOG.expects(:watch_log_since).with('foo', t)
359 God.running_log('foo', t)
362 def test_running_log_should_raise_on_unknown_watch
364 assert_raise(NoSuchWatchError) do
365 God.running_log('foo', Time.now)
371 def test_running_load_should_eval_code
380 God.running_load(code, '/foo/bar.god')
383 assert_equal 1, God.watches.size
386 def test_running_load_should_monitor_new_watches
394 Watch.any_instance.expects(:monitor)
396 God.running_load(code, '/foo/bar.god')
400 def test_running_load_should_not_monitor_new_watches_with_autostart_false
409 Watch.any_instance.expects(:monitor).never
411 God.running_load(code, '/foo/bar.god')
415 def test_running_load_should_return_array_of_affected_watches
425 w, e = *God.running_load(code, '/foo/bar.god')
427 assert_equal 1, w.size
428 assert_equal 'foo', w.first
431 def test_running_load_should_clear_pending_watches
440 God.running_load(code, '/foo/bar.god')
442 assert_equal 0, God.pending_watches.size
447 def test_load_should_collect_and_load_globbed_path
448 Dir.expects(:[]).with('/path/to/*.thing').returns(['a', 'b'])
449 Kernel.expects(:load).with('a').once
450 Kernel.expects(:load).with('b').once
451 God.load('/path/to/*.thing')
456 def test_start_should_kick_off_a_server_instance
457 Server.expects(:new).returns(true)
461 def test_start_should_start_event_handler
462 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
463 Timer.any_instance.expects(:join)
464 EventHandler.expects(:start).once
470 def test_start_should_begin_monitoring_autostart_watches
476 Timer.any_instance.expects(:join)
477 Watch.any_instance.expects(:monitor).once
481 def test_start_should_not_begin_monitoring_non_autostart_watches
488 Timer.any_instance.expects(:join)
489 Watch.any_instance.expects(:monitor).never
493 def test_start_should_get_and_join_timer
494 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
495 Timer.any_instance.expects(:join)
503 def test_at_exit_should_call_start
504 God.expects(:start).once
510 class TestGodOther < Test::Unit::TestCase
512 Server.stubs(:new).returns(true)
523 def test_setup_should_create_pid_file_directory_if_it_doesnt_exist
524 God.expects(:test).returns(false)
525 FileUtils.expects(:mkdir_p).with(God.pid_file_directory)
529 def test_setup_should_raise_if_no_permissions_to_create_pid_file_directory
530 God.expects(:test).returns(false)
531 FileUtils.expects(:mkdir_p).raises(Errno::EACCES)
540 def test_validate_should_abort_if_pid_file_directory_is_unwriteable
541 God.expects(:test).returns(false)
547 def test_validate_should_not_abort_if_pid_file_directory_is_writeable
548 God.expects(:test).returns(true)
549 assert_nothing_raised do