File: test_watch.rb

package info (click to toggle)
ruby-god 0.12.1-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 752 kB
  • sloc: ruby: 5,913; ansic: 217; makefile: 3
file content (286 lines) | stat: -rw-r--r-- 6,937 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
require File.dirname(__FILE__) + '/helper'

class TestWatch < Test::Unit::TestCase
  def setup
    God.internal_init
    @watch = Watch.new
    @watch.name = 'foo'
    @watch.start = lambda { }
    @watch.stop = lambda { }
    @watch.prepare
  end

  # new

  def test_new_should_have_no_behaviors
    assert_equal [], @watch.behaviors
  end

  def test_new_should_have_no_metrics
    Watch::VALID_STATES.each do |state|
      assert_equal [], @watch.metrics[state]
    end
  end

  def test_new_should_have_standard_attributes
    assert_nothing_raised do
      @watch.name = 'foo'
      @watch.start = 'start'
      @watch.stop = 'stop'
      @watch.restart = 'restart'
      @watch.interval = 30
      @watch.grace = 5
    end
  end

  def test_new_should_have_unmonitored_state
    assert_equal :unmonitored, @watch.state
  end

  # valid?

  def test_valid?
    God::Process.any_instance.expects(:valid?)
    @watch.valid?
  end

  # behavior

  def test_behavior_should_record_behavior
    beh = nil
    @watch.behavior(:fake_behavior) { |b| beh = b }
    assert_equal 1, @watch.behaviors.size
    assert_equal beh, @watch.behaviors.first
  end

  def test_invalid_behavior_should_abort
    assert_abort do
      @watch.behavior(:invalid)
    end
  end

  # transition

  def test_transition_should_abort_on_invalid_start_state
    assert_abort do
      @watch.transition(:foo, :bar)
    end
  end

  def test_transition_should_accept_all_valid_start_states
    assert_nothing_raised do
      Watch::VALID_STATES.each do |state|
        @watch.transition(state, :bar) { }
      end
    end
  end

  def test_transition_should_create_and_record_a_metric_for_the_given_start_state
    @watch.transition(:init, :start) { }
    assert_equal 1, @watch.metrics[:init].size
  end

  # lifecycle

  def test_lifecycle_should_create_and_record_a_metric_for_nil_start_state
    @watch.lifecycle { }
    assert_equal 1, @watch.metrics[nil].size
  end

  # keepalive

  def test_keepalive_should_place_metrics_on_up_state
    @watch.keepalive(:memory_max => 5.megabytes, :cpu_max => 50.percent)
    assert_equal 2, @watch.metrics[:up].size
  end

  # start_if

  def test_start_if_should_place_a_metric_on_up_state
    @watch.start_if { }
    assert_equal 1, @watch.metrics[:up].size
  end

  # restart_if

  def test_restart_if_should_place_a_metric_on_up_state
    @watch.restart_if { }
    assert_equal 1, @watch.metrics[:up].size
  end

  # monitor

  def test_monitor_should_move_to_init_if_available
    @watch.instance_eval do
      transition(:init, :up) { }
    end
    @watch.expects(:move).with(:init)
    @watch.monitor
  end

  def test_monitor_should_move_to_up_if_no_init_available
    @watch.expects(:move).with(:up)
    @watch.monitor
  end

  # unmonitor

  def test_unmonitor_should_move_to_nil
    @watch.expects(:move).with(:unmonitored)
    @watch.unmonitor
  end

  # move

  def test_move_should_not_clean_up_if_from_state_is_nil
    @watch.driver.stubs(:in_driver_context?).returns(true)
    @watch.driver.expects(:message).never

    metric = nil

    @watch.instance_eval do
      transition(:init, :up) do |on|
        metric = on
        on.condition(:process_running) do |c|
          c.running = true
          c.interval = 10
        end
      end
    end

    metric.expects(:disable).never

    @watch.move(:init)
  end

  def test_move_should_clean_up_from_state_if_not_nil
    @watch.driver.stubs(:in_driver_context?).returns(true)
    @watch.driver.expects(:message).never

    metric = nil

    @watch.instance_eval do
      transition(:init, :up) do |on|
        metric = on
        on.condition(:process_running) do |c|
          c.running = true
          c.interval = 10
        end
      end
    end

    @watch.move(:init)

    metric.expects(:disable)

    @watch.move(:up)
  end

  def test_move_should_call_action
    @watch.driver.stubs(:in_driver_context?).returns(true)
    @watch.driver.expects(:message).never

    @watch.expects(:action).with(:start)

    @watch.move(:start)
  end

  def test_move_should_move_to_up_state_if_no_start_or_restart_metric
    @watch.driver.stubs(:in_driver_context?).returns(true)
    @watch.driver.expects(:message).never

    [:start, :restart].each do |state|
      @watch.expects(:action)
      @watch.move(state)
      assert_equal :up, @watch.state
    end
  end

  def test_move_should_enable_destination_metric
    @watch.driver.stubs(:in_driver_context?).returns(true)
    @watch.driver.expects(:message).never

    metric = nil

    @watch.instance_eval do
      transition(:init, :up) do |on|
        metric = on
        on.condition(:process_running) do |c|
          c.running = true
          c.interval = 10
        end
      end
    end

    metric.expects(:enable)

    @watch.move(:init)
  end

  # action

  def test_action_should_pass_start_and_stop_actions_to_call_action
    @watch.driver.stubs(:in_driver_context?).returns(true)
    @watch.driver.expects(:message).never

    c = Conditions::FakePollCondition.new
    [:start, :stop].each do |cmd|
      @watch.expects(:call_action).with(c, cmd)
      @watch.action(cmd, c)
    end
  end

  def test_action_should_do_stop_then_start_if_no_restart_command
    @watch.driver.stubs(:in_driver_context?).returns(true)
    @watch.driver.expects(:message).never

    c = Conditions::FakePollCondition.new
    @watch.expects(:call_action).with(c, :stop)
    @watch.expects(:call_action).with(c, :start)
    @watch.action(:restart, c)
  end

  def test_action_should_restart_to_call_action_if_present
    @watch.driver.stubs(:in_driver_context?).returns(true)
    @watch.driver.expects(:message).never

    @watch.restart = lambda { }
    c = Conditions::FakePollCondition.new
    @watch.expects(:call_action).with(c, :restart)
    @watch.action(:restart, c)
  end

  # call_action

  def test_call_action
    c = Conditions::FakePollCondition.new
    God::Process.any_instance.expects(:call_action).with(:start)
    @watch.call_action(c, :start)
  end

  def test_call_action_should_call_before_start_when_behavior_has_that
    @watch.behavior(:fake_behavior)
    c = Conditions::FakePollCondition.new
    God::Process.any_instance.expects(:call_action).with(:start)
    Behaviors::FakeBehavior.any_instance.expects(:before_start)
    @watch.call_action(c, :start)
  end

  def test_call_action_should_call_after_start_when_behavior_has_that
    @watch.behavior(:fake_behavior)
    c = Conditions::FakePollCondition.new
    God::Process.any_instance.expects(:call_action).with(:start)
    Behaviors::FakeBehavior.any_instance.expects(:after_start)
    @watch.call_action(c, :start)
  end

  # canonical_hash_form

  def test_canonical_hash_form_should_convert_symbol_to_hash
    assert_equal({true => :foo}, @watch.canonical_hash_form(:foo))
  end

  def test_canonical_hash_form_should_convert_hash_to_hash
    assert_equal({true => :foo}, @watch.canonical_hash_form(true => :foo))
  end
end