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,109 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ class TestContact < Test::Unit::TestCase
4
+ def test_exists
5
+ Monitr::Contact
6
+ end
7
+
8
+ # generate
9
+
10
+ def test_generate_should_raise_on_invalid_kind
11
+ assert_raise(NoSuchContactError) do
12
+ Contact.generate(:invalid)
13
+ end
14
+ end
15
+
16
+ def test_generate_should_abort_on_invalid_contact
17
+ assert_abort do
18
+ Contact.generate(:invalid_contact)
19
+ end
20
+ end
21
+
22
+ # normalize
23
+
24
+ def test_normalize_should_accept_a_string
25
+ input = 'tom'
26
+ output = {:contacts => ['tom']}
27
+ assert_equal(output, Contact.normalize(input))
28
+ end
29
+
30
+ def test_normalize_should_accept_an_array_of_strings
31
+ input = ['tom', 'kevin']
32
+ output = {:contacts => ['tom', 'kevin']}
33
+ assert_equal(output, Contact.normalize(input))
34
+ end
35
+
36
+ def test_normalize_should_accept_a_hash_with_contacts_string
37
+ input = {:contacts => 'tom'}
38
+ output = {:contacts => ['tom']}
39
+ assert_equal(output, Contact.normalize(input))
40
+ end
41
+
42
+ def test_normalize_should_accept_a_hash_with_contacts_array_of_strings
43
+ input = {:contacts => ['tom', 'kevin']}
44
+ output = {:contacts => ['tom', 'kevin']}
45
+ assert_equal(output, Contact.normalize(input))
46
+ end
47
+
48
+ def test_normalize_should_stringify_priority
49
+ input = {:contacts => 'tom', :priority => 1}
50
+ output = {:contacts => ['tom'], :priority => '1'}
51
+ assert_equal(output, Contact.normalize(input))
52
+ end
53
+
54
+ def test_normalize_should_stringify_category
55
+ input = {:contacts => 'tom', :category => :product}
56
+ output = {:contacts => ['tom'], :category => 'product'}
57
+ assert_equal(output, Contact.normalize(input))
58
+ end
59
+
60
+ def test_normalize_should_raise_on_non_string_array_hash
61
+ input = 1
62
+ assert_raise ArgumentError do
63
+ Contact.normalize(input)
64
+ end
65
+ end
66
+
67
+ def test_normalize_should_raise_on_non_string_array_contacts_key
68
+ input = {:contacts => 1}
69
+ assert_raise ArgumentError do
70
+ Contact.normalize(input)
71
+ end
72
+ end
73
+
74
+ def test_normalize_should_raise_on_non_string_containing_array
75
+ input = [1]
76
+ assert_raise ArgumentError do
77
+ Contact.normalize(input)
78
+ end
79
+ end
80
+
81
+ def test_normalize_should_raise_on_non_string_containing_array_contacts_key
82
+ input = {:contacts => [1]}
83
+ assert_raise ArgumentError do
84
+ Contact.normalize(input)
85
+ end
86
+ end
87
+
88
+ def test_normalize_should_raise_on_absent_contacts_key
89
+ input = {}
90
+ assert_raise ArgumentError do
91
+ Contact.normalize(input)
92
+ end
93
+ end
94
+
95
+ def test_normalize_should_raise_on_extra_keys
96
+ input = {:contacts => ['tom'], :priority => 1, :category => 'product', :extra => 'foo'}
97
+ assert_raise ArgumentError do
98
+ Contact.normalize(input)
99
+ end
100
+ end
101
+
102
+ # notify
103
+
104
+ def test_notify_should_be_abstract
105
+ assert_raise(AbstractMethodNotOverriddenError) do
106
+ Contact.new.notify(:a, :b, :c, :d, :e)
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,62 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ class TestDependencyGraph < Test::Unit::TestCase
4
+ def setup
5
+ @dg = DependencyGraph.new
6
+ end
7
+
8
+ # new
9
+
10
+ def test_new_should_accept_zero_arguments
11
+ assert @dg.instance_of?(DependencyGraph)
12
+ end
13
+
14
+ # add
15
+
16
+ def test_add_should_create_and_store_two_new_nodes
17
+ @dg.add('foo', 'bar')
18
+ assert_equal 2, @dg.nodes.size
19
+ assert @dg.nodes['foo'].instance_of?(DependencyGraph::Node)
20
+ assert @dg.nodes['bar'].instance_of?(DependencyGraph::Node)
21
+ end
22
+
23
+ def test_add_should_record_dependency
24
+ @dg.add('foo', 'bar')
25
+ assert_equal 1, @dg.nodes['foo'].dependencies.size
26
+ assert_equal @dg.nodes['bar'], @dg.nodes['foo'].dependencies.first
27
+ end
28
+
29
+ def test_add_should_ignore_dups
30
+ @dg.add('foo', 'bar')
31
+ @dg.add('foo', 'bar')
32
+ assert_equal 2, @dg.nodes.size
33
+ assert_equal 1, @dg.nodes['foo'].dependencies.size
34
+ end
35
+ end
36
+
37
+
38
+ class TestDependencyGraphNode < Test::Unit::TestCase
39
+ def setup
40
+ @foo = DependencyGraph::Node.new('foo')
41
+ @bar = DependencyGraph::Node.new('bar')
42
+ end
43
+
44
+ # new
45
+
46
+ def test_new_should_accept_zero_arguments
47
+ assert @foo.instance_of?(DependencyGraph::Node)
48
+ end
49
+
50
+ # add
51
+
52
+ def test_add_should_store_node_as_dependency
53
+ @foo.add(@bar)
54
+ assert_equal 1, @foo.dependencies.size
55
+ end
56
+
57
+ # has_node?
58
+
59
+ def test_has_node
60
+ assert @foo.has_node?(@foo)
61
+ end
62
+ end
@@ -0,0 +1,11 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ class TestDriver < Test::Unit::TestCase
4
+ def setup
5
+
6
+ end
7
+
8
+ def test_
9
+
10
+ end
11
+ end
@@ -0,0 +1,45 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ class TestEmail < Test::Unit::TestCase
4
+ def test_exists
5
+ Monitr::Contacts::Email
6
+ end
7
+
8
+ def test_unknown_delivery_method_for_notify
9
+ assert_nothing_raised do
10
+ Monitr::Contacts::Email.any_instance.expects(:notify_smtp).never
11
+ Monitr::Contacts::Email.any_instance.expects(:notify_sendmail).never
12
+ Monitr::Contacts::Email.delivery_method = :foo_protocol
13
+ LOG.expects(:log).times(2)
14
+
15
+ g = Monitr::Contacts::Email.new
16
+ g.notify(:a, :b, :c, :d, :e)
17
+ assert_nil g.info
18
+ end
19
+ end
20
+
21
+ def test_smtp_delivery_method_for_notify
22
+ assert_nothing_raised do
23
+ Monitr::Contacts::Email.any_instance.expects(:notify_sendmail).never
24
+ Monitr::Contacts::Email.any_instance.expects(:notify_smtp).once.returns(nil)
25
+ Monitr::Contacts::Email.delivery_method = :smtp
26
+ g = Monitr::Contacts::Email.new
27
+ g.email = 'joe@example.com'
28
+ g.notify(:a, :b, :c, :d, :e)
29
+ assert_equal "sent email to joe@example.com", g.info
30
+ end
31
+ end
32
+
33
+ def test_sendmail_delivery_method_for_notify
34
+ assert_nothing_raised do
35
+ Monitr::Contacts::Email.any_instance.expects(:notify_smtp).never
36
+ Monitr::Contacts::Email.any_instance.expects(:notify_sendmail).once.returns(nil)
37
+ Monitr::Contacts::Email.delivery_method = :sendmail
38
+ g = Monitr::Contacts::Email.new
39
+ g.email = 'joe@example.com'
40
+ g.notify(:a, :b, :c, :d, :e)
41
+ assert_equal "sent email to joe@example.com", g.info
42
+ end
43
+ end
44
+
45
+ end
@@ -0,0 +1,80 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ module Monitr
4
+ class EventHandler
5
+
6
+ def self.actions=(value)
7
+ @@actions = value
8
+ end
9
+
10
+ def self.actions
11
+ @@actions
12
+ end
13
+
14
+ def self.handler=(value)
15
+ @@handler = value
16
+ end
17
+ end
18
+ end
19
+
20
+ class TestEventHandler < Test::Unit::TestCase
21
+ def setup
22
+ @h = Monitr::EventHandler
23
+ end
24
+
25
+ def test_register_one_event
26
+ pid = 4445
27
+ event = :proc_exit
28
+ block = lambda {
29
+ puts "Hi"
30
+ }
31
+
32
+ mock_handler = mock()
33
+ mock_handler.expects(:register_process).with(pid, [event])
34
+ @h.handler = mock_handler
35
+
36
+ @h.register(pid, event, &block)
37
+ assert_equal @h.actions, {pid => {event => block}}
38
+ end
39
+
40
+ def test_register_multiple_events_per_process
41
+ pid = 4445
42
+ exit_block = lambda { puts "Hi" }
43
+ @h.actions = {pid => {:proc_exit => exit_block}}
44
+
45
+ mock_handler = mock()
46
+ mock_handler.expects(:register_process).with do |a, b|
47
+ a == pid &&
48
+ b.to_set == [:proc_exit, :proc_fork].to_set
49
+ end
50
+ @h.handler = mock_handler
51
+
52
+ fork_block = lambda { puts "Forking" }
53
+ @h.register(pid, :proc_fork, &fork_block)
54
+ assert_equal @h.actions, {pid => {:proc_exit => exit_block,
55
+ :proc_fork => fork_block }}
56
+ end
57
+
58
+ # JIRA PLATFORM-75
59
+ def test_call_should_check_for_pid_and_action_before_executing
60
+ exit_block = mock()
61
+ exit_block.expects(:call).times 1
62
+ @h.actions = {4445 => {:proc_exit => exit_block}}
63
+ @h.call(4446, :proc_exit) # shouldn't call, bad pid
64
+ @h.call(4445, :proc_fork) # shouldn't call, bad event
65
+ @h.call(4445, :proc_exit) # should call
66
+ end
67
+
68
+ def teardown
69
+ # Reset handler
70
+ @h.actions = {}
71
+ @h.load
72
+ end
73
+ end
74
+
75
+ class TestEventHandlerOperational < Test::Unit::TestCase
76
+ def test_operational
77
+ Monitr::EventHandler.start
78
+ assert Monitr::EventHandler.loaded?
79
+ end
80
+ end
@@ -0,0 +1,16 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ if Monitr::EventHandler.event_system == "kqueue"
4
+
5
+ class TestHandlersKqueueHandler < Test::Unit::TestCase
6
+ def test_register_process
7
+ KQueueHandler.expects(:monitor_process).with(1234, 2147483648)
8
+ KQueueHandler.register_process(1234, [:proc_exit])
9
+ end
10
+
11
+ def test_events_mask
12
+ assert_equal 2147483648, KQueueHandler.events_mask([:proc_exit])
13
+ end
14
+ end
15
+
16
+ end
@@ -0,0 +1,63 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ class TestLogger < Test::Unit::TestCase
4
+ def setup
5
+ @log = Monitr::Logger.new
6
+ end
7
+
8
+ # log
9
+
10
+ def test_log
11
+ @log.expects(:info).with("qux")
12
+
13
+ no_stdout do
14
+ @log.log(stub(:name => 'foo'), :info, "qux")
15
+ end
16
+
17
+ assert_equal 1, @log.logs.size
18
+ assert_instance_of Time, @log.logs['foo'][0][0]
19
+ assert_match(/qux/, @log.logs['foo'][0][1])
20
+ end
21
+
22
+ def test_log_should_send_to_syslog
23
+ Syslog.expects(:crit).with('foo')
24
+
25
+ no_stdout do
26
+ @log.log(stub(:name => 'foo'), :fatal, "foo")
27
+ end
28
+ end
29
+
30
+ # watch_log_since
31
+
32
+ def test_watch_log_since
33
+ t1 = Time.now
34
+
35
+ no_stdout do
36
+ @log.log(stub(:name => 'foo'), :info, "one")
37
+ @log.log(stub(:name => 'foo'), :info, "two")
38
+ end
39
+
40
+ assert_match(/one.*two/m, @log.watch_log_since('foo', t1))
41
+
42
+ t2 = Time.now
43
+
44
+ no_stdout do
45
+ @log.log(stub(:name => 'foo'), :info, "three")
46
+ end
47
+
48
+ out = @log.watch_log_since('foo', t2)
49
+
50
+ assert_no_match(/one/, out)
51
+ assert_no_match(/two/, out)
52
+ assert_match(/three/, out)
53
+ end
54
+
55
+ # regular methods
56
+
57
+ def test_fatal
58
+ no_stdout do
59
+ @log.fatal('foo')
60
+ end
61
+ assert_equal 0, @log.logs.size
62
+ end
63
+ end
@@ -0,0 +1,72 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ class TestMetric < Test::Unit::TestCase
4
+ def setup
5
+ @metric = Metric.new(stub(:interval => 10), nil)
6
+ end
7
+
8
+ # watch
9
+
10
+ def test_watch
11
+ w = stub()
12
+ m = Metric.new(w, nil)
13
+ assert_equal w, m.watch
14
+ end
15
+
16
+ # destination
17
+
18
+ def test_destination
19
+ d = stub()
20
+ m = Metric.new(nil, d)
21
+ assert_equal d, m.destination
22
+ end
23
+
24
+ # condition
25
+
26
+ def test_condition_should_be_block_optional
27
+ @metric.condition(:fake_poll_condition)
28
+ assert_equal 1, @metric.conditions.size
29
+ end
30
+
31
+ def test_poll_condition_should_inherit_interval_from_watch_if_not_specified
32
+ @metric.condition(:fake_poll_condition)
33
+ assert_equal 10, @metric.conditions.first.interval
34
+ end
35
+
36
+ def test_poll_condition_should_abort_if_no_interval_and_no_watch_interval
37
+ metric = Metric.new(stub(:name => 'foo', :interval => nil), nil)
38
+
39
+ assert_abort do
40
+ metric.condition(:fake_poll_condition)
41
+ end
42
+ end
43
+
44
+ def test_condition_should_allow_generation_of_subclasses_of_poll_or_event
45
+ metric = Metric.new(stub(:name => 'foo', :interval => 10), nil)
46
+
47
+ assert_nothing_raised do
48
+ metric.condition(:fake_poll_condition)
49
+ metric.condition(:fake_event_condition)
50
+ end
51
+ end
52
+
53
+ def test_condition_should_abort_if_not_subclass_of_poll_or_event
54
+ metric = Metric.new(stub(:name => 'foo', :interval => 10), nil)
55
+
56
+ assert_abort do
57
+ metric.condition(:fake_condition) { |c| }
58
+ end
59
+ end
60
+
61
+ def test_condition_should_abort_on_invalid_condition
62
+ assert_abort do
63
+ @metric.condition(:fake_poll_condition) { |c| c.stubs(:valid?).returns(false) }
64
+ end
65
+ end
66
+
67
+ def test_condition_should_abort_on_no_such_condition
68
+ assert_abort do
69
+ @metric.condition(:invalid) { }
70
+ end
71
+ end
72
+ end