diff --git a/scripts/private-ci-diagnostics.py b/scripts/private-ci-diagnostics.py new file mode 100644 index 0000000..d0cb0b8 --- /dev/null +++ b/scripts/private-ci-diagnostics.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python3 +"""Report only allowlisted metadata from a private Rust command log.""" + +import hashlib +from pathlib import Path +import re +import sys + + +def report_compile(output): + codes = sorted(set(re.findall(r'error\[E[0-9]{4}\]', output))) + if codes: + print(f'::error::Rust diagnostic codes: {" ".join(codes)}.') + categories = [ + ('No space left on device', 'insufficient disk space'), + (r'signal: 9, SIGKILL|ld terminated with signal 9|Killed signal terminated program', + 'compiler or linker killed by SIGKILL'), + (r'undefined reference to|undefined symbol:', 'unresolved linker symbols'), + ('proc macro panicked', 'procedural macro failed'), + ] + for pattern, description in categories: + if re.search(pattern, output): + print(f'::error::Rust compilation: {description}.') + + +def report_tests(output): + names = set(re.findall(r'^test (.+?) \.\.\. FAILED\r?$', output, re.MULTILINE)) + for fingerprint in sorted(hashlib.sha256(name.encode()).hexdigest() for name in names): + print(f'::error::Rust test failure fingerprint: {fingerprint}') + + +def main(): + if len(sys.argv) != 3 or sys.argv[1] not in ('compile', 'tests'): + print('Usage: private-ci-diagnostics.py compile|tests LOGFILE', file=sys.stderr) + return 2 + try: + output = Path(sys.argv[2]).read_text(errors='replace') + except OSError: + print('Private Rust diagnostic log is unavailable.', file=sys.stderr) + return 1 + if sys.argv[1] == 'compile': + report_compile(output) + else: + report_tests(output) + return 0 + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/scripts/test-private-desktop.sh b/scripts/test-private-desktop.sh index da475ac..01808c9 100644 --- a/scripts/test-private-desktop.sh +++ b/scripts/test-private-desktop.sh @@ -1,19 +1,26 @@ #!/usr/bin/env bash -# Emit only host-owned stage names; never forward private command output. +# Never forward private command output; report stages and allowlisted diagnostics. set -euo pipefail source_directory=$1 container="gp-private-tests-$$" -trap 'timeout --kill-after=5s 30s docker rm --force "$container" >/dev/null 2>&1 || true' EXIT +private_output=$(mktemp) +script_directory=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd) +trap 'timeout --kill-after=5s 30s docker rm --force "$container" >/dev/null 2>&1 || true; unlink "$private_output"' EXIT run_stage() { local name=$1 limit=$2 status started=$SECONDS shift 2 echo "$name: started (limit $limit)." - if timeout --kill-after=30s "$limit" "$@" >/dev/null 2>&1; then + if timeout --kill-after=30s "$limit" "$@" >"$private_output" 2>&1; then echo "$name: passed ($((SECONDS - started))s)." else status=$? + if [[ "$name" == 'Compile Rust tests' ]]; then + python3 "$script_directory/private-ci-diagnostics.py" compile "$private_output" || true + elif [[ "$name" == 'Rust tests' ]]; then + python3 "$script_directory/private-ci-diagnostics.py" tests "$private_output" || true + fi if [[ "$status" == 124 ]]; then echo "::error::$name: timed out after $((SECONDS - started))s (limit $limit); diagnostics suppressed." elif [[ "$status" == 137 ]]; then diff --git a/scripts/tests/test_private_ci.py b/scripts/tests/test_private_ci.py index f09dc66..4706fcd 100644 --- a/scripts/tests/test_private_ci.py +++ b/scripts/tests/test_private_ci.py @@ -1,3 +1,4 @@ +import hashlib import json import os import plistlib @@ -140,10 +141,23 @@ class PrivateTestStageTests(unittest.TestCase): output.write(json.dumps(sys.argv[1:]) + '\\n') print('PRIVATE_STDOUT') print('PRIVATE_STDERR', file=sys.stderr) + if 'cargo' in sys.argv and '--no-run' in sys.argv: + diagnostics = { + 'compile-type': 'error[E0061]: PRIVATE_SOURCE\\nerror[E0308]: PRIVATE_SOURCE\\nerror[E0061]: PRIVATE_SOURCE', + 'compile-disk': 'PRIVATE_PATH: No space left on device', + 'compile-killed': 'PRIVATE_SOURCE (signal: 9, SIGKILL: kill)', + 'compile-link': 'PRIVATE_PATH: undefined reference to PRIVATE_SYMBOL', + 'compile-macro': 'error: proc macro panicked\\nPRIVATE_SOURCE', + } + diagnostic = diagnostics.get(os.environ['MODE']) + if diagnostic: + print(diagnostic, file=sys.stderr) + sys.exit(101) if 'cargo' in sys.argv and '--no-run' not in sys.argv: if os.environ['MODE'] == 'hang': time.sleep(60) if os.environ['MODE'] == 'fail': + print('test private::test_function ... FAILED') sys.exit(19) ''')) timeout = root / 'timeout' @@ -162,10 +176,14 @@ class PrivateTestStageTests(unittest.TestCase): executable.write_text(executable.read_text().replace("#!/usr/bin/env python3", f"#!{sys.executable}")) executable.chmod(0o755) calls = root / 'calls.jsonl' - env = dict(os.environ, PATH=f'{root}:{os.environ["PATH"]}', MODE=mode, CALLS=str(calls)) + env = dict(os.environ, PATH=f'{root}:{os.environ["PATH"]}', MODE=mode, CALLS=str(calls), TMPDIR=str(root)) result = subprocess.run(['bash', str(ROOT / 'scripts/test-private-desktop.sh'), str(root)], env=env, text=True, capture_output=True, timeout=10) self.assertNotIn('PRIVATE_STD', result.stdout + result.stderr) + self.assertNotIn('PRIVATE_SOURCE', result.stdout + result.stderr) + self.assertNotIn('PRIVATE_PATH', result.stdout + result.stderr) + self.assertNotIn('PRIVATE_SYMBOL', result.stdout + result.stderr) + self.assertFalse(list(root.glob('tmp.*')), 'Private command output must be deleted') recorded = [json.loads(line) for line in calls.read_text().splitlines()] self.assertEqual(recorded[-1][:2], ['rm', '--force']) return result @@ -182,6 +200,8 @@ class PrivateTestStageTests(unittest.TestCase): result = self.run_stages('fail') self.assertEqual(result.returncode, 19) self.assertIn('Rust tests: failed', result.stdout) + self.assertIn(hashlib.sha256(b'private::test_function').hexdigest(), result.stdout) + self.assertNotIn('private::test_function', result.stdout + result.stderr) self.assertNotRegex(result.stdout, r'(?m)^Rust tests: passed') def test_hung_tests_report_timeout_and_cleanup(self): @@ -190,6 +210,34 @@ class PrivateTestStageTests(unittest.TestCase): self.assertIn('Rust tests: timed out', result.stdout) self.assertNotRegex(result.stdout, r'(?m)^Rust tests: passed') + def test_compile_failure_reports_only_allowlisted_diagnostics(self): + cases = { + 'compile-type': 'Rust diagnostic codes: error[E0061] error[E0308]', + 'compile-disk': 'insufficient disk space', + 'compile-killed': 'compiler or linker killed by SIGKILL', + 'compile-link': 'unresolved linker symbols', + 'compile-macro': 'procedural macro failed', + } + for mode, expected in cases.items(): + with self.subTest(mode=mode): + result = self.run_stages(mode) + self.assertEqual(result.returncode, 101, result.stdout + result.stderr) + self.assertIn(expected, result.stdout) + self.assertNotRegex(result.stdout, r'(?m)^Rust tests: started') + + def test_test_diagnostics_hash_names_without_exposing_private_details(self): + with tempfile.TemporaryDirectory() as temporary: + log = Path(temporary) / 'private.log' + log.write_text('test private::one ... FAILED\nPRIVATE_SOURCE\n' + 'test private::one ... FAILED\n' + 'test private::two ... ok\n') + result = subprocess.run([sys.executable, str(ROOT / 'scripts/private-ci-diagnostics.py'), + 'tests', str(log)], capture_output=True, text=True) + self.assertEqual(result.returncode, 0, result.stderr) + digest = hashlib.sha256(b'private::one').hexdigest() + self.assertEqual(result.stdout, f'::error::Rust test failure fingerprint: {digest}\n') + self.assertEqual(result.stderr, '') + class MacOSArtifactSelectionTests(unittest.TestCase): def select(self, jobs, artifacts=None):