default stop for auto-daemonized; bug fixes
[god.git] / test / test_god.rb
blob677314fe838d58609f3c477ac88fb668d708e84e
1 require File.dirname(__FILE__) + '/helper'
3 class TestGod < Test::Unit::TestCase
4   def setup
5     Server.stubs(:new).returns(true)
6     God.reset
7   end
8   
9   def teardown
10     Timer.get.timer.kill
11   end
12   
13   # init
14   
15   def test_init_should_initialize_watches_to_empty_array
16     God.init { }
17     assert_equal Hash.new, God.watches
18   end
19   
20   def test_init_should_kick_off_a_server_instance
21     Server.expects(:new).returns(true)
22     God.init
23   end
24   
25   def test_init_should_abort_if_called_after_watch
26     God.watch { |w| w.name = 'foo'; w.start = 'bar' }
27     
28     assert_abort do
29       God.init { }
30     end
31   end
32   
33   # pid_file_directory
34   
35   def test_pid_file_directory_should_return_default_if_not_set_explicitly
36     assert_equal '/var/run/god', God.pid_file_directory
37   end
38   
39   def test_pid_file_directory_equals_should_set
40     God.pid_file_directory = '/foo'
41     assert_equal '/foo', God.pid_file_directory
42   end
43   
44   # watch
45   
46   def test_watch_should_get_stored
47     watch = nil
48     God.watch do |w|
49       w.name = 'foo'
50       w.start = 'bar'
51       watch = w
52     end
53     
54     assert_equal 1, God.watches.size
55     assert_equal watch, God.watches.values.first
56     
57     assert_equal 0, God.groups.size
58   end
59   
60   def test_watch_should_get_stored_in_pending_watches
61     watch = nil
62     God.watch do |w|
63       w.name = 'foo'
64       w.start = 'bar'
65       watch = w
66     end
67     
68     assert_equal 1, God.pending_watches.size
69     assert_equal watch, God.pending_watches.first
70   end
71   
72   def test_watch_should_register_processes
73     assert_nil God.registry['foo']
74     God.watch do |w|
75       w.name = 'foo'
76       w.start = 'bar'
77     end
78     assert_kind_of God::Process, God.registry['foo']
79   end
80   
81   def test_watch_should_get_stored_by_group
82     a = nil
83     
84     God.watch do |w|
85       a = w
86       w.name = 'foo'
87       w.start = 'bar'
88       w.group = 'test'
89     end
90     
91     assert_equal({'test' => [a]}, God.groups)
92   end
93   
94   def test_watches_should_get_stored_by_group
95     a = nil
96     b = nil
97     
98     God.watch do |w|
99       a = w
100       w.name = 'foo'
101       w.start = 'bar'
102       w.group = 'test'
103     end
104     
105     God.watch do |w|
106       b = w
107       w.name = 'bar'
108       w.start = 'baz'
109       w.group = 'test'
110     end
111     
112     assert_equal({'test' => [a, b]}, God.groups)
113   end
114       
115   def test_watch_should_allow_multiple_watches
116     God.watch { |w| w.name = 'foo'; w.start = 'bar' }
117     
118     assert_nothing_raised do
119       God.watch { |w| w.name = 'bar'; w.start = 'bar' }
120     end
121   end
122   
123   def test_watch_should_disallow_duplicate_watch_names
124     God.watch { |w| w.name = 'foo'; w.start = 'bar' }
125     
126     assert_abort do
127       God.watch { |w| w.name = 'foo'; w.start = 'bar' }
128     end
129   end
130   
131   def test_watch_should_disallow_identical_watch_and_group_names
132     God.watch { |w| w.name = 'foo'; w.group = 'bar'; w.start = 'bar' }
133     
134     assert_abort do
135       God.watch { |w| w.name = 'bar'; w.start = 'bar' }
136     end
137   end
138   
139   def test_watch_should_disallow_identical_watch_and_group_names_other_way
140     God.watch { |w| w.name = 'bar'; w.start = 'bar' }
141     
142     assert_abort do
143       God.watch { |w| w.name = 'foo'; w.group = 'bar'; w.start = 'bar' }
144     end
145   end
146   
147   def test_watch_should_unwatch_new_watch_if_running_and_duplicate_watch
148     God.watch { |w| w.name = 'foo'; w.start = 'bar' }
149     God.running = true
150     
151     assert_nothing_raised do
152       no_stdout do
153         God.watch { |w| w.name = 'foo'; w.start = 'bar' }
154       end
155     end
156   end
157   
158   # unwatch
159   
160   def test_unwatch_should_unmonitor_watch
161     God.watch { |w| w.name = 'bar'; w.start = 'bar' }
162     w = God.watches['bar']
163     w.expects(:unmonitor)
164     God.unwatch(w)
165   end
166   
167   def test_unwatch_should_unregister_watch
168     God.watch { |w| w.name = 'bar'; w.start = 'bar' }
169     w = God.watches['bar']
170     w.expects(:unregister!)
171     no_stdout do
172       God.unwatch(w)
173     end
174   end
175   
176   def test_unwatch_should_remove_same_name_watches
177     God.watch { |w| w.name = 'bar'; w.start = 'bar' }
178     w = God.watches['bar']
179     no_stdout do
180       God.unwatch(w)
181     end
182     assert_equal 0, God.watches.size
183   end
184   
185   def test_unwatch_should_remove_from_group
186     God.watch do |w|
187       w.name = 'bar'
188       w.start = 'baz'
189       w.group = 'test'
190     end
191     w = God.watches['bar']
192     no_stdout do
193       God.unwatch(w)
194     end
195     assert !God.groups[w.group].include?(w)
196   end
197   
198   # control
199   
200   def test_control_should_monitor_on_start
201     God.watch { |w| w.name = 'foo'; w.start = 'bar' }
202     
203     w = God.watches['foo']
204     w.expects(:monitor)
205     God.control('foo', 'start')
206   end
207   
208   def test_control_should_move_to_restart_on_restart
209     God.watch { |w| w.name = 'foo'; w.start = 'bar' }
210     
211     w = God.watches['foo']
212     w.expects(:move).with(:restart)
213     God.control('foo', 'restart')
214   end
215   
216   def test_control_should_unmonitor_and_stop_on_stop
217     God.watch { |w| w.name = 'foo'; w.start = 'bar' }
218     
219     w = God.watches['foo']
220     w.expects(:unmonitor).returns(w)
221     w.expects(:action).with(:stop)
222     God.control('foo', 'stop')
223   end
224   
225   def test_control_should_unmonitor_on_unmonitor
226     God.watch { |w| w.name = 'foo'; w.start = 'bar' }
227     
228     w = God.watches['foo']
229     w.expects(:unmonitor).returns(w)
230     God.control('foo', 'unmonitor')
231   end
232   
233   def test_control_should_raise_on_invalid_command
234     God.watch { |w| w.name = 'foo'; w.start = 'bar' }
235     
236     assert_raise InvalidCommandError do
237       God.control('foo', 'invalid')
238     end
239   end
240   
241   def test_control_should_operate_on_each_watch_in_group
242     God.watch do |w|
243       w.name = 'foo1'
244       w.start = 'go'
245       w.group = 'bar'
246     end
247     
248     God.watch do |w|
249       w.name = 'foo2'
250       w.start = 'go'
251       w.group = 'bar'
252     end
253     
254     God.watches['foo1'].expects(:monitor)
255     God.watches['foo2'].expects(:monitor)
256     
257     God.control('bar', 'start')
258   end
259   
260   # running_load
261   
262   def test_running_load_should_eval_code
263     code = <<-EOF
264       God.watch do |w|
265         w.name = 'foo'
266         w.start = 'go'
267       end
268     EOF
269     
270     no_stdout do
271       God.running_load(code)
272     end
273     
274     assert_equal 1, God.watches.size
275   end
276   
277   def test_running_load_should_monitor_new_watches
278     code = <<-EOF
279       God.watch do |w|
280         w.name = 'foo'
281         w.start = 'go'
282       end
283     EOF
284     
285     Watch.any_instance.expects(:monitor)
286     no_stdout do
287       God.running_load(code)
288     end
289   end
290   
291   def test_running_load_should_not_monitor_new_watches_with_autostart_false
292     code = <<-EOF
293       God.watch do |w|
294         w.name = 'foo'
295         w.start = 'go'
296         w.autostart = false
297       end
298     EOF
299     
300     Watch.any_instance.expects(:monitor).never
301     no_stdout do
302       God.running_load(code)
303     end
304   end
305   
306   def test_running_load_should_return_array_of_affected_watches
307     code = <<-EOF
308       God.watch do |w|
309         w.name = 'foo'
310         w.start = 'go'
311       end
312     EOF
313     
314     w = nil
315     no_stdout do
316       w = God.running_load(code)
317     end
318     assert_equal 1, w.size
319     assert_equal 'foo', w.first.name
320   end
321   
322   def test_running_load_should_clear_pending_watches
323     code = <<-EOF
324       God.watch do |w|
325         w.name = 'foo'
326         w.start = 'go'
327       end
328     EOF
329     
330     no_stdout do
331       God.running_load(code)
332     end
333     assert_equal 0, God.pending_watches.size
334   end
335   
336   # load
337   
338   def test_load_should_collect_and_load_globbed_path
339     Dir.expects(:[]).with('/path/to/*.thing').returns(['a', 'b'])
340     Kernel.expects(:load).with('a').once
341     Kernel.expects(:load).with('b').once
342     God.load('/path/to/*.thing')
343   end
344   
345   # start
346   
347   def test_start_should_check_for_at_least_one_watch
348     assert_abort do
349       God.start
350     end
351   end
352   
353   def test_start_should_start_event_handler
354     God.watch { |w| w.name = 'foo'; w.start = 'bar' }
355     Timer.any_instance.expects(:join)
356     EventHandler.expects(:start).once
357     no_stdout do
358       God.start
359     end
360   end
361   
362   def test_start_should_begin_monitoring_autostart_watches
363     God.watch do |w|
364       w.name = 'foo'
365       w.start = 'go'
366     end
367     
368     Timer.any_instance.expects(:join)
369     Watch.any_instance.expects(:monitor).once
370     God.start
371   end
372   
373   def test_start_should_not_begin_monitoring_non_autostart_watches
374     God.watch do |w|
375       w.name = 'foo'
376       w.start = 'go'
377       w.autostart = false
378     end
379     
380     Timer.any_instance.expects(:join)
381     Watch.any_instance.expects(:monitor).never
382     God.start
383   end
384   
385   def test_start_should_get_and_join_timer
386     God.watch { |w| w.name = 'foo'; w.start = 'bar' }
387     Timer.any_instance.expects(:join)
388     no_stdout do
389       God.start
390     end
391   end
392   
393   # at_exit
394   
395   def test_at_exit_should_call_start
396     God.expects(:start).once
397     God.at_exit_orig
398   end