monitr 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,598 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ class TestMonitr < Test::Unit::TestCase
4
+ def setup
5
+ Monitr::Socket.stubs(:new).returns(true)
6
+ Monitr.stubs(:setup).returns(true)
7
+ Monitr.stubs(:validater).returns(true)
8
+ Thread.any_instance.stubs(:join).returns(true)
9
+ Monitr.reset
10
+ Monitr.pid_file_directory = '/var/run/monitr'
11
+ end
12
+
13
+ def teardown
14
+ Monitr.main && Monitr.main.kill
15
+ if Monitr.watches
16
+ Monitr.watches.each do |k, w|
17
+ w.driver.thread.kill
18
+ end
19
+ end
20
+ end
21
+
22
+ # applog
23
+
24
+ def test_applog
25
+ LOG.expects(:log).with(nil, :debug, 'foo')
26
+ applog(nil, :debug, 'foo')
27
+ end
28
+
29
+ # internal_init
30
+
31
+ def test_init_should_initialize_watches_to_empty_array
32
+ Monitr.internal_init { }
33
+ assert_equal Hash.new, Monitr.watches
34
+ end
35
+
36
+ # init
37
+
38
+ def test_pid_file_directory_should_abort_if_called_after_watch
39
+ Monitr.watch { |w| w.name = 'foo'; w.start = 'bar' }
40
+
41
+ assert_abort do
42
+ Monitr.pid_file_directory = 'foo'
43
+ end
44
+ end
45
+
46
+ # pid_file_directory
47
+
48
+ def test_pid_file_directory_should_return_default_if_not_set_explicitly
49
+ Monitr.internal_init
50
+ assert_equal '/var/run/monitr', Monitr.pid_file_directory
51
+ end
52
+
53
+ def test_pid_file_directory_equals_should_set
54
+ Monitr.pid_file_directory = '/foo'
55
+ Monitr.internal_init
56
+ assert_equal '/foo', Monitr.pid_file_directory
57
+ end
58
+
59
+ # watch
60
+
61
+ def test_watch_should_get_stored
62
+ watch = nil
63
+ Monitr.watch do |w|
64
+ w.name = 'foo'
65
+ w.start = 'bar'
66
+ watch = w
67
+ end
68
+
69
+ assert_equal 1, Monitr.watches.size
70
+ assert_equal watch, Monitr.watches.values.first
71
+
72
+ assert_equal 0, Monitr.groups.size
73
+ end
74
+
75
+ def test_watch_should_get_stored_in_pending_watches
76
+ watch = nil
77
+ Monitr.watch do |w|
78
+ w.name = 'foo'
79
+ w.start = 'bar'
80
+ watch = w
81
+ end
82
+
83
+ assert_equal 1, Monitr.pending_watches.size
84
+ assert_equal watch, Monitr.pending_watches.first
85
+ end
86
+
87
+ def test_watch_should_register_processes
88
+ assert_nil Monitr.registry['foo']
89
+ Monitr.watch do |w|
90
+ w.name = 'foo'
91
+ w.start = 'bar'
92
+ end
93
+ assert_kind_of Monitr::Process, Monitr.registry['foo']
94
+ end
95
+
96
+ def test_watch_should_get_stored_by_group
97
+ a = nil
98
+
99
+ Monitr.watch do |w|
100
+ a = w
101
+ w.name = 'foo'
102
+ w.start = 'bar'
103
+ w.group = 'test'
104
+ end
105
+
106
+ assert_equal({'test' => [a]}, Monitr.groups)
107
+ end
108
+
109
+ def test_watches_should_get_stored_by_group
110
+ a = nil
111
+ b = nil
112
+
113
+ Monitr.watch do |w|
114
+ a = w
115
+ w.name = 'foo'
116
+ w.start = 'bar'
117
+ w.group = 'test'
118
+ end
119
+
120
+ Monitr.watch do |w|
121
+ b = w
122
+ w.name = 'bar'
123
+ w.start = 'baz'
124
+ w.group = 'test'
125
+ end
126
+
127
+ assert_equal({'test' => [a, b]}, Monitr.groups)
128
+ end
129
+
130
+ def test_watch_should_allow_multiple_watches
131
+ Monitr.watch { |w| w.name = 'foo'; w.start = 'bar' }
132
+
133
+ assert_nothing_raised do
134
+ Monitr.watch { |w| w.name = 'bar'; w.start = 'bar' }
135
+ end
136
+ end
137
+
138
+ def test_watch_should_disallow_duplicate_watch_names
139
+ Monitr.watch { |w| w.name = 'foo'; w.start = 'bar' }
140
+
141
+ assert_abort do
142
+ Monitr.watch { |w| w.name = 'foo'; w.start = 'bar' }
143
+ end
144
+ end
145
+
146
+ def test_watch_should_disallow_identical_watch_and_group_names
147
+ Monitr.watch { |w| w.name = 'foo'; w.group = 'bar'; w.start = 'bar' }
148
+
149
+ assert_abort do
150
+ Monitr.watch { |w| w.name = 'bar'; w.start = 'bar' }
151
+ end
152
+ end
153
+
154
+ def test_watch_should_disallow_identical_watch_and_group_names_other_way
155
+ Monitr.watch { |w| w.name = 'bar'; w.start = 'bar' }
156
+
157
+ assert_abort do
158
+ Monitr.watch { |w| w.name = 'foo'; w.group = 'bar'; w.start = 'bar' }
159
+ end
160
+ end
161
+
162
+ def test_watch_should_unwatch_new_watch_if_running_and_duplicate_watch
163
+ Monitr.watch { |w| w.name = 'foo'; w.start = 'bar' }
164
+ Monitr.running = true
165
+
166
+ assert_nothing_raised do
167
+ no_stdout do
168
+ Monitr.watch { |w| w.name = 'foo'; w.start = 'bar' }
169
+ end
170
+ end
171
+ end
172
+
173
+ # unwatch
174
+
175
+ def test_unwatch_should_unmonitor_watch
176
+ Monitr.watch { |w| w.name = 'bar'; w.start = 'bar' }
177
+ w = Monitr.watches['bar']
178
+ w.state = :up
179
+ w.expects(:unmonitor)
180
+ no_stdout do
181
+ Monitr.unwatch(w)
182
+ end
183
+ end
184
+
185
+ def test_unwatch_should_unregister_watch
186
+ Monitr.watch { |w| w.name = 'bar'; w.start = 'bar' }
187
+ w = Monitr.watches['bar']
188
+ w.expects(:unregister!)
189
+ no_stdout do
190
+ Monitr.unwatch(w)
191
+ end
192
+ end
193
+
194
+ def test_unwatch_should_remove_same_name_watches
195
+ Monitr.watch { |w| w.name = 'bar'; w.start = 'bar' }
196
+ w = Monitr.watches['bar']
197
+ no_stdout do
198
+ Monitr.unwatch(w)
199
+ end
200
+ assert_equal 0, Monitr.watches.size
201
+ end
202
+
203
+ def test_unwatch_should_remove_from_group
204
+ Monitr.watch do |w|
205
+ w.name = 'bar'
206
+ w.start = 'baz'
207
+ w.group = 'test'
208
+ end
209
+ w = Monitr.watches['bar']
210
+ no_stdout do
211
+ Monitr.unwatch(w)
212
+ end
213
+ assert !Monitr.groups[w.group].include?(w)
214
+ end
215
+
216
+ # contact
217
+
218
+ def test_contact_should_ensure_init_is_called
219
+ Monitr.contact(:fake_contact) { |c| c.name = 'tom' }
220
+ assert Monitr.inited
221
+ end
222
+
223
+ def test_contact_should_abort_on_invalid_contact_kind
224
+ assert_abort do
225
+ Monitr.contact(:foo) { |c| c.name = 'tom' }
226
+ end
227
+ end
228
+
229
+ def test_contact_should_create_and_store_contact
230
+ contact = nil
231
+ Monitr.contact(:fake_contact) { |c| c.name = 'tom'; contact = c }
232
+ assert [contact], Monitr.contacts
233
+ end
234
+
235
+ def test_contact_should_add_to_group
236
+ Monitr.contact(:fake_contact) { |c| c.name = 'tom'; c.group = 'devs' }
237
+ Monitr.contact(:fake_contact) { |c| c.name = 'chris'; c.group = 'devs' }
238
+ assert 2, Monitr.contact_groups.size
239
+ end
240
+
241
+ def test_contact_should_abort_on_no_name
242
+ no_stdout do
243
+ assert_abort do
244
+ Monitr.contact(:fake_contact) { |c| }
245
+ end
246
+ end
247
+ end
248
+
249
+ def test_contact_should_abort_on_duplicate_contact_name
250
+ Monitr.contact(:fake_contact) { |c| c.name = 'tom' }
251
+ no_stdout do
252
+ assert_nothing_raised do
253
+ Monitr.contact(:fake_contact) { |c| c.name = 'tom' }
254
+ end
255
+ end
256
+ end
257
+
258
+ def test_contact_should_abort_on_contact_with_same_name_as_group
259
+ Monitr.contact(:fake_contact) { |c| c.name = 'tom'; c.group = 'devs' }
260
+ no_stdout do
261
+ assert_nothing_raised do
262
+ Monitr.contact(:fake_contact) { |c| c.name = 'devs' }
263
+ end
264
+ end
265
+ end
266
+
267
+ def test_contact_should_abort_on_contact_with_same_group_as_name
268
+ Monitr.contact(:fake_contact) { |c| c.name = 'tom' }
269
+ assert_abort do
270
+ Monitr.contact(:fake_contact) { |c| c.name = 'chris'; c.group = 'tom' }
271
+ end
272
+ end
273
+
274
+ def test_contact_should_abort_if_contact_is_invalid
275
+ assert_abort do
276
+ Monitr.contact(:fake_contact) do |c|
277
+ c.name = 'tom'
278
+ c.stubs(:valid?).returns(false)
279
+ end
280
+ end
281
+ end
282
+
283
+ # control
284
+
285
+ def test_control_should_monitor_on_start
286
+ Monitr.watch { |w| w.name = 'foo'; w.start = 'bar' }
287
+
288
+ w = Monitr.watches['foo']
289
+ w.expects(:monitor)
290
+ Monitr.control('foo', 'start')
291
+ end
292
+
293
+ def test_control_should_move_to_restart_on_restart
294
+ Monitr.watch { |w| w.name = 'foo'; w.start = 'bar' }
295
+
296
+ w = Monitr.watches['foo']
297
+ w.expects(:move).with(:restart)
298
+ Monitr.control('foo', 'restart')
299
+ end
300
+
301
+ def test_control_should_unmonitor_and_stop_on_stop
302
+ Monitr.watch { |w| w.name = 'foo'; w.start = 'bar' }
303
+
304
+ w = Monitr.watches['foo']
305
+ w.state = :up
306
+ w.expects(:unmonitor).returns(w)
307
+ w.expects(:action).with(:stop)
308
+ Monitr.control('foo', 'stop')
309
+ end
310
+
311
+ def test_control_should_unmonitor_on_unmonitor
312
+ Monitr.watch { |w| w.name = 'foo'; w.start = 'bar' }
313
+
314
+ w = Monitr.watches['foo']
315
+ w.state = :up
316
+ w.expects(:unmonitor).returns(w)
317
+ Monitr.control('foo', 'unmonitor')
318
+ end
319
+
320
+ def test_control_should_unwatch_on_remove
321
+ Monitr.watch { |w| w.name = 'foo'; w.start = 'bar' }
322
+
323
+ w = Monitr.watches['foo']
324
+ w.state = :up
325
+ Monitr.expects(:unwatch)
326
+ Monitr.control('foo', 'remove')
327
+ end
328
+
329
+ def test_control_should_raise_on_invalid_command
330
+ Monitr.watch { |w| w.name = 'foo'; w.start = 'bar' }
331
+
332
+ assert_raise InvalidCommandError do
333
+ Monitr.control('foo', 'invalid')
334
+ end
335
+ end
336
+
337
+ def test_control_should_operate_on_each_watch_in_group
338
+ Monitr.watch do |w|
339
+ w.name = 'foo1'
340
+ w.start = 'go'
341
+ w.group = 'bar'
342
+ end
343
+
344
+ Monitr.watch do |w|
345
+ w.name = 'foo2'
346
+ w.start = 'go'
347
+ w.group = 'bar'
348
+ end
349
+
350
+ Monitr.watches['foo1'].expects(:monitor)
351
+ Monitr.watches['foo2'].expects(:monitor)
352
+
353
+ Monitr.control('bar', 'start')
354
+ end
355
+
356
+ # stop_all
357
+
358
+ # terminate
359
+
360
+ def test_terminate_should_exit
361
+ Monitr.pid = nil
362
+ FileUtils.expects(:rm_f).never
363
+ Monitr.expects(:exit!)
364
+ Monitr.terminate
365
+ end
366
+
367
+ def test_terminate_should_delete_pid
368
+ Monitr.pid = '/foo/bar'
369
+ FileUtils.expects(:rm_f).with("/foo/bar")
370
+ Monitr.expects(:exit!)
371
+ Monitr.terminate
372
+ end
373
+
374
+ # status
375
+
376
+ def test_status_should_show_state
377
+ Monitr.watch { |w| w.name = 'foo'; w.start = 'bar' }
378
+
379
+ w = Monitr.watches['foo']
380
+ w.state = :up
381
+ assert_equal({'foo' => {:state => :up, :group => nil}}, Monitr.status)
382
+ end
383
+
384
+ def test_status_should_show_state_with_group
385
+ Monitr.watch { |w| w.name = 'foo'; w.start = 'bar'; w.group = 'g' }
386
+
387
+ w = Monitr.watches['foo']
388
+ w.state = :up
389
+ assert_equal({'foo' => {:state => :up, :group => 'g'}}, Monitr.status)
390
+ end
391
+
392
+ def test_status_should_show_unmonitored_for_nil_state
393
+ Monitr.watch { |w| w.name = 'foo'; w.start = 'bar' }
394
+
395
+ w = Monitr.watches['foo']
396
+ assert_equal({'foo' => {:state => :unmonitored, :group => nil}}, Monitr.status)
397
+ end
398
+
399
+ # running_log
400
+
401
+ def test_running_log_should_call_watch_log_since_on_main_log
402
+ Monitr.watch { |w| w.name = 'foo'; w.start = 'bar' }
403
+ t = Time.now
404
+ LOG.expects(:watch_log_since).with('foo', t)
405
+ Monitr.running_log('foo', t)
406
+ end
407
+
408
+ def test_running_log_should_raise_on_unknown_watch
409
+ Monitr.internal_init
410
+ assert_raise(NoSuchWatchError) do
411
+ Monitr.running_log('foo', Time.now)
412
+ end
413
+ end
414
+
415
+ # running_load
416
+
417
+ def test_running_load_should_eval_code
418
+ code = <<-EOF
419
+ Monitr.watch do |w|
420
+ w.name = 'foo'
421
+ w.start = 'go'
422
+ end
423
+ EOF
424
+
425
+ no_stdout do
426
+ Monitr.running_load(code, '/foo/bar.monitr')
427
+ end
428
+
429
+ assert_equal 1, Monitr.watches.size
430
+ end
431
+
432
+ def test_running_load_should_monitor_new_watches
433
+ code = <<-EOF
434
+ Monitr.watch do |w|
435
+ w.name = 'foo'
436
+ w.start = 'go'
437
+ end
438
+ EOF
439
+
440
+ Watch.any_instance.expects(:monitor)
441
+ no_stdout do
442
+ Monitr.running_load(code, '/foo/bar.monitr')
443
+ end
444
+ end
445
+
446
+ def test_running_load_should_not_monitor_new_watches_with_autostart_false
447
+ code = <<-EOF
448
+ Monitr.watch do |w|
449
+ w.name = 'foo'
450
+ w.start = 'go'
451
+ w.autostart = false
452
+ end
453
+ EOF
454
+
455
+ Watch.any_instance.expects(:monitor).never
456
+ no_stdout do
457
+ Monitr.running_load(code, '/foo/bar.monitr')
458
+ end
459
+ end
460
+
461
+ def test_running_load_should_return_array_of_affected_watches
462
+ code = <<-EOF
463
+ Monitr.watch do |w|
464
+ w.name = 'foo'
465
+ w.start = 'go'
466
+ end
467
+ EOF
468
+
469
+ w = nil
470
+ no_stdout do
471
+ w, e = *Monitr.running_load(code, '/foo/bar.monitr')
472
+ end
473
+ assert_equal 1, w.size
474
+ assert_equal 'foo', w.first
475
+ end
476
+
477
+ def test_running_load_should_clear_pending_watches
478
+ code = <<-EOF
479
+ Monitr.watch do |w|
480
+ w.name = 'foo'
481
+ w.start = 'go'
482
+ end
483
+ EOF
484
+
485
+ no_stdout do
486
+ Monitr.running_load(code, '/foo/bar.monitr')
487
+ end
488
+ assert_equal 0, Monitr.pending_watches.size
489
+ end
490
+
491
+ # load
492
+
493
+ def test_load_should_collect_and_load_globbed_path
494
+ Dir.expects(:[]).with('/path/to/*.thing').returns(['a', 'b'])
495
+ Kernel.expects(:load).with('a').once
496
+ Kernel.expects(:load).with('b').once
497
+ Monitr.load('/path/to/*.thing')
498
+ end
499
+
500
+ # start
501
+
502
+ def test_start_should_kick_off_a_server_instance
503
+ Monitr::Socket.expects(:new).returns(true)
504
+ Monitr.start
505
+ end
506
+
507
+ def test_start_should_begin_monitoring_autostart_watches
508
+ Monitr.watch do |w|
509
+ w.name = 'foo'
510
+ w.start = 'go'
511
+ end
512
+
513
+ Watch.any_instance.expects(:monitor).once
514
+ Monitr.start
515
+ end
516
+
517
+ def test_start_should_not_begin_monitoring_non_autostart_watches
518
+ Monitr.watch do |w|
519
+ w.name = 'foo'
520
+ w.start = 'go'
521
+ w.autostart = false
522
+ end
523
+
524
+ Watch.any_instance.expects(:monitor).never
525
+ Monitr.start
526
+ end
527
+
528
+ def test_start_should_get_and_join_timer
529
+ Monitr.watch { |w| w.name = 'foo'; w.start = 'bar' }
530
+ no_stdout do
531
+ Monitr.start
532
+ end
533
+ end
534
+
535
+ # at_exit
536
+
537
+ def test_at_exit_should_call_start
538
+ Monitr.expects(:start).once
539
+ Monitr.at_exit
540
+ end
541
+
542
+ # pattern_match
543
+
544
+ def test_pattern_match
545
+ list = %w{ mongrel-3000 mongrel-3001 fuzed fuzed2 apache mysql}
546
+
547
+ assert_equal %w{ mongrel-3000 }, Monitr.pattern_match('m3000', list)
548
+ assert_equal %w{ mongrel-3001 }, Monitr.pattern_match('m31', list)
549
+ assert_equal %w{ fuzed fuzed2 }, Monitr.pattern_match('fu', list)
550
+ assert_equal %w{ mysql }, Monitr.pattern_match('sql', list)
551
+ end
552
+ end
553
+
554
+
555
+ # class TestMonitrOther < Test::Unit::TestCase
556
+ # def setup
557
+ # Monitr::Socket.stubs(:new).returns(true)
558
+ # Monitr.internal_init
559
+ # Monitr.reset
560
+ # end
561
+ #
562
+ # def teardown
563
+ # Monitr.main && Monitr.main.kill
564
+ # end
565
+ #
566
+ # # setup
567
+ #
568
+ # def test_setup_should_create_pid_file_directory_if_it_doesnt_exist
569
+ # Monitr.expects(:test).returns(false)
570
+ # FileUtils.expects(:mkdir_p).with(Monitr.pid_file_directory)
571
+ # Monitr.setup
572
+ # end
573
+ #
574
+ # def test_setup_should_raise_if_no_permissions_to_create_pid_file_directory
575
+ # Monitr.expects(:test).returns(false)
576
+ # FileUtils.expects(:mkdir_p).raises(Errno::EACCES)
577
+ #
578
+ # assert_abort do
579
+ # Monitr.setup
580
+ # end
581
+ # end
582
+ #
583
+ # # validate
584
+ #
585
+ # def test_validate_should_abort_if_pid_file_directory_is_unwriteable
586
+ # Monitr.expects(:test).returns(false)
587
+ # assert_abort do
588
+ # Monitr.validater
589
+ # end
590
+ # end
591
+ #
592
+ # def test_validate_should_not_abort_if_pid_file_directory_is_writeable
593
+ # Monitr.expects(:test).returns(true)
594
+ # assert_nothing_raised do
595
+ # Monitr.validater
596
+ # end
597
+ # end
598
+ # end