Files
mtusi2026lab7/student-tests/rotation_recovery.py
T
lxstatmose 2c2734df60
Skeleton contracts / contracts (pull_request) Canceled after 0s
Лабораторная 7: TLS, mTLS и OpenBao
2026-11-28 01:28:30 +03:00

28 lines
1.7 KiB
Python

"""Regression: reconcile lost KV write responses without rolling the DB back."""
import json,sys,tempfile
from pathlib import Path
from unittest.mock import patch
ROOT=Path(__file__).resolve().parents[1];sys.path.insert(0,str(ROOT/'scripts'))
import rotate
for unreadable in (False,True):
with tempfile.TemporaryDirectory() as folder:
ops=Path(folder);roles={'worker':{'database_url':'postgresql://worker:old@postgres/messenger'}}
(ops/'roles.json').write_text(json.dumps(roles));written={};calls=[]
def request(path,data=None,token=None):
if data is not None:
written.update(data['data']);raise TimeoutError()
if unreadable:raise TimeoutError()
return {'data':{'data':written}}
with patch.object(rotate,'OPS',ops),patch.object(rotate.bao,'root_token',return_value='test'),patch.object(rotate.bao,'call',side_effect=request),patch.object(rotate.subprocess,'run',side_effect=lambda *a,**kw:calls.append(kw['input'])):
if unreadable:
try:rotate.worker_secret()
except RuntimeError:pass
else:raise AssertionError('ambiguous failure hidden')
pending=json.loads((ops/'worker-rotation-pending.json').read_text())
with patch.object(rotate.bao,'call',return_value={}):rotate.worker_secret()
assert pending['password'] in calls[-1],'retry changed target password'
else:rotate.worker_secret()
assert all("PASSWORD 'old'" not in sql for sql in calls),'unsafe rollback after committed KV write'
assert not (ops/'worker-rotation-pending.json').exists()
print('PASS: committed-timeout reconciled; unknown outcome resumes saved target')