Withhold addresses after the database is recreated without a backup

When ripe.db is corrupted and no valid backup exists, a new empty database
is created with a db_recreated.json marker; /addresses and /addresses/diff
answer 503 until the collector gathers data again, /health reports
db_recreated. Tests now isolate all state files via conftest.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
ayurishchevandClaude Sonnet 5 committed 2026-09-21 09:56:43 +03:00
1 parent 46d56654d8
commit 0155fd3f38
13 files changed
+226 -32

No files matched your search

+23 -3
View File
@@ -19,6 +19,8 @@ def files(tmp_path, monkeypatch):
monkeypatch.setattr(cc, attr, str(tmp_path / name))
monkeypatch.setattr(cc, "BACKUP_DIR", str(tmp_path / "backups"))
monkeypatch.setattr(cc, "RESTORE_FILE", str(tmp_path / "last_restore.json"))
monkeypatch.setattr(cc, "RECREATED_FILE", str(tmp_path / "db_recreated.json"))
monkeypatch.setattr(cc, "CONFIG_FILE", str(tmp_path / "config.json"))
return tmp_path
@@ -121,9 +123,27 @@ def test_restore_from_backup(files):
assert len(glob.glob(str(files / "ripe.db.corrupt-*"))) == 1
assert json.loads((files / "last_restore.json").read_text())["backup"].endswith(".db")
# Без исправных копий - прежнее поведение: ошибка хранилища, база в карантине
# Без исправных копий создаётся новая пустая база (с меткой, см. test_recreated_database_guard)
for backup in db.list_backups(cc.BACKUP_DIR):
os.unlink(backup)
(files / "ripe.db").write_bytes(b"garbage" * 1000)
with pytest.raises(db.StorageError):
db.connect()
with db.session() as conn:
assert db.get_values(conn) == []
assert os.path.exists(cc.RECREATED_FILE) and glob.glob(str(files / "ripe.db.corrupt-*"))
def test_recreated_database_guard(files):
(files / "config.json").write_text('{"asns": [1], "fqdns": []}')
(files / "ripe.db").write_bytes(b"garbage" * 1000) # порча, копий нет
with db.session() as conn:
assert os.path.exists(cc.RECREATED_FILE)
# ASN настроен, данных нет - ожидание; FQDN не настроен - пустой список законен
assert db.recreated_pending(conn, ("asn",)) and not db.recreated_pending(conn, ("fqdn",))
assert db.get_changes(conn, {"asn"}, cursor=5) is None # курсор старой базы недействителен
# Данные собраны заново - сборщик снимает метку
with db.transaction(conn):
db.merge_source(conn, "asn", "1", {"a"}, datetime.datetime.now(), 90)
db.settle_recreated(conn)
assert not os.path.exists(cc.RECREATED_FILE)