put watch back into previous state after a god load
[god.git] / test / test_god.rb
blob5f4c4cd4fa6409ba39791b9ad2acb5d2ae6b4be3
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.expects(:unmonitor)
165     God.unwatch(w)
166   end
167   
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!)
172     no_stdout do
173       God.unwatch(w)
174     end
175   end
176   
177   def test_unwatch_should_remove_same_name_watches
178     God.watch { |w| w.name = 'bar'; w.start = 'bar' }
179     w = God.watches['bar']
180     no_stdout do
181       God.unwatch(w)
182     end
183     assert_equal 0, God.watches.size
184   end
185   
186   def test_unwatch_should_remove_from_group
187     God.watch do |w|
188       w.name = 'bar'
189       w.start = 'baz'
190       w.group = 'test'
191     end
192     w = God.watches['bar']
193     no_stdout do
194       God.unwatch(w)
195     end
196     assert !God.groups[w.group].include?(w)
197   end
198   
199   # contact
200   
201   def test_contact_should_ensure_init_is_called
202     God.contact(:fake_contact) { |c| c.name = 'tom' }
203     assert God.inited
204   end
205   
206   def test_contact_should_abort_on_invalid_contact_kind
207     assert_abort do
208       God.contact(:foo) { |c| c.name = 'tom' }
209     end
210   end
211   
212   def test_contact_should_create_and_store_contact
213     contact = nil
214     God.contact(:fake_contact) { |c| c.name = 'tom'; contact = c }
215     assert [contact], God.contacts
216   end
217   
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
222   end
223   
224   def test_contact_should_abort_on_no_name
225     no_stdout do
226       assert_abort do
227         God.contact(:fake_contact) { |c| }
228       end
229     end
230   end
231   
232   def test_contact_should_abort_on_duplicate_contact_name
233     God.contact(:fake_contact) { |c| c.name = 'tom' }
234     assert_abort do
235       God.contact(:fake_contact) { |c| c.name = 'tom' }
236     end
237   end
238   
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' }
241     assert_abort do
242       God.contact(:fake_contact) { |c| c.name = 'devs' }
243     end
244   end
245   
246   def test_contact_should_abort_on_contact_with_same_group_as_name
247     God.contact(:fake_contact) { |c| c.name = 'tom' }
248     assert_abort do
249       God.contact(:fake_contact) { |c| c.name = 'chris'; c.group = 'tom' }
250     end
251   end
252   
253   def test_contact_should_abort_if_contact_is_invalid
254     assert_abort do
255       God.contact(:fake_contact) do |c|
256         c.name = 'tom'
257         c.stubs(:valid?).returns(false)
258       end
259     end
260   end
261   
262   # control
263   
264   def test_control_should_monitor_on_start
265     God.watch { |w| w.name = 'foo'; w.start = 'bar' }
266     
267     w = God.watches['foo']
268     w.expects(:monitor)
269     God.control('foo', 'start')
270   end
271   
272   def test_control_should_move_to_restart_on_restart
273     God.watch { |w| w.name = 'foo'; w.start = 'bar' }
274     
275     w = God.watches['foo']
276     w.expects(:move).with(:restart)
277     God.control('foo', 'restart')
278   end
279   
280   def test_control_should_unmonitor_and_stop_on_stop
281     God.watch { |w| w.name = 'foo'; w.start = 'bar' }
282     
283     w = God.watches['foo']
284     w.state = :up
285     w.expects(:unmonitor).returns(w)
286     w.expects(:action).with(:stop)
287     God.control('foo', 'stop')
288   end
289   
290   def test_control_should_unmonitor_on_unmonitor
291     God.watch { |w| w.name = 'foo'; w.start = 'bar' }
292     
293     w = God.watches['foo']
294     w.state = :up
295     w.expects(:unmonitor).returns(w)
296     God.control('foo', 'unmonitor')
297   end
298   
299   def test_control_should_raise_on_invalid_command
300     God.watch { |w| w.name = 'foo'; w.start = 'bar' }
301     
302     assert_raise InvalidCommandError do
303       God.control('foo', 'invalid')
304     end
305   end
306   
307   def test_control_should_operate_on_each_watch_in_group
308     God.watch do |w|
309       w.name = 'foo1'
310       w.start = 'go'
311       w.group = 'bar'
312     end
313     
314     God.watch do |w|
315       w.name = 'foo2'
316       w.start = 'go'
317       w.group = 'bar'
318     end
319     
320     God.watches['foo1'].expects(:monitor)
321     God.watches['foo2'].expects(:monitor)
322     
323     God.control('bar', 'start')
324   end
325   
326   # stop_all
327   
328   # terminate
329   
330   def test_terminate_should_exit
331     God.expects(:exit!)
332     God.terminate
333   end
334   
335   # status
336   
337   def test_status_should_show_state
338     God.watch { |w| w.name = 'foo'; w.start = 'bar' }
339     
340     w = God.watches['foo']
341     w.state = :up
342     assert_equal({'foo' => {:state => :up}}, God.status)
343   end
344   
345   def test_status_should_show_unmonitored_for_nil_state
346     God.watch { |w| w.name = 'foo'; w.start = 'bar' }
347     
348     w = God.watches['foo']
349     assert_equal({'foo' => {:state => :unmonitored}}, God.status)
350   end
351   
352   # running_log
353   
354   def test_running_log_should_call_watch_log_since_on_main_log
355     God.watch { |w| w.name = 'foo'; w.start = 'bar' }
356     t = Time.now
357     LOG.expects(:watch_log_since).with('foo', t)
358     God.running_log('foo', t)
359   end
360   
361   def test_running_log_should_raise_on_unknown_watch
362     God.internal_init
363     assert_raise(NoSuchWatchError) do
364       God.running_log('foo', Time.now)
365     end
366   end
367   
368   # running_load
369   
370   def test_running_load_should_eval_code
371     code = <<-EOF
372       God.watch do |w|
373         w.name = 'foo'
374         w.start = 'go'
375       end
376     EOF
377     
378     no_stdout do
379       God.running_load(code, '/foo/bar.god')
380     end
381     
382     assert_equal 1, God.watches.size
383   end
384   
385   def test_running_load_should_monitor_new_watches
386     code = <<-EOF
387       God.watch do |w|
388         w.name = 'foo'
389         w.start = 'go'
390       end
391     EOF
392     
393     Watch.any_instance.expects(:monitor)
394     no_stdout do
395       God.running_load(code, '/foo/bar.god')
396     end
397   end
398   
399   def test_running_load_should_not_monitor_new_watches_with_autostart_false
400     code = <<-EOF
401       God.watch do |w|
402         w.name = 'foo'
403         w.start = 'go'
404         w.autostart = false
405       end
406     EOF
407     
408     Watch.any_instance.expects(:monitor).never
409     no_stdout do
410       God.running_load(code, '/foo/bar.god')
411     end
412   end
413   
414   def test_running_load_should_return_array_of_affected_watches
415     code = <<-EOF
416       God.watch do |w|
417         w.name = 'foo'
418         w.start = 'go'
419       end
420     EOF
421     
422     w = nil
423     no_stdout do
424       w, e = *God.running_load(code, '/foo/bar.god')
425     end
426     assert_equal 1, w.size
427     assert_equal 'foo', w.first.name
428   end
429   
430   def test_running_load_should_clear_pending_watches
431     code = <<-EOF
432       God.watch do |w|
433         w.name = 'foo'
434         w.start = 'go'
435       end
436     EOF
437     
438     no_stdout do
439       God.running_load(code, '/foo/bar.god')
440     end
441     assert_equal 0, God.pending_watches.size
442   end
443   
444   # load
445   
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')
451   end
452   
453   # start
454   
455   def test_start_should_kick_off_a_server_instance
456     Server.expects(:new).returns(true)
457     God.start
458   end
459     
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
464     no_stdout do
465       God.start
466     end
467   end
468   
469   def test_start_should_begin_monitoring_autostart_watches
470     God.watch do |w|
471       w.name = 'foo'
472       w.start = 'go'
473     end
474     
475     Timer.any_instance.expects(:join)
476     Watch.any_instance.expects(:monitor).once
477     God.start
478   end
479   
480   def test_start_should_not_begin_monitoring_non_autostart_watches
481     God.watch do |w|
482       w.name = 'foo'
483       w.start = 'go'
484       w.autostart = false
485     end
486     
487     Timer.any_instance.expects(:join)
488     Watch.any_instance.expects(:monitor).never
489     God.start
490   end
491   
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)
495     no_stdout do
496       God.start
497     end
498   end
499   
500   # at_exit
501   
502   def test_at_exit_should_call_start
503     God.expects(:start).once
504     God.at_exit
505   end
509 class TestGodOther < Test::Unit::TestCase
510   def setup
511     Server.stubs(:new).returns(true)
512     God.internal_init
513     God.reset
514   end
515   
516   def teardown
517     Timer.get.timer.kill
518   end
519   
520   # setup
521   
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)
525     God.setup
526   end
527   
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)
531     
532     assert_abort do
533       God.setup
534     end
535   end
536   
537   # validate
538     
539   def test_validate_should_abort_if_pid_file_directory_is_unwriteable
540     God.expects(:test).returns(false)
541     assert_abort do
542       God.validater
543     end
544   end
545   
546   def test_validate_should_not_abort_if_pid_file_directory_is_writeable
547     God.expects(:test).returns(true)
548     assert_nothing_raised do
549       God.validater
550     end
551   end