fix process#alive? to not raise on no such file
[god.git] / test / test_watch.rb
blobcffede4125a6eb9aa9165ad7f2f04b7982dba61f
1 require File.dirname(__FILE__) + '/helper'
3 class TestWatch < Test::Unit::TestCase
4   def setup
5     @watch = Watch.new
6     @watch.name = 'foo'
7     @watch.start = lambda { }
8     @watch.stop = lambda { }
9   end
10   
11   # new
12   
13   def test_new_should_have_no_behaviors
14     assert_equal [], @watch.behaviors
15   end
16   
17   def test_new_should_have_no_metrics
18     Watch::VALID_STATES.each do |state|
19       assert_equal [], @watch.metrics[state]
20     end
21   end
22     
23   def test_new_should_have_standard_attributes
24     assert_nothing_raised do
25       @watch.name = 'foo'
26       @watch.start = 'start'
27       @watch.stop = 'stop'
28       @watch.restart = 'restart'
29       @watch.interval = 30
30       @watch.grace = 5
31     end
32   end
33   
34   def test_new_should_have_nil_state
35     assert_equal nil, @watch.state
36   end
37   
38   # mutex
39   
40   def test_mutex_should_return_the_same_mutex_each_time
41     assert_equal @watch.mutex, @watch.mutex
42   end
43   
44   # valid?
45   
46   def test_valid?
47     God::Process.any_instance.expects(:valid?)
48     @watch.valid?
49   end
50   
51   # behavior
52   
53   def test_behavior_should_record_behavior
54     beh = nil
55     @watch.behavior(:fake_behavior) { |b| beh = b }
56     assert_equal 1, @watch.behaviors.size
57     assert_equal beh, @watch.behaviors.first
58   end
59   
60   def test_invalid_behavior_should_abort
61     assert_abort do
62       @watch.behavior(:invalid)
63     end
64   end
65   
66   # transition
67   
68   def test_transition_should_abort_on_invalid_start_state
69     assert_abort do
70       @watch.transition(:foo, :bar)
71     end
72   end
73   
74   def test_transition_should_accept_all_valid_start_states
75     assert_nothing_raised do
76       Watch::VALID_STATES.each do |state|
77         @watch.transition(state, :bar) { }
78       end
79     end
80   end
81   
82   def test_transition_should_create_and_record_a_metric_for_the_given_start_state
83     @watch.transition(:init, :start) { }
84     assert_equal 1, @watch.metrics[:init].size
85   end
86   
87   # start_if
88   
89   def test_start_if_should_place_a_metric_on_up_state
90     @watch.start_if { }
91     assert_equal 1, @watch.metrics[:up].size
92   end
93   
94   # restart_if
95   
96   def test_restart_if_should_place_a_metric_on_up_state
97     @watch.restart_if { }
98     assert_equal 1, @watch.metrics[:up].size
99   end
100   
101   # monitor
102   
103   def test_monitor_should_move_to_init_if_available
104     @watch.instance_eval do
105       transition(:init, :up) { }
106     end
107     @watch.expects(:move).with(:init)
108     @watch.monitor
109   end
110   
111   def test_monitor_should_move_to_up_if_no_init_available
112     @watch.expects(:move).with(:up)
113     @watch.monitor
114   end
115   
116   # unmonitor
117   
118   def test_unmonitor_should_move_to_nil
119     @watch.expects(:move).with(nil)
120     @watch.unmonitor
121   end
122   
123   # move
124   
125   def test_move_should_not_clean_up_if_from_state_is_nil
126     metric = nil
127     
128     @watch.instance_eval do
129       transition(:init, :up) do |on|
130         metric = on
131         on.condition(:process_running) do |c|
132           c.running = true
133           c.interval = 10
134         end
135       end
136     end
137     
138     metric.expects(:disable).never
139     
140     no_stdout { @watch.move(:init) }
141   end
142   
143   def test_move_should_clean_up_from_state_if_not_nil
144     metric = nil
145     
146     @watch.instance_eval do
147       transition(:init, :up) do |on|
148         metric = on
149         on.condition(:process_running) do |c|
150           c.running = true
151           c.interval = 10
152         end
153       end
154     end
155     
156     no_stdout { @watch.move(:init) }
157     
158     metric.expects(:disable)
159     
160     no_stdout { @watch.move(:up) }
161   end
162   
163   def test_move_should_call_action
164     @watch.expects(:action).with(:start)
165     
166     no_stdout { @watch.move(:start) }
167   end
168   
169   def test_move_should_move_to_up_state_if_no_start_or_restart_metric
170     [:start, :restart].each do |state|
171       @watch.expects(:action)
172       no_stdout { @watch.move(state) }
173       assert_equal :up, @watch.state
174     end
175   end
176   
177   def test_move_should_enable_destination_metric
178     metric = nil
179     
180     @watch.instance_eval do
181       transition(:init, :up) do |on|
182         metric = on
183         on.condition(:process_running) do |c|
184           c.running = true
185           c.interval = 10
186         end
187       end
188     end
189     
190     metric.expects(:enable)
191     
192     no_stdout { @watch.move(:init) }
193   end
194   
195   # action
196   
197   def test_action_should_pass_start_and_stop_actions_to_call_action
198     c = Conditions::FakePollCondition.new
199     [:start, :stop].each do |cmd|
200       @watch.expects(:call_action).with(c, cmd)
201       no_stdout { @watch.action(cmd, c) }
202     end
203   end
204   
205   def test_action_should_do_stop_then_start_if_no_restart_command
206     c = Conditions::FakePollCondition.new
207     @watch.expects(:call_action).with(c, :stop)
208     @watch.expects(:call_action).with(c, :start)
209     no_stdout { @watch.action(:restart, c) }
210   end
211   
212   def test_action_should_restart_to_call_action_if_present
213     @watch.restart = lambda { }
214     c = Conditions::FakePollCondition.new
215     @watch.expects(:call_action).with(c, :restart)
216     no_stdout { @watch.action(:restart, c) }
217   end
218   
219   # call_action
220   
221   def test_call_action
222     c = Conditions::FakePollCondition.new
223     God::Process.any_instance.expects(:call_action).with(:start)
224     @watch.call_action(c, :start)
225   end
226   
227   # canonical_hash_form
228   
229   def test_canonical_hash_form_should_convert_symbol_to_hash
230     assert_equal({true => :foo}, @watch.canonical_hash_form(:foo))
231   end
232   
233   def test_canonical_hash_form_should_convert_hash_to_hash
234     assert_equal({true => :foo}, @watch.canonical_hash_form(true => :foo))
235   end