71 lines
2.7 KiB
Python
71 lines
2.7 KiB
Python
from datetime import datetime
|
|
|
|
from app.alerts import format_message
|
|
from app.alerts.delta import format_duration
|
|
from app.models import KIND_FLAPPING, KIND_PROBLEM, KIND_RECOVERY, KIND_REMINDER, Event
|
|
|
|
NOW = int(datetime(2026, 11, 6, 20, 0).timestamp())
|
|
SINCE = int(datetime(2026, 11, 6, 18, 5).timestamp())
|
|
HOUR = 3600
|
|
|
|
|
|
def make(kind=KIND_PROBLEM, state=2, **kwargs):
|
|
return Event(ts=NOW, host="h1", service="zpool", kind=kind, state=state, output="kaputt", **kwargs)
|
|
|
|
|
|
def test_format_duration_is_compact():
|
|
assert format_duration(10) == "unter 1 min"
|
|
assert format_duration(60) == "1 min"
|
|
assert format_duration(59 * 60) == "59 min"
|
|
assert format_duration(2 * HOUR) == "2 h"
|
|
assert format_duration(3 * HOUR + 12 * 60) == "3 h 12 min"
|
|
assert format_duration(24 * HOUR) == "1 d"
|
|
assert format_duration(26 * HOUR + 600) == "1 d 2 h"
|
|
assert format_duration(-5) == "unter 1 min"
|
|
|
|
|
|
def test_transition_line_shows_arrow_since_and_previous_duration():
|
|
event = make(previous_state=1, previous_duration=2 * HOUR, since_ts=SINCE)
|
|
text = format_message([event])
|
|
assert "<b>WARN → CRIT</b> zpool, seit 18:05 (vorher WARN seit 2 h): kaputt" in text
|
|
|
|
|
|
def test_new_problem_line_has_no_arrow():
|
|
text = format_message([make(since_ts=SINCE)])
|
|
assert "<b>CRIT</b> zpool, seit 18:05: kaputt" in text and "→" not in text
|
|
|
|
|
|
def test_old_since_includes_date():
|
|
event = make(since_ts=int(datetime(2026, 11, 4, 9, 30).timestamp()))
|
|
assert "seit 04.11. 09:30" in format_message([event])
|
|
|
|
|
|
def test_problem_without_delta_keeps_plain_format():
|
|
assert "<b>CRIT</b> zpool: kaputt" in format_message([make()])
|
|
|
|
|
|
def test_recovery_shows_duration_and_last_state():
|
|
event = make(kind=KIND_RECOVERY, state=0, previous_state=2, problem_since_ts=NOW - (3 * HOUR + 12 * 60))
|
|
assert "<b>OK</b> zpool: wieder OK nach 3 h 12 min (zuletzt CRIT)" in format_message([event])
|
|
|
|
|
|
def test_recovery_without_delta_is_plain():
|
|
assert "<b>OK</b> zpool: wieder OK" in format_message([make(kind=KIND_RECOVERY, state=0)])
|
|
|
|
|
|
def test_reminder_line():
|
|
event = make(kind=KIND_REMINDER, since_ts=NOW - 4 * HOUR)
|
|
assert "<b>CRIT</b> zpool: weiter CRIT seit 4 h: kaputt" in format_message([event])
|
|
|
|
|
|
def test_flapping_line_unchanged():
|
|
assert "<b>FLAPPING (CRIT)</b> zpool: kaputt" in format_message([make(kind=KIND_FLAPPING)])
|
|
|
|
|
|
def test_foreign_text_stays_escaped_with_delta():
|
|
event = Event(ts=NOW, host="<h>", service="<s>&", kind=KIND_PROBLEM, state=2, output="<script>x</script>",
|
|
previous_state=1, previous_duration=60, since_ts=SINCE)
|
|
text = format_message([event])
|
|
assert "<script>" not in text and "<script>" in text
|
|
assert "<s>&" in text and "<h>" not in text
|