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