Skip to content

Commit

Permalink
Python3 version:
Browse files Browse the repository at this point in the history
- trying to clean some broken tests on Python 3
- pylint
  • Loading branch information
mohierf committed May 15, 2018
1 parent b0be73c commit d98dc1e
Showing 1 changed file with 144 additions and 72 deletions.
216 changes: 144 additions & 72 deletions test/test_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,9 @@ def test_3_notifications(self):
print(("Last hard state: %s" % svc.last_hard_state))
assert "WARNING" == svc.last_hard_state

# Time warp 5 minutes
frozen_datetime.tick(delta=datetime.timedelta(minutes=5))

self.scheduler_loop(1, [[svc, 2, 'CRITICAL']])
# The notifications are created to be launched in the next second when they happen !
# Time warp 1 second
Expand All @@ -546,9 +549,16 @@ def test_3_notifications(self):
assert "CRITICAL" == svc.last_hard_state
assert 2 == svc.current_notification_number, 'Critical HARD, must have 2 notifications'
self.assert_actions_count(3)
self.assert_actions_match(0, 'serviceoutput WARNING', 'command')
self.assert_actions_match(1, 'serviceoutput CRITICAL', 'command')
self.assert_actions_match(2, 'VOID', 'command')
self.show_actions()
self.assert_actions_match(0,
'--notificationtype PROBLEM --servicestate WARNING',
'command')
self.assert_actions_match(1,
'--notificationtype PROBLEM --servicestate CRITICAL',
'command')
self.assert_actions_match(2,
'VOID',
'command')

self.scheduler_loop(1, [[svc, 0, 'OK']])
# The notifications are created to be launched in the next second when they happen !
Expand All @@ -558,17 +568,15 @@ def test_3_notifications(self):
assert 0 == svc.current_notification_number
self.show_actions()
self.assert_actions_count(3)
# 1st notification for service warning
self.assert_actions_match(0, 'notifier.pl --hostname test_host_0 --servicedesc test_ok_0 --notificationtype PROBLEM --servicestate WARNING --serviceoutput WARNING', 'command')
self.assert_actions_match(0, 'NOTIFICATIONTYPE=PROBLEM, NOTIFICATIONRECIPIENTS=test_contact, NOTIFICATIONISESCALATED=False, NOTIFICATIONAUTHOR=n/a, NOTIFICATIONAUTHORNAME=n/a, NOTIFICATIONAUTHORALIAS=n/a, NOTIFICATIONCOMMENT=n/a, HOSTNOTIFICATIONNUMBER=1, SERVICENOTIFICATIONNUMBER=1', 'command')

# # 2nd notification for service critical
self.assert_actions_match(1, 'notifier.pl --hostname test_host_0 --servicedesc test_ok_0 --notificationtype PROBLEM --servicestate CRITICAL --serviceoutput CRITICAL', 'command')
self.assert_actions_match(1, 'HOSTNOTIFICATIONNUMBER=2, SERVICENOTIFICATIONNUMBER=2', 'command')

# 1st recovery notification for service recovery
self.assert_actions_match(2, 'notifier.pl --hostname test_host_0 --servicedesc test_ok_0 --notificationtype RECOVERY --servicestate OK --serviceoutput OK', 'command')
self.assert_actions_match(2, 'NOTIFICATIONTYPE=RECOVERY, NOTIFICATIONRECIPIENTS=test_contact, NOTIFICATIONISESCALATED=False, NOTIFICATIONAUTHOR=n/a, NOTIFICATIONAUTHORNAME=n/a, NOTIFICATIONAUTHORALIAS=n/a, NOTIFICATIONCOMMENT=n/a, HOSTNOTIFICATIONNUMBER=0, SERVICENOTIFICATIONNUMBER=0', 'command')
self.assert_actions_match(0,
'--notificationtype PROBLEM --servicestate WARNING',
'command')
self.assert_actions_match(1,
'--notificationtype PROBLEM --servicestate CRITICAL',
'command')
self.assert_actions_match(2,
'--notificationtype RECOVERY --servicestate OK',
'command')

