team-ljyzipelo

공개
94줄4.6 KB
1import gzip
2import importlib.util
3import io
4import os
5from pathlib import Path
6import tarfile
7import tempfile
8import unittest
9import zipfile
10
11
12ROOT = Path(__file__).resolve().parents[2]
13SPEC = importlib.util.spec_from_file_location(
14 "large_fixture_generator", ROOT / "tests/large-archive-fixtures/generate.py"
15)
16GENERATOR = importlib.util.module_from_spec(SPEC)
17SPEC.loader.exec_module(GENERATOR)
18
19
20class LargeArchiveFixtureTests(unittest.TestCase):
21 def test_boundary_archives_are_valid_and_expectations_straddle_limit(self):
22 with tempfile.TemporaryDirectory(dir=os.environ["ZIPELO_TEST_WORK_DIR"]) as scratch:
23 directory = Path(scratch) / "corpus"
24 manifest = GENERATOR.build(directory)
25 self.assertEqual(len(manifest["cases"]), 11)
26 self.assertFalse(manifest["worker_execution_verified"])
27 for name in ("boundary.zip", "boundary.tar.gz"):
28 artifact = manifest["artifacts"][name]
29 path = directory / name
30 self.assertEqual(GENERATOR.digest(path), artifact["sha256"])
31 if name.endswith(".zip"):
32 with zipfile.ZipFile(path) as archive:
33 self.assertIsNone(archive.testzip())
34 payload = archive.read(GENERATOR.NAME)
35 decoded = len(payload)
36 else:
37 data = gzip.decompress(path.read_bytes())
38 decoded = len(data)
39 with tarfile.open(fileobj=io.BytesIO(data), mode="r:") as archive:
40 payload = archive.extractfile(GENERATOR.NAME).read()
41 self.assertEqual(payload, bytes(artifact["payload_bytes"]))
42 self.assertEqual(decoded, artifact["decoded_bytes"])
43 self.assertEqual(decoded, 65536)
44 self.assertGreater(decoded / artifact["archive_bytes"], 100)
45 for case in manifest["cases"]:
46 decoded = manifest["artifacts"][case["file"]]["decoded_bytes"]
47 exceeded = GENERATOR.violations(manifest["artifacts"][case["file"]], case["limits"])
48 self.assertEqual(exceeded, case["violated_limits"])
49 self.assertLessEqual(len(exceeded), 1)
50 self.assertEqual(case["limits"]["input"], 2 ** 64 - 1)
51 failed = bool(exceeded)
52 self.assertEqual(case["expected"]["error"], "Limit" if failed else "None")
53 self.assertEqual(case["expected"]["created_count"], 0 if failed else 1)
54
55 def test_former_ratio_samples_all_expect_success(self):
56 with tempfile.TemporaryDirectory(dir=os.environ["ZIPELO_TEST_WORK_DIR"]) as scratch:
57 manifest = GENERATOR.build(Path(scratch) / "corpus")
58 for suffix, delta in (("below", -1), ("equal", 0), ("above", 1)):
59 name = "ratio-" + suffix + ".gz"
60 artifact = manifest["artifacts"][name]
61 self.assertEqual(artifact["decoded_bytes"] - 1000 * artifact["ratio_input_bytes"], delta)
62 self.assertLess(artifact["decoded_bytes"], GENERATOR.OUTPUT_LIMIT)
63 case = next(c for c in manifest["cases"] if c["file"] == name)
64 self.assertEqual(case["expected"]["outcome"], "Completed")
65 self.assertIsNone(manifest["policy"]["ratio"])
66 payload = gzip.decompress((Path(scratch) / "corpus" / name).read_bytes())
67 self.assertEqual(payload, bytes(artifact["payload_bytes"]))
68
69 def test_generator_rejects_existing_destination_without_changes(self):
70 with tempfile.TemporaryDirectory(dir=os.environ["ZIPELO_TEST_WORK_DIR"]) as scratch:
71 directory = Path(scratch)
72 marker = directory / "keep"
73 marker.write_bytes(b"preserve")
74 with self.assertRaises(FileExistsError):
75 GENERATOR.build(directory)
76 self.assertEqual(list(directory.iterdir()), [marker])
77 self.assertEqual(marker.read_bytes(), b"preserve")
78
79 def test_generator_requires_project_test_directory(self):
80 with self.assertRaises(ValueError):
81 GENERATOR.build(Path(os.environ["ZIPELO_TEST_WORK_DIR"]).parent / "outside-tests")
82
83 def test_payload_source_enforces_bounded_reads(self):
84 source = GENERATOR.ZeroStream(GENERATOR.GIB)
85 with self.assertRaises(ValueError):
86 source.read()
87 with self.assertRaises(ValueError):
88 source.read(GENERATOR.CHUNK + 1)
89 self.assertEqual(source.read(GENERATOR.CHUNK), bytes(GENERATOR.CHUNK))
90 self.assertEqual(source.remaining, GENERATOR.GIB - GENERATOR.CHUNK)
91
92
93if __name__ == "__main__":
94 unittest.main()