| 1 | """생성 표본을 실제 작업자 프로세스에 전달해 현재 main의 기대값을 검사함.""" |
| 2 | |
| 3 | import argparse |
| 4 | import gzip |
| 5 | import json |
| 6 | import os |
| 7 | from pathlib import Path |
| 8 | import signal |
| 9 | import subprocess |
| 10 | import tempfile |
| 11 | import tarfile |
| 12 | import zipfile |
| 13 | |
| 14 | from generate import CHUNK, NAME, ROOT, digest |
| 15 | |
| 16 | |
| 17 | |
| 18 | def check_payload(source, size): |
| 19 | remaining = size |
| 20 | while remaining: |
| 21 | chunk = source.read(min(CHUNK, remaining)) |
| 22 | assert chunk and chunk == bytes(len(chunk)) |
| 23 | remaining -= len(chunk) |
| 24 | |
| 25 | |
| 26 | def check_reference(path, artifact): |
| 27 | size = artifact["payload_bytes"] |
| 28 | if path.name.endswith(".zip"): |
| 29 | with zipfile.ZipFile(path) as archive: |
| 30 | assert archive.namelist() == [NAME] |
| 31 | assert archive.getinfo(NAME).file_size == size |
| 32 | with archive.open(NAME) as source: |
| 33 | check_payload(source, size) |
| 34 | assert source.read(1) == b"" |
| 35 | else: |
| 36 | with gzip.open(path, "rb") as source: |
| 37 | header = source.read(512) |
| 38 | info = tarfile.TarInfo.frombuf(header, "utf-8", "strict") |
| 39 | assert info.name == NAME and info.size == size and info.isfile() |
| 40 | check_payload(source, size) |
| 41 | tail_size = (-size) % 512 + 1024 |
| 42 | assert source.read(tail_size) == bytes(tail_size) |
| 43 | assert source.read(1) == b"" |
| 44 | |
| 45 | |
| 46 | def verify(directory, probe, worker): |
| 47 | directory = directory.resolve() |
| 48 | directory.relative_to(ROOT / "tests") |
| 49 | manifest = json.loads((directory / "expectations.json").read_text(encoding="utf-8")) |
| 50 | for name, artifact in manifest["artifacts"].items(): |
| 51 | assert (directory / name).stat().st_size == artifact["archive_bytes"] |
| 52 | assert digest(directory / name) == artifact["sha256"] |
| 53 | check_reference(directory / name, artifact) |
| 54 | results = [] |
| 55 | for case in manifest["cases"]: |
| 56 | path = directory / case["file"] |
| 57 | artifact = manifest["artifacts"][case["file"]] |
| 58 | before = path.stat() |
| 59 | with tempfile.TemporaryDirectory(prefix="extract-", dir=directory) as scratch: |
| 60 | destination = Path(scratch) |
| 61 | command = [ |
| 62 | str(probe.resolve()), str(worker.resolve()), str(path), str(destination), |
| 63 | str(case["limits"]["output"]), case["expected"]["outcome"], |
| 64 | str(artifact["payload_bytes"]), |
| 65 | ] |
| 66 | process = subprocess.Popen( |
| 67 | command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
| 68 | start_new_session=(os.name == "posix"), |
| 69 | ) |
| 70 | try: |
| 71 | stdout, stderr = process.communicate(timeout=360) |
| 72 | except subprocess.TimeoutExpired: |
| 73 | if os.name == "posix": |
| 74 | os.killpg(process.pid, signal.SIGKILL) |
| 75 | else: |
| 76 | process.kill() |
| 77 | process.communicate() |
| 78 | raise |
| 79 | assert process.returncode == 0, (case["id"], stdout, stderr) |
| 80 | if case["expected"]["outcome"] == "Completed": |
| 81 | with (destination / "payload.bin").open("rb") as source: |
| 82 | while chunk := source.read(65536): |
| 83 | assert not any(chunk) |
| 84 | else: |
| 85 | assert not list(destination.iterdir()) |
| 86 | after = path.stat() |
| 87 | assert (before.st_size, before.st_mtime_ns) == (after.st_size, after.st_mtime_ns) |
| 88 | results.append({"case": case["id"], "status": "passed"}) |
| 89 | print(f"PASS {case['id']}", flush=True) |
| 90 | for name, artifact in manifest["artifacts"].items(): |
| 91 | assert digest(directory / name) == artifact["sha256"] |
| 92 | return results |
| 93 | |
| 94 | |
| 95 | if __name__ == "__main__": |
| 96 | parser = argparse.ArgumentParser() |
| 97 | parser.add_argument("--directory", required=True, type=Path) |
| 98 | parser.add_argument("--probe", required=True, type=Path) |
| 99 | parser.add_argument("--worker", required=True, type=Path) |
| 100 | args = parser.parse_args() |
| 101 | verify(args.directory, args.probe, args.worker) |