78 lines
3.6 KiB
Python
78 lines
3.6 KiB
Python
"""Run acceptance evidence on the local Compose stack; no secrets in artifacts."""
|
|
import json
|
|
import os
|
|
import platform
|
|
import subprocess
|
|
import sys
|
|
import time
|
|
from datetime import datetime, timezone
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
EVIDENCE = ROOT / 'evidence'
|
|
ENV = {**os.environ, 'PYTHONUTF8': '1',
|
|
'BASE_URL': os.environ.get('BASE_URL', 'http://127.0.0.1:8080')}
|
|
|
|
|
|
def run(args, artifact=None):
|
|
result = subprocess.run(args, cwd=ROOT, text=True, encoding='utf-8', errors='replace',
|
|
capture_output=True, env=ENV)
|
|
output = result.stdout + result.stderr
|
|
print(output, end='', flush=True)
|
|
if artifact:
|
|
(EVIDENCE / artifact).write_text(output, encoding='utf-8')
|
|
if result.returncode:
|
|
raise SystemExit(result.returncode)
|
|
return result.stdout
|
|
|
|
|
|
def containers():
|
|
ids = run(['docker', 'compose', 'ps', '-q']).split()
|
|
result = subprocess.run(['docker', 'inspect', *ids], cwd=ROOT, text=True,
|
|
capture_output=True, check=True)
|
|
return [{'name': item['Name'], 'id': item['Id'],
|
|
'started_at': item['State']['StartedAt'], 'image': item['Config']['Image']}
|
|
for item in json.loads(result.stdout)]
|
|
|
|
|
|
def main():
|
|
run([sys.executable, 'scripts/check_contract.py'], 'contract.txt')
|
|
run([sys.executable, 'tests/smoke.py'], 'smoke.txt')
|
|
before = containers()
|
|
hook = 'scripts/restart.cmd' if os.name == 'nt' else 'scripts/restart.sh'
|
|
run([sys.executable, 'tests/resilience.py', '--mode', 'persistence',
|
|
'--action-script', hook], 'persistence.txt')
|
|
after = containers()
|
|
prior = {item['id']: item for item in before}
|
|
assert len(after) == 3 and all(item['id'] in prior and
|
|
item['started_at'] != prior[item['id']]['started_at']
|
|
for item in after), 'All three containers must really restart'
|
|
(EVIDENCE / 'restart.json').write_text(json.dumps({'before': before, 'after': after}, indent=2), encoding='utf-8')
|
|
run([sys.executable, 'student-tests/acceptance.py'], 'student-tests.txt')
|
|
run(['docker', 'compose', 'exec', '-T', 'api', 'python', 'scripts/explain.py'], 'query-plan.json')
|
|
process = subprocess.Popen([sys.executable, 'tests/load.py', '--duration', '30', '--concurrency', '4',
|
|
'--output', 'evidence/load.json'], cwd=ROOT,
|
|
stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
|
|
text=True, encoding='utf-8', env=ENV)
|
|
ids = [item['id'] for item in after]
|
|
samples = []
|
|
while process.poll() is None:
|
|
stats = subprocess.run(['docker', 'stats', '--no-stream', '--format',
|
|
'{{.Name}}: CPU={{.CPUPerc}} RAM={{.MemUsage}}', *ids],
|
|
capture_output=True, text=True, check=True)
|
|
samples.append({'at': datetime.now(timezone.utc).isoformat(), 'stats': stats.stdout.strip()})
|
|
time.sleep(2)
|
|
output = process.communicate()[0]
|
|
print(output, end='', flush=True)
|
|
(EVIDENCE / 'load.txt').write_text(output, encoding='utf-8')
|
|
(EVIDENCE / 'resources.json').write_text(json.dumps(samples, indent=2), encoding='utf-8')
|
|
if process.returncode:
|
|
raise SystemExit(process.returncode)
|
|
system = {'observed_at': datetime.now(timezone.utc).isoformat(), 'platform': platform.platform(),
|
|
'cpu': platform.processor(), 'logical_cpus': os.cpu_count(), 'python': platform.python_version()}
|
|
(EVIDENCE / 'environment.json').write_text(json.dumps(system, indent=2), encoding='utf-8')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|