Files
mtusi2026lab4/scripts/verify.py
T

76 lines
3.0 KiB
Python

"""Reproduce acceptance evidence without printing credentials or tokens."""
import datetime
import json
import os
from pathlib import Path
import subprocess
import sys
import time
import urllib.request
os.chdir(Path(__file__).resolve().parents[1])
lab = json.load(open('lab.json'))['number']
base = 'http://localhost:'+('8080' if lab==3 else '8084')
def command(*args):
print('\n$ '+' '.join(args),flush=True)
subprocess.run(args,check=True)
def wait():
for _ in range(90):
try:
with urllib.request.urlopen(base+'/health/ready',timeout=3) as response:
if response.status == 200:
statuses = subprocess.check_output(['docker','compose','ps','--format','json','api1','api2'],text=True)
docs = [json.loads(line) for line in statuses.splitlines() if line.strip()]
if len(docs)==2 and all(d.get('Health')=='healthy' for d in docs):
return
except Exception:
pass
time.sleep(1)
raise RuntimeError('Stand did not become ready')
def state():
ids = subprocess.check_output(['docker','compose','ps','-q'],text=True).split()
command('docker','inspect','--format','{{.Name}} {{.Config.Hostname}} {{.State.Status}} {{.State.StartedAt}}',*ids)
print('Verification UTC:',datetime.datetime.now(datetime.timezone.utc).isoformat(),flush=True)
command('docker','compose','config','--quiet')
wait()
command(sys.executable,'scripts/check_contract.py')
command('docker','compose','ps')
command(sys.executable,'tests/smoke.py','--base-url',base)
command(sys.executable,'tests/acceptance_extra.py','--base-url',base)
print('\nBefore persistence:',flush=True)
state()
command(sys.executable,'tests/resilience.py','--mode','persistence','--action-script','scripts/restart.sh','--base-url',base)
wait()
print('\nAfter persistence:',flush=True)
state()
command(sys.executable,'tests/resilience.py','--mode','failover','--action-script','scripts/stop-one.sh','--base-url',base)
state()
command('docker','compose','ps','--all')
command('docker','compose','up','-d','api1','api2')
wait()
if lab==4:
command(sys.executable,'tests/attachment_failover.py','--base-url',base)
command(sys.executable,'tests/storage_failure.py','--base-url',base)
wait()
code = '''import os,psycopg,urllib.request,urllib.error
with psycopg.connect(os.environ['DATABASE_URL']) as db:
ident=str(db.execute('SELECT id FROM attachments LIMIT 1').fetchone()[0])
try:
urllib.request.urlopen(os.environ['S3_ENDPOINT']+'/'+os.environ['S3_BUCKET']+'/'+ident,timeout=5)
except urllib.error.HTTPError as e:
assert e.code in (401,403), e.code
print('PASS: anonymous GET of existing S3 object denied:',e.code)
else:
raise AssertionError('Private object publicly accessible')
'''
print('\nChecking private bucket from internal network',flush=True)
subprocess.run(['docker','compose','exec','-T','api1','python','-c',code],check=True)
command('docker','compose','ps')
print('\nPASS: all recorded checks',flush=True)