monitr 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,55 @@
1
+ require 'mkmf'
2
+
3
+ fail = false
4
+
5
+ def create_dummy_makefile
6
+ File.open("Makefile", 'w') do |f|
7
+ f.puts "all:"
8
+ f.puts "install:"
9
+ end
10
+ end
11
+
12
+ case RUBY_PLATFORM
13
+ when /bsd/i, /darwin/i
14
+ unless have_header('sys/event.h')
15
+ puts
16
+ puts "Missing 'sys/event.h' header"
17
+ fail = true
18
+ end
19
+
20
+ if fail
21
+ puts
22
+ puts "Events handler could not be compiled (see above error). Your monitr installation will not support event conditions."
23
+ create_dummy_makefile
24
+ else
25
+ create_makefile 'kqueue_handler_ext'
26
+ end
27
+ when /linux/i
28
+ unless have_header('linux/netlink.h')
29
+ puts
30
+ puts "Missing 'linux/netlink.h' header(s)"
31
+ puts "You may need to install a header package for your system"
32
+ fail = true
33
+ end
34
+
35
+ unless have_header('linux/connector.h') && have_header('linux/cn_proc.h')
36
+ puts
37
+ puts "Missing 'linux/connector.h', or 'linux/cn_proc.h' header(s)"
38
+ puts "These are only available in Linux kernel 2.6.15 and later (run `uname -a` to find yours)"
39
+ puts "You may need to install a header package for your system"
40
+ fail = true
41
+ end
42
+
43
+ if fail
44
+ puts
45
+ puts "Events handler could not be compiled (see above error). Your monitr installation will not support event conditions."
46
+ create_dummy_makefile
47
+ else
48
+ create_makefile 'netlink_handler_ext'
49
+ end
50
+ else
51
+ puts
52
+ puts "Unsupported platform '#{RUBY_PLATFORM}'. Supported platforms are BSD, DARWIN, and LINUX."
53
+ puts "Your monitr installation will not support event conditions."
54
+ create_dummy_makefile
55
+ end
@@ -0,0 +1,21 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ class TestBehavior < Test::Unit::TestCase
4
+ def test_generate_should_return_an_object_corresponding_to_the_given_type
5
+ assert_equal Behaviors::FakeBehavior, Behavior.generate(:fake_behavior, nil).class
6
+ end
7
+
8
+ def test_generate_should_raise_on_invalid_type
9
+ assert_raise NoSuchBehaviorError do
10
+ Behavior.generate(:foo, nil)
11
+ end
12
+ end
13
+
14
+ def test_complain
15
+ Syslog.expects(:err).with('foo')
16
+ # Kernel.expects(:puts).with('foo')
17
+ no_stdout do
18
+ assert !Behavior.allocate.bypass.complain('foo')
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,41 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+ require 'tinder'
3
+
4
+ class TestCampfire < Test::Unit::TestCase
5
+ def test_exists
6
+ Monitr::Contacts::Campfire
7
+ end
8
+
9
+ # should notify
10
+ def test_campfire_delivery_method_for_notify
11
+ assert_nothing_raised do
12
+
13
+ room = mock()
14
+ room.expects(:speak).returns(nil)
15
+
16
+ g = Monitr::Contacts::Campfire.new
17
+ Monitr::Contacts::Campfire.format.expects(:call).with(:a,:e)
18
+ g.expects(:room).returns(room)
19
+ g.notify(:a, :b, :c, :d, :e)
20
+ assert_equal "notified campfire: ", g.info
21
+ end
22
+ end
23
+
24
+ # should not establish a new connection because the older is alive
25
+ def test_campfire_room_method
26
+ assert_nothing_raised do
27
+ room = mock()
28
+ g = Monitr::Contacts::Campfire.new
29
+ g.instance_variable_set(:@room,room)
30
+ assert_equal g.send(:room), room
31
+ end
32
+ end
33
+
34
+ # should raise because the connections parameters have not been set
35
+ def test_campfire_delivery_method_for_notify_without_campfire_params
36
+ LOG.expects(:log).times(3) # 3 calls: 2 debug (credentials, backtrace) + 1 info (failed message)
37
+ g = Monitr::Contacts::Campfire.new
38
+ g.notify(:a, :b, :c, :d, :e)
39
+ end
40
+
41
+ end
@@ -0,0 +1,50 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ class BadlyImplementedCondition < PollCondition
4
+ end
5
+
6
+ class TestCondition < Test::Unit::TestCase
7
+
8
+ # generate
9
+
10
+ def test_generate_should_return_an_object_corresponding_to_the_given_type
11
+ assert_equal Conditions::ProcessRunning, Condition.generate(:process_running, nil).class
12
+ end
13
+
14
+ def test_generate_should_raise_on_invalid_type
15
+ assert_raise NoSuchConditionError do
16
+ Condition.generate(:foo, nil)
17
+ end
18
+ end
19
+
20
+ def test_generate_should_abort_on_event_condition_without_loaded_event_system
21
+ Monitr::EventHandler.stubs(:operational?).returns(false)
22
+ assert_abort do
23
+ Monitr::EventHandler.start
24
+ Condition.generate(:process_exits, nil).class
25
+ end
26
+ end
27
+
28
+ def test_generate_should_return_a_good_error_message_for_invalid_types
29
+ emsg = "No Condition found with the class name Monitr::Conditions::FooBar"
30
+ rmsg = nil
31
+
32
+ begin
33
+ Condition.generate(:foo_bar, nil)
34
+ rescue => e
35
+ rmsg = e.message
36
+ end
37
+
38
+ assert_equal emsg, rmsg
39
+ end
40
+
41
+ # test
42
+
43
+ def test_test_should_raise_if_not_defined_in_subclass
44
+ c = BadlyImplementedCondition.new
45
+
46
+ assert_raise AbstractMethodNotOverriddenError do
47
+ c.test
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,56 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ class TestConditionsDiskUsage < Test::Unit::TestCase
4
+ # valid?
5
+
6
+ def test_valid_should_return_false_if_no_above_given
7
+ c = Conditions::DiskUsage.new
8
+ c.mount_point = '/'
9
+ c.watch = stub(:name => 'foo')
10
+
11
+ no_stdout do
12
+ assert_equal false, c.valid?
13
+ end
14
+ end
15
+
16
+ def test_valid_should_return_false_if_no_mount_point_given
17
+ c = Conditions::DiskUsage.new
18
+ c.above = 90
19
+ c.watch = stub(:name => 'foo')
20
+
21
+ no_stdout do
22
+ assert_equal false, c.valid?
23
+ end
24
+ end
25
+
26
+ def test_valid_should_return_true_if_required_options_all_set
27
+ c = Conditions::DiskUsage.new
28
+ c.above = 90
29
+ c.mount_point = '/'
30
+ c.watch = stub(:name => 'foo')
31
+
32
+ assert_equal true, c.valid?
33
+ end
34
+
35
+ # test
36
+
37
+ def test_test_should_return_true_if_above_limit
38
+ c = Conditions::DiskUsage.new
39
+ c.above = 90
40
+ c.mount_point = '/'
41
+
42
+ c.expects(:`).returns('91')
43
+
44
+ assert_equal true, c.test
45
+ end
46
+
47
+ def test_test_should_return_false_if_below_limit
48
+ c = Conditions::DiskUsage.new
49
+ c.above = 90
50
+ c.mount_point = '/'
51
+
52
+ c.expects(:`).returns('90')
53
+
54
+ assert_equal false, c.test
55
+ end
56
+ end
@@ -0,0 +1,109 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ class TestHttpResponseCode < Test::Unit::TestCase
4
+ def valid_condition
5
+ c = Conditions::HttpResponseCode.new()
6
+ c.watch = stub(:name => 'foo')
7
+ c.host = 'localhost'
8
+ c.port = 8080
9
+ c.path = '/'
10
+ c.timeout = 10
11
+ c.code_is = 200
12
+ c.times = 1
13
+ yield(c) if block_given?
14
+ c.prepare
15
+ c
16
+ end
17
+
18
+ # valid?
19
+
20
+ def test_valid_condition_is_valid
21
+ c = valid_condition
22
+ assert c.valid?
23
+ end
24
+
25
+ def test_valid_should_return_false_if_both_code_is_and_code_is_not_are_set
26
+ c = valid_condition do |cc|
27
+ cc.code_is_not = 500
28
+ end
29
+ no_stdout { assert !c.valid? }
30
+ end
31
+
32
+ def test_valid_should_return_false_if_no_host_set
33
+ c = valid_condition do |cc|
34
+ cc.host = nil
35
+ end
36
+ no_stdout { assert !c.valid? }
37
+ end
38
+
39
+ # test
40
+
41
+ def test_test_should_return_false_if_code_is_is_set_to_200_but_response_is_500
42
+ c = valid_condition
43
+ Net::HTTP.expects(:start).yields(stub(:read_timeout= => nil, :get => stub(:code => 500)))
44
+ assert_equal false, c.test
45
+ end
46
+
47
+ def test_test_should_return_false_if_code_is_not_is_set_to_200_and_response_is_200
48
+ c = valid_condition do |cc|
49
+ cc.code_is = nil
50
+ cc.code_is_not = [200]
51
+ end
52
+ Net::HTTP.expects(:start).yields(stub(:read_timeout= => nil, :get => stub(:code => 200)))
53
+ assert_equal false, c.test
54
+ end
55
+
56
+ def test_test_should_return_true_if_code_is_is_set_to_200_and_response_is_200
57
+ c = valid_condition
58
+ Net::HTTP.expects(:start).yields(stub(:read_timeout= => nil, :get => stub(:code => 200)))
59
+ assert_equal true, c.test
60
+ end
61
+
62
+ def test_test_should_return_false_if_code_is_not_is_set_to_200_but_response_is_500
63
+ c = valid_condition do |cc|
64
+ cc.code_is = nil
65
+ cc.code_is_not = [200]
66
+ end
67
+ Net::HTTP.expects(:start).yields(stub(:read_timeout= => nil, :get => stub(:code => 500)))
68
+ assert_equal true, c.test
69
+ end
70
+
71
+ def test_test_should_return_false_if_code_is_is_set_to_200_but_response_times_out
72
+ c = valid_condition
73
+ Net::HTTP.expects(:start).raises(Timeout::Error, '')
74
+ assert_equal false, c.test
75
+ end
76
+
77
+ def test_test_should_return_true_if_code_is_not_is_set_to_200_and_response_times_out
78
+ c = valid_condition do |cc|
79
+ cc.code_is = nil
80
+ cc.code_is_not = [200]
81
+ end
82
+ Net::HTTP.expects(:start).raises(Timeout::Error, '')
83
+ assert_equal true, c.test
84
+ end
85
+
86
+ def test_test_should_return_false_if_code_is_is_set_to_200_but_cant_connect
87
+ c = valid_condition
88
+ Net::HTTP.expects(:start).raises(Errno::ECONNREFUSED, '')
89
+ assert_equal false, c.test
90
+ end
91
+
92
+ def test_test_should_return_true_if_code_is_not_is_set_to_200_and_cant_connect
93
+ c = valid_condition do |cc|
94
+ cc.code_is = nil
95
+ cc.code_is_not = [200]
96
+ end
97
+ Net::HTTP.expects(:start).raises(Errno::ECONNREFUSED, '')
98
+ assert_equal true, c.test
99
+ end
100
+
101
+ def test_test_should_return_true_if_code_is_is_set_to_200_and_response_is_200_twice_for_times_two_of_two
102
+ c = valid_condition do |cc|
103
+ cc.times = [2, 2]
104
+ end
105
+ Net::HTTP.expects(:start).yields(stub(:read_timeout= => nil, :get => stub(:code => 200))).times(2)
106
+ assert_equal false, c.test
107
+ assert_equal true, c.test
108
+ end
109
+ end
@@ -0,0 +1,44 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ class TestConditionsProcessRunning < Test::Unit::TestCase
4
+ def test_missing_pid_file_returns_opposite
5
+ [true, false].each do |r|
6
+ c = Conditions::ProcessRunning.new
7
+ c.running = r
8
+
9
+ c.stubs(:watch).returns(stub(:pid => 99999999, :name => 'foo'))
10
+
11
+ # no_stdout do
12
+ assert_equal !r, c.test
13
+ # end
14
+ end
15
+ end
16
+
17
+ def test_not_running_returns_opposite
18
+ [true, false].each do |r|
19
+ c = Conditions::ProcessRunning.new
20
+ c.running = r
21
+
22
+ File.stubs(:exist?).returns(true)
23
+ c.stubs(:watch).returns(stub(:pid => 123))
24
+ File.stubs(:read).returns('5')
25
+ System::Process.any_instance.stubs(:exists?).returns(false)
26
+
27
+ assert_equal !r, c.test
28
+ end
29
+ end
30
+
31
+ def test_running_returns_same
32
+ [true, false].each do |r|
33
+ c = Conditions::ProcessRunning.new
34
+ c.running = r
35
+
36
+ File.stubs(:exist?).returns(true)
37
+ c.stubs(:watch).returns(stub(:pid => 123))
38
+ File.stubs(:read).returns('5')
39
+ System::Process.any_instance.stubs(:exists?).returns(true)
40
+
41
+ assert_equal r, c.test
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,67 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ class TestConditionsTries < Test::Unit::TestCase
4
+ # valid?
5
+
6
+ def test_valid_should_return_false_if_times_not_set
7
+ c = Conditions::Tries.new
8
+ c.watch = stub(:name => 'foo')
9
+ no_stdout { assert !c.valid? }
10
+ end
11
+ end
12
+
13
+
14
+ class TestConditionsTries < Test::Unit::TestCase
15
+ def setup
16
+ @c = Conditions::Tries.new
17
+ @c.times = 3
18
+ @c.prepare
19
+ end
20
+
21
+ # prepare
22
+
23
+ def test_prepare_should_create_timeline
24
+ assert 3, @c.instance_variable_get(:@timeline).instance_variable_get(:@max_size)
25
+ end
26
+
27
+ # test
28
+
29
+ def test_test_should_return_true_if_called_three_times_within_one_second
30
+ assert !@c.test
31
+ assert !@c.test
32
+ assert @c.test
33
+ end
34
+
35
+ # reset
36
+
37
+ def test_test_should_return_false_on_fourth_call_if_called_three_times_within_one_second
38
+ 3.times { @c.test }
39
+ @c.reset
40
+ assert !@c.test
41
+ end
42
+ end
43
+
44
+
45
+ class TestConditionsTriesWithin < Test::Unit::TestCase
46
+ def setup
47
+ @c = Conditions::Tries.new
48
+ @c.times = 3
49
+ @c.within = 1.seconds
50
+ @c.prepare
51
+ end
52
+
53
+ # test
54
+
55
+ def test_test_should_return_true_if_called_three_times_within_one_second
56
+ assert !@c.test
57
+ assert !@c.test
58
+ assert @c.test
59
+ end
60
+
61
+ def test_test_should_return_false_if_called_three_times_within_two_seconds
62
+ assert !@c.test
63
+ assert !@c.test
64
+ assert sleep(1.1)
65
+ assert !@c.test
66
+ end
67
+ end