Files
mtusi2026lab7/scripts/check.py
T

62 lines
2.9 KiB
Python

"""Run checks with captured exit codes, sanitized output and guaranteed restore."""
import argparse
import os
from pathlib import Path
import subprocess
import sys
ROOT=Path(__file__).resolve().parents[1]
sys.path.insert(0,str(ROOT/'scripts'))
from manage import env,ready
def run(name,command):
result=subprocess.run(command,cwd=ROOT,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
output=result.stdout.decode('utf-8',errors='replace')
(ROOT/'evidence'/name).write_text('COMMAND: '+' '.join(command)+'\nEXIT: '+str(result.returncode)+'\n'+output,encoding='utf-8')
print(output,flush=True)
if result.returncode:
raise RuntimeError(name+' failed with exit '+str(result.returncode))
def main():
p=argparse.ArgumentParser();p.add_argument('check',choices=['internal','public','persistence','failover','business','all']);a=p.parse_args()
os.chdir(ROOT)
base='https://localhost:'+env()['HTTPS_PORT']
common=['--base-url',base,'--ca-file',str(ROOT/'secrets/ca.crt')]
py=[sys.executable,'-X','utf8']
if a.check in ('internal','all'):
run('internal.txt',['docker','compose','run','--rm','verifier'])
if a.check in ('public','all'):
for script in ('smoke','security'):
run(script+'.txt',py+['tests/'+script+'.py']+common)
if a.check in ('business','all'):
run('business-security.txt',py+['student-tests/security_suite.py'])
if a.check in ('persistence','all'):
hook='scripts/restart.'+('cmd' if os.name=='nt' else 'sh')
run('persistence.txt',py+['tests/resilience.py','--mode','persistence','--action-script',hook]+common)
if a.check in ('failover','all'):
hook='scripts/stop-one.'+('cmd' if os.name=='nt' else 'sh')
completed=set()
for path in ROOT.glob('evidence/stop-api*.json'):
path.unlink() # Only this runner's known generated summaries.
for attempt in range(6):
try:
# Reject execution against a different checkout/project.
assert env()['COMPOSE_PROJECT_NAME'].startswith('lyakhonov-lab7')
config=subprocess.check_output(['docker','compose','config','--format','json'],cwd=ROOT)
import json
assert json.loads(config)['name']==env()['COMPOSE_PROJECT_NAME']
ready()
run('failover-'+str(attempt+1)+'.txt',py+['tests/resilience.py','--mode','failover','--action-script',hook]+common)
completed={path.stem for path in ROOT.glob('evidence/stop-api*.json')}
finally:
subprocess.run(['docker','compose','start','api1','api2'],cwd=ROOT,check=True)
ready()
if completed=={'stop-api1','stop-api2'}:
print('PASS sequential failover proved for both real API containers; both restored.');return
raise RuntimeError('Both replica failures were not observed in six public scenarios')
if __name__=='__main__':main()