fix: address track2-reconciler verify findings
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1848,6 +1848,178 @@ def test_reconcile_change_limit(
|
||||
assert ids <= lost_ids(stand)
|
||||
|
||||
|
||||
@pytest.mark.spec(
|
||||
"storage-consistency",
|
||||
"Объект, на который ссылается строка, не удаляется",
|
||||
)
|
||||
def test_reconcile_keeps_referenced_objects(
|
||||
api: Api,
|
||||
stand: Stand,
|
||||
alice: Actor,
|
||||
chat: dict[str, Any],
|
||||
) -> None:
|
||||
"""Замечание W1 verify: объект `<X>/original`, у строки которого другой
|
||||
ключ, и объект `<Z>/original`, на который ссылается строка Y другого
|
||||
`id`, — чужие, а не объекты без метаданных."""
|
||||
data = images.sample_png()
|
||||
x, y = (api.upload(alice, chat["id"], data) for _ in range(2))
|
||||
z = str(uuid.uuid4())
|
||||
keys = {
|
||||
"x": object_key(x["id"]),
|
||||
"y": object_key(y["id"]),
|
||||
"z": object_key(z),
|
||||
# ключ X: под префиксом вложений, объекта по нему нет
|
||||
"other": f"{FAULT_DATABASE}/attachments/x-other-{uuid.uuid4()}",
|
||||
}
|
||||
put_object(stand, keys["z"], data)
|
||||
try:
|
||||
stand("set-key", x["id"], keys["other"])
|
||||
stand("set-key", y["id"], keys["z"])
|
||||
for item in (x, y):
|
||||
stand("age-row", item["id"], str(AGED_S))
|
||||
# объект Z записан только что: после G + 1 все объекты старше grace
|
||||
time.sleep(OBJECT_AGE_WAIT_S)
|
||||
unknown = {keys[name]: "unknown_object" for name in ("x", "y", "z")}
|
||||
dry = run_reconcile(stand, "dry-run")
|
||||
assert dry.kinds(FOUND, keys=keys.values()) == unknown | {
|
||||
keys["other"]: "missing_object",
|
||||
}
|
||||
assert dry.exit_code == 3
|
||||
|
||||
applied = run_reconcile(stand, "apply")
|
||||
assert applied.kinds(FIXED, keys=keys.values()) == {
|
||||
keys["other"]: "missing_object",
|
||||
}
|
||||
assert applied.kinds(FOUND, keys=keys.values()) == unknown
|
||||
assert applied.exit_code == 3
|
||||
# ни один из трёх объектов не удалён, строки на месте
|
||||
assert {x["id"], y["id"], z} <= stored_ids(stand)
|
||||
rows = attachment_rows(stand)
|
||||
assert rows[x["id"]] == rows[y["id"]] == "ready"
|
||||
# X без объекта по своему ключу помечено, у Y объект по ключу есть
|
||||
lost = lost_ids(stand)
|
||||
assert x["id"] in lost
|
||||
assert y["id"] not in lost
|
||||
finally:
|
||||
# прежние ключи; объект Z, на который больше никто не ссылается, —
|
||||
# прочь, чтобы следующие проходы не удаляли его как свой
|
||||
stand("set-key", x["id"], keys["x"])
|
||||
stand("set-key", y["id"], keys["y"])
|
||||
stand("delete-object", keys["z"])
|
||||
|
||||
|
||||
@pytest.mark.spec(
|
||||
"storage-consistency",
|
||||
"Предел не останавливает уборку зависших",
|
||||
)
|
||||
def test_reconcile_limit_keeps_cleanup(
|
||||
api: Api,
|
||||
stand: Stand,
|
||||
alice: Actor,
|
||||
chat: dict[str, Any],
|
||||
) -> None:
|
||||
"""Замечание W1 verify: отказ по пределу не мешает уборке зависшей
|
||||
загрузки — её уборщик делает и без сверки."""
|
||||
data = images.sample_png()
|
||||
limit = 2
|
||||
# уборщик стенда стоит: зависшую загрузку P должна убрать сверка
|
||||
with outage(stand, "stop", "cleaner"):
|
||||
filename = fault_filename()
|
||||
before = attachment_rows(stand)
|
||||
stand("fail-attachments", "commit", filename)
|
||||
try:
|
||||
assert_error(
|
||||
upload_raw(api, alice, chat, data, filename=filename),
|
||||
503,
|
||||
)
|
||||
finally:
|
||||
stand("clear-fail-attachments")
|
||||
(pending,) = new_ids(before, attachment_rows(stand))
|
||||
assert pending in stored_ids(stand)
|
||||
stand("age-row", pending, str(AGED_S))
|
||||
batch = [api.upload(alice, chat["id"], data) for _ in range(3)]
|
||||
ids = {item["id"] for item in batch}
|
||||
for item in batch:
|
||||
lose_original(stand, item)
|
||||
|
||||
refused = run_reconcile(stand, "apply", limit)
|
||||
assert refused.exit_code == 3
|
||||
(error,) = refused.events(LIMIT_EXCEEDED)
|
||||
assert error["changes"] >= len(batch), error
|
||||
assert refused.finished["refused"] is True
|
||||
assert refused.kinds(FIXED, ids={pending}) == {
|
||||
pending: "stale_pending",
|
||||
}
|
||||
(record,) = refused.about(FIXED, ids={pending})
|
||||
assert record["action"] == "removed_pending"
|
||||
assert pending not in attachment_rows(stand)
|
||||
assert pending not in stored_ids(stand)
|
||||
# изменений под пределом нет: ни одной пометки
|
||||
assert not refused.about(FIXED, ids=ids)
|
||||
assert not ids & lost_ids(stand)
|
||||
# стенд — без внесённых потерь для следующих тестов
|
||||
applied = run_reconcile(stand, "apply", 100)
|
||||
assert set(applied.kinds(FIXED, ids=ids)) == ids
|
||||
|
||||
|
||||
@pytest.mark.spec(
|
||||
"storage-consistency",
|
||||
"Ключ вне префикса вложений не трогается",
|
||||
)
|
||||
def test_reconcile_foreign_key_untouched(
|
||||
api: Api,
|
||||
stand: Stand,
|
||||
alice: Actor,
|
||||
chat: dict[str, Any],
|
||||
) -> None:
|
||||
"""Замечание W2 verify: строка с ключом вне `<база>/attachments/` —
|
||||
готовая и зависшая загрузка — только в отчёте как чужой объект: сверка
|
||||
не делает HEAD чужого префикса, не помечает и не убирает строку."""
|
||||
data = images.sample_png()
|
||||
# уборщик стенда стоит: иначе зависшую загрузку убрал бы он
|
||||
with outage(stand, "stop", "cleaner"):
|
||||
ready = api.upload(alice, chat["id"], data)
|
||||
filename = fault_filename()
|
||||
before = attachment_rows(stand)
|
||||
stand("fail-attachments", "commit", filename)
|
||||
try:
|
||||
assert_error(
|
||||
upload_raw(api, alice, chat, data, filename=filename),
|
||||
503,
|
||||
)
|
||||
finally:
|
||||
stand("clear-fail-attachments")
|
||||
(pending,) = new_ids(before, attachment_rows(stand))
|
||||
ids = {ready["id"], pending}
|
||||
try:
|
||||
for item in ids:
|
||||
# префикс своей базы, но не вложений: список его не видит
|
||||
stand("set-key", item, f"{FAULT_DATABASE}/elsewhere/{item}")
|
||||
stand("age-row", item, str(AGED_S))
|
||||
unknown = dict.fromkeys(ids, "unknown_object")
|
||||
dry = run_reconcile(stand, "dry-run")
|
||||
assert dry.kinds(FOUND, ids=ids) == unknown
|
||||
assert dry.exit_code == 3
|
||||
applied = run_reconcile(stand, "apply")
|
||||
assert applied.kinds(FOUND, ids=ids) == unknown
|
||||
assert not applied.about(FIXED, ids=ids)
|
||||
assert applied.exit_code == 3
|
||||
# ни пометки, ни уборки: строки и объекты под префиксом на месте
|
||||
rows = attachment_rows(stand)
|
||||
assert (rows.get(ready["id"]), rows.get(pending)) == (
|
||||
"ready",
|
||||
"pending",
|
||||
)
|
||||
assert ready["id"] not in lost_ids(stand)
|
||||
assert ids <= stored_ids(stand)
|
||||
finally:
|
||||
# прежние ключи у тех строк, что остались: зависшую загрузку
|
||||
# после теста уберёт уборщик стенда вместе с её объектом
|
||||
rows = attachment_rows(stand)
|
||||
for item in ids & set(rows):
|
||||
stand("set-key", item, object_key(item))
|
||||
|
||||
|
||||
@pytest.mark.spec("storage-consistency", "S3 недоступен во время сверки")
|
||||
def test_reconcile_object_storage_stopped(
|
||||
api: Api,
|
||||
|
||||
Reference in New Issue
Block a user