parent
47a8bdf4a0
commit
d005129afe
@ -123,6 +123,7 @@ class Monitor(object):
|
||||
self.alert_every = settings.get('alert_every')
|
||||
|
||||
self.last_check = None
|
||||
self.last_success = None
|
||||
self.total_failure_count = 0
|
||||
self.alert_count = 0
|
||||
|
||||
@ -163,6 +164,7 @@ class Monitor(object):
|
||||
"""Handles success tasks"""
|
||||
self.total_failure_count = 0
|
||||
self.alert_count = 0
|
||||
self.last_success = datetime.now()
|
||||
|
||||
def failure(self):
|
||||
"""Handles failure tasks and possibly raises MinitorAlert"""
|
||||
@ -206,6 +208,12 @@ class Alert(object):
|
||||
args.append(arg.format(**kwargs))
|
||||
return args
|
||||
|
||||
def _format_datetime(self, dt):
|
||||
"""Formats a datetime for an alert"""
|
||||
if dt is None:
|
||||
return 'Never'
|
||||
return dt.isoformat()
|
||||
|
||||
def alert(self, monitor):
|
||||
"""Calls the alert command for the provided monitor"""
|
||||
output, ex = call_output(
|
||||
@ -213,6 +221,7 @@ class Alert(object):
|
||||
alert_count=monitor.alert_count,
|
||||
monitor_name=monitor.name,
|
||||
failure_count=monitor.total_failure_count,
|
||||
last_success=self._format_datetime(monitor.last_success),
|
||||
),
|
||||
shell=isinstance(self.command, str),
|
||||
)
|
||||
|
@ -1,3 +1,4 @@
|
||||
from datetime import datetime
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
@ -23,18 +24,28 @@ class TestAlert(object):
|
||||
'command': [
|
||||
'echo', (
|
||||
'{monitor_name} has failed {failure_count} time(s)!\n'
|
||||
'We have alerted {alert_count} time(s)'
|
||||
'We have alerted {alert_count} time(s)\n'
|
||||
'Last success was {last_success}'
|
||||
)
|
||||
]
|
||||
}
|
||||
)
|
||||
|
||||
def test_simple_alert(self, monitor, echo_alert):
|
||||
@pytest.mark.parametrize(
|
||||
'last_success',
|
||||
[
|
||||
(None, 'Never'),
|
||||
(datetime(2018, 4, 10), '2018-04-10T00:00:00')
|
||||
]
|
||||
)
|
||||
def test_simple_alert(self, monitor, echo_alert, last_success):
|
||||
monitor.total_failure_count = 1
|
||||
monitor.alert_count = 1
|
||||
monitor.last_success = last_success[0]
|
||||
with patch.object(echo_alert.logger, 'error') as mock_error:
|
||||
echo_alert.alert(monitor)
|
||||
mock_error.assert_called_once_with(
|
||||
'Dummy Monitor has failed 1 time(s)!\n'
|
||||
'We have alerted 1 time(s)'
|
||||
'We have alerted 1 time(s)\n'
|
||||
'Last success was ' + last_success[1]
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user