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