Files
thor/tests/test_engine.py
T

161 lines
6.5 KiB
Python
Raw Normal View History

from thor.config import Settings
from thor.engine import CLEAR_AFTER_S, Engine
from thor.hosts import HostRegistry
from thor.model import CAUSE, CLEARED, LAYER_RESOURCE, LAYER_SCRAPE, NOTE, OPEN, PENDING, SYMPTOM, Finding
T0 = 1_795_410_668.0
class Rules:
name = 'memory'
def summarize(self, inc):
return f'{len(inc.events)} kills on {inc.subject}'
def priority(self, inc):
return 2 if len(inc.events) >= 3 else 3
def engine(mode=None):
writes = []
s = Settings()
if mode:
s.checks = {'memory': mode}
e = Engine(s, HostRegistry(), {'memory': Rules()}, lambda m, *a, **k: writes.append((m, a, k)))
return e, writes
def cause(at, host='compute-storage-01', n=1):
return Finding(kind='oom', subject=host, role=CAUSE, layer=LAYER_RESOURCE, start=at, end=at,
key=f'host:{host}:oom', summary='x', check='memory',
events=[{'id': f'{host}:{at + i}', 'at': at + i, 'text': 'kill'} for i in range(n)])
def symptom(at, host, kind='scrape', cluster=True):
return Finding(kind=kind, subject=host, role=SYMPTOM, layer=LAYER_SCRAPE, start=at, end=at,
key=f'host:{host}:{kind}', summary=f'{host} {kind}', cluster_effect=cluster)
def kinds(actions):
return [a.kind for a in actions]
def test_new_cause_is_held_one_cycle_then_opened_in_shadow():
e, writes = engine()
assert e.process([cause(T0)], T0 + 30) == []
inc = e.incidents['host:compute-storage-01:oom']
assert inc.state == PENDING and inc.mode == 'shadow'
acts = e.process([], T0 + 150)
assert kinds(acts) == ['open']
a = acts[0]
assert a.mode == 'shadow' and a.payload['dedup_key'] == inc.key
assert a.payload['title'] == '[compute-storage-01][auto][thor][software] 1 kills on compute-storage-01 ' \
'[single-node][production][issue]'
assert [w[1][0]['kind'] for w in writes if w[0] == 'add_action'] == ['open']
assert inc.state == OPEN
def test_symptoms_attach_to_the_cause_even_on_other_hosts_when_it_can_stall_the_cluster():
e, _ = engine()
e.process([cause(T0), symptom(T0 - 5, 'monitor-01', 'pvestatd')], T0 + 30)
inc = e.incidents['host:compute-storage-01:oom']
assert 'host:monitor-01:pvestatd' in inc.symptoms # 5 s before the cause is fine
acts = e.process([symptom(T0 + 100, 'storage-01')], T0 + 150)
assert kinds(acts) == ['open'] # attached while pending: it's in the first text
assert 'storage-01 scrape' in acts[0].payload['description']
acts = e.process([symptom(T0 + 200, 'compute-storage-02')], T0 + 270)
assert kinds(acts) == ['also_seen'] # after opening: a comment
def test_local_symptom_does_not_attach_to_a_host_without_osds():
e, _ = engine()
e.process([cause(T0, host='monitor-02'), symptom(T0, 'storage-01')], T0 + 30)
assert not e.incidents['host:monitor-02:oom'].symptoms
e.process([symptom(T0 + 10, 'monitor-02')], T0 + 40)
assert 'host:monitor-02:scrape' in e.incidents['host:monitor-02:oom'].symptoms
def test_unexplained_symptom_opens_nothing():
e, writes = engine()
assert e.process([symptom(T0, 'storage-01')], T0) == []
assert not e.incidents
assert any(w[0] == 'save_observation' for w in writes)
def test_notes_never_open_anything():
e, _ = engine()
note = Finding(kind='oom_cgroup', subject='compute-storage-01', role=NOTE, layer=LAYER_RESOURCE, start=T0,
end=T0, key='host:compute-storage-01:oom_cgroup', summary='cgroup')
e.process([note], T0)
e.process([], T0 + 300)
assert not e.incidents
def test_also_seen_after_open_and_escalation_only_upwards():
e, _ = engine()
e.process([cause(T0)], T0)
e.process([], T0 + 120)
acts = e.process([symptom(T0 + 130, 'storage-01'), cause(T0 + 140, n=3)], T0 + 240)
assert 'escalate' in kinds(acts) and 'also_seen' in kinds(acts)
esc = [a for a in acts if a.kind == 'escalate'][0]
assert (esc.payload['old'], esc.payload['new']) == (3, 2)
assert e.process([], T0 + 360) == [] # nothing new: no repeat actions
def test_events_are_deduplicated_when_a_window_is_reviewed_again():
e, _ = engine()
e.process([cause(T0)], T0)
e.process([cause(T0)], T0 + 120)
assert len(e.incidents['host:compute-storage-01:oom'].events) == 1
def test_clears_after_fifteen_quiet_minutes_and_comes_back_as_a_new_round():
e, _ = engine()
e.process([cause(T0)], T0)
e.process([], T0 + 120)
acts = e.process([], T0 + CLEAR_AFTER_S + 1)
assert 'cleared' in kinds(acts)
inc = e.incidents['host:compute-storage-01:oom']
assert inc.state == CLEARED
e.process([cause(T0 + 5000)], T0 + 5000)
assert inc.state == PENDING and inc.first_seen == T0 + 5000 and inc.symptoms == {}
acts = e.process([], T0 + 5120)
assert kinds(acts) == ['open']
def test_live_mode_actions_and_catch_up_open_at_once():
e, _ = engine(mode='live')
acts = e.process([cause(T0)], T0 + 3600, catching_up=True)
assert kinds(acts) == ['open'] and acts[0].mode == 'live'
inc = e.incidents['host:compute-storage-01:oom']
assert inc.seen_after_the_fact
assert 'Seen after the fact' in acts[0].payload['description']
def test_refresh_when_summary_changes_rate_limited_and_once_more_when_clearing():
class Flat(Rules):
def priority(self, inc):
return 3
e, _ = engine()
e.checks['memory'] = Flat()
e.process([cause(T0)], T0)
e.process([], T0 + 120) # opened, sent at T0+120
e.process([cause(T0 + 200)], T0 + 240) # summary changed, but < 30 min since sent
assert 'refresh' not in kinds(e.process([], T0 + 360))
for t in range(600, 1900, 300): # keep it active
e.process([cause(T0 + t)], T0 + t + 10)
assert 'refresh' in kinds(e.process([cause(T0 + 1950)], T0 + 1960))
e.process([cause(T0 + 2000)], T0 + 2010) # changed again, too soon to refresh
acts = kinds(e.process([], T0 + 2000 + CLEAR_AFTER_S + 1))
assert acts == ['refresh', 'cleared'] # final text goes out before the note
def test_load_and_covering():
e, _ = engine()
e.process([cause(T0)], T0)
saved = e.incidents['host:compute-storage-01:oom'].to_dict()
e2, _ = engine()
e2.load([saved])
assert [i.key for i in e2.covering(host='compute-storage-01')] == ['host:compute-storage-01:oom']
assert [i.key for i in e2.covering(scope='cluster')] == ['host:compute-storage-01:oom']
assert e2.covering(host='monitor-01') == []