lab2done
Skeleton contracts / contracts (pull_request) Canceled after 0s

This commit is contained in:
2026-11-13 11:08:23 +03:00
parent 65003b6119
commit 76f0b45143
52 changed files with 1511 additions and 74 deletions
+20
View File
@@ -0,0 +1,20 @@
#!/usr/bin/env python3
"""Optional static SQL/YAML check: pip install pglast PyYAML. Does not execute SQL."""
from pathlib import Path
import re
import yaml
from pglast import parse_sql
root=Path(__file__).resolve().parents[1]
for file in (root/'src/Messenger/Migrations').glob('*.sql'):
parse_sql(file.read_text())
print('PASS SQL syntax:',file.name)
parse_sql((root/'scripts/explain.sql').read_text())
for file in (root/'src/Messenger').rglob('*.cs'):
for query in re.findall(r'"((?:SELECT|INSERT INTO|DELETE FROM|CREATE TABLE)[^"\n]+)"',file.read_text()):
if not query.endswith('FROM chats c '): parse_sql(query)
config=yaml.safe_load((root/'compose.yaml').read_text())
assert set(config['services'])=={'api','postgres','redis'}
assert len(config['volumes'])==2
assert 'ports' not in config['services']['postgres']
assert 'ports' not in config['services']['redis']
print('PASS literal SQL and Compose YAML syntax; runtime NOT tested')
+17
View File
@@ -0,0 +1,17 @@
-- All synthetic rows are rolled back. EXPLAIN ANALYZE really runs SELECT.
BEGIN;
INSERT INTO users(id,username,display_name,password_hash) VALUES('10000000-0000-0000-0000-000000000001','plan_'||substr(md5(random()::text),1,20),'Plan fixture','not-a-login-hash');
INSERT INTO chats(id,title) VALUES('20000000-0000-0000-0000-000000000001','EXPLAIN temporary fixture');
INSERT INTO chat_members VALUES('20000000-0000-0000-0000-000000000001','10000000-0000-0000-0000-000000000001');
INSERT INTO messages(id,chat_id,sender_id,text,created_at)
SELECT md5('lab2-plan-'||g)::uuid,'20000000-0000-0000-0000-000000000001','10000000-0000-0000-0000-000000000001','fixture '||g,
timestamptz '2026-01-01 00:00:00+00'+g*interval '1 millisecond'
FROM generate_series(1,10000) AS g;
ANALYZE messages;
SELECT 'fixture_rows='||count(*) FROM messages WHERE chat_id='20000000-0000-0000-0000-000000000001';
EXPLAIN (ANALYZE, BUFFERS)
SELECT id,chat_id,sender_id,text,created_at FROM messages
WHERE chat_id='20000000-0000-0000-0000-000000000001'
AND (created_at,id)>(timestamptz '2026-01-01 00:00:09+00','00000000-0000-0000-0000-000000000000'::uuid)
ORDER BY created_at,id LIMIT 51;
ROLLBACK;
+36
View File
@@ -0,0 +1,36 @@
#!/usr/bin/env python3
"""Restart all lab2 services, preserving named volumes; safe evidence only."""
from pathlib import Path
import subprocess
import sys
ROOT=Path(__file__).resolve().parents[1]
EVIDENCE=ROOT/'evidence'
def run(*args, timeout=75):
return subprocess.run(args,cwd=ROOT,check=True,capture_output=True,
encoding='utf-8',errors='replace',timeout=timeout).stdout
def snapshot(name):
ids=run('docker','compose','ps','-a','-q').split()
if len(ids)!=3:
raise RuntimeError('Expected three lab2 containers before/after restart')
data=run('docker','inspect','--format','{{.Name}} {{.State.StartedAt}} {{.State.Status}}',*ids)
(EVIDENCE/name).write_text(data,encoding='utf-8')
def main():
EVIDENCE.mkdir(exist_ok=True)
snapshot('restart-before.txt')
run('docker','compose','stop','--timeout','5','api','postgres','redis',timeout=25)
run('docker','compose','up','-d','--wait','--wait-timeout','60',timeout=70)
snapshot('restart-after.txt')
print('Restarted API, PostgreSQL and Redis; named volumes preserved.')
if __name__=='__main__':
try: main()
except Exception as ex:
EVIDENCE.mkdir(exist_ok=True)
# Do not dump Docker configuration, environment or secret values.
message='Restart failed: '+type(ex).__name__+'; inspect docker compose ps and local service logs.'
(EVIDENCE/'restart-error.txt').write_text(message+'\n',encoding='utf-8')
print(message,file=sys.stderr)
sys.exit(1)
+9
View File
@@ -0,0 +1,9 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
mkdir -p evidence
# Safe inspect fields only: never serialize Config.Env or secret content.
docker compose ps -q | xargs -r docker inspect --format '{{.Name}} {{.State.StartedAt}}' > evidence/restart-before.txt
docker compose stop api postgres redis
docker compose up -d --wait --wait-timeout 75
docker compose ps -q | xargs -r docker inspect --format '{{.Name}} {{.State.StartedAt}}' > evidence/restart-after.txt
+7
View File
@@ -0,0 +1,7 @@
@echo off
if defined LAB2_PYTHON goto configured
py -X utf8 "%~dp0restart.py"
exit /b %errorlevel%
:configured
"%LAB2_PYTHON%" -X utf8 "%~dp0restart.py"
exit /b %errorlevel%
+21
View File
@@ -0,0 +1,21 @@
#!/usr/bin/env python3
"""Create local secrets once. Never print their contents or rotate existing values."""
import base64, secrets, shutil
from pathlib import Path
root=Path(__file__).resolve().parents[1]
directory=root/'secrets'
directory.mkdir(exist_ok=True,mode=0o700)
password_file=directory/'postgres_password'
if not password_file.exists():
password_file.write_text(secrets.token_hex(24))
password=password_file.read_text().strip()
values={'database_url':f'Host=postgres;Port=5432;Database=messenger;Username=messenger;Password={password}',
'cache_url':'redis:6379','cursor_key':base64.b64encode(secrets.token_bytes(32)).decode()}
for name,value in values.items():
p=directory/name
if not p.exists(): p.write_text(value)
# Compose file-backed secrets must be readable by non-root API UID.
# Parent directory is owner-only on the host; no secret files enter the image.
for p in directory.iterdir(): p.chmod(0o444)
if not (root/'.env').exists(): shutil.copyfile(root/'.env.example',root/'.env')
print('Local configuration ready (existing secrets preserved).')
+145
View File
@@ -0,0 +1,145 @@
#!/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)
+9
View File
@@ -0,0 +1,9 @@
#!/usr/bin/env python3
import time, urllib.request
for attempt in range(90):
try:
with urllib.request.urlopen('http://127.0.0.1:8080/health/ready',timeout=4) as r:
if r.status==200: print('Ready'); break
except (OSError, TimeoutError): pass
time.sleep(1)
else: raise SystemExit('API did not become ready')