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