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