/
test_notifway.py
executable file
·110 lines (88 loc) · 4.61 KB
/
test_notifway.py
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
#!/usr/bin/env python
# Copyright (C) 2009-2010:
# Gabes Jean, naparuba@gmail.com
# Gerhard Lausser, Gerhard.Lausser@consol.de
#
# This file is part of Shinken.
#
# Shinken is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Shinken is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Shinken. If not, see <http://www.gnu.org/licenses/>.
#
# This file is used to test reading and processing of config files
#
import time
from shinken_test import unittest, ShinkenTest
class TestConfig(ShinkenTest):
def setUp(self):
self.setup_with_file('etc/nagios_notif_way.cfg')
def test_contact_def(self):
#
# Config is not correct because of a wrong relative path
# in the main config file
#
print "Get the contact"
now = time.time()
contact = self.sched.contacts.find_by_name("test_contact")
print "The contact", contact.__dict__
print "All notification Way:"
for nw in self.sched.notificationways:
print "\t", nw.notificationway_name
email_in_day = self.sched.notificationways.find_by_name('email_in_day')
self.assert_(email_in_day in contact.notificationways)
email_s_cmd = email_in_day.service_notification_commands.pop()
email_h_cmd = email_in_day.host_notification_commands.pop()
sms_the_night = self.sched.notificationways.find_by_name('sms_the_night')
self.assert_(sms_the_night in contact.notificationways)
sms_s_cmd = sms_the_night.service_notification_commands.pop()
sms_h_cmd = sms_the_night.host_notification_commands.pop()
# And check the criticity values
self.assert_(email_in_day.min_business_impact == 0)
self.assert_(sms_the_night.min_business_impact == 5)
print "Contact notification way(s):"
for nw in contact.notificationways:
print "\t", nw.notificationway_name
for c in nw.service_notification_commands:
print "\t\t", c.get_name()
contact_simple = self.sched.contacts.find_by_name("test_contact_simple")
# It's the created notifway for this simple contact
test_contact_simple_inner_notificationway = self.sched.notificationways.find_by_name("test_contact_simple_inner_notificationway")
print "Simple contact"
for nw in contact_simple.notificationways:
print "\t", nw.notificationway_name
for c in nw.service_notification_commands:
print "\t\t", c.get_name()
self.assert_(test_contact_simple_inner_notificationway in contact_simple.notificationways)
# we take as criticity a huge value from now
huge_criticity = 5
# Now all want* functions
# First is ok with warning alerts
self.assert_(email_in_day.want_service_notification(now, 'WARNING', 'PROBLEM', huge_criticity) == True)
# But a SMS is now WAY for warning. When we sleep, we wake up for critical only guy!
self.assert_(sms_the_night.want_service_notification(now, 'WARNING', 'PROBLEM', huge_criticity) == False)
# Same with contacts now
# First is ok for warning in the email_in_day nw
self.assert_(contact.want_service_notification(now, 'WARNING', 'PROBLEM', huge_criticity) == True)
# Simple is not ok for it
self.assert_(contact_simple.want_service_notification(now, 'WARNING', 'PROBLEM', huge_criticity) == False)
# Then for host notification
# First is ok for warning in the email_in_day nw
self.assert_(contact.want_host_notification(now, 'FLAPPING', 'PROBLEM', huge_criticity) == True)
# Simple is not ok for it
self.assert_(contact_simple.want_host_notification(now, 'FLAPPING', 'PROBLEM', huge_criticity) == False)
# And now we check that we refuse SMS for a low level criticity
# I do not want to be awaken by a dev server! When I sleep, I sleep!
# (and my wife will kill me if I do...)
# We take the EMAIL test because SMS got the night ony, so we take a very low value for criticity here
self.assert_(email_in_day.want_service_notification(now, 'WARNING', 'PROBLEM', -1) == False)
if __name__ == '__main__':
unittest.main()