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