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