"""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')