2018-04-14 05:02:03 +00:00
|
|
|
from datetime import datetime
|
2018-04-10 18:06:42 +00:00
|
|
|
from unittest.mock import patch
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from minitor.main import Alert
|
|
|
|
from minitor.main import Monitor
|
2019-07-31 04:17:00 +00:00
|
|
|
from tests.util import assert_called_once_with
|
2018-04-10 18:06:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestAlert(object):
|
|
|
|
@pytest.fixture
|
|
|
|
def monitor(self):
|
2022-04-05 03:23:15 +00:00
|
|
|
return Monitor(
|
|
|
|
{
|
|
|
|
"name": "Dummy Monitor",
|
|
|
|
"command": ["echo", "foo"],
|
|
|
|
}
|
|
|
|
)
|
2018-04-10 18:06:42 +00:00
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def echo_alert(self):
|
|
|
|
return Alert(
|
2022-04-05 03:23:15 +00:00
|
|
|
"log",
|
2018-04-11 04:58:01 +00:00
|
|
|
{
|
2022-04-05 03:23:15 +00:00
|
|
|
"command": [
|
|
|
|
"echo",
|
|
|
|
(
|
|
|
|
"{monitor_name} has failed {failure_count} time(s)!\n"
|
|
|
|
"We have alerted {alert_count} time(s)\n"
|
|
|
|
"Last success was {last_success}\n"
|
|
|
|
"Last output was: {last_output}"
|
|
|
|
),
|
2018-04-11 04:58:01 +00:00
|
|
|
]
|
2022-04-05 03:23:15 +00:00
|
|
|
},
|
2018-04-10 18:06:42 +00:00
|
|
|
)
|
|
|
|
|
2018-04-14 05:02:03 +00:00
|
|
|
@pytest.mark.parametrize(
|
2022-04-05 03:23:15 +00:00
|
|
|
"last_success,expected_success",
|
|
|
|
[(None, "Never"), (datetime(2018, 4, 10), "2018-04-10T00:00:00")],
|
2018-04-14 05:02:03 +00:00
|
|
|
)
|
2022-04-05 03:23:15 +00:00
|
|
|
def test_simple_alert(self, monitor, echo_alert, last_success, expected_success):
|
2018-04-11 04:58:01 +00:00
|
|
|
monitor.alert_count = 1
|
2022-04-05 03:23:15 +00:00
|
|
|
monitor.last_output = "beep boop"
|
2018-04-15 00:27:37 +00:00
|
|
|
monitor.last_success = last_success
|
2018-04-19 00:05:33 +00:00
|
|
|
monitor.total_failure_count = 1
|
2022-04-05 03:23:15 +00:00
|
|
|
with patch.object(echo_alert._logger, "error") as mock_error:
|
|
|
|
echo_alert.alert("Exception message", monitor)
|
2019-07-31 04:17:00 +00:00
|
|
|
assert_called_once_with(
|
|
|
|
mock_error,
|
2022-04-05 03:23:15 +00:00
|
|
|
"Dummy Monitor has failed 1 time(s)!\n"
|
|
|
|
"We have alerted 1 time(s)\n"
|
|
|
|
"Last success was " + expected_success + "\n"
|
|
|
|
"Last output was: beep boop",
|
2019-07-31 04:17:00 +00:00
|
|
|
)
|