#!/usr/bin/env python3 """Check saved source snapshots, run completion and actual container budgets.""" import argparse from collections import Counter import hashlib import json from pathlib import Path def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('directory', type=Path) args = parser.parse_args() root = args.directory manifest = json.loads((root / 'manifest.json').read_text(encoding='utf-8')) sources = {name: hashlib.sha256((root / 'sources' / name).read_bytes()).hexdigest() == digest for name, digest in manifest['source_sha256'].items()} budgets = {} images = set() for profile, containers in manifest['containers'].items(): apis = [c for c in containers if '-api-' in c['name']] images.update(c['image'] for c in apis) budgets[profile] = {'replicas': len(apis), 'cpu': sum(c['nano_cpus'] for c in apis) / 1e9, 'ram_mib': sum(c['memory_bytes'] for c in apis) / 1024**2, 'total_pool': sum(int(c['pool_max']) for c in apis)} runs = {} groups = Counter() for label in manifest['runs']: path = root / label summary = json.loads((path / 'summary.json').read_text(encoding='utf-8')) execution = json.loads((path / 'execution.json').read_text(encoding='utf-8')) profile = next(p for p in manifest['profiles'] if label.startswith(p + '-r')) expected = budgets[profile]['replicas'] rate = summary['profile']['offered_rps'] if summary['profile'].get('phase') == 'confirmation': groups[f'{profile}/r{rate}'] += 1 runs[label] = {'returncode': execution['returncode'], 'diagnostics_complete': summary.get('diagnostics_complete', False), 'replicas_match': summary['profile']['expected_instances'] == expected, 'criterion': summary.get('criterion')} requested = manifest.get('requested_confirmation_rates') complete_groups = all(count == manifest['repetitions'] for count in groups.values()) if requested: complete_groups = complete_groups and all( groups[f'{profile}/r{rate}'] == manifest['repetitions'] for profile in manifest['profiles'] for rate in requested) checks = {'finished': bool(manifest.get('finished')), 'source_snapshots_match': all(sources.values()), 'one_api_image': len(images) == 1, 'all_runs_complete': bool(runs) and all( r['returncode'] == 0 and r['diagnostics_complete'] and r['replicas_match'] for r in runs.values()), 'confirmation_groups_complete': bool(groups) and complete_groups, 'pool_budget_matches': all(b['total_pool'] == manifest['pool_budget'] for b in budgets.values())} audit = {'checks_pass': all(checks.values()), 'checks': checks, 'budgets': budgets, 'source_snapshots': sources, 'confirmation_groups': dict(groups), 'runs': runs} (root / 'audit.json').write_text(json.dumps(audit, indent=2) + '\n', encoding='utf-8') print(json.dumps({'checks_pass': audit['checks_pass'], 'checks': checks, 'budgets': budgets, 'runs': len(runs)}, indent=2)) if not audit['checks_pass']: raise SystemExit(1) if __name__ == '__main__': main()