From cac93574ce893e49d540970d8f783efc8dab23cd Mon Sep 17 00:00:00 2001 From: Ranveer Soni Date: Tue, 24 Nov 2026 17:08:18 +0530 Subject: [PATCH] feat(qa): add structured multi-suite test orchestration reports. Records durations, exit codes, ignored/unexecuted suites, CPU/GPU separation, failure excerpts and reproduction commands. Signed-off-by: Ranveer Soni --- scripts/qa/orchestrate.py | 172 +++++++++++++++++++++++++++++++++++ tests/qa/test_orchestrate.py | 59 ++++++++++++ 2 files changed, 231 insertions(+) create mode 100644 scripts/qa/orchestrate.py create mode 100644 tests/qa/test_orchestrate.py diff --git a/scripts/qa/orchestrate.py b/scripts/qa/orchestrate.py new file mode 100644 index 0000000..cecb0a1 --- /dev/null +++ b/scripts/qa/orchestrate.py @@ -0,0 +1,172 @@ +# SPDX-License-Identifier: GPL-3.0-or-later +"""Independent test orchestration with structured JSON reports. + +Runs listed commands, records durations and exit codes, and never promotes a +failed command to passed because output was filtered. +""" +from __future__ import annotations + +import json +import time +from dataclasses import asdict, dataclass, field +from typing import Any, Sequence + +from .exit_pipe import classify_status, run_preserving_status + + +@dataclass(frozen=True) +class SuiteSpec: + name: str + command: tuple[str, ...] + kind: str = "cpu" # cpu | gpu | other + + +@dataclass +class SuiteResult: + name: str + kind: str + command: list[str] + status: str + returncode: int + duration_seconds: float + stdout: str + stderr: str + reproduction: str + + +@dataclass +class OrchestrationReport: + suites: list[SuiteResult] = field(default_factory=list) + started_at: float = 0.0 + finished_at: float = 0.0 + + @property + def passed(self) -> int: + return sum(1 for s in self.suites if s.status == "passed") + + @property + def failed(self) -> int: + return sum(1 for s in self.suites if s.status == "failed") + + @property + def ignored(self) -> int: + return sum(1 for s in self.suites if s.status == "ignored") + + @property + def unexecuted(self) -> int: + return sum(1 for s in self.suites if s.status == "unexecuted") + + def overall_returncode(self) -> int: + codes = [s.returncode for s in self.suites if s.status == "failed"] + if not codes: + return 0 + return codes[0] if codes[0] != 0 else 1 + + def to_dict(self) -> dict[str, Any]: + return { + "started_at": self.started_at, + "finished_at": self.finished_at, + "duration_seconds": self.finished_at - self.started_at, + "counts": { + "passed": self.passed, + "failed": self.failed, + "ignored": self.ignored, + "unexecuted": self.unexecuted, + }, + "suites": [asdict(s) for s in self.suites], + "overall_returncode": self.overall_returncode(), + } + + +def shell_quote(part: str) -> str: + if part == "": + return "''" + if all(c.isalnum() or c in "@%_+:=,./-" for c in part): + return part + return "'" + part.replace("'", "'\"'\"'") + "'" + + +def run_suites( + specs: Sequence[SuiteSpec], + *, + stop_on_fail: bool = False, +) -> OrchestrationReport: + report = OrchestrationReport(started_at=time.time()) + halted = False + for spec in specs: + if halted: + report.suites.append( + SuiteResult( + name=spec.name, + kind=spec.kind, + command=list(spec.command), + status="unexecuted", + returncode=-1, + duration_seconds=0.0, + stdout="", + stderr="not executed because a previous suite failed", + reproduction=" ".join(shell_quote(p) for p in spec.command), + ) + ) + continue + if spec.kind == "ignored": + report.suites.append( + SuiteResult( + name=spec.name, + kind=spec.kind, + command=list(spec.command), + status="ignored", + returncode=0, + duration_seconds=0.0, + stdout="", + stderr="suite marked ignored", + reproduction=" ".join(shell_quote(p) for p in spec.command), + ) + ) + continue + started = time.time() + result = run_preserving_status(spec.command) + finished = time.time() + status = classify_status(result.returncode) + report.suites.append( + SuiteResult( + name=spec.name, + kind=spec.kind, + command=list(spec.command), + status=status, + returncode=result.returncode, + duration_seconds=finished - started, + stdout=result.stdout, + stderr=result.stderr, + reproduction=" ".join(shell_quote(p) for p in spec.command), + ) + ) + if stop_on_fail and status == "failed": + halted = True + report.finished_at = time.time() + return report + + +def dumps_report(report: OrchestrationReport) -> str: + return json.dumps(report.to_dict(), indent=2, sort_keys=True) + "\n" + + +def failure_log_excerpt(report: OrchestrationReport, *, limit: int = 40) -> str: + lines: list[str] = [] + for suite in report.suites: + if suite.status != "failed": + continue + lines.append(f"## {suite.name} (exit {suite.returncode})") + lines.append(f"reproduce: {suite.reproduction}") + err = (suite.stderr or suite.stdout).splitlines() + lines.extend(err[:limit]) + lines.append("") + return "\n".join(lines) + + +def separate_cpu_gpu(report: OrchestrationReport) -> dict[str, list[SuiteResult]]: + out = {"cpu": [], "gpu": [], "other": []} + for suite in report.suites: + bucket = suite.kind if suite.kind in out else "other" + out[bucket].append(suite) + return out diff --git a/tests/qa/test_orchestrate.py b/tests/qa/test_orchestrate.py new file mode 100644 index 0000000..da58225 --- /dev/null +++ b/tests/qa/test_orchestrate.py @@ -0,0 +1,59 @@ +# SPDX-License-Identifier: GPL-3.0-or-later +import json +import unittest + +from qa.orchestrate import ( + SuiteSpec, + dumps_report, + failure_log_excerpt, + run_suites, + separate_cpu_gpu, +) + + +class Orchestrate(unittest.TestCase): + def test_records_pass_and_fail_with_durations(self): + report = run_suites([ + SuiteSpec("ok", ("python3", "-c", "print(1)"), "cpu"), + SuiteSpec("bad", ("python3", "-c", "import sys; sys.exit(2)"), "cpu"), + ]) + self.assertEqual(report.passed, 1) + self.assertEqual(report.failed, 1) + self.assertGreaterEqual(report.suites[0].duration_seconds, 0.0) + self.assertEqual(report.overall_returncode(), 2) + payload = json.loads(dumps_report(report)) + self.assertEqual(payload["counts"]["failed"], 1) + + def test_stop_on_fail_marks_unexecuted(self): + report = run_suites( + [ + SuiteSpec("bad", ("python3", "-c", "import sys; sys.exit(5)"), "cpu"), + SuiteSpec("later", ("python3", "-c", "print('nope')"), "gpu"), + ], + stop_on_fail=True, + ) + self.assertEqual(report.suites[1].status, "unexecuted") + self.assertEqual(report.unexecuted, 1) + + def test_ignored_and_cpu_gpu_split(self): + report = run_suites([ + SuiteSpec("skip", ("true",), "ignored"), + SuiteSpec("cpu", ("python3", "-c", "print('c')"), "cpu"), + SuiteSpec("gpu", ("python3", "-c", "print('g')"), "gpu"), + ]) + self.assertEqual(report.ignored, 1) + buckets = separate_cpu_gpu(report) + self.assertEqual(len(buckets["cpu"]), 1) + self.assertEqual(len(buckets["gpu"]), 1) + + def test_failure_excerpt_includes_reproduction(self): + report = run_suites([ + SuiteSpec("bad", ("python3", "-c", "import sys; sys.stderr.write('boom\\n'); sys.exit(1)"), "cpu"), + ]) + text = failure_log_excerpt(report) + self.assertIn("reproduce:", text) + self.assertIn("boom", text) + + +if __name__ == "__main__": + unittest.main()