146 lines
7.8 KiB
Python
146 lines
7.8 KiB
Python
#!/usr/bin/env python3
|
|
"""Acceptance runner for a LOCAL disposable teaching stack; never prints tokens/secrets.
|
|
Run from any directory: python3 scripts/verify.py
|
|
Requires Docker Compose, Python 3.9+. Preserves named volumes and restores normal TTL.
|
|
"""
|
|
import base64
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import subprocess
|
|
import sys
|
|
import time
|
|
import uuid
|
|
ROOT=Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0,str(ROOT/'tests'))
|
|
from client import Client, check
|
|
EVIDENCE=ROOT/'evidence'
|
|
EVIDENCE.mkdir(exist_ok=True)
|
|
BASE=os.environ.get('BASE_URL','http://127.0.0.1:8080')
|
|
results=[]
|
|
|
|
def record(name):
|
|
results.append(name)
|
|
print('PASS: '+name,flush=True)
|
|
(EVIDENCE/'acceptance.json').write_text(json.dumps({'passed':results,'completed':False},indent=2))
|
|
|
|
def compose(*args,env=None):
|
|
print('Docker Compose: '+args[0],flush=True)
|
|
return subprocess.run(['docker','compose',*args],cwd=ROOT,env=env,check=True,capture_output=True,text=True,encoding="utf-8",errors="replace").stdout
|
|
|
|
def sql(statement):
|
|
return subprocess.run(['docker','compose','exec','-T','postgres','psql','-X','-U','messenger','-d','messenger','-v','ON_ERROR_STOP=1','-At'],input=statement,cwd=ROOT,check=True,capture_output=True,text=True,encoding="utf-8",errors="replace").stdout.strip()
|
|
|
|
def wait():
|
|
c=Client(BASE)
|
|
deadline=time.monotonic()+90
|
|
while time.monotonic()<deadline:
|
|
try: c.call('GET','/health/ready'); return
|
|
except Exception: time.sleep(1)
|
|
raise AssertionError('Readiness timeout')
|
|
|
|
def run_public(name,*args):
|
|
print('Running '+name,flush=True)
|
|
p=subprocess.run([sys.executable,"-X","utf8",str(ROOT/'tests'/name),'--base-url',BASE,*args],cwd=ROOT,capture_output=True,text=True,encoding="utf-8",errors="replace")
|
|
(EVIDENCE/(name+'.txt')).write_text(p.stdout+p.stderr,encoding='utf-8')
|
|
check(p.returncode==0,name+' failed; see evidence')
|
|
record(name)
|
|
|
|
def main():
|
|
(EVIDENCE/'acceptance.json').write_text(json.dumps({'passed':[], 'completed':False},indent=2),encoding='utf-8')
|
|
os.environ['LAB2_PYTHON']=sys.executable
|
|
print('Preparing local stack; full verification will temporarily stop services.',flush=True)
|
|
subprocess.run([sys.executable,'-X','utf8','scripts/setup.py'],cwd=ROOT,check=True)
|
|
compose('up','-d','--build','--wait','--wait-timeout','120')
|
|
wait()
|
|
run_public('smoke.py')
|
|
c=Client(BASE)
|
|
alice,bob,eve,chat=c.bootstrap()
|
|
bad=base64.b64encode((alice['registration']['username']+':wrong-password').encode()).decode()
|
|
c.call('POST','/api/v1/auth/sessions',401,headers={'Authorization':'Basic '+bad})
|
|
c.call('GET','/api/v1/users/me',401,headers={'X-User-Id':alice['id']})
|
|
me=c.call('GET','/api/v1/users/me',actor=alice,headers={'X-User-Id':eve['id']},schema='User')['json']
|
|
check(me['id']==alice['id'],'X-User-Id changed bearer identity')
|
|
record('wrong password and X-User-Id bypass rejected')
|
|
# Exactly two simultaneous KDF requests fit the documented admission bound.
|
|
body={'username':'race_'+uuid.uuid4().hex[:20],'display_name':'Race','password':'race-password-12345'}
|
|
def create(_): return Client(BASE).call('POST','/api/v1/users',[201,409],body=body)['status']
|
|
with ThreadPoolExecutor(max_workers=2) as pool: codes=list(pool.map(create,range(2)))
|
|
check(sorted(codes)==[201,409],'username race not serialized by UNIQUE')
|
|
record('concurrent username registration: exactly one 201 and one 409')
|
|
title='rollback_'+uuid.uuid4().hex
|
|
c.call('POST','/api/v1/chats',400,actor=alice,body={'title':title,'member_ids':[alice['id'],str(uuid.uuid4())]})
|
|
check(sql("SELECT count(*) FROM chats WHERE title='"+title+"';")=='0','orphan chat after failed transaction')
|
|
record('failed member insertion rolled back parent chat (SQL count=0)')
|
|
# Unicode password is >72 UTF-8 bytes; colon is valid in Basic password.
|
|
reg={'username':'unicode_'+uuid.uuid4().hex[:16],'display_name':'Тест','password':'🔑'*90+':пароль'}
|
|
c.call('POST','/api/v1/users',201,body=reg)
|
|
basic=base64.b64encode((reg['username']+':'+reg['password']).encode()).decode()
|
|
c.call('POST','/api/v1/auth/sessions',201,headers={'Authorization':'Basic '+basic},schema='Session')
|
|
record('long Unicode password with colon')
|
|
# Cache is warmed by authenticated requests. Verify actual key and TTL, not only configuration.
|
|
c.call('GET','/api/v1/users/me',actor=alice)
|
|
ttl=int(compose('exec','-T','redis','redis-cli','TTL','profile:v1:'+alice['id']).strip())
|
|
check(0<ttl<=300,'Redis profile cache missing TTL')
|
|
compose('exec','-T','redis','redis-cli','FLUSHDB')
|
|
c.call('GET','/api/v1/users/me',actor=alice)
|
|
record('Redis profile cache TTL and recovery after flush')
|
|
try:
|
|
compose('stop','redis')
|
|
start=time.monotonic()
|
|
c.call('GET','/api/v1/users/me',actor=alice)
|
|
c.call('GET','/health/ready')
|
|
check(time.monotonic()-start<10,'Redis fallback exceeded budget')
|
|
c.call('DELETE','/api/v1/auth/sessions/current',204,actor=alice)
|
|
c.call('GET','/api/v1/users/me',401,actor=alice)
|
|
record('Redis unavailable: SQL fallback; logout still effective')
|
|
finally: compose('start','redis')
|
|
try:
|
|
compose('stop','postgres')
|
|
start=time.monotonic()
|
|
c.call('GET','/api/v1/users/me',503,actor=bob)
|
|
c.call('GET','/health/ready',503)
|
|
c.call('GET','/health/live',200)
|
|
check(time.monotonic()-start<15,'Postgres failure exceeded budget')
|
|
record('Postgres unavailable: bounded 503, liveness 200')
|
|
finally: compose('start','postgres'); wait()
|
|
# Same token must work after the failure.
|
|
c.call('GET','/api/v1/users/me',actor=bob)
|
|
from urllib.parse import quote
|
|
c.message(bob,chat,'cursor persistence A')
|
|
c.message(bob,chat,'cursor persistence B')
|
|
page=c.call('GET','/api/v1/chats/'+chat['id']+'/messages?limit=1',actor=bob,schema='MessagePage')['json']
|
|
next_path='/api/v1/chats/'+chat['id']+'/messages?limit=1&cursor='+quote(page['next_cursor'],safe='')
|
|
expected=c.call('GET',next_path,actor=bob,schema='MessagePage')['json']
|
|
run_public('resilience.py','--mode','persistence','--action-script',('scripts/restart_windows.cmd' if os.name=='nt' else 'scripts/restart.sh'))
|
|
actual=c.call('GET',next_path,actor=bob,schema='MessagePage')['json']
|
|
check(actual==expected,'cursor became invalid or changed page after restart')
|
|
record('existing signed cursor works across full restart')
|
|
check(sql('SELECT count(*) FROM schema_migrations;')=='1','migration repeated')
|
|
record('migration repeated safely across full restart')
|
|
try:
|
|
env=dict(os.environ,SESSION_TTL_SECONDS='2')
|
|
compose('up','-d','--no-deps','--force-recreate','api',env=env); wait()
|
|
short=Client(BASE).user('expiry')
|
|
time.sleep(3)
|
|
c.call('GET','/api/v1/users/me',401,actor=short)
|
|
record('SESSION_TTL_SECONDS=2 expires without logout')
|
|
finally:
|
|
compose('up','-d','--no-deps','--force-recreate','api'); wait()
|
|
plan=sql((ROOT/'scripts/explain.sql').read_text(encoding='utf-8'))
|
|
(EVIDENCE/'explain.txt').write_text(plan+'\n',encoding='utf-8')
|
|
check('ix_messages_history' in plan and 'Index' in plan,'history index not selected; inspect explain.txt')
|
|
record('EXPLAIN ANALYZE BUFFERS: indexed deep page on 10000 rows')
|
|
(EVIDENCE/'acceptance.json').write_text(json.dumps({'passed':results,'completed':True},indent=2))
|
|
print('ALL CHECKS PASSED. Evidence saved. Keep it for REPORT.md.')
|
|
|
|
if __name__=='__main__':
|
|
try: main()
|
|
except subprocess.CalledProcessError as ex:
|
|
# Build logs may be useful, but dependency commands may contain private values.
|
|
print('FAIL: external command failed (exit '+str(ex.returncode)+'). Inspect docker compose logs locally.',file=sys.stderr)
|
|
sys.exit(1)
|
|
except Exception as ex:
|
|
print('FAIL: '+str(ex),file=sys.stderr); sys.exit(1)
|