Datasets:
Download experiments/common/tests/test_image.py from PV-SJTU/PV-Bench: direct link, hf CLI and curl.
- Browser
- Download file 8.11 kB
-
https://huggingface.co/datasets/PV-SJTU/PV-Bench/resolve/main/experiments/common/tests/test_image.py
- Command line
-
hf download hf://datasets/PV-SJTU/PV-Bench/experiments/common/tests/test_image.py
-
curl -L -o test_image.py https://huggingface.co/datasets/PV-SJTU/PV-Bench/resolve/main/experiments/common/tests/test_image.py
8.11 kB
| import contextlib | |
| import hashlib | |
| import io | |
| import json | |
| import os | |
| import subprocess | |
| import sys | |
| import tempfile | |
| import unittest | |
| from pathlib import Path | |
| sys.path.insert(0, str(Path(__file__).resolve().parents[1])) | |
| from pvbench_common.image import (proxy_build_args, # noqa: E402 | |
| ImageError, ImageRecord, build, export_context, latest, lookup, main, record, | |
| record_path, tag_for) | |
| FAKE_ID = "sha256:" + "ab" * 32 | |
| FAKE_DOCKER = """#!/bin/sh | |
| echo "$@" >> "{log}" | |
| if [ "$1" = "image" ] && [ "$2" = "inspect" ]; then echo "{id}"; exit 0; fi | |
| if [ "$1" = "build" ]; then | |
| i=0; while [ $i -lt 40 ]; do echo "step $i"; i=$((i+1)); done | |
| exit {build_rc} | |
| fi | |
| exit 0 | |
| """ | |
| def git(repo, *args): | |
| return subprocess.run(["git", "-C", str(repo), *args], check=True, capture_output=True, | |
| text=True).stdout.strip() | |
| def files_under(root): | |
| return sorted(p.relative_to(root).as_posix() for p in root.rglob("*") if p.is_file()) | |
| class ImageCase(unittest.TestCase): | |
| def setUp(self): | |
| self._tmp = tempfile.TemporaryDirectory() | |
| self.root = Path(self._tmp.name) | |
| self.repo = self.root / "repo" | |
| for rel in ("backend/Rocq/a.v", "backend/Lean/l.lean", "backend/QCP_examples/x.h", "README.md"): | |
| (self.repo / rel).parent.mkdir(parents=True, exist_ok=True) | |
| (self.repo / rel).write_text(rel + "\n") | |
| git(self.root, "init", "-q", str(self.repo)) | |
| git(self.repo, "config", "user.email", "t@example.com") | |
| git(self.repo, "config", "user.name", "t") | |
| git(self.repo, "config", "commit.gpgsign", "false") | |
| git(self.repo, "add", ".") | |
| git(self.repo, "commit", "-q", "-m", "init") | |
| git(self.repo, "branch", "-M", "with-backend") | |
| self.sha = git(self.repo, "rev-parse", "HEAD") | |
| (self.repo / "backend/Rocq/a.vo").write_text("untracked build product\n") | |
| self.bins = self.root / "bin" | |
| self.bins.mkdir() | |
| for b in ("symexec", "StrategyCheck"): | |
| (self.bins / b).write_text("#!/bin/sh\n") | |
| self.dockerdir = self.root / "docker" | |
| (self.dockerdir / "agents").mkdir(parents=True) | |
| (self.dockerdir / "agents" / "codex.sh").write_text("#!/bin/sh\n") | |
| (self.dockerdir / "lean").mkdir() | |
| (self.dockerdir / "lean" / "lakefile.lean").write_text("import Lake\n") | |
| self.dockerfile = self.dockerdir / "Dockerfile" | |
| self.dockerfile.write_text("FROM scratch\n") | |
| def tearDown(self): | |
| self._tmp.cleanup() | |
| def fake_docker(self, build_rc=0): | |
| self.docker_log = self.root / "docker.argv" | |
| script = self.root / f"docker{build_rc}" | |
| script.write_text(FAKE_DOCKER.format(log=self.docker_log, id=FAKE_ID, | |
| build_rc=build_rc)) | |
| script.chmod(0o755) | |
| return str(script) | |
| class ImageTests(ImageCase): | |
| def test_tag_for(self): | |
| self.assertEqual(tag_for("0123456789abcdef0123"), "pvbench-env:0123456789ab") | |
| def test_export_context_from_tiny_repo(self): | |
| dest = self.root / "ctx" | |
| export_context(self.repo, self.sha, self.bins, self.dockerfile, dest) | |
| self.assertEqual(files_under(dest), [ | |
| "agents/codex.sh", "backend/Lean/l.lean", "backend/QCP_examples/x.h", "backend/Rocq/a.v", | |
| "backend/binary/linux-binary/StrategyCheck", "backend/binary/linux-binary/symexec", | |
| "lean/lakefile.lean"]) | |
| self.assertFalse((dest / "backend/Rocq/a.vo").exists()) | |
| self.assertFalse((dest / "README.md").exists()) | |
| def test_export_context_errors(self): | |
| busy = self.root / "busy" | |
| busy.mkdir() | |
| (busy / "f").write_text("x") | |
| with self.assertRaisesRegex(ImageError, "not empty"): | |
| export_context(self.repo, self.sha, self.bins, self.dockerfile, busy) | |
| with self.assertRaisesRegex(ImageError, "git archive"): | |
| export_context(self.repo, "f" * 40, self.bins, self.dockerfile, self.root / "c1") | |
| (self.bins / "StrategyCheck").unlink() | |
| with self.assertRaisesRegex(ImageError, "StrategyCheck"): | |
| export_context(self.repo, self.sha, self.bins, self.dockerfile, self.root / "c2") | |
| (self.bins / "StrategyCheck").write_text("") | |
| lone = self.root / "lone" / "Dockerfile" | |
| lone.parent.mkdir() | |
| lone.write_text("FROM scratch\n") | |
| with self.assertRaisesRegex(ImageError, "agents"): | |
| export_context(self.repo, self.sha, self.bins, lone, self.root / "c3") | |
| def test_build_with_fake_docker_and_real_git(self): | |
| log = self.root / "build.log" | |
| rec = build(self.repo, "with-backend", binary_dir=self.bins, dockerfile=self.dockerfile, | |
| docker=self.fake_docker(), log=log) | |
| self.assertEqual(rec.commit, self.sha) | |
| self.assertEqual(rec.tag, tag_for(self.sha)) | |
| self.assertEqual(rec.id, FAKE_ID) | |
| self.assertEqual(rec.platform, "linux/amd64") | |
| self.assertEqual(rec.dockerfile_sha256, | |
| hashlib.sha256(self.dockerfile.read_bytes()).hexdigest()) | |
| self.assertTrue(rec.built.endswith("+00:00")) | |
| self.assertIn("step 39", log.read_text()) | |
| calls = [line.split() for line in self.docker_log.read_text().splitlines()] | |
| argv = calls[0] | |
| self.assertEqual(argv[0], "build") | |
| for flag, value in (("--platform", "linux/amd64"), | |
| ("-f", str(self.dockerfile.resolve())), ("-t", rec.tag)): | |
| self.assertEqual(argv[argv.index(flag) + 1], value) | |
| self.assertEqual(calls[1][:2], ["image", "inspect"]) | |
| self.assertEqual(calls[1][-1], rec.tag) | |
| with self.assertRaises(ImageError) as cm: | |
| build(self.repo, self.sha, binary_dir=self.bins, dockerfile=self.dockerfile, | |
| docker=self.fake_docker(build_rc=1)) | |
| msg = str(cm.exception) | |
| self.assertIn("step 39", msg) | |
| self.assertIn("step 10", msg) | |
| self.assertNotIn("step 9\n", msg) | |
| def test_record_lookup_latest(self): | |
| d = self.root / "recs" | |
| d.mkdir() | |
| self.assertIsNone(latest(d)) | |
| a = ImageRecord("pvbench-env:aaa", "id-a", "a" * 40, "2026-01-02T00:00:00+00:00", "h") | |
| b = ImageRecord("pvbench-env:bbb", "id-b", "b" * 40, "2026-01-01T00:00:00+00:00", "h") | |
| record(d, a) | |
| record(d, b) | |
| self.assertEqual([r["tag"] for r in json.loads(record_path(d).read_text())], | |
| [b.tag, a.tag]) | |
| self.assertEqual(latest(d), a) | |
| b2 = ImageRecord(b.tag, "id-b2", b.commit, "2026-01-03T00:00:00+00:00", "h") | |
| record(d, b2) | |
| self.assertEqual(len(json.loads(record_path(d).read_text())), 2) | |
| self.assertEqual(lookup(d, b.tag), b2) | |
| self.assertEqual(latest(d), b2) | |
| self.assertIsNone(lookup(d, "pvbench-env:none")) | |
| self.assertEqual(os.listdir(d), ["images.json"]) | |
| def test_main_cli_with_fake_docker(self): | |
| base = ["--repo", str(self.repo), "--binary-dir", str(self.bins), | |
| "--dockerfile", str(self.dockerfile), "--docker", self.fake_docker()] | |
| out = io.StringIO() | |
| with contextlib.redirect_stdout(out): | |
| self.assertEqual(main(base + ["--commit", "with-backend"]), 0) | |
| printed = json.loads(out.getvalue()) | |
| self.assertEqual(printed["commit"], self.sha) | |
| self.assertEqual(lookup(self.dockerdir, printed["tag"]).id, FAKE_ID) | |
| err = io.StringIO() | |
| with contextlib.redirect_stderr(err): | |
| self.assertEqual(main(base + ["--commit", "no-such-branch"]), 1) | |
| self.assertIn("no-such-branch", err.getvalue()) | |
| if __name__ == "__main__": | |
| unittest.main() | |
| class ProxyArgsTests(unittest.TestCase): | |
| def test_proxy_build_args(self): | |
| self.assertEqual(proxy_build_args(None), []) | |
| args = proxy_build_args("http://host.lima.internal:7897") | |
| self.assertEqual(args[:2], ["--build-arg", "HTTP_PROXY=http://host.lima.internal:7897"]) | |
| self.assertEqual(len(args), 8) | |
| self.assertIn("https_proxy=http://host.lima.internal:7897", args) | |