team-ljyzipelo

공개
125줄6.1 KB
1import gzip
2import importlib.util
3import io
4import os
5import subprocess
6import sys
7from pathlib import Path
8import tarfile
9import tempfile
10import unittest
11from unittest.mock import patch
12import zipfile
13
14
15import run_rust
16
17ROOT = Path(__file__).resolve().parents[2]
18SPEC = importlib.util.spec_from_file_location(
19 "large_fixture_generator", ROOT / "tests/large-archive-fixtures/generate.py"
20)
21GENERATOR = importlib.util.module_from_spec(SPEC)
22SPEC.loader.exec_module(GENERATOR)
23
24
25class LargeArchiveFixtureTests(unittest.TestCase):
26 def test_standalone_generator_uses_common_work_directory(self):
27 expected = run_rust.test_work_directory(ROOT)
28 with tempfile.TemporaryDirectory(dir=expected) as scratch:
29 relative = Path(scratch).name + "/corpus"
30 environment = dict(os.environ, ZIPELO_CI_JOB=expected.name)
31 environment.pop("ZIPELO_TEST_WORK_DIR", None)
32 result = subprocess.run(
33 ["python3", str(ROOT / "tests/large-archive-fixtures/generate.py"),
34 "--output", relative],
35 cwd=ROOT, env=environment, capture_output=True, text=True, check=True,
36 )
37 self.assertTrue((Path(scratch) / "corpus/expectations.json").is_file())
38 self.assertIn("11 cases", result.stdout)
39
40 def test_standalone_verifier_uses_work_target(self):
41 job = run_rust.test_work_directory(ROOT).name
42 with patch.dict(os.environ, {"ZIPELO_CI_JOB": job}), patch.dict(sys.modules, {"generate": GENERATOR}):
43 os.environ.pop("ZIPELO_TEST_WORK_DIR", None)
44 os.environ.pop("CARGO_TARGET_DIR", None)
45 spec = importlib.util.spec_from_file_location(
46 "large_fixture_verifier", ROOT / "tests/large-archive-fixtures/verify.py"
47 )
48 verifier = importlib.util.module_from_spec(spec)
49 spec.loader.exec_module(verifier)
50 self.assertEqual(verifier.default_target(), GENERATOR.work_root() / "target/debug")
51
52 def test_boundary_archives_are_valid_and_expectations_straddle_limit(self):
53 with tempfile.TemporaryDirectory(dir=run_rust.test_work_directory(ROOT)) as scratch:
54 directory = Path(scratch) / "corpus"
55 manifest = GENERATOR.build(directory)
56 self.assertEqual(len(manifest["cases"]), 11)
57 self.assertFalse(manifest["worker_execution_verified"])
58 for name in ("boundary.zip", "boundary.tar.gz"):
59 artifact = manifest["artifacts"][name]
60 path = directory / name
61 self.assertEqual(GENERATOR.digest(path), artifact["sha256"])
62 if name.endswith(".zip"):
63 with zipfile.ZipFile(path) as archive:
64 self.assertIsNone(archive.testzip())
65 payload = archive.read(GENERATOR.NAME)
66 decoded = len(payload)
67 else:
68 data = gzip.decompress(path.read_bytes())
69 decoded = len(data)
70 with tarfile.open(fileobj=io.BytesIO(data), mode="r:") as archive:
71 payload = archive.extractfile(GENERATOR.NAME).read()
72 self.assertEqual(payload, bytes(artifact["payload_bytes"]))
73 self.assertEqual(decoded, artifact["decoded_bytes"])
74 self.assertEqual(decoded, 65536)
75 self.assertGreater(decoded / artifact["archive_bytes"], 100)
76 for case in manifest["cases"]:
77 decoded = manifest["artifacts"][case["file"]]["decoded_bytes"]
78 exceeded = GENERATOR.violations(manifest["artifacts"][case["file"]], case["limits"])
79 self.assertEqual(exceeded, case["violated_limits"])
80 self.assertLessEqual(len(exceeded), 1)
81 self.assertEqual(case["limits"]["input"], 2 ** 64 - 1)
82 failed = bool(exceeded)
83 self.assertEqual(case["expected"]["error"], "Limit" if failed else "None")
84 self.assertEqual(case["expected"]["created_count"], 0 if failed else 1)
85
86 def test_former_ratio_samples_all_expect_success(self):
87 with tempfile.TemporaryDirectory(dir=run_rust.test_work_directory(ROOT)) as scratch:
88 manifest = GENERATOR.build(Path(scratch) / "corpus")
89 for suffix, delta in (("below", -1), ("equal", 0), ("above", 1)):
90 name = "ratio-" + suffix + ".gz"
91 artifact = manifest["artifacts"][name]
92 self.assertEqual(artifact["decoded_bytes"] - 1000 * artifact["ratio_input_bytes"], delta)
93 self.assertLess(artifact["decoded_bytes"], GENERATOR.OUTPUT_LIMIT)
94 case = next(c for c in manifest["cases"] if c["file"] == name)
95 self.assertEqual(case["expected"]["outcome"], "Completed")
96 self.assertIsNone(manifest["policy"]["ratio"])
97 payload = gzip.decompress((Path(scratch) / "corpus" / name).read_bytes())
98 self.assertEqual(payload, bytes(artifact["payload_bytes"]))
99
100 def test_generator_rejects_existing_destination_without_changes(self):
101 with tempfile.TemporaryDirectory(dir=run_rust.test_work_directory(ROOT)) as scratch:
102 directory = Path(scratch)
103 marker = directory / "keep"
104 marker.write_bytes(b"preserve")
105 with self.assertRaises(FileExistsError):
106 GENERATOR.build(directory)
107 self.assertEqual(list(directory.iterdir()), [marker])
108 self.assertEqual(marker.read_bytes(), b"preserve")
109
110 def test_generator_requires_project_test_directory(self):
111 with self.assertRaises(ValueError):
112 GENERATOR.build(run_rust.test_work_directory(ROOT).parent / "outside-tests")
113
114 def test_payload_source_enforces_bounded_reads(self):
115 source = GENERATOR.ZeroStream(GENERATOR.GIB)
116 with self.assertRaises(ValueError):
117 source.read()
118 with self.assertRaises(ValueError):
119 source.read(GENERATOR.CHUNK + 1)
120 self.assertEqual(source.read(GENERATOR.CHUNK), bytes(GENERATOR.CHUNK))
121 self.assertEqual(source.remaining, GENERATOR.GIB - GENERATOR.CHUNK)
122
123
124if __name__ == "__main__":
125 unittest.main()