monitr 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,37 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ class TestTimeline < Test::Unit::TestCase
4
+ def setup
5
+ @timeline = Timeline.new(5)
6
+ end
7
+
8
+ def test_new_should_be_empty
9
+ assert_equal 0, @timeline.size
10
+ end
11
+
12
+ def test_should_not_grow_to_more_than_size
13
+ (1..10).each do |i|
14
+ @timeline.push(i)
15
+ end
16
+
17
+ assert_equal [6, 7, 8, 9, 10], @timeline
18
+ end
19
+
20
+ def test_clear_should_clear_array
21
+ @timeline << 1
22
+ assert_equal [1], @timeline
23
+ assert_equal [], @timeline.clear
24
+ end
25
+
26
+ # def test_benchmark
27
+ # require 'benchmark'
28
+ #
29
+ # count = 1_000_000
30
+ #
31
+ # t = Timeline.new(10)
32
+ #
33
+ # Benchmark.bmbm do |x|
34
+ # x.report("go") { count.times { t.push(5) } }
35
+ # end
36
+ # end
37
+ end
@@ -0,0 +1,59 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ class TestTrigger < Test::Unit::TestCase
4
+ def setup
5
+ Trigger.reset
6
+ end
7
+
8
+ # base case
9
+
10
+ def test_should_have_empty_triggers
11
+ assert_equal({}, Trigger.triggers)
12
+ end
13
+
14
+ # register
15
+
16
+ def test_register_should_add_condition_to_triggers
17
+ c = Condition.new
18
+ c.watch = stub(:name => 'foo')
19
+ Trigger.register(c)
20
+
21
+ assert_equal({'foo' => [c]}, Trigger.triggers)
22
+ end
23
+
24
+ def test_register_should_add_condition_to_triggers_twice
25
+ watch = stub(:name => 'foo')
26
+ c = Condition.new
27
+ c.watch = watch
28
+ Trigger.register(c)
29
+
30
+ c2 = Condition.new
31
+ c2.watch = watch
32
+ Trigger.register(c2)
33
+
34
+ assert_equal({'foo' => [c, c2]}, Trigger.triggers)
35
+ end
36
+
37
+ # deregister
38
+
39
+ def test_deregister_should_remove_condition_from_triggers
40
+ c = Condition.new
41
+ c.watch = stub(:name => 'foo')
42
+ Trigger.register(c)
43
+ Trigger.deregister(c)
44
+
45
+ assert_equal({}, Trigger.triggers)
46
+ end
47
+
48
+ # broadcast
49
+
50
+ def test_broadcast_should_call_process_on_each_condition
51
+ c = Condition.new
52
+ c.watch = stub(:name => 'foo')
53
+ Trigger.register(c)
54
+
55
+ c.expects(:process).with(:state_change, [:up, :start])
56
+
57
+ Trigger.broadcast(c.watch, :state_change, [:up, :start])
58
+ end
59
+ end
@@ -0,0 +1,279 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ class TestWatch < Test::Unit::TestCase
4
+ def setup
5
+ Monitr.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
+ # valid?
41
+
42
+ def test_valid?
43
+ Monitr::Process.any_instance.expects(:valid?)
44
+ @watch.valid?
45
+ end
46
+
47
+ # behavior
48
+
49
+ def test_behavior_should_record_behavior
50
+ beh = nil
51
+ @watch.behavior(:fake_behavior) { |b| beh = b }
52
+ assert_equal 1, @watch.behaviors.size
53
+ assert_equal beh, @watch.behaviors.first
54
+ end
55
+
56
+ def test_invalid_behavior_should_abort
57
+ assert_abort do
58
+ @watch.behavior(:invalid)
59
+ end
60
+ end
61
+
62
+ # transition
63
+
64
+ def test_transition_should_abort_on_invalid_start_state
65
+ assert_abort do
66
+ @watch.transition(:foo, :bar)
67
+ end
68
+ end
69
+
70
+ def test_transition_should_accept_all_valid_start_states
71
+ assert_nothing_raised do
72
+ Watch::VALID_STATES.each do |state|
73
+ @watch.transition(state, :bar) { }
74
+ end
75
+ end
76
+ end
77
+
78
+ def test_transition_should_create_and_record_a_metric_for_the_given_start_state
79
+ @watch.transition(:init, :start) { }
80
+ assert_equal 1, @watch.metrics[:init].size
81
+ end
82
+
83
+ # lifecycle
84
+
85
+ def test_lifecycle_should_create_and_record_a_metric_for_nil_start_state
86
+ @watch.lifecycle { }
87
+ assert_equal 1, @watch.metrics[nil].size
88
+ end
89
+
90
+ # start_if
91
+
92
+ def test_start_if_should_place_a_metric_on_up_state
93
+ @watch.start_if { }
94
+ assert_equal 1, @watch.metrics[:up].size
95
+ end
96
+
97
+ # restart_if
98
+
99
+ def test_restart_if_should_place_a_metric_on_up_state
100
+ @watch.restart_if { }
101
+ assert_equal 1, @watch.metrics[:up].size
102
+ end
103
+
104
+ # monitor
105
+
106
+ def test_monitor_should_move_to_init_if_available
107
+ @watch.instance_eval do
108
+ transition(:init, :up) { }
109
+ end
110
+ @watch.expects(:move).with(:init)
111
+ @watch.monitor
112
+ end
113
+
114
+ def test_monitor_should_move_to_up_if_no_init_available
115
+ @watch.expects(:move).with(:up)
116
+ @watch.monitor
117
+ end
118
+
119
+ # unmonitor
120
+
121
+ def test_unmonitor_should_move_to_nil
122
+ @watch.expects(:move).with(:unmonitored)
123
+ @watch.unmonitor
124
+ end
125
+
126
+ # move
127
+
128
+ def test_move_should_not_clean_up_if_from_state_is_nil
129
+ Thread.current.stubs(:==).returns(true)
130
+ @watch.driver.expects(:message).never
131
+
132
+ metric = nil
133
+
134
+ @watch.instance_eval do
135
+ transition(:init, :up) do |on|
136
+ metric = on
137
+ on.condition(:process_running) do |c|
138
+ c.running = true
139
+ c.interval = 10
140
+ end
141
+ end
142
+ end
143
+
144
+ metric.expects(:disable).never
145
+
146
+ no_stdout { @watch.move(:init) }
147
+ end
148
+
149
+ def test_move_should_clean_up_from_state_if_not_nil
150
+ Thread.current.stubs(:==).returns(true)
151
+ @watch.driver.expects(:message).never
152
+
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
+ Thread.current.stubs(:==).returns(true)
174
+ @watch.driver.expects(:message).never
175
+
176
+ @watch.expects(:action).with(:start)
177
+
178
+ no_stdout { @watch.move(:start) }
179
+ end
180
+
181
+ def test_move_should_move_to_up_state_if_no_start_or_restart_metric
182
+ Thread.current.stubs(:==).returns(true)
183
+ @watch.driver.expects(:message).never
184
+
185
+ [:start, :restart].each do |state|
186
+ @watch.expects(:action)
187
+ no_stdout { @watch.move(state) }
188
+ assert_equal :up, @watch.state
189
+ end
190
+ end
191
+
192
+ def test_move_should_enable_destination_metric
193
+ Thread.current.stubs(:==).returns(true)
194
+ @watch.driver.expects(:message).never
195
+
196
+ metric = nil
197
+
198
+ @watch.instance_eval do
199
+ transition(:init, :up) do |on|
200
+ metric = on
201
+ on.condition(:process_running) do |c|
202
+ c.running = true
203
+ c.interval = 10
204
+ end
205
+ end
206
+ end
207
+
208
+ metric.expects(:enable)
209
+
210
+ no_stdout { @watch.move(:init) }
211
+ end
212
+
213
+ # action
214
+
215
+ def test_action_should_pass_start_and_stop_actions_to_call_action
216
+ Thread.current.stubs(:==).returns(true)
217
+ @watch.driver.expects(:message).never
218
+
219
+ c = Conditions::FakePollCondition.new
220
+ [:start, :stop].each do |cmd|
221
+ @watch.expects(:call_action).with(c, cmd)
222
+ @watch.action(cmd, c)
223
+ end
224
+ end
225
+
226
+ def test_action_should_do_stop_then_start_if_no_restart_command
227
+ Thread.current.stubs(:==).returns(true)
228
+ @watch.driver.expects(:message).never
229
+
230
+ c = Conditions::FakePollCondition.new
231
+ @watch.expects(:call_action).with(c, :stop)
232
+ @watch.expects(:call_action).with(c, :start)
233
+ @watch.action(:restart, c)
234
+ end
235
+
236
+ def test_action_should_restart_to_call_action_if_present
237
+ Thread.current.stubs(:==).returns(true)
238
+ @watch.driver.expects(:message).never
239
+
240
+ @watch.restart = lambda { }
241
+ c = Conditions::FakePollCondition.new
242
+ @watch.expects(:call_action).with(c, :restart)
243
+ @watch.action(:restart, c)
244
+ end
245
+
246
+ # call_action
247
+
248
+ def test_call_action
249
+ c = Conditions::FakePollCondition.new
250
+ Monitr::Process.any_instance.expects(:call_action).with(:start)
251
+ no_stdout { @watch.call_action(c, :start) }
252
+ end
253
+
254
+ def test_call_action_should_call_before_start_when_behavior_has_that
255
+ @watch.behavior(:fake_behavior)
256
+ c = Conditions::FakePollCondition.new
257
+ Monitr::Process.any_instance.expects(:call_action).with(:start)
258
+ Behaviors::FakeBehavior.any_instance.expects(:before_start)
259
+ no_stdout { @watch.call_action(c, :start) }
260
+ end
261
+
262
+ def test_call_action_should_call_after_start_when_behavior_has_that
263
+ @watch.behavior(:fake_behavior)
264
+ c = Conditions::FakePollCondition.new
265
+ Monitr::Process.any_instance.expects(:call_action).with(:start)
266
+ Behaviors::FakeBehavior.any_instance.expects(:after_start)
267
+ no_stdout { @watch.call_action(c, :start) }
268
+ end
269
+
270
+ # canonical_hash_form
271
+
272
+ def test_canonical_hash_form_should_convert_symbol_to_hash
273
+ assert_equal({true => :foo}, @watch.canonical_hash_form(:foo))
274
+ end
275
+
276
+ def test_canonical_hash_form_should_convert_hash_to_hash
277
+ assert_equal({true => :foo}, @watch.canonical_hash_form(true => :foo))
278
+ end
279
+ end
@@ -0,0 +1,17 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ class TestWebhook < Test::Unit::TestCase
4
+ def test_exists
5
+ Monitr::Contacts::Webhook
6
+ end
7
+
8
+ def test_notify
9
+ assert_nothing_raised do
10
+ g = Monitr::Contacts::Webhook.new
11
+ g.hook_url = 'http://test/switch'
12
+ Net::HTTP.expects(:post_form)
13
+ g.notify(:a, :b, :c, :d, :e)
14
+ assert_equal "sent webhook to http://test/switch", g.info
15
+ end
16
+ end
17
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: monitr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - onesupercoder
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-02-09 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hoe
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.8.2
24
+ version:
25
+ description: Monitr is an easy to configure, easy to extend monitoring framework written in Ruby.
26
+ email: onesupercoder@gmail.com
27
+ executables: []
28
+
29
+ extensions:
30
+ - ext/monitr/extconf.rb
31
+ extra_rdoc_files: []
32
+
33
+ files: []
34
+
35
+ has_rdoc: true
36
+ homepage: http://monitr.rubyforge.org/
37
+ post_install_message:
38
+ rdoc_options:
39
+ - --main
40
+ - README.txt
41
+ require_paths:
42
+ - lib
43
+ - ext
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ requirements: []
57
+
58
+ rubyforge_project: monitr
59
+ rubygems_version: 1.3.1
60
+ signing_key:
61
+ specification_version: 2
62
+ summary: Like monit, only awesome
63
+ test_files:
64
+ - test/test_behavior.rb
65
+ - test/test_campfire.rb
66
+ - test/test_condition.rb
67
+ - test/test_conditions_disk_usage.rb
68
+ - test/test_conditions_http_response_code.rb
69
+ - test/test_conditions_process_running.rb
70
+ - test/test_conditions_tries.rb
71
+ - test/test_contact.rb
72
+ - test/test_dependency_graph.rb
73
+ - test/test_driver.rb
74
+ - test/test_email.rb
75
+ - test/test_event_handler.rb
76
+ - test/test_handlers_kqueue_handler.rb
77
+ - test/test_logger.rb
78
+ - test/test_metric.rb
79
+ - test/test_monitr.rb
80
+ - test/test_process.rb
81
+ - test/test_registry.rb
82
+ - test/test_socket.rb
83
+ - test/test_sugar.rb
84
+ - test/test_system_portable_poller.rb
85
+ - test/test_system_process.rb
86
+ - test/test_task.rb
87
+ - test/test_timeline.rb
88
+ - test/test_trigger.rb
89
+ - test/test_watch.rb
90
+ - test/test_webhook.rb