fix process#alive? to not raise on no such file
[god.git] / test / test_god.rb
blob7aac86449e6ef9749dfa642f46d6032e5e76e182
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   # control
198   
199   def test_control_should_monitor_on_start
200     God.watch { |w| w.name = 'foo'; w.start = 'bar' }
201     
202     w = God.watches['foo']
203     w.expects(:monitor)
204     God.control('foo', 'start')
205   end
206   
207   def test_control_should_move_to_restart_on_restart
208     God.watch { |w| w.name = 'foo'; w.start = 'bar' }
209     
210     w = God.watches['foo']
211     w.expects(:move).with(:restart)
212     God.control('foo', 'restart')
213   end
214   
215   def test_control_should_unmonitor_and_stop_on_stop
216     God.watch { |w| w.name = 'foo'; w.start = 'bar' }
217     
218     w = God.watches['foo']
219     w.expects(:unmonitor).returns(w)
220     w.expects(:action).with(:stop)
221     God.control('foo', 'stop')
222   end
223   
224   def test_control_should_unmonitor_on_unmonitor
225     God.watch { |w| w.name = 'foo'; w.start = 'bar' }
226     
227     w = God.watches['foo']
228     w.expects(:unmonitor).returns(w)
229     God.control('foo', 'unmonitor')
230   end
231   
232   def test_control_should_raise_on_invalid_command
233     God.watch { |w| w.name = 'foo'; w.start = 'bar' }
234     
235     assert_raise InvalidCommandError do
236       God.control('foo', 'invalid')
237     end
238   end
239   
240   def test_control_should_operate_on_each_watch_in_group
241     God.watch do |w|
242       w.name = 'foo1'
243       w.start = 'go'
244       w.group = 'bar'
245     end
246     
247     God.watch do |w|
248       w.name = 'foo2'
249       w.start = 'go'
250       w.group = 'bar'
251     end
252     
253     God.watches['foo1'].expects(:monitor)
254     God.watches['foo2'].expects(:monitor)
255     
256     God.control('bar', 'start')
257   end
258   
259   # stop_all
260   
261   # terminate
262   
263   def test_terminate_should_exit
264     God.expects(:exit!).with(0)
265     God.terminate
266   end
267   
268   # status
269   
270   def test_status_should_show_state
271     God.watch { |w| w.name = 'foo'; w.start = 'bar' }
272     
273     w = God.watches['foo']
274     w.state = :up
275     assert_equal({'foo' => {:state => :up}}, God.status)
276   end
277   
278   def test_status_should_show_unmonitored_for_nil_state
279     God.watch { |w| w.name = 'foo'; w.start = 'bar' }
280     
281     w = God.watches['foo']
282     assert_equal({'foo' => {:state => :unmonitored}}, God.status)
283   end
284   
285   # running_load
286   
287   def test_running_load_should_eval_code
288     code = <<-EOF
289       God.watch do |w|
290         w.name = 'foo'
291         w.start = 'go'
292       end
293     EOF
294     
295     no_stdout do
296       God.running_load(code)
297     end
298     
299     assert_equal 1, God.watches.size
300   end
301   
302   def test_running_load_should_monitor_new_watches
303     code = <<-EOF
304       God.watch do |w|
305         w.name = 'foo'
306         w.start = 'go'
307       end
308     EOF
309     
310     Watch.any_instance.expects(:monitor)
311     no_stdout do
312       God.running_load(code)
313     end
314   end
315   
316   def test_running_load_should_not_monitor_new_watches_with_autostart_false
317     code = <<-EOF
318       God.watch do |w|
319         w.name = 'foo'
320         w.start = 'go'
321         w.autostart = false
322       end
323     EOF
324     
325     Watch.any_instance.expects(:monitor).never
326     no_stdout do
327       God.running_load(code)
328     end
329   end
330   
331   def test_running_load_should_return_array_of_affected_watches
332     code = <<-EOF
333       God.watch do |w|
334         w.name = 'foo'
335         w.start = 'go'
336       end
337     EOF
338     
339     w = nil
340     no_stdout do
341       w = God.running_load(code)
342     end
343     assert_equal 1, w.size
344     assert_equal 'foo', w.first.name
345   end
346   
347   def test_running_load_should_clear_pending_watches
348     code = <<-EOF
349       God.watch do |w|
350         w.name = 'foo'
351         w.start = 'go'
352       end
353     EOF
354     
355     no_stdout do
356       God.running_load(code)
357     end
358     assert_equal 0, God.pending_watches.size
359   end
360   
361   # load
362   
363   def test_load_should_collect_and_load_globbed_path
364     Dir.expects(:[]).with('/path/to/*.thing').returns(['a', 'b'])
365     Kernel.expects(:load).with('a').once
366     Kernel.expects(:load).with('b').once
367     God.load('/path/to/*.thing')
368   end
369   
370   # start
371   
372   def test_start_should_kick_off_a_server_instance
373     Server.expects(:new).returns(true)
374     God.start
375   end
376     
377   def test_start_should_start_event_handler
378     God.watch { |w| w.name = 'foo'; w.start = 'bar' }
379     Timer.any_instance.expects(:join)
380     EventHandler.expects(:start).once
381     no_stdout do
382       God.start
383     end
384   end
385   
386   def test_start_should_begin_monitoring_autostart_watches
387     God.watch do |w|
388       w.name = 'foo'
389       w.start = 'go'
390     end
391     
392     Timer.any_instance.expects(:join)
393     Watch.any_instance.expects(:monitor).once
394     God.start
395   end
396   
397   def test_start_should_not_begin_monitoring_non_autostart_watches
398     God.watch do |w|
399       w.name = 'foo'
400       w.start = 'go'
401       w.autostart = false
402     end
403     
404     Timer.any_instance.expects(:join)
405     Watch.any_instance.expects(:monitor).never
406     God.start
407   end
408   
409   def test_start_should_get_and_join_timer
410     God.watch { |w| w.name = 'foo'; w.start = 'bar' }
411     Timer.any_instance.expects(:join)
412     no_stdout do
413       God.start
414     end
415   end
416   
417   # at_exit
418   
419   def test_at_exit_should_call_start
420     God.expects(:start).once
421     God.at_exit_orig
422   end
426 class TestGodOther < Test::Unit::TestCase
427   def setup
428     Server.stubs(:new).returns(true)
429     God.internal_init
430     God.reset
431   end
432   
433   def teardown
434     Timer.get.timer.kill
435   end
436   
437   # setup
438   
439   def test_setup_should_create_pid_file_directory_if_it_doesnt_exist
440     God.expects(:test).returns(false)
441     FileUtils.expects(:mkdir_p).with(God.pid_file_directory)
442     God.setup
443   end
444   
445   def test_setup_should_raise_if_no_permissions_to_create_pid_file_directory
446     God.expects(:test).returns(false)
447     FileUtils.expects(:mkdir_p).raises(Errno::EACCES)
448     
449     assert_abort do
450       God.setup
451     end
452   end
453   
454   # validate
455     
456   def test_validate_should_abort_if_pid_file_directory_is_unwriteable
457     God.expects(:test).returns(false)
458     assert_abort do
459       God.validater
460     end
461   end
462   
463   def test_validate_should_not_abort_if_pid_file_directory_is_writeable
464     God.expects(:test).returns(true)
465     assert_nothing_raised do
466       God.validater
467     end
468   end