added rdoc to http_response_code
[god.git] / test / test_god.rb
blob246bda14b9efe1f637cf103fdc7d6c947952bc7c
1 require File.dirname(__FILE__) + '/helper'
3 class TestGod < Test::Unit::TestCase
4   def setup
5     God::Socket.stubs(:new).returns(true)
6     God.stubs(:setup).returns(true)
7     God.stubs(:validater).returns(true)
8     God.reset
9   end
10   
11   def teardown
12     Timer.get.timer.kill
13   end
14   
15   # applog
16   
17   def test_applog
18     LOG.expects(:log).with(nil, :debug, 'foo')
19     applog(nil, :debug, 'foo')
20   end
21   
22   # internal_init
23   
24   def test_init_should_initialize_watches_to_empty_array
25     God.internal_init { }
26     assert_equal Hash.new, God.watches
27   end
28   
29   # init
30   
31   def test_pid_file_directory_should_abort_if_called_after_watch
32     God.watch { |w| w.name = 'foo'; w.start = 'bar' }
33     
34     assert_abort do
35       God.pid_file_directory = 'foo'
36     end
37   end
38   
39   # pid_file_directory
40   
41   def test_pid_file_directory_should_return_default_if_not_set_explicitly
42     God.internal_init
43     assert_equal '/var/run/god', God.pid_file_directory
44   end
45   
46   def test_pid_file_directory_equals_should_set
47     God.pid_file_directory = '/foo'
48     God.internal_init
49     assert_equal '/foo', God.pid_file_directory
50   end
51   
52   # watch
53   
54   def test_watch_should_get_stored
55     watch = nil
56     God.watch do |w|
57       w.name = 'foo'
58       w.start = 'bar'
59       watch = w
60     end
61     
62     assert_equal 1, God.watches.size
63     assert_equal watch, God.watches.values.first
64     
65     assert_equal 0, God.groups.size
66   end
67   
68   def test_watch_should_get_stored_in_pending_watches
69     watch = nil
70     God.watch do |w|
71       w.name = 'foo'
72       w.start = 'bar'
73       watch = w
74     end
75     
76     assert_equal 1, God.pending_watches.size
77     assert_equal watch, God.pending_watches.first
78   end
79   
80   def test_watch_should_register_processes
81     assert_nil God.registry['foo']
82     God.watch do |w|
83       w.name = 'foo'
84       w.start = 'bar'
85     end
86     assert_kind_of God::Process, God.registry['foo']
87   end
88   
89   def test_watch_should_get_stored_by_group
90     a = nil
91     
92     God.watch do |w|
93       a = w
94       w.name = 'foo'
95       w.start = 'bar'
96       w.group = 'test'
97     end
98     
99     assert_equal({'test' => [a]}, God.groups)
100   end
101   
102   def test_watches_should_get_stored_by_group
103     a = nil
104     b = nil
105     
106     God.watch do |w|
107       a = w
108       w.name = 'foo'
109       w.start = 'bar'
110       w.group = 'test'
111     end
112     
113     God.watch do |w|
114       b = w
115       w.name = 'bar'
116       w.start = 'baz'
117       w.group = 'test'
118     end
119     
120     assert_equal({'test' => [a, b]}, God.groups)
121   end
122       
123   def test_watch_should_allow_multiple_watches
124     God.watch { |w| w.name = 'foo'; w.start = 'bar' }
125     
126     assert_nothing_raised do
127       God.watch { |w| w.name = 'bar'; w.start = 'bar' }
128     end
129   end
130   
131   def test_watch_should_disallow_duplicate_watch_names
132     God.watch { |w| w.name = 'foo'; w.start = 'bar' }
133     
134     assert_abort do
135       God.watch { |w| w.name = 'foo'; w.start = 'bar' }
136     end
137   end
138   
139   def test_watch_should_disallow_identical_watch_and_group_names
140     God.watch { |w| w.name = 'foo'; w.group = 'bar'; w.start = 'bar' }
141     
142     assert_abort do
143       God.watch { |w| w.name = 'bar'; w.start = 'bar' }
144     end
145   end
146   
147   def test_watch_should_disallow_identical_watch_and_group_names_other_way
148     God.watch { |w| w.name = 'bar'; w.start = 'bar' }
149     
150     assert_abort do
151       God.watch { |w| w.name = 'foo'; w.group = 'bar'; w.start = 'bar' }
152     end
153   end
154   
155   def test_watch_should_unwatch_new_watch_if_running_and_duplicate_watch
156     God.watch { |w| w.name = 'foo'; w.start = 'bar' }
157     God.running = true
158     
159     assert_nothing_raised do
160       no_stdout do
161         God.watch { |w| w.name = 'foo'; w.start = 'bar' }
162       end
163     end
164   end
165   
166   # unwatch
167   
168   def test_unwatch_should_unmonitor_watch
169     God.watch { |w| w.name = 'bar'; w.start = 'bar' }
170     w = God.watches['bar']
171     w.state = :up
172     w.expects(:unmonitor)
173     God.unwatch(w)
174   end
175   
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!)
180     no_stdout do
181       God.unwatch(w)
182     end
183   end
184   
185   def test_unwatch_should_remove_same_name_watches
186     God.watch { |w| w.name = 'bar'; w.start = 'bar' }
187     w = God.watches['bar']
188     no_stdout do
189       God.unwatch(w)
190     end
191     assert_equal 0, God.watches.size
192   end
193   
194   def test_unwatch_should_remove_from_group
195     God.watch do |w|
196       w.name = 'bar'
197       w.start = 'baz'
198       w.group = 'test'
199     end
200     w = God.watches['bar']
201     no_stdout do
202       God.unwatch(w)
203     end
204     assert !God.groups[w.group].include?(w)
205   end
206   
207   # contact
208   
209   def test_contact_should_ensure_init_is_called
210     God.contact(:fake_contact) { |c| c.name = 'tom' }
211     assert God.inited
212   end
213   
214   def test_contact_should_abort_on_invalid_contact_kind
215     assert_abort do
216       God.contact(:foo) { |c| c.name = 'tom' }
217     end
218   end
219   
220   def test_contact_should_create_and_store_contact
221     contact = nil
222     God.contact(:fake_contact) { |c| c.name = 'tom'; contact = c }
223     assert [contact], God.contacts
224   end
225   
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
230   end
231   
232   def test_contact_should_abort_on_no_name
233     no_stdout do
234       assert_abort do
235         God.contact(:fake_contact) { |c| }
236       end
237     end
238   end
239   
240   def test_contact_should_abort_on_duplicate_contact_name
241     God.contact(:fake_contact) { |c| c.name = 'tom' }
242     assert_abort do
243       God.contact(:fake_contact) { |c| c.name = 'tom' }
244     end
245   end
246   
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' }
249     assert_abort do
250       God.contact(:fake_contact) { |c| c.name = 'devs' }
251     end
252   end
253   
254   def test_contact_should_abort_on_contact_with_same_group_as_name
255     God.contact(:fake_contact) { |c| c.name = 'tom' }
256     assert_abort do
257       God.contact(:fake_contact) { |c| c.name = 'chris'; c.group = 'tom' }
258     end
259   end
260   
261   def test_contact_should_abort_if_contact_is_invalid
262     assert_abort do
263       God.contact(:fake_contact) do |c|
264         c.name = 'tom'
265         c.stubs(:valid?).returns(false)
266       end
267     end
268   end
269   
270   # control
271   
272   def test_control_should_monitor_on_start
273     God.watch { |w| w.name = 'foo'; w.start = 'bar' }
274     
275     w = God.watches['foo']
276     w.expects(:monitor)
277     God.control('foo', 'start')
278   end
279   
280   def test_control_should_move_to_restart_on_restart
281     God.watch { |w| w.name = 'foo'; w.start = 'bar' }
282     
283     w = God.watches['foo']
284     w.expects(:move).with(:restart)
285     God.control('foo', 'restart')
286   end
287   
288   def test_control_should_unmonitor_and_stop_on_stop
289     God.watch { |w| w.name = 'foo'; w.start = 'bar' }
290     
291     w = God.watches['foo']
292     w.state = :up
293     w.expects(:unmonitor).returns(w)
294     w.expects(:action).with(:stop)
295     God.control('foo', 'stop')
296   end
297   
298   def test_control_should_unmonitor_on_unmonitor
299     God.watch { |w| w.name = 'foo'; w.start = 'bar' }
300     
301     w = God.watches['foo']
302     w.state = :up
303     w.expects(:unmonitor).returns(w)
304     God.control('foo', 'unmonitor')
305   end
306   
307   def test_control_should_raise_on_invalid_command
308     God.watch { |w| w.name = 'foo'; w.start = 'bar' }
309     
310     assert_raise InvalidCommandError do
311       God.control('foo', 'invalid')
312     end
313   end
314   
315   def test_control_should_operate_on_each_watch_in_group
316     God.watch do |w|
317       w.name = 'foo1'
318       w.start = 'go'
319       w.group = 'bar'
320     end
321     
322     God.watch do |w|
323       w.name = 'foo2'
324       w.start = 'go'
325       w.group = 'bar'
326     end
327     
328     God.watches['foo1'].expects(:monitor)
329     God.watches['foo2'].expects(:monitor)
330     
331     God.control('bar', 'start')
332   end
333   
334   # stop_all
335   
336   # terminate
337   
338   def test_terminate_should_exit
339     God.pid = nil
340     FileUtils.expects(:rm_f).never
341     God.expects(:exit!)
342     God.terminate
343   end
344   
345   def test_terminate_should_delete_pid
346     God.pid = '/foo/bar'
347     FileUtils.expects(:rm_f).with("/foo/bar")
348     God.expects(:exit!)
349     God.terminate
350   end
351   
352   # status
353   
354   def test_status_should_show_state
355     God.watch { |w| w.name = 'foo'; w.start = 'bar' }
356     
357     w = God.watches['foo']
358     w.state = :up
359     assert_equal({'foo' => {:state => :up}}, God.status)
360   end
361   
362   def test_status_should_show_unmonitored_for_nil_state
363     God.watch { |w| w.name = 'foo'; w.start = 'bar' }
364     
365     w = God.watches['foo']
366     assert_equal({'foo' => {:state => :unmonitored}}, God.status)
367   end
368   
369   # running_log
370   
371   def test_running_log_should_call_watch_log_since_on_main_log
372     God.watch { |w| w.name = 'foo'; w.start = 'bar' }
373     t = Time.now
374     LOG.expects(:watch_log_since).with('foo', t)
375     God.running_log('foo', t)
376   end
377   
378   def test_running_log_should_raise_on_unknown_watch
379     God.internal_init
380     assert_raise(NoSuchWatchError) do
381       God.running_log('foo', Time.now)
382     end
383   end
384   
385   # running_load
386   
387   def test_running_load_should_eval_code
388     code = <<-EOF
389       God.watch do |w|
390         w.name = 'foo'
391         w.start = 'go'
392       end
393     EOF
394     
395     no_stdout do
396       God.running_load(code, '/foo/bar.god')
397     end
398     
399     assert_equal 1, God.watches.size
400   end
401   
402   def test_running_load_should_monitor_new_watches
403     code = <<-EOF
404       God.watch do |w|
405         w.name = 'foo'
406         w.start = 'go'
407       end
408     EOF
409     
410     Watch.any_instance.expects(:monitor)
411     no_stdout do
412       God.running_load(code, '/foo/bar.god')
413     end
414   end
415   
416   def test_running_load_should_not_monitor_new_watches_with_autostart_false
417     code = <<-EOF
418       God.watch do |w|
419         w.name = 'foo'
420         w.start = 'go'
421         w.autostart = false
422       end
423     EOF
424     
425     Watch.any_instance.expects(:monitor).never
426     no_stdout do
427       God.running_load(code, '/foo/bar.god')
428     end
429   end
430   
431   def test_running_load_should_return_array_of_affected_watches
432     code = <<-EOF
433       God.watch do |w|
434         w.name = 'foo'
435         w.start = 'go'
436       end
437     EOF
438     
439     w = nil
440     no_stdout do
441       w, e = *God.running_load(code, '/foo/bar.god')
442     end
443     assert_equal 1, w.size
444     assert_equal 'foo', w.first
445   end
446   
447   def test_running_load_should_clear_pending_watches
448     code = <<-EOF
449       God.watch do |w|
450         w.name = 'foo'
451         w.start = 'go'
452       end
453     EOF
454     
455     no_stdout do
456       God.running_load(code, '/foo/bar.god')
457     end
458     assert_equal 0, God.pending_watches.size
459   end
460   
461   # load
462   
463   def test_load_should_collect_and_load_globbed_path
464     Dir.expects(:[]).with('/path/to/*.thing').returns(['a', 'b'])
465     Kernel.expects(:load).with('a').once
466     Kernel.expects(:load).with('b').once
467     God.load('/path/to/*.thing')
468   end
469   
470   # start
471   
472   def test_start_should_kick_off_a_server_instance
473     God::Socket.expects(:new).returns(true)
474     God.start
475   end
476     
477   def test_start_should_start_event_handler
478     God.watch { |w| w.name = 'foo'; w.start = 'bar' }
479     Timer.any_instance.expects(:join)
480     EventHandler.expects(:start).once
481     no_stdout do
482       God.start
483     end
484   end
485   
486   def test_start_should_begin_monitoring_autostart_watches
487     God.watch do |w|
488       w.name = 'foo'
489       w.start = 'go'
490     end
491     
492     Timer.any_instance.expects(:join)
493     Watch.any_instance.expects(:monitor).once
494     God.start
495   end
496   
497   def test_start_should_not_begin_monitoring_non_autostart_watches
498     God.watch do |w|
499       w.name = 'foo'
500       w.start = 'go'
501       w.autostart = false
502     end
503     
504     Timer.any_instance.expects(:join)
505     Watch.any_instance.expects(:monitor).never
506     God.start
507   end
508   
509   def test_start_should_get_and_join_timer
510     God.watch { |w| w.name = 'foo'; w.start = 'bar' }
511     Timer.any_instance.expects(:join)
512     no_stdout do
513       God.start
514     end
515   end
516   
517   # at_exit
518   
519   def test_at_exit_should_call_start
520     God.expects(:start).once
521     God.at_exit
522   end
526 class TestGodOther < Test::Unit::TestCase
527   def setup
528     God::Socket.stubs(:new).returns(true)
529     God.internal_init
530     God.reset
531   end
532   
533   def teardown
534     Timer.get.timer.kill
535   end
536   
537   # setup
538   
539   def test_setup_should_create_pid_file_directory_if_it_doesnt_exist
540     God.expects(:test).returns(false)
541     FileUtils.expects(:mkdir_p).with(God.pid_file_directory)
542     God.setup
543   end
544   
545   def test_setup_should_raise_if_no_permissions_to_create_pid_file_directory
546     God.expects(:test).returns(false)
547     FileUtils.expects(:mkdir_p).raises(Errno::EACCES)
548     
549     assert_abort do
550       God.setup
551     end
552   end
553   
554   # validate
555     
556   def test_validate_should_abort_if_pid_file_directory_is_unwriteable
557     God.expects(:test).returns(false)
558     assert_abort do
559       God.validater
560     end
561   end
562   
563   def test_validate_should_not_abort_if_pid_file_directory_is_writeable
564     God.expects(:test).returns(true)
565     assert_nothing_raised do
566       God.validater
567     end
568   end