def test_4_notifications(self):
""" Test notifications of service states OK -> CRITICAL -> WARNING -> OK
Expand Down Expand Up @@ -625,8 +633,13 @@ def test_4_notifications(self):
assert 1 == svc.current_notification_number, 'Critical HARD, must have 1 ' \
'notification'
self.assert_actions_count(2)
self.assert_actions_match(0, 'serviceoutput CRITICAL', 'command')
self.assert_actions_match(1, 'VOID', 'command')
self.show_actions()
self.assert_actions_match(0,
'--notificationtype PROBLEM --servicestate CRITICAL',
'command')
self.assert_actions_match(1,
'VOID',
'command')

# Time warp 5 minutes
frozen_datetime.tick(delta=datetime.timedelta(minutes=5))
Expand All @@ -639,23 +652,16 @@ def test_4_notifications(self):
assert "HARD" == svc.state_type
assert 2 == svc.current_notification_number, 'Warning HARD, must have 3 ' \
'notification'
self.assert_actions_count(3)
self.show_actions()
self.assert_actions_match(0, 'serviceoutput CRITICAL', 'command')
self.assert_actions_match(1, 'serviceoutput WARNING', 'command')
self.assert_actions_match(2, 'VOID', 'command')

self.show_actions()
# 1st notification for service critical
self.assert_actions_match(0, 'notifier.pl --hostname test_host_0 --servicedesc test_ok_0 --notificationtype PROBLEM --servicestate CRITICAL --serviceoutput CRITICAL', 'command')
self.assert_actions_match(0, 'NOTIFICATIONTYPE=PROBLEM, NOTIFICATIONRECIPIENTS=test_contact, NOTIFICATIONISESCALATED=False, NOTIFICATIONAUTHOR=n/a, NOTIFICATIONAUTHORNAME=n/a, NOTIFICATIONAUTHORALIAS=n/a, NOTIFICATIONCOMMENT=n/a, HOSTNOTIFICATIONNUMBER=1, SERVICENOTIFICATIONNUMBER=1', 'command')

# 2nd notification for service warning
self.assert_actions_match(1, 'notifier.pl --hostname test_host_0 --servicedesc test_ok_0 --notificationtype PROBLEM --servicestate WARNING --serviceoutput WARNING', 'command')
self.assert_actions_match(1, 'HOSTNOTIFICATIONNUMBER=2, SERVICENOTIFICATIONNUMBER=2', 'command')

# 3rd notification is VOID
self.assert_actions_match(2, 'VOID', 'command')
self.assert_actions_match(0,
'--notificationtype PROBLEM --servicestate CRITICAL',
'command')
self.assert_actions_match(1,
'--notificationtype PROBLEM --servicestate WARNING',
'command')
self.assert_actions_match(2,
'VOID',
'command')

def test_notifications_passive_host(self):
""" Test notifications for passively check hosts
Expand Down Expand Up @@ -928,51 +934,117 @@ def test_notifications_ack(self):
svc.act_depend_of = [] # no hostchecks on critical checkresults
svc.event_handler_enabled = False

self.scheduler_loop(1, [[host, 0, 'UP'], [svc, 0, 'OK']])
time.sleep(0.1)
assert 0 == svc.current_notification_number, 'All OK no notifications'
self.assert_actions_count(0)
# Freeze the time !
initial_datetime = datetime.datetime(year=2018, month=6, day=1,
hour=18, minute=30, second=0)
with freeze_time(initial_datetime) as frozen_datetime:
assert frozen_datetime() == initial_datetime

self.scheduler_loop(1, [[svc, 2, 'CRITICAL']])
time.sleep(0.1)
assert "SOFT" == svc.state_type
assert 0 == svc.current_notification_number, 'Critical SOFT, no notifications'
self.assert_actions_count(0)
self.scheduler_loop(1, [[host, 0, 'UP'], [svc, 0, 'OK']])
# The notifications are created to be launched in the next second when they happen !
# Time warp 1 second
frozen_datetime.tick(delta=datetime.timedelta(seconds=1))
self.scheduler_loop(1)
assert 0 == svc.current_notification_number, 'All OK no notifications'
self.assert_actions_count(0)

self.scheduler_loop(1, [[svc, 2, 'CRITICAL']])
time.sleep(0.1)
assert "HARD" == svc.state_type
assert 1 == svc.current_notification_number, 'Critical HARD, must have 1 ' \
'notification'
self.show_actions()
self.assert_actions_count(2)
self.scheduler_loop(1, [[svc, 2, 'CRITICAL']])
# The notifications are created to be launched in the next second when they happen !
# Time warp 1 second
frozen_datetime.tick(delta=datetime.timedelta(seconds=1))
self.scheduler_loop(1)
assert "SOFT" == svc.state_type
assert 0 == svc.current_notification_number, 'Critical SOFT, no notifications'
self.assert_actions_count(0)

