#!/usr/bin/env python3 """Print `go test -json` like plain `go test` and record per-test seconds. Usage: go test -json ... | record-internal-api-test-seconds.py \ [ ] `go test -json` reports every test the way `-v` does, which for internal/api means thousands of lines per shard. This filter keeps the log as readable as plain `go test`: package lines (build errors, `ok`, `FAIL`, panics outside a test) print straight away, and a test's own output is held until the test ends, printed if it failed and dropped if it passed or was skipped. Output of a test that never finished (a panic or timeout killed the binary) is printed at the end, so a crash is never hidden. Every finished top-level test is written to as ` `, after a `# package-seconds ` comment holding the whole test binary's run time, the same shape as .github/scripts/internal-api-test-seconds.txt, so refresh-internal-api-test-seconds.py can rebuild the shard weights from CI. A bounded .failures.json sidecar names failed and unfinished source-level tests without test output or parameterised subtest labels. The existing timing-artifact upload makes it readable as soon as the shard ends, even while another workflow job is still running. It is an observation index, not a passing verdict: absent package completion and omitted names are explicit. The recorder also requires one completed package and complete, non-repeated top-level test executions. CI supplies its source-derived selected test list and package identity: every selected test must run and finish exactly once, with no other test or package substituted. Individual test skips are explicit completed executions, not missing coverage. The sidecar distinguishes fully skipped top-level tests from parents with skipped subtests, exporting only bounded source identifiers and aggregate child-event counts, never skip reasons or subtest labels. Empty, malformed and truncated streams fail closed; their original observations remain in the sidecar. The exit status is non-zero when any test or package failed, but the caller must still run with `set -o pipefail` so a `go test` failure that produced no JSON (for example a vet or build error) fails the step. """ from __future__ import annotations from datetime import datetime import json from pathlib import Path import re import shutil import sys import tempfile from collections import defaultdict MAX_FAILURE_IDENTIFIERS = 256 MAX_IDENTIFIER_LENGTH = 256 SOURCE_TEST_NAME = re.compile(r"(?:Test|Fuzz|Example)[A-Za-z0-9_]*") TIMESTAMP_FRACTION = re.compile(r"(\.\d+)(?=[+-]\d\d:\d\d$)") # Retain diagnostics, not an output-sized Python heap. Short roots stay in # memory; noisy roots spill to an anonymous, private temporary file. This is # a storage threshold, never an output limit or a different coverage verdict. HELD_OUTPUT_MEMORY_BYTES = 64 * 1024 def parse_event_time(value: object) -> float | None: """Parse Go's RFC3339Nano test event timestamp, ignoring absent/invalid values.""" if not isinstance(value, str) or not value: return None text = value[:-1] + "+00:00" if value.endswith("Z") else value text = TIMESTAMP_FRACTION.sub(lambda match: match.group(1)[:7], text, count=1) try: return datetime.fromisoformat(text).timestamp() except ValueError: return None def bounded_identifiers(names: set[str]) -> dict[str, object]: """Never export output, subtest parameters or unsafe source names.""" safe = sorted(name for name in names if len(name) <= MAX_IDENTIFIER_LENGTH and SOURCE_TEST_NAME.fullmatch(name)) retained = safe[:MAX_FAILURE_IDENTIFIERS] return {"names": retained, "observed_count": len(names), "omitted_count": len(names) - len(retained)} def main() -> int: if len(sys.argv) not in (2, 4): print(f"usage: {sys.argv[0]} [ ]", file=sys.stderr) return 2 expected = None expected_package = None if len(sys.argv) == 4: try: names = Path(sys.argv[2]).read_text(encoding="utf-8").splitlines() except (OSError, UnicodeError): print("API shard selection is unavailable; no coverage verdict", file=sys.stderr) return 2 # These are source-level identifiers from go test -list, never # parameterised subtest labels. Valid Unicode Go names are not subject # to the narrower ASCII export allowlist used by the bounded sidecar. if (not names or len(set(names)) != len(names) or any(not name.isidentifier() or not name.startswith(("Test", "Fuzz", "Example")) for name in names) or not sys.argv[3]): print("API shard selection is invalid; no coverage verdict", file=sys.stderr) return 2 expected = set(names) expected_package = sys.argv[3] held = defaultdict(lambda: tempfile.SpooledTemporaryFile( max_size=HELD_OUTPUT_MEMORY_BYTES, mode="w+", encoding="utf-8", newline="")) def finish_output(top: str, *, show: bool = False) -> None: buffer = held.pop(top, None) if buffer is not None: with buffer: if show: buffer.seek(0) shutil.copyfileobj(buffer, sys.stdout, length=HELD_OUTPUT_MEMORY_BYTES) running: set[str] = set() seconds: dict[str, float] = {} # Go reports a parent test's Elapsed before waiting for parallel subtests, # leaving parents that spawn t.Parallel() subtests near 0.00s even when # their subtests consume tens of seconds of package wall time. Track active # leaf execution slots across event timestamps so those roots retain their # fractional wall-clock cost without double-counting concurrent roots. active_parents: set[str] = set() active_children: dict[str, set[str]] = defaultdict(set) live_children: dict[str, set[str]] = defaultdict(set) has_paused_child: set[str] = set() slot_seconds: dict[str, float] = defaultdict(float) last_event_time: float | None = None package_seconds = None failed = False failed_tests: set[str] = set() skipped_tests: set[str] = set() parents_with_skipped_subtests: set[str] = set() skipped_subtest_events = 0 package_terminal_action = None package_terminal_count = 0 non_json_lines = 0 packages: set[str] = set() started: set[str] = set() completed: set[str] = set() invalid_events = 0 unexpected_package_events = 0 out = sys.stdout def advance_active_slots(timestamp: float | None) -> None: nonlocal last_event_time if timestamp is None: return if last_event_time is not None and timestamp > last_event_time: dt = timestamp - last_event_time weights: dict[str, int] = {} for root in active_parents | active_children.keys(): active = active_children.get(root) live = live_children.get(root) leaf_count = 0 if active: leaf_count = sum( 1 for child in active if not live or not any(other.startswith(child + "/") for other in live) ) if root in has_paused_child and leaf_count > 0: weights[root] = leaf_count elif root in active_parents and root not in has_paused_child: weights[root] = 1 elif leaf_count > 0: weights[root] = leaf_count total_weight = sum(weights.values()) if total_weight > 0: for root, weight in weights.items(): slot_seconds[root] += dt * (weight / total_weight) if last_event_time is None or timestamp > last_event_time: last_event_time = timestamp for raw in sys.stdin: try: event = json.loads(raw) except ValueError: event = None if not isinstance(event, dict): non_json_lines += 1 out.write(raw) out.flush() continue action = event.get("Action") test = event.get("Test", "") text = event.get("Output", "") # Recent Go versions send compiler/vet diagnostics with ImportPath, # not Package. Keep those real diagnostics visible without confusing # a dependency's build event with the selected package's test result. if action in ("build-output", "build-fail") and isinstance(text, str): out.write(text) out.flush() if action == "build-fail": failed = True if package_terminal_count: invalid_events += 1 continue package = event.get("Package") if not isinstance(package, str) or not package: invalid_events += 1 else: packages.add(package) if expected_package is not None and package != expected_package: unexpected_package_events += 1 # No new event may follow this single package's terminal verdict. # Keep the stream's observations rather than overwriting that history. if package_terminal_count: invalid_events += 1 if (action not in ("start", "run", "pause", "cont", "pass", "bench", "fail", "output", "skip") or not isinstance(test, str) or not isinstance(text, str)): invalid_events += 1 if isinstance(text, str): out.write(text) out.flush() continue advance_active_slots(parse_event_time(event.get("Time"))) top = test.split("/", 1)[0] if not top: # Package level: build output, `ok`/`FAIL` summary, panics and # timeouts reported outside a test. if text: out.write(text) out.flush() if action in ("pass", "fail", "skip"): package_terminal_action = action package_terminal_count += 1 if action == "fail": failed = True if action in ("pass", "fail") and isinstance(event.get("Elapsed"), (int, float)): package_seconds = float(event["Elapsed"]) continue # Retain explicit skip observations even if the package later fails # or never finishes. A passing parent can contain unavailable native # cases, so top-level completion alone is not exercised-case evidence. # Count child events without ever retaining their parameterised labels. if action == "skip": if test == top: skipped_tests.add(top) else: parents_with_skipped_subtests.add(top) skipped_subtest_events += 1 if test == top: if action == "run": if top in started: invalid_events += 1 started.add(top) running.add(top) active_parents.add(top) elif action == "pause": active_parents.discard(top) elif action == "cont": active_parents.add(top) else: if action == "run": active_children[top].add(test) live_children[top].add(test) elif action == "pause": has_paused_child.add(top) active_children[top].discard(test) elif action == "cont": active_children[top].add(test) live_children[top].add(test) elif action in ("pass", "fail", "skip"): active_children[top].discard(test) live_children[top].discard(test) if not active_children[top]: active_children.pop(top, None) if not live_children[top]: live_children.pop(top, None) if text: held[top].write(text) if test != top or action not in ("pass", "fail", "skip"): continue if top not in running or top in completed: invalid_events += 1 running.discard(top) active_parents.discard(top) active_children.pop(top, None) live_children.pop(top, None) completed.add(top) finish_output(top, show=action == "fail") if action == "fail": failed = True failed_tests.add(top) out.flush() elapsed = event.get("Elapsed") if isinstance(elapsed, (int, float)): recorded = float(elapsed) if top in has_paused_child and slot_seconds.get(top, 0.0) > recorded: recorded = slot_seconds[top] seconds[top] = recorded if package_seconds is not None and package_seconds > 0: for top in has_paused_child: if top in seconds and seconds[top] > package_seconds: seconds[top] = package_seconds for top in sorted(running): failed = True out.write(f"--- {top} did not finish; its output follows\n") finish_output(top, show=True) # Malformed streams can contain output without a root start. They still # fail below; do not leave their temporary buffers open until GC. for top in list(held): finish_output(top) out.flush() missing = expected - completed if expected is not None else set() unexpected = (started | completed) - expected if expected is not None else set() if (package_terminal_action != "pass" or package_terminal_count != 1 or len(packages) != 1 or not completed or started != completed or non_json_lines or invalid_events or unexpected_package_events or missing or unexpected): failed = True # Fixed diagnostic only: the sidecar retains bounded source identities # and counts, not package names or private event payloads. print("API shard execution evidence is incomplete or inconsistent; no passing verdict", file=sys.stderr) with open(sys.argv[1], "w", encoding="utf-8") as handle: if package_seconds is not None: handle.write(f"# package-seconds {package_seconds:.2f}\n") for name, value in seconds.items(): handle.write(f"{name} {value:.2f}\n") # Upload alongside the unchanged timing text; never include raw output or # parameterised subtest names. The CI artifact supplies run/source binding. index = { "schema_version": 1, "recorder_exit_code": 1 if failed else 0, "failed_top_level_tests": bounded_identifiers(failed_tests), "unfinished_top_level_tests": bounded_identifiers(running), "skipped_top_level_tests": bounded_identifiers(skipped_tests), "top_level_tests_with_skipped_subtests": bounded_identifiers(parents_with_skipped_subtests), "skipped_subtest_event_count": skipped_subtest_events, # Only the final package action matters; retain contrary earlier # actions as a count instead of exporting an unbounded event stream. "package_terminal_action": package_terminal_action, "package_terminal_count": package_terminal_count, "non_json_line_count": non_json_lines, "invalid_event_count": invalid_events, "package_identity_count": len(packages), "unexpected_package_event_count": unexpected_package_events, "selected_test_count": len(expected) if expected is not None else None, "completed_top_level_test_count": len(completed), "missing_selected_tests": bounded_identifiers(missing), "unexpected_top_level_tests": bounded_identifiers(unexpected), } with open(sys.argv[1] + ".failures.json", "w", encoding="utf-8") as handle: json.dump(index, handle, indent=2, sort_keys=True) handle.write("\n") return 1 if failed else 0 if __name__ == "__main__": raise SystemExit(main())