Files
fips/testing/chaos/sim/logs.py
Johnathan Corgan 9e63b42bd9 Consolidate Docker test harness infrastructure
Replace 4 near-identical per-harness Docker setups with unified shared
infrastructure. Net result: -463 lines across 55 files, faster CI
(8.5 min vs ~13.5 min), 14 scenarios (down from 21).

Unified Docker image (testing/docker/):
- Single Dockerfile (trixie-slim) with FIPS_TEST_MODE env var for
  mode dispatch: default, chaos, sidecar, tor-socks5, tor-directory
- Single entrypoint.sh with conditional logic per mode
- Replaces 5 Dockerfiles, 3 entrypoints, 4 resolv.conf copies

Shared build and libraries (testing/scripts/, testing/lib/):
- testing/scripts/build.sh: single build script with macOS zigbuild
  support, replaces 4 per-harness copies
- testing/lib/derive_keys.py: shared key derivation module, replaces
  3 copies of derive-keys.py
- testing/lib/log_analysis.py: shared log analysis extracted from
  chaos sim/logs.py, with CLI interface and rekey cutover tracking
- testing/lib/wait-converge.sh: shared convergence wait helpers
  (wait_for_links, wait_for_peers) using fipsctl JSON polling

Scenario consolidation:
- Remove 7 redundant scenarios: tcp-chain (subsumed by tcp-mesh),
  tcp-only (subsumed by tcp-mesh), chaos-10 (replaced by
  churn-mixed --nodes 10), churn-10/churn-20/churn-20-mixed
  (subsumed by parameterized churn-mixed), cost-mixed-7node
  (overlaps mixed-technology)
- Add churn-mixed scenario with --nodes flag for scale testing
- Reduce idle scenario durations: smoke-10 60s→30s,
  ethernet-only 90s→30s, cost-avoidance 120s→45s,
  depth-vs-cost 120s→45s, bottleneck-parent 120s→60s,
  mixed-technology 180s→90s

CI updates:
- ci-local.sh: unified image build, structured chaos suite entries
  with per-scenario flags, --skip-build for sidecar
- ci.yml: shared binary install + image build step, updated scenario
  matrix, chaos_flags support for parameterized scenarios
- Add tcp-mesh and congestion-stress to CI matrix
- Static ping test uses active peer convergence detection instead
  of hardcoded 5s sleep

Chaos infrastructure improvements (from discovery-rework branch):
- Pre-built Docker image instead of per-service build at scale
- --nodes flag in chaos.sh for runtime topology size override
2026-03-19 18:08:36 +00:00

86 lines
2.5 KiB
Python

"""Log collection and post-run analysis for chaos simulations.
Delegates core analysis to the shared testing.lib.log_analysis module.
Adds chaos-specific collection (Docker container logs, sim metadata).
"""
from __future__ import annotations
import logging
import os
import subprocess
# Import shared analysis from testing/lib/
import sys
_TESTING_DIR = os.path.join(os.path.dirname(__file__), "..", "..")
if _TESTING_DIR not in sys.path:
sys.path.insert(0, _TESTING_DIR)
from lib.log_analysis import ( # noqa: E402
AnalysisResult,
analyze_logs,
strip_ansi,
)
log = logging.getLogger(__name__)
# Re-export for existing callers
__all__ = ["AnalysisResult", "analyze_logs", "collect_logs", "write_sim_metadata"]
def collect_logs(container_names: list[str], output_dir: str) -> dict[str, str]:
"""Collect all output (stdout + stderr) from all containers."""
os.makedirs(output_dir, exist_ok=True)
logs = {}
for name in container_names:
try:
result = subprocess.run(
["docker", "logs", name],
capture_output=True,
text=True,
timeout=30,
)
raw = result.stdout + result.stderr
log_text = strip_ansi(raw)
logs[name] = log_text
path = os.path.join(output_dir, f"{name}.log")
with open(path, "w") as f:
f.write(log_text)
except (subprocess.TimeoutExpired, Exception) as e:
log.warning("Failed to collect logs from %s: %s", name, e)
logs[name] = ""
return logs
def write_sim_metadata(
output_dir: str,
scenario_name: str,
seed: int,
num_nodes: int,
num_edges: int,
duration_secs: int,
topology=None,
):
"""Write simulation metadata for reproducibility."""
path = os.path.join(output_dir, "metadata.txt")
with open(path, "w") as f:
f.write(f"scenario: {scenario_name}\n")
f.write(f"seed: {seed}\n")
f.write(f"nodes: {num_nodes}\n")
f.write(f"edges: {num_edges}\n")
f.write(f"duration_secs: {duration_secs}\n")
if topology:
f.write("\nadjacency:\n")
for nid in sorted(topology.nodes):
node = topology.nodes[nid]
peers = sorted(node.peers)
f.write(f" {nid} ({node.docker_ip}): {', '.join(peers)}\n")
f.write("\nedges:\n")
for a, b in sorted(topology.edges):
f.write(f" {a} -- {b}\n")