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_contact_should_ensure_init_is_called
200 God.contact(:fake_contact) { |c| c.name = 'tom' }
204 def test_contact_should_abort_on_invalid_contact_kind
206 God.contact(:foo) { |c| c.name = 'tom' }
210 def test_contact_should_create_and_store_contact
212 God.contact(:fake_contact) { |c| c.name = 'tom'; contact = c }
213 assert [contact], God.contacts
216 def test_contact_should_add_to_group
217 God.contact(:fake_contact) { |c| c.name = 'tom'; c.group = 'devs' }
218 God.contact(:fake_contact) { |c| c.name = 'chris'; c.group = 'devs' }
219 assert 2, God.contact_groups.size
222 def test_contact_should_abort_on_no_name
225 God.contact(:fake_contact) { |c| }
230 def test_contact_should_abort_on_duplicate_contact_name
231 God.contact(:fake_contact) { |c| c.name = 'tom' }
233 God.contact(:fake_contact) { |c| c.name = 'tom' }
237 def test_contact_should_abort_on_contact_with_same_name_as_group
238 God.contact(:fake_contact) { |c| c.name = 'tom'; c.group = 'devs' }
240 God.contact(:fake_contact) { |c| c.name = 'devs' }
244 def test_contact_should_abort_on_contact_with_same_group_as_name
245 God.contact(:fake_contact) { |c| c.name = 'tom' }
247 God.contact(:fake_contact) { |c| c.name = 'chris'; c.group = 'tom' }
251 def test_contact_should_abort_if_contact_is_invalid
253 God.contact(:fake_contact) do |c|
255 c.stubs(:valid?).returns(false)
262 def test_control_should_monitor_on_start
263 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
265 w = God.watches['foo']
267 God.control('foo', 'start')
270 def test_control_should_move_to_restart_on_restart
271 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
273 w = God.watches['foo']
274 w.expects(:move).with(:restart)
275 God.control('foo', 'restart')
278 def test_control_should_unmonitor_and_stop_on_stop
279 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
281 w = God.watches['foo']
282 w.expects(:unmonitor).returns(w)
283 w.expects(:action).with(:stop)
284 God.control('foo', 'stop')
287 def test_control_should_unmonitor_on_unmonitor
288 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
290 w = God.watches['foo']
291 w.expects(:unmonitor).returns(w)
292 God.control('foo', 'unmonitor')
295 def test_control_should_raise_on_invalid_command
296 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
298 assert_raise InvalidCommandError do
299 God.control('foo', 'invalid')
303 def test_control_should_operate_on_each_watch_in_group
316 God.watches['foo1'].expects(:monitor)
317 God.watches['foo2'].expects(:monitor)
319 God.control('bar', 'start')
326 def test_terminate_should_exit
333 def test_status_should_show_state
334 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
336 w = God.watches['foo']
338 assert_equal({'foo' => {:state => :up}}, God.status)
341 def test_status_should_show_unmonitored_for_nil_state
342 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
344 w = God.watches['foo']
345 assert_equal({'foo' => {:state => :unmonitored}}, God.status)
350 def test_running_log_should_call_watch_log_since_on_main_log
351 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
353 LOG.expects(:watch_log_since).with('foo', t)
354 God.running_log('foo', t)
357 def test_running_log_should_raise_on_unknown_watch
359 assert_raise(NoSuchWatchError) do
360 God.running_log('foo', Time.now)
366 def test_running_load_should_eval_code
375 God.running_load(code, '/foo/bar.god')
378 assert_equal 1, God.watches.size
381 def test_running_load_should_monitor_new_watches
389 Watch.any_instance.expects(:monitor)
391 God.running_load(code, '/foo/bar.god')
395 def test_running_load_should_not_monitor_new_watches_with_autostart_false
404 Watch.any_instance.expects(:monitor).never
406 God.running_load(code, '/foo/bar.god')
410 def test_running_load_should_return_array_of_affected_watches
420 w, e = *God.running_load(code, '/foo/bar.god')
422 assert_equal 1, w.size
423 assert_equal 'foo', w.first.name
426 def test_running_load_should_clear_pending_watches
435 God.running_load(code, '/foo/bar.god')
437 assert_equal 0, God.pending_watches.size
442 def test_load_should_collect_and_load_globbed_path
443 Dir.expects(:[]).with('/path/to/*.thing').returns(['a', 'b'])
444 Kernel.expects(:load).with('a').once
445 Kernel.expects(:load).with('b').once
446 God.load('/path/to/*.thing')
451 def test_start_should_kick_off_a_server_instance
452 Server.expects(:new).returns(true)
456 def test_start_should_start_event_handler
457 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
458 Timer.any_instance.expects(:join)
459 EventHandler.expects(:start).once
465 def test_start_should_begin_monitoring_autostart_watches
471 Timer.any_instance.expects(:join)
472 Watch.any_instance.expects(:monitor).once
476 def test_start_should_not_begin_monitoring_non_autostart_watches
483 Timer.any_instance.expects(:join)
484 Watch.any_instance.expects(:monitor).never
488 def test_start_should_get_and_join_timer
489 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
490 Timer.any_instance.expects(:join)
498 def test_at_exit_should_call_start
499 God.expects(:start).once
505 class TestGodOther < Test::Unit::TestCase
507 Server.stubs(:new).returns(true)
518 def test_setup_should_create_pid_file_directory_if_it_doesnt_exist
519 God.expects(:test).returns(false)
520 FileUtils.expects(:mkdir_p).with(God.pid_file_directory)
524 def test_setup_should_raise_if_no_permissions_to_create_pid_file_directory
525 God.expects(:test).returns(false)
526 FileUtils.expects(:mkdir_p).raises(Errno::EACCES)
535 def test_validate_should_abort_if_pid_file_directory_is_unwriteable
536 God.expects(:test).returns(false)
542 def test_validate_should_not_abort_if_pid_file_directory_is_writeable
543 God.expects(:test).returns(true)
544 assert_nothing_raised do