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']
164 w.expects(:unmonitor)
168 def test_unwatch_should_unregister_watch
169 God.watch { |w| w.name = 'bar'; w.start = 'bar' }
170 w = God.watches['bar']
171 w.expects(:unregister!)
177 def test_unwatch_should_remove_same_name_watches
178 God.watch { |w| w.name = 'bar'; w.start = 'bar' }
179 w = God.watches['bar']
183 assert_equal 0, God.watches.size
186 def test_unwatch_should_remove_from_group
192 w = God.watches['bar']
196 assert !God.groups[w.group].include?(w)
201 def test_contact_should_ensure_init_is_called
202 God.contact(:fake_contact) { |c| c.name = 'tom' }
206 def test_contact_should_abort_on_invalid_contact_kind
208 God.contact(:foo) { |c| c.name = 'tom' }
212 def test_contact_should_create_and_store_contact
214 God.contact(:fake_contact) { |c| c.name = 'tom'; contact = c }
215 assert [contact], God.contacts
218 def test_contact_should_add_to_group
219 God.contact(:fake_contact) { |c| c.name = 'tom'; c.group = 'devs' }
220 God.contact(:fake_contact) { |c| c.name = 'chris'; c.group = 'devs' }
221 assert 2, God.contact_groups.size
224 def test_contact_should_abort_on_no_name
227 God.contact(:fake_contact) { |c| }
232 def test_contact_should_abort_on_duplicate_contact_name
233 God.contact(:fake_contact) { |c| c.name = 'tom' }
235 God.contact(:fake_contact) { |c| c.name = 'tom' }
239 def test_contact_should_abort_on_contact_with_same_name_as_group
240 God.contact(:fake_contact) { |c| c.name = 'tom'; c.group = 'devs' }
242 God.contact(:fake_contact) { |c| c.name = 'devs' }
246 def test_contact_should_abort_on_contact_with_same_group_as_name
247 God.contact(:fake_contact) { |c| c.name = 'tom' }
249 God.contact(:fake_contact) { |c| c.name = 'chris'; c.group = 'tom' }
253 def test_contact_should_abort_if_contact_is_invalid
255 God.contact(:fake_contact) do |c|
257 c.stubs(:valid?).returns(false)
264 def test_control_should_monitor_on_start
265 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
267 w = God.watches['foo']
269 God.control('foo', 'start')
272 def test_control_should_move_to_restart_on_restart
273 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
275 w = God.watches['foo']
276 w.expects(:move).with(:restart)
277 God.control('foo', 'restart')
280 def test_control_should_unmonitor_and_stop_on_stop
281 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
283 w = God.watches['foo']
285 w.expects(:unmonitor).returns(w)
286 w.expects(:action).with(:stop)
287 God.control('foo', 'stop')
290 def test_control_should_unmonitor_on_unmonitor
291 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
293 w = God.watches['foo']
295 w.expects(:unmonitor).returns(w)
296 God.control('foo', 'unmonitor')
299 def test_control_should_raise_on_invalid_command
300 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
302 assert_raise InvalidCommandError do
303 God.control('foo', 'invalid')
307 def test_control_should_operate_on_each_watch_in_group
320 God.watches['foo1'].expects(:monitor)
321 God.watches['foo2'].expects(:monitor)
323 God.control('bar', 'start')
330 def test_terminate_should_exit
337 def test_status_should_show_state
338 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
340 w = God.watches['foo']
342 assert_equal({'foo' => {:state => :up}}, God.status)
345 def test_status_should_show_unmonitored_for_nil_state
346 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
348 w = God.watches['foo']
349 assert_equal({'foo' => {:state => :unmonitored}}, God.status)
354 def test_running_log_should_call_watch_log_since_on_main_log
355 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
357 LOG.expects(:watch_log_since).with('foo', t)
358 God.running_log('foo', t)
361 def test_running_log_should_raise_on_unknown_watch
363 assert_raise(NoSuchWatchError) do
364 God.running_log('foo', Time.now)
370 def test_running_load_should_eval_code
379 God.running_load(code, '/foo/bar.god')
382 assert_equal 1, God.watches.size
385 def test_running_load_should_monitor_new_watches
393 Watch.any_instance.expects(:monitor)
395 God.running_load(code, '/foo/bar.god')
399 def test_running_load_should_not_monitor_new_watches_with_autostart_false
408 Watch.any_instance.expects(:monitor).never
410 God.running_load(code, '/foo/bar.god')
414 def test_running_load_should_return_array_of_affected_watches
424 w, e = *God.running_load(code, '/foo/bar.god')
426 assert_equal 1, w.size
427 assert_equal 'foo', w.first.name
430 def test_running_load_should_clear_pending_watches
439 God.running_load(code, '/foo/bar.god')
441 assert_equal 0, God.pending_watches.size
446 def test_load_should_collect_and_load_globbed_path
447 Dir.expects(:[]).with('/path/to/*.thing').returns(['a', 'b'])
448 Kernel.expects(:load).with('a').once
449 Kernel.expects(:load).with('b').once
450 God.load('/path/to/*.thing')
455 def test_start_should_kick_off_a_server_instance
456 Server.expects(:new).returns(true)
460 def test_start_should_start_event_handler
461 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
462 Timer.any_instance.expects(:join)
463 EventHandler.expects(:start).once
469 def test_start_should_begin_monitoring_autostart_watches
475 Timer.any_instance.expects(:join)
476 Watch.any_instance.expects(:monitor).once
480 def test_start_should_not_begin_monitoring_non_autostart_watches
487 Timer.any_instance.expects(:join)
488 Watch.any_instance.expects(:monitor).never
492 def test_start_should_get_and_join_timer
493 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
494 Timer.any_instance.expects(:join)
502 def test_at_exit_should_call_start
503 God.expects(:start).once
509 class TestGodOther < Test::Unit::TestCase
511 Server.stubs(:new).returns(true)
522 def test_setup_should_create_pid_file_directory_if_it_doesnt_exist
523 God.expects(:test).returns(false)
524 FileUtils.expects(:mkdir_p).with(God.pid_file_directory)
528 def test_setup_should_raise_if_no_permissions_to_create_pid_file_directory
529 God.expects(:test).returns(false)
530 FileUtils.expects(:mkdir_p).raises(Errno::EACCES)
539 def test_validate_should_abort_if_pid_file_directory_is_unwriteable
540 God.expects(:test).returns(false)
546 def test_validate_should_not_abort_if_pid_file_directory_is_writeable
547 God.expects(:test).returns(true)
548 assert_nothing_raised do