Files
mtusi2026lab2/scripts/verify_stack.py
T

124 lines
6.6 KiB
Python

"""Integration checks against real Compose services; no mocked dependencies."""
from concurrent.futures import ThreadPoolExecutor
import json
import os
from pathlib import Path
import subprocess
import sys
import tempfile
import time
import uuid
import httpx
ROOT=Path(__file__).resolve().parents[1]
OUT=ROOT/'evidence'
OUT.mkdir(exist_ok=True)
evidence={}
def docker(*args):
return subprocess.check_output(['docker',*args],cwd=ROOT,text=True,encoding='utf-8',stderr=subprocess.PIPE).strip()
def sql(query):
return docker('compose','exec','-T','db','psql','-U','messenger','-d','messenger','-Atc',query)
def wait_ready(client):
deadline=time.monotonic()+60
while time.monotonic()<deadline:
try:
if client.get('/health/ready').status_code==200: return
except httpx.HTTPError: pass
time.sleep(.3)
raise AssertionError('readiness timeout')
def register(client):
name='v_'+uuid.uuid4().hex[:20]
body={'username':name,'display_name':'Проверка','password':'correct-horse-test-123'}
r=client.post('/api/v1/users',json=body); assert r.status_code==201,r.text
return body,r.json()
def login(client,body):
r=client.post('/api/v1/auth/sessions',auth=(body['username'],body['password']))
assert r.status_code==201,r.text
return {'Authorization':'Bearer '+r.json()['token']}
with httpx.Client(base_url='http://127.0.0.1:8080',timeout=12) as c:
wait_ready(c)
smoke=subprocess.check_output([sys.executable,'-X','utf8','tests/smoke.py'],cwd=ROOT,text=True,encoding='utf-8')
evidence['public_smoke']=smoke.strip()
body,user=register(c); headers=login(c,body)
bad=c.post('/api/v1/auth/sessions',auth=(body['username'],'definitely-wrong-password'))
assert bad.status_code==401
assert c.get('/api/v1/users/me',headers={'X-User-Id':user['id']}).status_code==401
assert c.get('/api/v1/users/me',auth=(body['username'],body['password'])).status_code==401
evidence['authentication_negative']='wrong password, Basic on protected route, X-User-Id: 401'
race={'username':'race_'+uuid.uuid4().hex[:18],'display_name':'Race','password':body['password']}
with ThreadPoolExecutor(max_workers=8) as pool:
statuses=list(pool.map(lambda _:c.post('/api/v1/users',json=race).status_code,range(8)))
assert statuses.count(201)==1 and statuses.count(409)==7,statuses
evidence['registration_race']=statuses
before=sql('SELECT count(*) FROM chats')
bad=c.post('/api/v1/chats',headers=headers,json={'title':'rollback','member_ids':[user['id'],str(uuid.uuid4())]})
assert bad.status_code==400,bad.text
assert before==sql('SELECT count(*) FROM chats')
evidence['rollback']='missing member: 400; chats count unchanged'
chat=c.post('/api/v1/chats',headers=headers,json={'title':'parallel','member_ids':[user['id']]}).json()
path='/api/v1/chats/'+chat['id']+'/messages'
with ThreadPoolExecutor(max_workers=10) as pool:
replies=list(pool.map(lambda n:c.post(path,headers=headers,json={'text':f'parallel {n}'}),range(100)))
assert all(r.status_code==201 for r in replies),[r.status_code for r in replies]
records=[]; cursor=None
while True:
page=c.get(path,headers=headers,params={'limit':17,**({'cursor':cursor} if cursor else {})}).json()
records+=page['items']; cursor=page['next_cursor']
if cursor is None: break
assert len(records)==100 and len({r['id'] for r in records})==100
assert {r['id'] for r in records}=={r.json()['id'] for r in replies}
evidence['concurrent_messages']={'sent':100,'read':len(records),'page_size':17}
def hits():
stats=docker('compose','exec','-T','cache','valkey-cli','INFO','stats')
return int(next(line.split(':')[1] for line in stats.splitlines() if line.startswith('keyspace_hits:')))
hits_before=hits()
for _ in range(5): assert c.get('/api/v1/users/me',headers=headers).status_code==200
evidence['cache_hit_delta']=hits()-hits_before
assert evidence['cache_hit_delta']>=5
docker('compose','stop','cache')
try:
start=time.perf_counter()
r=c.get('/api/v1/users/me',headers=headers)
evidence['cache_down']={'status':r.status_code,'seconds':round(time.perf_counter()-start,3)}
assert r.status_code==200
assert c.get('/api/v1/users/me',headers={'Authorization':'Bearer '+'x'*43}).status_code==401
finally: docker('compose','start','cache')
docker('compose','stop','db')
try:
start=time.perf_counter(); r=c.get('/api/v1/users/me',headers=headers)
evidence['database_down']={'status':r.status_code,'seconds':round(time.perf_counter()-start,3)}
assert r.status_code==503 and time.perf_counter()-start<10
assert c.get('/api/v1/users/me').status_code==401
finally: docker('compose','start','db')
wait_ready(c)
assert c.get('/api/v1/users/me',headers=headers).status_code==200
assert c.delete('/api/v1/auth/sessions/current',headers=headers).status_code==204
assert c.get('/api/v1/users/me',headers=headers).status_code==401
evidence['logout']='204 then 401'
# A separate API process with short TTL uses the same persistent dependencies.
name='lab2-expiry-'+uuid.uuid4().hex[:8]
docker('compose','run','-d','--no-deps','--name',name,'-p','127.0.0.1::8080','-e','SESSION_TTL_SECONDS=2','api')
try:
binding=docker('port',name,'8080/tcp').split(':')[-1]
with httpx.Client(base_url='http://127.0.0.1:'+binding,timeout=10) as c:
wait_ready(c); body,user=register(c); headers=login(c,body)
assert c.get('/api/v1/users/me',headers=headers).status_code==200
time.sleep(2.2)
assert c.get('/api/v1/users/me',headers=headers).status_code==401
evidence['short_ttl']='SESSION_TTL_SECONDS=2: 200 then 401 after 2.2s'
finally: docker('rm','-f',name)
evidence['migration_repeat']=docker('compose','run','--rm','--no-deps','migrate')
if os.name=='nt':
with tempfile.TemporaryDirectory(prefix='lab2-hook-') as tmp:
hook=Path(tmp)/'restart.cmd'
hook.write_text('@echo off\n"'+sys.executable+'" "'+str(ROOT/'scripts/restart.py')+'"\n',encoding='utf-8')
evidence['public_persistence']=subprocess.check_output([sys.executable,'-X','utf8','tests/resilience.py','--mode','persistence','--action-script',str(hook)],cwd=ROOT,text=True,encoding='utf-8').strip()
else:
evidence['public_persistence']=subprocess.check_output([sys.executable,'tests/resilience.py','--mode','persistence','--action-script','scripts/restart.sh'],cwd=ROOT,text=True).strip()
evidence['passed']=True
(OUT/'integration.json').write_text(json.dumps(evidence,ensure_ascii=False,indent=2)+'\n',encoding='utf-8')
print(json.dumps(evidence,ensure_ascii=False,indent=2))