Files
thor/tests/test_monitor.py
T
jaredandClaude Opus 5.5 983ba27386
CI / Lint (flake8, house style) (pull_request) Successful in 50s
CI / Test (pytest) (pull_request) Successful in 52s
CI / Security (bandit, pip-audit) (pull_request) Successful in 1m1s
CI / Integration (MariaDB) (pull_request) Successful in 1m40s
CI / Scan (secrets, workflows, shell, patterns) (pull_request) Successful in 1m15s
CI / Deploy (pull_request) Skipped
Engine, memory check, symptom checks, outbox, T1 replay
- thor/engine.py: one incident per root cause. New causes held one cycle, symptoms attached
  to the highest cause they relate to (same host, or cluster-wide effects for causes that
  can stall the cluster), priority only rises, rate-limited refresh, cleared after 15 min.
- thor/checks/memory.py: node_vmstat_oom_kill + kernel oom-kill lines; global vs cgroup,
  victim and CT, trigger cpuset; P3 / P2 (loop) / P1 (Ceph or other hosts stalling).
- thor/checks/symptoms.py: scrape timeouts, pvestatd timeouts, Ceph alerts switching on.
- thor/outbox.py: live actions to Tinker with backoff; shadow actions are only recorded.
- Migration 2: incidents, actions, observations.
- PromQL/LogQL regex escaping fixed (found by recording against the real Prometheus).
- tests/replay.py + tests/fixtures/t1.json: 2026-11-23 replayed through the real monitor:
  exactly one ticket (P1 from the start: pvestatd timed out on three other hosts with the
  first kill), 15 symptoms as Also seen, cleared 06:18, Loki's 06:00 outage delays only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 23:02:10 -05:00

210 lines
6.5 KiB
Python

