fixed god binary exiting with -1; fixed god binary group control
[god.git] / test / test_god.rb
blob9528c14b4ce06b976b1e32aa67ce43808aa46bee
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_register_processes
49     assert_nil God.registry['foo']
50     God.watch { |w| w.name = 'foo' }
51     assert_kind_of God::Process, God.registry['foo']
52   end
53   
54   def test_watch_should_get_stored_by_group
55     a = nil
56     
57     God.watch do |w|
58       a = w
59       w.name = 'foo'
60       w.group = 'test'
61     end
62     
63     assert_equal({'test' => [a]}, God.groups)
64   end
65   
66   def test_watches_should_get_stored_by_group
67     a = nil
68     b = nil
69     
70     God.watch do |w|
71       a = w
72       w.name = 'foo'
73       w.group = 'test'
74     end
75     
76     God.watch do |w|
77       b = w
78       w.name = 'bar'
79       w.group = 'test'
80     end
81     
82     assert_equal({'test' => [a, b]}, God.groups)
83   end
84       
85   def test_watch_should_allow_multiple_watches
86     God.watch { |w| w.name = 'foo' }
87     
88     assert_nothing_raised do
89       God.watch { |w| w.name = 'bar' }
90     end
91   end
92   
93   def test_watch_should_disallow_duplicate_watch_names
94     God.watch { |w| w.name = 'foo' }
95     
96     assert_abort do
97       God.watch { |w| w.name = 'foo' }
98     end
99   end
100   
101   def test_watch_should_disallow_identical_watch_and_group_names
102     God.watch { |w| w.name = 'foo'; w.group = 'bar' }
103     
104     assert_abort do
105       God.watch { |w| w.name = 'bar' }
106     end
107   end
108   
109   def test_watch_should_disallow_identical_watch_and_group_names_other_way
110     God.watch { |w| w.name = 'bar' }
111     
112     assert_abort do
113       God.watch { |w| w.name = 'foo'; w.group = 'bar' }
114     end
115   end
116   
117   # control
118   
119   def test_control_should_monitor_on_start
120     God.watch { |w| w.name = 'foo' }
121     
122     w = God.watches['foo']
123     w.expects(:monitor)
124     God.control('foo', 'start')
125   end
126   
127   def test_control_should_move_to_restart_on_restart
128     God.watch { |w| w.name = 'foo' }
129     
130     w = God.watches['foo']
131     w.expects(:move).with(:restart)
132     God.control('foo', 'restart')
133   end
134   
135   def test_control_should_unmonitor_and_stop_on_stop
136     God.watch { |w| w.name = 'foo' }
137     
138     w = God.watches['foo']
139     w.expects(:unmonitor).returns(w)
140     w.expects(:action).with(:stop)
141     God.control('foo', 'stop')
142   end
143   
144   def test_control_should_unmonitor_on_unmonitor
145     God.watch { |w| w.name = 'foo' }
146     
147     w = God.watches['foo']
148     w.expects(:unmonitor).returns(w)
149     God.control('foo', 'unmonitor')
150   end
151   
152   def test_control_should_raise_on_invalid_command
153     God.watch { |w| w.name = 'foo' }
154     
155     assert_raise InvalidCommandError do
156       God.control('foo', 'invalid')
157     end
158   end
159   
160   def test_control_should_operate_on_each_watch_in_group
161     God.watch do |w|
162       w.name = 'foo1'
163       w.group = 'bar'
164     end
165     
166     God.watch do |w|
167       w.name = 'foo2'
168       w.group = 'bar'
169     end
170     
171     God.watches['foo1'].expects(:monitor)
172     God.watches['foo2'].expects(:monitor)
173     
174     God.control('bar', 'start')
175   end
176   
177   # start
178   
179   def test_start_should_check_for_at_least_one_watch
180     assert_abort do
181       God.start
182     end
183   end
184   
185   def test_start_should_start_event_handler
186     God.watch { |w| w.name = 'foo' }
187     Timer.any_instance.expects(:join)
188     EventHandler.expects(:start).once
189     no_stdout do
190       God.start
191     end
192   end
193   
194   def test_start_should_begin_monitoring_autostart_watches
195     God.watch do |w|
196       w.name = 'foo'
197     end
198     
199     Timer.any_instance.expects(:join)
200     Watch.any_instance.expects(:monitor).once
201     God.start
202   end
203   
204   def test_start_should_not_begin_monitoring_non_autostart_watches
205     God.watch do |w|
206       w.name = 'foo'
207       w.autostart = false
208     end
209     
210     Timer.any_instance.expects(:join)
211     Watch.any_instance.expects(:monitor).never
212     God.start
213   end
214   
215   def test_start_should_get_and_join_timer
216     God.watch { |w| w.name = 'foo' }
217     Timer.any_instance.expects(:join)
218     no_stdout do
219       God.start
220     end
221   end
222   
223   # at_exit
224   
225   def test_at_exit_should_call_start
226     God.expects(:start).once
227     God.at_exit_orig
228   end
229   
230   # load
231   
232   def test_load_should_collect_and_load_globbed_path
233     Dir.expects(:[]).with('/path/to/*.thing').returns(['a', 'b'])
234     Kernel.expects(:load).with('a').once
235     Kernel.expects(:load).with('b').once
236     God.load('/path/to/*.thing')
237   end