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)
176 def test_unwatch_should_unregister_watch
177 God.watch { |w| w.name = 'bar'; w.start = 'bar' }
178 w = God.watches['bar']
179 w.expects(:unregister!)
185 def test_unwatch_should_remove_same_name_watches
186 God.watch { |w| w.name = 'bar'; w.start = 'bar' }
187 w = God.watches['bar']
191 assert_equal 0, God.watches.size
194 def test_unwatch_should_remove_from_group
200 w = God.watches['bar']
204 assert !God.groups[w.group].include?(w)
209 def test_contact_should_ensure_init_is_called
210 God.contact(:fake_contact) { |c| c.name = 'tom' }
214 def test_contact_should_abort_on_invalid_contact_kind
216 God.contact(:foo) { |c| c.name = 'tom' }
220 def test_contact_should_create_and_store_contact
222 God.contact(:fake_contact) { |c| c.name = 'tom'; contact = c }
223 assert [contact], God.contacts
226 def test_contact_should_add_to_group
227 God.contact(:fake_contact) { |c| c.name = 'tom'; c.group = 'devs' }
228 God.contact(:fake_contact) { |c| c.name = 'chris'; c.group = 'devs' }
229 assert 2, God.contact_groups.size
232 def test_contact_should_abort_on_no_name
235 God.contact(:fake_contact) { |c| }
240 def test_contact_should_abort_on_duplicate_contact_name
241 God.contact(:fake_contact) { |c| c.name = 'tom' }
243 God.contact(:fake_contact) { |c| c.name = 'tom' }
247 def test_contact_should_abort_on_contact_with_same_name_as_group
248 God.contact(:fake_contact) { |c| c.name = 'tom'; c.group = 'devs' }
250 God.contact(:fake_contact) { |c| c.name = 'devs' }
254 def test_contact_should_abort_on_contact_with_same_group_as_name
255 God.contact(:fake_contact) { |c| c.name = 'tom' }
257 God.contact(:fake_contact) { |c| c.name = 'chris'; c.group = 'tom' }
261 def test_contact_should_abort_if_contact_is_invalid
263 God.contact(:fake_contact) do |c|
265 c.stubs(:valid?).returns(false)
272 def test_control_should_monitor_on_start
273 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
275 w = God.watches['foo']
277 God.control('foo', 'start')
280 def test_control_should_move_to_restart_on_restart
281 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
283 w = God.watches['foo']
284 w.expects(:move).with(:restart)
285 God.control('foo', 'restart')
288 def test_control_should_unmonitor_and_stop_on_stop
289 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
291 w = God.watches['foo']
293 w.expects(:unmonitor).returns(w)
294 w.expects(:action).with(:stop)
295 God.control('foo', 'stop')
298 def test_control_should_unmonitor_on_unmonitor
299 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
301 w = God.watches['foo']
303 w.expects(:unmonitor).returns(w)
304 God.control('foo', 'unmonitor')
307 def test_control_should_unwatch_on_remove
308 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
310 w = God.watches['foo']
312 God.expects(:unwatch)
313 God.control('foo', 'remove')
316 def test_control_should_raise_on_invalid_command
317 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
319 assert_raise InvalidCommandError do
320 God.control('foo', 'invalid')
324 def test_control_should_operate_on_each_watch_in_group
337 God.watches['foo1'].expects(:monitor)
338 God.watches['foo2'].expects(:monitor)
340 God.control('bar', 'start')
347 def test_terminate_should_exit
349 FileUtils.expects(:rm_f).never
354 def test_terminate_should_delete_pid
356 FileUtils.expects(:rm_f).with("/foo/bar")
363 def test_status_should_show_state
364 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
366 w = God.watches['foo']
368 assert_equal({'foo' => {:state => :up}}, God.status)
371 def test_status_should_show_unmonitored_for_nil_state
372 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
374 w = God.watches['foo']
375 assert_equal({'foo' => {:state => :unmonitored}}, God.status)
380 def test_running_log_should_call_watch_log_since_on_main_log
381 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
383 LOG.expects(:watch_log_since).with('foo', t)
384 God.running_log('foo', t)
387 def test_running_log_should_raise_on_unknown_watch
389 assert_raise(NoSuchWatchError) do
390 God.running_log('foo', Time.now)
396 def test_running_load_should_eval_code
405 God.running_load(code, '/foo/bar.god')
408 assert_equal 1, God.watches.size
411 def test_running_load_should_monitor_new_watches
419 Watch.any_instance.expects(:monitor)
421 God.running_load(code, '/foo/bar.god')
425 def test_running_load_should_not_monitor_new_watches_with_autostart_false
434 Watch.any_instance.expects(:monitor).never
436 God.running_load(code, '/foo/bar.god')
440 def test_running_load_should_return_array_of_affected_watches
450 w, e = *God.running_load(code, '/foo/bar.god')
452 assert_equal 1, w.size
453 assert_equal 'foo', w.first
456 def test_running_load_should_clear_pending_watches
465 God.running_load(code, '/foo/bar.god')
467 assert_equal 0, God.pending_watches.size
472 def test_load_should_collect_and_load_globbed_path
473 Dir.expects(:[]).with('/path/to/*.thing').returns(['a', 'b'])
474 Kernel.expects(:load).with('a').once
475 Kernel.expects(:load).with('b').once
476 God.load('/path/to/*.thing')
481 def test_start_should_kick_off_a_server_instance
482 God::Socket.expects(:new).returns(true)
486 def test_start_should_start_event_handler
487 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
488 Timer.any_instance.expects(:join)
489 EventHandler.expects(:start).once
495 def test_start_should_begin_monitoring_autostart_watches
501 Timer.any_instance.expects(:join)
502 Watch.any_instance.expects(:monitor).once
506 def test_start_should_not_begin_monitoring_non_autostart_watches
513 Timer.any_instance.expects(:join)
514 Watch.any_instance.expects(:monitor).never
518 def test_start_should_get_and_join_timer
519 God.watch { |w| w.name = 'foo'; w.start = 'bar' }
520 Timer.any_instance.expects(:join)
528 def test_at_exit_should_call_start
529 God.expects(:start).once
535 def test_pattern_match
536 list = %w{ mongrel-3000 mongrel-3001 fuzed fuzed2 apache mysql}
538 assert_equal %w{ mongrel-3000 }, God.pattern_match(list, 'm3000')
539 assert_equal %w{ mongrel-3001 }, God.pattern_match(list, 'm31')
540 assert_equal %w{ fuzed fuzed2 }, God.pattern_match(list, 'fu')
541 assert_equal %w{ mysql }, God.pattern_match(list, 'sql')
546 class TestGodOther < Test::Unit::TestCase
548 God::Socket.stubs(:new).returns(true)
559 def test_setup_should_create_pid_file_directory_if_it_doesnt_exist
560 God.expects(:test).returns(false)
561 FileUtils.expects(:mkdir_p).with(God.pid_file_directory)
565 def test_setup_should_raise_if_no_permissions_to_create_pid_file_directory
566 God.expects(:test).returns(false)
567 FileUtils.expects(:mkdir_p).raises(Errno::EACCES)
576 def test_validate_should_abort_if_pid_file_directory_is_unwriteable
577 God.expects(:test).returns(false)
583 def test_validate_should_not_abort_if_pid_file_directory_is_writeable
584 God.expects(:test).returns(true)
585 assert_nothing_raised do