|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Verify that each node stdout log contains the expected completion message.""" |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import argparse |
| 7 | +import sys |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | +MESSAGE_SUBSTRING = '"msg":"All parts received"' |
| 11 | + |
| 12 | + |
| 13 | +def parse_args() -> argparse.Namespace: |
| 14 | + parser = argparse.ArgumentParser( |
| 15 | + description=( |
| 16 | + "Validate that every node stdout log inside a Shadow output directory " |
| 17 | + "contains the expected completion message." |
| 18 | + ) |
| 19 | + ) |
| 20 | + parser.add_argument( |
| 21 | + "shadow_output", |
| 22 | + help="Path to the Shadow output directory (the one containing the hosts/ folder).", |
| 23 | + ) |
| 24 | + parser.add_argument( |
| 25 | + "--count", |
| 26 | + type=int, |
| 27 | + default=1, |
| 28 | + help="Minimum number of times each stdout log must contain the target message (default: 1).", |
| 29 | + ) |
| 30 | + return parser.parse_args() |
| 31 | + |
| 32 | + |
| 33 | +def iter_stdout_logs(hosts_dir: Path): |
| 34 | + """Yield all stdout log files under the given hosts directory.""" |
| 35 | + for stdout_file in sorted(hosts_dir.rglob("*.stdout")): |
| 36 | + if stdout_file.is_file(): |
| 37 | + yield stdout_file |
| 38 | + |
| 39 | + |
| 40 | +def count_occurrences(path: Path, needle: str) -> int: |
| 41 | + """Count how many times the string appears inside the file.""" |
| 42 | + if not needle: |
| 43 | + return 0 |
| 44 | + total = 0 |
| 45 | + with path.open("r", encoding="utf-8", errors="replace") as handle: |
| 46 | + for line in handle: |
| 47 | + total += line.count(needle) |
| 48 | + return total |
| 49 | + |
| 50 | + |
| 51 | +def main() -> int: |
| 52 | + args = parse_args() |
| 53 | + base_dir = Path(args.shadow_output).expanduser().resolve() |
| 54 | + if not base_dir.exists(): |
| 55 | + print(f"shadow output directory does not exist: {base_dir}", file=sys.stderr) |
| 56 | + return 1 |
| 57 | + |
| 58 | + hosts_dir = base_dir / "hosts" |
| 59 | + if not hosts_dir.is_dir(): |
| 60 | + print(f"hosts directory not found under: {base_dir}", file=sys.stderr) |
| 61 | + return 1 |
| 62 | + |
| 63 | + stdout_logs = list(iter_stdout_logs(hosts_dir)) |
| 64 | + if not stdout_logs: |
| 65 | + print(f"no stdout logs found under: {hosts_dir}", file=sys.stderr) |
| 66 | + return 1 |
| 67 | + |
| 68 | + missing = [] |
| 69 | + for log_path in stdout_logs: |
| 70 | + occurrences = count_occurrences(log_path, MESSAGE_SUBSTRING) |
| 71 | + if occurrences < args.count: |
| 72 | + missing.append((log_path, occurrences)) |
| 73 | + |
| 74 | + if missing: |
| 75 | + print( |
| 76 | + "The following stdout logs do not contain the required message:", |
| 77 | + file=sys.stderr, |
| 78 | + ) |
| 79 | + for log_path, occurrences in missing: |
| 80 | + rel_path = log_path.relative_to(base_dir) |
| 81 | + print( |
| 82 | + f" - {rel_path}: found {occurrences} occurrences (expected >= {args.count})", |
| 83 | + file=sys.stderr, |
| 84 | + ) |
| 85 | + print( |
| 86 | + f"{len(missing)} / {len(stdout_logs)} logs missing the message.", |
| 87 | + file=sys.stderr, |
| 88 | + ) |
| 89 | + return 1 |
| 90 | + |
| 91 | + print( |
| 92 | + f"All {len(stdout_logs)} stdout logs under {hosts_dir} contain the required message." |
| 93 | + ) |
| 94 | + return 0 |
| 95 | + |
| 96 | + |
| 97 | +if __name__ == "__main__": |
| 98 | + sys.exit(main()) |
0 commit comments