|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 4 | +# or more contributor license agreements. See the NOTICE file |
| 5 | +# distributed with this work for additional information |
| 6 | +# regarding copyright ownership. The ASF licenses this file |
| 7 | +# to you under the Apache License, Version 2.0 (the |
| 8 | +# "License"); you may not use this file except in compliance |
| 9 | +# with the License. You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, |
| 14 | +# software distributed under the License is distributed on an |
| 15 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 | +# KIND, either express or implied. See the License for the |
| 17 | +# specific language governing permissions and limitations |
| 18 | +# under the License. |
| 19 | + |
| 20 | +"""Fast regression tests for the Iceberg write report summary.""" |
| 21 | + |
| 22 | +import importlib.util |
| 23 | +import json |
| 24 | +from pathlib import Path |
| 25 | +import tempfile |
| 26 | +import unittest |
| 27 | + |
| 28 | + |
| 29 | +SPEC = importlib.util.spec_from_file_location( |
| 30 | + "summarize_iceberg_writes", Path(__file__).with_name("summarize-iceberg-writes.py")) |
| 31 | +SUMMARIZE = importlib.util.module_from_spec(SPEC) |
| 32 | +SPEC.loader.exec_module(SUMMARIZE) |
| 33 | + |
| 34 | +ARTIFACT = "iceberg-spark-1.11.0-spark-4.1.3-scala-2.13-jdk17-shard-{}-attempt-{}" |
| 35 | + |
| 36 | + |
| 37 | +def records(*writers): |
| 38 | + return "".join( |
| 39 | + json.dumps({"writer": w, "node": "AppendData", "reasons": [], "failed": False}) + "\n" |
| 40 | + for w in writers) |
| 41 | + |
| 42 | + |
| 43 | +class SummarizeIcebergWritesTest(unittest.TestCase): |
| 44 | + def setUp(self): |
| 45 | + self.temp = tempfile.TemporaryDirectory(prefix="comet-iceberg-writes-test-") |
| 46 | + self.addCleanup(self.temp.cleanup) |
| 47 | + self.root = Path(self.temp.name) |
| 48 | + |
| 49 | + def attempt(self, shard, attempt, *writers): |
| 50 | + """A shard attempt's artifact as the coverage job downloads it. |
| 51 | +
|
| 52 | + Every attempt uploads its test reports. Only an attempt that recorded writes has a |
| 53 | + report file. |
| 54 | + """ |
| 55 | + artifact = self.root / ARTIFACT.format(shard, attempt) |
| 56 | + reports = artifact / "build/test-results/test" |
| 57 | + reports.mkdir(parents=True) |
| 58 | + (reports / "TEST-org.example.TestFixture.xml").write_text("<testsuite/>") |
| 59 | + if writers: |
| 60 | + writes = artifact / "build/comet-iceberg-writes" |
| 61 | + writes.mkdir(parents=True) |
| 62 | + (writes / "iceberg-writes-fixture.jsonl").write_text(records(*writers)) |
| 63 | + |
| 64 | + def load(self): |
| 65 | + writes, missing = SUMMARIZE.load([self.root]) |
| 66 | + return sorted(w["writer"] for w in writes), missing |
| 67 | + |
| 68 | + def test_every_shard_counts_once(self): |
| 69 | + self.attempt(1, 1, "native") |
| 70 | + self.attempt(2, 1, "jvm", "spark") |
| 71 | + self.assertEqual(self.load(), (["jvm", "native", "spark"], [])) |
| 72 | + |
| 73 | + def test_latest_attempt_replaces_an_earlier_one(self): |
| 74 | + self.attempt(1, 1, "native", "native") |
| 75 | + self.attempt(1, 2, "jvm") |
| 76 | + self.assertEqual(self.load(), (["jvm"], [])) |
| 77 | + |
| 78 | + def test_retry_without_writes_is_reported_missing_instead_of_stale(self): |
| 79 | + self.attempt(1, 1, "native") |
| 80 | + self.attempt(1, 2) |
| 81 | + self.attempt(2, 1, "spark") |
| 82 | + self.assertEqual(self.load(), (["spark"], [(1, 2)])) |
| 83 | + summary = SUMMARIZE.summarize("fixture", *SUMMARIZE.load([self.root])) |
| 84 | + self.assertIn("Shard 1 recorded no Iceberg writes in its latest attempt (2)", summary) |
| 85 | + self.assertIn("| Comet native writer | 0 | 0.0% |", summary) |
| 86 | + self.assertIn("| Spark V2 write, not planned by Comet's split operator | 1 | 100.0% |", |
| 87 | + summary) |
| 88 | + |
| 89 | + def test_root_that_is_itself_a_shard_artifact(self): |
| 90 | + self.attempt(1, 3) |
| 91 | + writes, missing = SUMMARIZE.load([self.root / ARTIFACT.format(1, 3)]) |
| 92 | + self.assertEqual((writes, missing), ([], [(1, 3)])) |
| 93 | + |
| 94 | + def test_files_outside_shard_artifacts_always_count(self): |
| 95 | + # A shard job and dev/local-ci.sh summarize their own report directory directly. |
| 96 | + (self.root / "iceberg-writes-local.jsonl").write_text(records("native", "jvm")) |
| 97 | + self.assertEqual(self.load(), (["jvm", "native"], [])) |
| 98 | + |
| 99 | + def test_no_writes_at_all(self): |
| 100 | + summary = SUMMARIZE.summarize("fixture", *SUMMARIZE.load([self.root / "missing"])) |
| 101 | + self.assertIn("No Iceberg writes were recorded", summary) |
| 102 | + |
| 103 | + |
| 104 | +if __name__ == "__main__": |
| 105 | + unittest.main() |
0 commit comments