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)