lab4 for lab5
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Sequential lab3-5 verification; capture actual output and exit status, fail fast."""
|
||||
from datetime import datetime, timezone
|
||||
import argparse
|
||||
import json
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
DESTINATION = ROOT / "evidence/audit"
|
||||
CHECKS = [
|
||||
("contracts", ["make", "check", "validate", "PYTHON=.venv/bin/python"]),
|
||||
("compose", ["docker", "compose", "config", "--quiet"]),
|
||||
("smoke", ["make", "test"]),
|
||||
("attachments", ["make", "test-attachments"]),
|
||||
("idempotency", [sys.executable, "student-tests/idempotency_concurrency.py"]),
|
||||
("regressions", [sys.executable, "student-tests/review_regressions.py"]),
|
||||
("private-bucket", ["make", "test-private"]),
|
||||
("s3-permissions", ["make", "test-s3-permissions"]),
|
||||
("bootstrap", ["docker", "compose", "run", "--rm", "--no-deps", "s3-init"]),
|
||||
("upload-failover", ["make", "test-upload-failover"]),
|
||||
("public-failover", ["make", "test-failover", "ACTION_SCRIPT=scripts/stop-one.sh"]),
|
||||
("restore-replicas", ["docker", "compose", "up", "-d", "--wait", "api-a", "api-b", "load_balancer"]),
|
||||
("s3-outage", ["make", "test-s3-outage"]),
|
||||
("persistence", [sys.executable, "scripts/verify-persistence.py"]),
|
||||
("reconcile", ["make", "test-reconcile"]),
|
||||
("reconcile-audit", ["make", "test-reconcile-audit"]),
|
||||
("upload-memory", ["make", "test-upload-memory"]),
|
||||
("reconcile-benchmark", ["make", "test-reconcile-benchmark"]),
|
||||
("final-smoke", ["make", "test"]),
|
||||
("environment", [sys.executable, "scripts/record-environment.py"]),
|
||||
]
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('--resume-from', choices=[name for name, _ in CHECKS])
|
||||
args = parser.parse_args()
|
||||
DESTINATION.mkdir(parents=True, exist_ok=True)
|
||||
results = []
|
||||
checks = CHECKS
|
||||
if args.resume_from:
|
||||
offset = [name for name, _ in CHECKS].index(args.resume_from)
|
||||
previous = json.loads((DESTINATION / 'results.json').read_text())['checks']
|
||||
results = previous[:offset]
|
||||
if [item['name'] for item in results] != [name for name, _ in CHECKS[:offset]] or any(item['exit_code'] for item in results):
|
||||
parser.error('resume requires successful evidence for every earlier step')
|
||||
checks = CHECKS[offset:]
|
||||
for name, command in checks:
|
||||
print(f"RUN {name}", flush=True)
|
||||
started = time.monotonic()
|
||||
with (DESTINATION / f"{name}.txt").open("w") as stream:
|
||||
result = subprocess.run(command, cwd=ROOT, stdout=stream, stderr=subprocess.STDOUT)
|
||||
results.append({"name": name, "command": command, "exit_code": result.returncode,
|
||||
"seconds": round(time.monotonic() - started, 3)})
|
||||
(DESTINATION / "results.json").write_text(json.dumps({
|
||||
"recorded_at_utc": datetime.now(timezone.utc).isoformat(),
|
||||
"checks": results, "complete": len(results) == len(CHECKS),
|
||||
}, indent=2) + "\n")
|
||||
print(f"{'PASS' if result.returncode == 0 else 'FAIL'} {name}: exit={result.returncode}", flush=True)
|
||||
if result.returncode:
|
||||
print((DESTINATION / f"{name}.txt").read_text()[-8000:], flush=True)
|
||||
return result.returncode
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
@@ -0,0 +1,36 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Create local credentials without printing or overwriting existing values."""
|
||||
import json
|
||||
from pathlib import Path
|
||||
import secrets
|
||||
|
||||
root = Path(__file__).resolve().parents[1] / "secrets"
|
||||
root.mkdir(mode=0o700, exist_ok=True)
|
||||
|
||||
|
||||
def write(name, value):
|
||||
path = root / name
|
||||
if path.exists():
|
||||
return path.read_text().strip()
|
||||
with path.open("x") as stream:
|
||||
path.chmod(0o600)
|
||||
stream.write(value + "\n")
|
||||
return value
|
||||
|
||||
|
||||
password = write("postgres_password", secrets.token_hex(32))
|
||||
write("database_url", f"postgresql://messenger:{password}@postgres:5432/messenger")
|
||||
write("cursor_secret", secrets.token_hex(32))
|
||||
admin_key = write("s3_admin_key", secrets.token_hex(16))
|
||||
admin_secret = write("s3_admin_secret", secrets.token_hex(32))
|
||||
app_key = write("s3_access_key_id", secrets.token_hex(16))
|
||||
app_secret = write("s3_secret_access_key", secrets.token_hex(32))
|
||||
config_path = root / "s3.json"
|
||||
config_path.write_text(json.dumps({"identities": [
|
||||
{"name": "bootstrap", "credentials": [{"accessKey": admin_key, "secretKey": admin_secret}],
|
||||
"actions": ["Admin", "Read", "Write", "List", "Tagging"]},
|
||||
{"name": "api", "credentials": [{"accessKey": app_key, "secretKey": app_secret}],
|
||||
"actions": ["Read:messenger-attachments", "Write:messenger-attachments", "List:messenger-attachments"]},
|
||||
]}, indent=2) + "\n")
|
||||
config_path.chmod(0o600)
|
||||
print("Local secrets ready; existing values preserved.")
|
||||
@@ -0,0 +1,44 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Sanitized environment and source fingerprint for local test evidence."""
|
||||
from datetime import datetime, timezone
|
||||
from hashlib import sha256
|
||||
import json
|
||||
from pathlib import Path
|
||||
import platform
|
||||
import subprocess
|
||||
|
||||
root = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
def command(*args):
|
||||
return subprocess.check_output(args, cwd=root, text=True).strip()
|
||||
|
||||
|
||||
paths = [root / name for name in ('Dockerfile', 'compose.yaml', 'pyproject.toml', 'uv.lock', 'Makefile')]
|
||||
for directory in ('src', 'migrations', 'infra', 'scripts', 'student-tests'):
|
||||
paths.extend(path for path in (root / directory).rglob('*')
|
||||
if path.is_file() and '__pycache__' not in path.parts)
|
||||
report = {
|
||||
'recorded_at_utc': datetime.now(timezone.utc).isoformat(),
|
||||
'base_commit': command('git', 'rev-parse', 'HEAD'),
|
||||
'branch': command('git', 'branch', '--show-current'),
|
||||
'note': 'Results apply to this working tree, not only the base commit. Functional tests; no load benchmark.',
|
||||
'host': platform.platform(),
|
||||
'docker': json.loads(command('docker', 'info', '--format',
|
||||
'{"server":"{{.ServerVersion}}","cpus":{{.NCPU}},"memory_bytes":{{.MemTotal}}}')),
|
||||
'compose_version': command('docker', 'compose', 'version', '--short'),
|
||||
'sources_sha256': {str(path.relative_to(root)): sha256(path.read_bytes()).hexdigest() for path in sorted(paths)},
|
||||
'services': {},
|
||||
}
|
||||
for service in ('postgres', 's3', 'api-a', 'api-b', 'load_balancer'):
|
||||
container = command('docker', 'compose', 'ps', '-q', service)
|
||||
info = json.loads(command('docker', 'inspect', container))[0]
|
||||
report['services'][service] = {
|
||||
'image': info['Config']['Image'], 'image_id': info['Image'],
|
||||
'state': info['State']['Status'], 'health': info['State'].get('Health', {}).get('Status'),
|
||||
'memory_limit_bytes': info['HostConfig']['Memory'],
|
||||
'nano_cpus_limit': info['HostConfig']['NanoCpus'],
|
||||
'volumes': [mount['Name'] for mount in info['Mounts'] if mount['Type'] == 'volume'],
|
||||
}
|
||||
(root / 'evidence/environment.json').write_text(json.dumps(report, ensure_ascii=False, indent=2) + '\n')
|
||||
print('PASS: sanitized environment and source hashes saved')
|
||||
@@ -0,0 +1,27 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Public persistence test plus proof of actual container restarts."""
|
||||
import json
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
def snapshot():
|
||||
result = {}
|
||||
for service in ('postgres', 's3', 'api-a', 'api-b', 'load_balancer'):
|
||||
container = subprocess.check_output(['docker', 'compose', 'ps', '-q', service], cwd=ROOT, text=True).strip()
|
||||
started = subprocess.check_output(['docker', 'inspect', '--format', '{{.State.StartedAt}}', container], text=True).strip()
|
||||
result[service] = {'container': container[:12], 'started_at': started}
|
||||
return result
|
||||
|
||||
|
||||
before = snapshot()
|
||||
print('Before:', json.dumps(before, sort_keys=True), flush=True)
|
||||
subprocess.run([sys.executable, 'tests/resilience.py', '--mode', 'persistence',
|
||||
'--action-script', 'scripts/restart.sh'], cwd=ROOT, check=True)
|
||||
after = snapshot()
|
||||
print('After:', json.dumps(after, sort_keys=True), flush=True)
|
||||
assert all(before[key]['started_at'] != after[key]['started_at'] for key in before)
|
||||
print('PASS: PostgreSQL, S3, both API replicas and LB actually restarted; volumes preserved')
|
||||
Reference in New Issue
Block a user