now = int(time.time())
cmd = "[{0}] ACKNOWLEDGE_SVC_PROBLEM;{1};{2};{3};{4};{5};{6};{7}\n".\
format(now, svc.host_name, svc.service_description, 1, 0, 1, 'darth vader',
'normal process')
self._scheduler.run_external_commands([cmd])
self.scheduler_loop(1, [[svc, 2, 'CRITICAL']])
time.sleep(0.1)
assert "HARD" == svc.state_type
assert 1 == svc.current_notification_number, 'Critical HARD, must have 1 ' \
'notification'
self.show_actions()
self.assert_actions_count(2)
self.scheduler_loop(1, [[svc, 2, 'CRITICAL']])
# The notifications are created to be launched in the next second when they happen !
# Time warp 1 second
frozen_datetime.tick(delta=datetime.timedelta(seconds=1))
self.scheduler_loop(1)
assert "HARD" == svc.state_type
assert 1 == svc.current_notification_number, \
'Critical HARD, must have 1 notification'
self.show_actions()
self.assert_actions_count(2)
self.assert_actions_match(0,
'--notificationtype PROBLEM --servicestate CRITICAL',
'command')
self.assert_actions_match(1,
'VOID',
'command')

self.scheduler_loop(1, [[svc, 2, 'CRITICAL']])
time.sleep(0.1)
assert "HARD" == svc.state_type
assert 1 == svc.current_notification_number, 'Critical HARD, must have 1 ' \
'notification'
self.assert_actions_count(2)
# Time warp 5 minutes
frozen_datetime.tick(delta=datetime.timedelta(minutes=5))

self.scheduler_loop(1, [[svc, 1, 'WARNING']])
time.sleep(0.1)
assert "HARD" == svc.state_type
assert 2 == svc.current_notification_number, 'Warning HARD, must have 2 ' \
'notifications'
self.assert_actions_count(3)
now = int(time.time())
cmd = "[{0}] ACKNOWLEDGE_SVC_PROBLEM;{1};{2};{3};{4};{5};{6};{7}\n".\
format(now, svc.host_name, svc.service_description, 1, 1, 1, 'darth vader',
'normal process')
self._scheduler.run_external_commands([cmd])
self.scheduler_loop(1)
# The notifications are created to be launched in the next second when they happen !
# Time warp 1 second
frozen_datetime.tick(delta=datetime.timedelta(seconds=1))
self.scheduler_loop(1)
assert "HARD" == svc.state_type
assert 1 == svc.current_notification_number, \
'Critical HARD and ack, but must have 1 notification'
self.show_actions()
self.assert_actions_count(3)
self.assert_actions_match(0,
'--notificationtype PROBLEM --servicestate CRITICAL',
'command')
self.assert_actions_match(1,
'--notificationtype ACKNOWLEDGEMENT',
'command')
self.assert_actions_match(2,
'VOID',
'command')

# Time warp 5 minutes
frozen_datetime.tick(delta=datetime.timedelta(minutes=5))

self.scheduler_loop(1, [[svc, 2, 'CRITICAL']])
self.scheduler_loop(1)
# The notifications are created to be launched in the next second when they happen !
# Time warp 1 second
frozen_datetime.tick(delta=datetime.timedelta(seconds=1))
self.scheduler_loop(1)
assert "HARD" == svc.state_type
assert 1 == svc.current_notification_number, \
'Critical HARD, must have 1 notification'
self.assert_actions_count(3)

# Time warp 5 minutes
frozen_datetime.tick(delta=datetime.timedelta(minutes=5))

self.scheduler_loop(1, [[svc, 1, 'WARNING']])
# The notifications are created to be launched in the next second when they happen !
# Time warp 1 second
frozen_datetime.tick(delta=datetime.timedelta(seconds=1))
self.scheduler_loop(1)
assert "HARD" == svc.state_type
assert 3 == svc.current_notification_number, \
'Warning HARD, must have 3 notifications'
self.show_actions()
# TODO: 2 warning notifications raised ! Looks strange !!!! But seems correct...
self.assert_actions_count(5)
self.assert_actions_match(0,
'--notificationtype PROBLEM --servicestate CRITICAL',
'command')
self.assert_actions_match(1,
'--notificationtype ACKNOWLEDGEMENT',
'command')
self.assert_actions_match(2,
'--notificationtype PROBLEM --servicestate WARNING',
'command')
self.assert_actions_match(3,
'--notificationtype PROBLEM --servicestate WARNING',
'command')
self.assert_actions_match(4,
'VOID',
'command')

def test_notifications_downtime(self):
""" Test notifications not sent when a downtime is scheduled
Expand Down

0 comments on commit d98dc1e

Please sign in to comment.