Lint / JS (eslint) (push) Successful in 12s
Lint / Python (flake8) (push) Successful in 1m34s
Lint / Notify on failure (push) Skipped
Lint / Deploy (push) Skipped
Security / Python Security (bandit) (push) Successful in 1m54s
Lint / JS (eslint) (pull_request) Successful in 37s
Security / Python Security (bandit) (pull_request) Successful in 2m36s
Test / Python Tests (pytest) (push) Successful in 2m17s
Lint / Python (flake8) (pull_request) Successful in 57s
Lint / Notify on failure (pull_request) Skipped
Lint / Deploy (pull_request) Skipped
Test / Python Tests (pytest) (pull_request) Successful in 1m16s
When a host stops reporting, ask Loki whether its systemd-logind logged a shutdown request just before. If so, show it as "rebooting (planned)" instead of an outage, keep it out of the cluster check, and ticket only if it has not come back within planned_reboot_grace (default 20 min, per host overrides) as "Planned reboot did not come back". No line, no Loki config, or a Loki error: alert exactly as before. Refs #47 (the "unexpected reboot" alert for crashes that recover goes to thor) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
91 lines
4.1 KiB
Python
91 lines
4.1 KiB
Python
"""Tests for reboots.py: telling a planned reboot from a crash using the journal in Loki."""
|
|
import os
|
|
import sys
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
import pytest
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
|
|
|
|
import reboots # noqa: E402
|
|
|
|
NOW = 1_794_007_200.0 # 2026-11-06 23:20 UTC, during monitor-01's planned reboot
|
|
|
|
|
|
class FakeLoki:
|
|
def __init__(self, lines=None, error=None):
|
|
self._lines, self._error, self.calls = lines or [], error, []
|
|
|
|
def lines(self, logql, start, end, limit=20):
|
|
self.calls.append((logql, start, end))
|
|
if self._error:
|
|
raise reboots.LokiError(self._error)
|
|
return [(ts, line) for ts, line in self._lines if start <= ts <= end]
|
|
|
|
|
|
class TestFindShutdown:
|
|
def test_planned_reboot_line(self):
|
|
# monitor-01 2026-11-06: logind logged this ~10s before node_exporter stopped
|
|
loki = FakeLoki([(NOW - 131, 'The system will reboot now!')])
|
|
found = reboots.find_shutdown(loki, 'monitor-01', 120, NOW)
|
|
assert found['kind'] == 'reboot' and found['at'] == NOW - 131
|
|
assert 'host="monitor-01"' in loki.calls[0][0] and 'systemd-logind.service' in loki.calls[0][0]
|
|
|
|
def test_power_off_counts(self):
|
|
loki = FakeLoki([(NOW - 60, 'The system will power off now!')])
|
|
assert reboots.find_shutdown(loki, 'compute-storage-01', 120, NOW)['kind'] == 'power off'
|
|
|
|
def test_crash_has_no_line(self):
|
|
# compute-storage-02 2026-11-21: the journal just stops, nothing from logind
|
|
assert reboots.find_shutdown(FakeLoki([]), 'compute-storage-02', 120, NOW) is None
|
|
|
|
def test_an_old_shutdown_is_not_this_one(self):
|
|
loki = FakeLoki([(NOW - 3 * 3600, 'The system will reboot now!')])
|
|
assert reboots.find_shutdown(loki, 'monitor-01', 120, NOW) is None
|
|
|
|
def test_window_covers_the_whole_outage_plus_lookback(self):
|
|
loki = FakeLoki([])
|
|
reboots.find_shutdown(loki, 'pbs', 600, NOW)
|
|
_, start, end = loki.calls[0]
|
|
assert end == NOW and start == NOW - 600 - reboots.LOOKBACK_S
|
|
|
|
def test_loki_failure_is_raised_not_hidden(self):
|
|
with pytest.raises(reboots.LokiError):
|
|
reboots.find_shutdown(FakeLoki(error='timeout'), 'pbs', 120, NOW)
|
|
|
|
def test_host_name_is_quoted(self):
|
|
assert 'host="a\\"b"' in reboots.shutdown_query('a"b')
|
|
|
|
|
|
class TestLokiClient:
|
|
def test_parses_streams_newest_first(self):
|
|
resp = MagicMock()
|
|
resp.json.return_value = {'status': 'success', 'data': {'result': [
|
|
{'stream': {'host': 'x'}, 'values': [['1794007069971036000', 'The system will reboot now!'],
|
|
['1794007000000000000', 'older']]}]}}
|
|
with patch.object(reboots.requests, 'get', return_value=resp) as get:
|
|
out = reboots.LokiClient('http://loki:3100/').lines('{q}', 1, 2)
|
|
assert out[0] == (1794007069.971036, 'The system will reboot now!') and len(out) == 2
|
|
assert get.call_args.args[0] == 'http://loki:3100/loki/api/v1/query_range'
|
|
assert get.call_args.kwargs['params']['start'] == '1000000000'
|
|
|
|
def test_http_error_is_a_loki_error(self):
|
|
with patch.object(reboots.requests, 'get', side_effect=OSError('refused')):
|
|
with pytest.raises(reboots.LokiError):
|
|
reboots.LokiClient('http://loki:3100').lines('{q}', 1, 2)
|
|
|
|
def test_unsuccessful_status_is_a_loki_error(self):
|
|
resp = MagicMock()
|
|
resp.json.return_value = {'status': 'error', 'error': 'query too long'}
|
|
with patch.object(reboots.requests, 'get', return_value=resp):
|
|
with pytest.raises(reboots.LokiError):
|
|
reboots.LokiClient('http://loki:3100').lines('{q}', 1, 2)
|
|
|
|
|
|
class TestGrace:
|
|
def test_default_global_and_per_host(self):
|
|
assert reboots.grace_for('pbs', {}) == reboots.DEFAULT_GRACE_S
|
|
assert reboots.grace_for('pbs', {'planned_reboot_grace': 900}) == 900
|
|
cfg = {'planned_reboot_grace': 900, 'planned_reboot_grace_hosts': {'storage-01': 2400}}
|
|
assert reboots.grace_for('storage-01', cfg) == 2400 and reboots.grace_for('pbs', cfg) == 900
|