155 lines
5.5 KiB
Python
155 lines
5.5 KiB
Python
"""Disk ownership and interrupted-job recovery through the real CLI."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import hashlib
|
|
import io
|
|
import os
|
|
import selectors
|
|
import signal
|
|
import subprocess
|
|
import sys
|
|
from typing import TYPE_CHECKING, Literal, assert_never
|
|
|
|
import pytest
|
|
from PIL import Image
|
|
|
|
from tests.conftest import ROOT, TOKEN, api_server, encoded, routes_for
|
|
from tests.test_import import run_cli
|
|
|
|
if TYPE_CHECKING:
|
|
from pathlib import Path
|
|
|
|
|
|
def png(color: str) -> bytes:
|
|
"""Encode a deterministic opaque asset for cache ownership scenarios."""
|
|
with io.BytesIO() as buffer:
|
|
Image.new("RGBA", (128, 128), color).save(buffer, format="PNG")
|
|
return buffer.getvalue()
|
|
|
|
|
|
def test_recovery_and_replacement_reclaim_only_owned_unreferenced_assets(
|
|
tmp_path: Path,
|
|
conversion_assets: dict[str, bytes],
|
|
) -> None:
|
|
"""Retain shared and unrelated files while reclaiming stale owned assets."""
|
|
cache = tmp_path / "cache"
|
|
assets = cache / "assets"
|
|
assets.mkdir(parents=True)
|
|
ids: list[str] = []
|
|
for color in ("red", "green", "blue", "yellow"):
|
|
data = png(color)
|
|
asset_id = hashlib.sha256(data).hexdigest()
|
|
_ = (assets / (asset_id + ".png")).write_bytes(data)
|
|
ids.append(asset_id)
|
|
_ = (assets / "unrelated.txt").write_text("keep")
|
|
_ = (cache / "catalog.json").write_bytes(
|
|
encoded(
|
|
{
|
|
"version": 1,
|
|
"packs": [
|
|
{
|
|
"id": "FixturePack",
|
|
"title": "Old",
|
|
"stickers": [
|
|
{"id": ids[0], "emoji": "", "frames": 1},
|
|
{"id": ids[1], "emoji": "", "frames": 1},
|
|
],
|
|
},
|
|
{
|
|
"id": "KeepMe",
|
|
"title": "Protected",
|
|
"stickers": [
|
|
{"id": ids[0], "emoji": "", "frames": 1},
|
|
],
|
|
},
|
|
],
|
|
}
|
|
)
|
|
)
|
|
root = cache / ".wowmine-stickers-importer-v1"
|
|
job = root / "job-12345678"
|
|
job.mkdir(parents=True)
|
|
_ = (root / "owner").write_bytes(b"wowmine-stickers-importer-v1\n")
|
|
_ = (job / "journal.json").write_bytes(
|
|
encoded({"version": 1, "assets": [ids[0], ids[2]]})
|
|
)
|
|
_ = (job / "input.webp").write_bytes(b"stale input")
|
|
_ = (root / "unrelated.txt").write_text("keep too")
|
|
with api_server(
|
|
routes_for({"static.webp": conversion_assets["static.webp"]})
|
|
) as origin:
|
|
result = run_cli(tmp_path, origin)
|
|
assert result.returncode == 0, result.stderr
|
|
assert (assets / (ids[0] + ".png")).read_bytes() == png("red")
|
|
assert not (assets / (ids[1] + ".png")).exists()
|
|
assert not (assets / (ids[2] + ".png")).exists()
|
|
assert (assets / (ids[3] + ".png")).read_bytes() == png("yellow")
|
|
assert (assets / "unrelated.txt").read_text() == "keep"
|
|
assert (root / "unrelated.txt").read_text() == "keep too"
|
|
assert sorted(p.name for p in root.iterdir()) == ["owner", "unrelated.txt"]
|
|
|
|
|
|
@pytest.mark.parametrize("interrupt", [signal.SIGKILL, signal.SIGALRM])
|
|
def test_interrupted_job_retains_catalog_and_next_import_recovers_staging(
|
|
tmp_path: Path,
|
|
conversion_assets: dict[str, bytes],
|
|
interrupt: Literal[signal.Signals.SIGKILL, signal.Signals.SIGALRM],
|
|
) -> None:
|
|
"""Recover killed or timed-out jobs without changing the committed catalog."""
|
|
token = tmp_path / "token"
|
|
_ = token.write_text(TOKEN)
|
|
cache = tmp_path / "cache"
|
|
cache.mkdir()
|
|
original = b'{"version":1,"packs":[]}'
|
|
_ = (cache / "catalog.json").write_bytes(original)
|
|
command = (
|
|
[os.environ["IMPORTER_BINARY"]]
|
|
if "IMPORTER_BINARY" in os.environ
|
|
else [
|
|
sys.executable,
|
|
str(ROOT / "import_pack.py"),
|
|
]
|
|
)
|
|
with api_server(routes_for(conversion_assets)) as origin:
|
|
with subprocess.Popen(
|
|
[
|
|
*command,
|
|
"--token-file",
|
|
str(token),
|
|
"--pack",
|
|
"FixturePack",
|
|
"--output",
|
|
str(cache),
|
|
"--api-base",
|
|
origin,
|
|
],
|
|
stdout=subprocess.PIPE,
|
|
stderr=subprocess.PIPE,
|
|
bufsize=0,
|
|
) as child:
|
|
assert child.stdout is not None
|
|
with selectors.DefaultSelector() as selector:
|
|
_ = selector.register(child.stdout, selectors.EVENT_READ)
|
|
assert selector.select(timeout=10)
|
|
assert child.stdout.readline() == b'{"status":"fetching"}\n'
|
|
assert selector.select(timeout=10)
|
|
assert b'"status": "converting"' in child.stdout.readline()
|
|
child.send_signal(interrupt)
|
|
stdout, stderr = child.communicate(timeout=10)
|
|
match interrupt:
|
|
case signal.SIGKILL:
|
|
assert child.returncode == -signal.SIGKILL
|
|
case signal.SIGALRM:
|
|
assert child.returncode == 1
|
|
assert stderr == b'{"status":"error","code":"job_timeout"}\n'
|
|
case _:
|
|
assert_never(interrupt)
|
|
assert TOKEN.encode() not in stdout + stderr
|
|
assert (cache / "catalog.json").read_bytes() == original
|
|
result = run_cli(tmp_path, origin)
|
|
assert result.returncode == 0, result.stderr
|
|
assert sorted(
|
|
p.name for p in (cache / ".wowmine-stickers-importer-v1").iterdir()
|
|
) == ["owner"]
|