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
- 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>
126 lines
5.3 KiB
Python
126 lines
5.3 KiB
Python
from thor import ticket_text
|
|
from thor.clients.tinker import TicketResult
|
|
from thor.db import DatabaseError
|
|
from thor.hosts import HostRegistry
|
|
from thor.model import Incident
|
|
from thor.outbox import MAX_ATTEMPTS, Outbox
|
|
|
|
from .fakes import FakeStore
|
|
|
|
|
|
class FakeTinker:
|
|
configured = True
|
|
|
|
def __init__(self, ok=True):
|
|
self.ok = ok
|
|
self.created, self.comments = [], []
|
|
|
|
def create(self, **kw):
|
|
self.created.append(kw)
|
|
return TicketResult('555', 'created', ok=True) if self.ok else TicketResult(None, error='down')
|
|
|
|
def comment(self, ticket_id, text):
|
|
self.comments.append((ticket_id, text))
|
|
return self.ok
|
|
|
|
|
|
def live(store, kind, key='host:h:oom', **payload):
|
|
base = {'title': 't', 'description': 'd', 'priority': 2, 'category': 'Software', 'type': 'Issue',
|
|
'dedup_key': key}
|
|
base.update(payload)
|
|
return store.add_action({'incident_key': key, 'kind': kind, 'created': 100.0, 'mode': 'live', 'payload': base})
|
|
|
|
|
|
def test_outbox_sends_creates_then_comments_and_skips_shadow():
|
|
store, tinker = FakeStore(), FakeTinker()
|
|
store.add_action({'incident_key': 'k', 'kind': 'open', 'created': 1.0, 'mode': 'shadow', 'payload': {}})
|
|
live(store, 'open')
|
|
live(store, 'escalate', comment='raised')
|
|
live(store, 'also_seen', comment='also')
|
|
assert Outbox(store, tinker).flush(200.0) == 3
|
|
assert len(tinker.created) == 2 and tinker.created[0]['dedup_key'] == 'host:h:oom'
|
|
assert tinker.comments == [('555', 'raised'), ('555', 'also')]
|
|
assert store.actions[0]['status'] == 'shadow'
|
|
|
|
|
|
def test_outbox_retries_with_backoff_and_gives_up():
|
|
store, tinker = FakeStore(), FakeTinker(ok=False)
|
|
aid = live(store, 'open')
|
|
Outbox(store, tinker).flush(200.0)
|
|
a = store.actions[aid - 1]
|
|
assert a['status'] == 'pending' and a['attempts'] == 1 and a['next_attempt_at'] == 320.0
|
|
a['attempts'] = MAX_ATTEMPTS - 1
|
|
a['next_attempt_at'] = 0
|
|
Outbox(store, tinker).flush(500.0)
|
|
assert a['status'] == 'failed'
|
|
|
|
|
|
def test_outbox_comment_waits_for_its_ticket_and_unknown_kinds_fail():
|
|
store, tinker = FakeStore(), FakeTinker()
|
|
aid = live(store, 'cleared', comment='done')
|
|
bad = live(store, 'weird', key='other')
|
|
Outbox(store, tinker).flush(200.0)
|
|
assert 'no ticket yet' in store.actions[aid - 1]['error']
|
|
assert 'unknown action' in store.actions[bad - 1]['error']
|
|
|
|
|
|
def test_outbox_does_nothing_unconfigured_or_without_db():
|
|
store = FakeStore()
|
|
t = FakeTinker()
|
|
t.configured = False
|
|
assert Outbox(store, t).flush(1.0) == 0
|
|
store.down = True
|
|
assert Outbox(store, FakeTinker()).flush(1.0) == 0
|
|
|
|
|
|
def test_outbox_stops_when_db_goes_away_mid_flush():
|
|
store, tinker = FakeStore(), FakeTinker()
|
|
live(store, 'open')
|
|
|
|
def boom(*a):
|
|
raise DatabaseError('gone')
|
|
store.mark_action_sent = boom
|
|
assert Outbox(store, tinker).flush(200.0) == 0
|
|
|
|
|
|
def test_hosts_by_instance_aliases_and_proxmox_updates():
|
|
h = HostRegistry()
|
|
assert h.by_instance('10.10.10.4:9100') == 'compute-storage-01'
|
|
assert h.by_instance('10.10.10.99:9100') is None
|
|
assert h.canonical('large1') == 'compute-storage-02'
|
|
assert h.canonical('nope') is None
|
|
assert h.loki_names('compute-storage-02') == ['compute-storage-02', 'large1']
|
|
assert h.loki_names('unknown') == ['unknown']
|
|
h.update_from_proxmox([
|
|
{'type': 'cluster', 'name': 'lotuscluster'},
|
|
{'type': 'node', 'name': 'monitor-02', 'ip': '10.10.10.19'}, # IP change
|
|
{'type': 'node', 'name': 'storage-02', 'ip': '10.10.10.11'}, # rename of storage-01
|
|
{'type': 'node', 'name': 'brand-new', 'ip': '10.10.10.12'},
|
|
])
|
|
assert h.get('monitor-02').ip == '10.10.10.19'
|
|
assert h.canonical('storage-01') == 'storage-02' and h.get('storage-02').ceph_osds
|
|
assert h.get('brand-new').cluster_node and h.get('pbs').ip == '10.10.10.3'
|
|
assert len(h.all()) == 8
|
|
|
|
|
|
def test_ticket_text_pieces():
|
|
assert ticket_text.duration(5) == '5 s'
|
|
assert ticket_text.duration(125) == '2 min'
|
|
assert ticket_text.duration(3600 * 3 + 60) == '3 h 1 min'
|
|
assert ticket_text.duration(3600 * 5) == '5 h'
|
|
assert ticket_text.duration(86400 * 3) == '3 d'
|
|
assert ticket_text.duration(86400 * 3 + 7200) == '3 d 2 h'
|
|
assert ticket_text.utc(None) == 'unknown'
|
|
inc = Incident(key='cluster:x', kind='partition', subject='cluster', layer=60, check='c', summary='Split',
|
|
first_seen=1795410668.0, last_seen=1795410968.0, priority=1,
|
|
facts={'display': {'Hosts': 'a, b'}, 'escalation_reason': 'two hosts fenced'},
|
|
events=[{'at': 1795410668.0 + i, 'text': f'e{i}'} for i in range(20)],
|
|
symptoms={'s': {'summary': 'slow', 'first': 1795410668.0, 'last': 1795410900.0}})
|
|
assert ticket_text.title(inc) == '[proxmox-cluster][auto][thor][software] Split [cluster-wide][production][issue]'
|
|
d = ticket_text.description(inc, 'https://thor', related=['673679581'])
|
|
assert '- **Hosts:** a, b' in d and 'Why this priority' in d and '(5 earlier, see Thor)' in d
|
|
assert '#673679581' in d and 'to 05:15:00' in d and '[Thor](https://thor/incident/cluster:x)' in d
|
|
assert 'Thor raised the priority: P3 Medium to P1 Critical' in ticket_text.escalation_comment(3, 1, 'r')
|
|
assert 'Also seen: slow' in ticket_text.also_seen_comment('slow', 1795410668.0)
|
|
assert 'Lasted:** 5 min' in ticket_text.cleared_comment(inc)
|