| 1 | """참조 판독기와 실제 작업자로 독립된 한도 기대값을 검사함.""" |
| 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, build, digest, violations |
| 15 | |
| 16 | |
| 17 | def check_payload(source, size): |
| 18 | remaining = size |
| 19 | while remaining: |
| 20 | chunk = source.read(min(CHUNK, remaining)) |
| 21 | assert chunk and chunk == bytes(len(chunk)) |
| 22 | remaining -= len(chunk) |
| 23 | |
| 24 | |
| 25 | def check_reference(path, artifact): |
| 26 | size = artifact["payload_bytes"] |
| 27 | if artifact["format"] == "Zip": |
| 28 | with zipfile.ZipFile(path) as archive: |
| 29 | assert archive.namelist() == [NAME] |
| 30 | info = archive.getinfo(NAME) |
| 31 | assert info.file_size == size |
| 32 | assert info.compress_size == artifact["ratio_input_bytes"] |
| 33 | with archive.open(NAME) as source: |
| 34 | check_payload(source, size) |
| 35 | assert source.read(1) == b"" |
| 36 | else: |
| 37 | assert path.stat().st_size == artifact["ratio_input_bytes"] |
| 38 | with gzip.open(path, "rb") as source: |
| 39 | if artifact["format"] == "TarGzip": |
| 40 | header = source.read(512) |
| 41 | info = tarfile.TarInfo.frombuf(header, "utf-8", "strict") |
| 42 | assert info.name == NAME and info.size == size and info.isfile() |
| 43 | check_payload(source, size) |
| 44 | if artifact["format"] == "TarGzip": |
| 45 | tail_size = (-size) % 512 + 1024 |
| 46 | assert source.read(tail_size) == bytes(tail_size) |
| 47 | assert source.read(1) == b"" |
| 48 | |
| 49 | |
| 50 | def verify(directory, probe, worker, large=False): |
| 51 | directory = directory.resolve() |
| 52 | directory.relative_to(ROOT / "tests") |
| 53 | manifest = json.loads((directory / "expectations.json").read_text(encoding="utf-8")) |
| 54 | assert not large or manifest["large"] |
| 55 | for name, artifact in manifest["artifacts"].items(): |
| 56 | assert (directory / name).stat().st_size == artifact["archive_bytes"] |
| 57 | assert digest(directory / name) == artifact["sha256"] |
| 58 | check_reference(directory / name, artifact) |
| 59 | print(f"REFERENCE {name}", flush=True) |
| 60 | results = [] |
| 61 | for case in manifest["cases"]: |
| 62 | path = directory / case["file"] |
| 63 | artifact = manifest["artifacts"][case["file"]] |
| 64 | assert violations(artifact, case["limits"]) == case["violated_limits"] |
| 65 | before = path.stat() |
| 66 | with tempfile.TemporaryDirectory(prefix="case-", dir=directory) as scratch: |
| 67 | destination = Path(scratch) / "output" |
| 68 | staging = Path(scratch) / "staging" |
| 69 | destination.mkdir(mode=0o700) |
| 70 | staging.mkdir(mode=0o700) |
| 71 | command = [ |
| 72 | str(probe.resolve()), str(worker.resolve()), str(path), str(destination), |
| 73 | str(case["limits"]["input"]), str(case["limits"]["output"]), |
| 74 | str(case["limits"]["entries"]), case["expected"]["outcome"], |
| 75 | str(artifact["payload_bytes"]), artifact["format"], |
| 76 | ] |
| 77 | environment = os.environ.copy() |
| 78 | environment["TMPDIR"] = str(staging) |
| 79 | environment["TMP"] = str(staging) |
| 80 | environment["TEMP"] = str(staging) |
| 81 | process = subprocess.Popen( |
| 82 | command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=environment, |
| 83 | start_new_session=(os.name == "posix"), |
| 84 | ) |
| 85 | try: |
| 86 | stdout, stderr = process.communicate(timeout=360) |
| 87 | except subprocess.TimeoutExpired: |
| 88 | if os.name == "posix": |
| 89 | os.killpg(process.pid, signal.SIGKILL) |
| 90 | else: |
| 91 | process.kill() |
| 92 | process.communicate() |
| 93 | raise |
| 94 | assert process.returncode == 0, (case["id"], stdout, stderr) |
| 95 | assert not list(staging.iterdir()) |
| 96 | if case["expected"]["outcome"] == "Completed": |
| 97 | with (destination / NAME).open("rb") as source: |
| 98 | check_payload(source, artifact["payload_bytes"]) |
| 99 | assert source.read(1) == b"" |
| 100 | else: |
| 101 | assert not list(destination.iterdir()) |
| 102 | after = path.stat() |
| 103 | assert (before.st_size, before.st_mtime_ns) == (after.st_size, after.st_mtime_ns) |
| 104 | results.append({"case": case["id"], "status": "passed"}) |
| 105 | print(f"PASS {case['id']}", flush=True) |
| 106 | for name, artifact in manifest["artifacts"].items(): |
| 107 | assert digest(directory / name) == artifact["sha256"] |
| 108 | return results |
| 109 | |
| 110 | |
| 111 | def main(): |
| 112 | parser = argparse.ArgumentParser() |
| 113 | parser.add_argument("--directory", type=Path, help="기존 표본 또는 새 표본 경로") |
| 114 | parser.add_argument("--large", action="store_true", help="1 GiB 성공과 16 GiB 초과도 검사함") |
| 115 | target = Path(os.environ.get("CARGO_TARGET_DIR", ROOT / "tests/ci/target")) / "debug" |
| 116 | suffix = ".exe" if os.name == "nt" else "" |
| 117 | parser.add_argument("--probe", type=Path, default=target / ("zipelo-large-archive-probe" + suffix)) |
| 118 | parser.add_argument("--worker", type=Path, default=target / ("zipelo-archive-worker" + suffix)) |
| 119 | args = parser.parse_args() |
| 120 | assert args.probe.is_file() and args.worker.is_file(), "local 빌드를 먼저 실행해야 함" |
| 121 | if args.directory: |
| 122 | if not args.directory.exists(): |
| 123 | build(args.directory, args.large) |
| 124 | verify(args.directory, args.probe, args.worker, args.large) |
| 125 | else: |
| 126 | with tempfile.TemporaryDirectory(prefix="fixture-", dir=ROOT / "tests/large-archive-fixtures") as scratch: |
| 127 | directory = Path(scratch) / "corpus" |
| 128 | build(directory, args.large) |
| 129 | verify(directory, args.probe, args.worker, args.large) |
| 130 | |
| 131 | |
| 132 | if __name__ == "__main__": |
| 133 | main() |