from thor.config import Settings
from thor.model import NOTE, Finding
from thor.monitor import MAX_PENDING_WRITES, Monitor
from .fakes import Clock, FakeSource, FakeStore
def make(store=None, sources=None, checks=None, clock=None, prepare=None):
settings = Settings()
clock = clock or Clock()
sources = sources if sources is not None else {n: FakeSource(n) for n in ('prometheus', 'loki')}
return Monitor(settings, store or FakeStore(), sources, checks=checks, clock=clock, prepare=prepare), clock
def test_first_cycle_covers_one_cycle_and_records_heartbeat():
store = FakeStore()
m, clock = make(store)
r = m.cycle()
assert r.ok and not r.blind and not r.catching_up
assert r.window == (clock.t - 120, clock.t)
assert store.state['last_good_window_end'] == clock.t
assert store.state['last_good_cycle'] == clock.t
assert store.cycles[0]['ok'] is True
assert store.sources['prometheus']['up'] is True
assert store.blind == []
def test_next_cycle_starts_where_the_last_good_one_ended():
m, clock = make()
first = m.cycle()
clock.advance(120)
second = m.cycle()
assert second.window[0] == first.window[1]
assert not second.catching_up
def test_restart_resumes_from_stored_window_and_catches_up():
"""T2 shape: Thor goes dark with the rack, comes back 22 minutes later, reviews the gap."""
store = FakeStore()
m, clock = make(store)
m.cycle()
gone_at = clock.t
clock.advance(22 * 60)
m2, _ = make(store, clock=clock) # new process
r = m2.cycle()
assert r.window == (gone_at, clock.t)
assert r.catching_up
def test_catch_up_is_capped_at_24_hours():
store = FakeStore()
m, clock = make(store)
m.cycle()
clock.advance(3 * 86400)
r = make(store, clock=clock)[0].cycle()
assert r.window == (clock.t - 24 * 3600, clock.t)
assert r.catching_up
def test_blind_source_opens_and_closes_a_blind_period():
store = FakeStore()
loki = FakeSource('loki', up=False, error='HTTP 500')
m, clock = make(store, {'prometheus': FakeSource('prometheus'), 'loki': loki})
r = m.cycle()
assert r.ok and r.blind == ['loki'] # blind is recorded, it doesn't fail the cycle
assert store.blind == [{'source': 'loki', 'started': clock.t, 'ended': None, 'reason': 'HTTP 500'}]
assert store.state['last_cycle']['blind'] == ['loki']
clock.advance(120)
m.cycle() # still down: no second period
assert len(store.blind) == 1
loki.up = True
clock.advance(120)
m.cycle()
assert store.blind[0]['ended'] == clock.t
assert store.sources['loki']['up'] is True
def test_known_down_source_from_previous_process_is_not_reopened():
store = FakeStore()
store.sources['loki'] = {'up': False, 'since': 10.0, 'last_ok': None, 'last_error': 'x'}
store.blind.append({'source': 'loki', 'started': 10.0, 'ended': None, 'reason': 'x'})
m, _ = make(store, {'loki': FakeSource('loki', up=False)})
m.cycle()
assert len(store.blind) == 1
assert store.sources['loki']['since'] == 10.0
def test_database_away_buffers_writes_and_flushes_later():
"""T1 shape: CT 149 freezes with Ceph; Thor keeps cycling and writes when it's back."""
store = FakeStore()
m, clock = make(store)
m.cycle() # loaded while the database answered
store.down = True
clock.advance(120)
r1 = m.cycle()
assert r1.ok
assert m.pending # nothing written yet
clock.advance(120)
r2 = m.cycle()
assert r2.window[0] == r1.window[1] # the window still advances in memory
store.down = False
clock.advance(120)
m.cycle()
assert not m.pending
assert len(store.cycles) == 4
assert store.state['last_good_window_end'] == clock.t
def test_database_away_at_start_holds_the_window_until_incidents_load():
store = FakeStore()
store.down = True
m, clock = make(store)
r = m.cycle()
assert not r.ok and 'waiting for the database' in r.error
assert m.last_good_end is None
def test_pending_writes_are_bounded():
store = FakeStore()
store.down = True
m, _ = make(store)
for i in range(MAX_PENDING_WRITES + 10):
m._write('set_state', 'k', i, 0)
assert len(m.pending) == MAX_PENDING_WRITES
assert m.pending[-1][1][1] == MAX_PENDING_WRITES + 9
def test_failing_check_keeps_the_window_for_next_cycle_and_others_still_run():
ran = []
class Boom:
name = 'boom'
def run(self, ctx):
raise RuntimeError('bug')
class Fine:
name = 'fine'
def run(self, ctx):
ran.append((ctx.window_start, ctx.window_end, dict(ctx.up)))
return [Finding(kind='x', subject='storage-01', role=NOTE, layer=10, start=ctx.window_start,
end=ctx.window_end, key='host:storage-01:x', summary='x')]
store = FakeStore()
m, clock = make(store, checks=[Boom(), Fine()])
r = m.cycle()
assert not r.ok and 'boom: RuntimeError: bug' in r.error
assert r.findings == 1 and ran
assert 'last_good_window_end' not in store.state
assert 'last_good_cycle' not in store.state
def test_source_that_raises_unexpectedly_counts_as_blind():
class Broken:
name = 'proxmox'
def ping(self):
raise KeyError('bug')
m, _ = make(sources={'proxmox': Broken()})
r = m.cycle()
assert r.blind == ['proxmox']
def test_prepare_runs_once_and_is_retried_until_db_answers():
store = FakeStore()
store.down = True
calls = []
def prepare():
calls.append(1)
if store.down:
from thor.db import DatabaseError
raise DatabaseError('down')
m, clock = make(store, prepare=prepare)
m.cycle()
store.down = False
clock.advance(120)
m.cycle()
clock.advance(120)
m.cycle()
assert len(calls) == 2
def test_stop_ends_run_forever():
m, clock = make()
m.settings.cycle_seconds = 1
m.stop()
m.run_forever() # returns at once
def test_restart_while_db_away_still_catches_up_once_db_answers():
"""T2 order: the rack comes back, Thor starts before CT 149 does."""
store = FakeStore()
m, clock = make(store)
m.cycle()
went_dark = clock.t
clock.advance(22 * 60)
store.down = True
m2, _ = make(store, clock=clock)
first = m2.cycle()
assert first.window == (clock.t - 120, clock.t) # nothing known yet
clock.advance(120)
store.down = False
second = m2.cycle()
# The stored end wins, so the whole time Thor was away is reviewed, not just the last cycle.
assert second.window == (went_dark, clock.t) and second.catching_up