Add second lab code

This commit is contained in:
2026-10-23 15:12:43 +03:00
parent 0ab3a341aa
commit cd9c3402fa
40 changed files with 1692 additions and 10 deletions
+53
View File
@@ -0,0 +1,53 @@
"""Explicitly stop each storage service on the local lab stack, then restore it."""
import subprocess
import sys
import time
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / 'tests'))
from client import Client
def compose(*args):
subprocess.run(['docker', 'compose', *args], cwd=ROOT, check=True, timeout=60)
def main():
client = Client(timeout=6)
actor = client.user('failure')
chat = client.call('POST', '/api/v1/chats', 201, actor=actor,
body={'title': 'Failure test', 'member_ids': [actor['id']]}, schema='Chat')['json']
message = client.message(actor, chat, 'Must survive storage failure')['json']
for service in ('redis', 'postgres'):
try:
compose('stop', service)
elapsed = []
for _ in range(3):
result = client.call('GET', '/api/v1/users/me', 503, actor=actor)
assert result['elapsed'] < 5, 'Dependency error took too long'
elapsed.append(round(result['elapsed'], 3))
client.call('GET', '/health/ready', 503)
client.call('GET', '/health/live', 200, schema='Health')
client.call('GET', '/api/v1/users/me', 401, headers={'X-User-Id': actor['id']})
client.call('GET', '/api/v1/users/me', 401, actor={'token': 'unknown'})
print(f'PASS {service} unavailable: 503; seconds={elapsed}; anonymous=401; live=200', flush=True)
finally:
compose('start', service)
start = time.monotonic()
while time.monotonic() - start < 60:
try:
client.call('GET', '/health/ready')
break
except AssertionError:
time.sleep(0.5)
else:
raise AssertionError('Storage recovery timed out')
client.call('GET', '/api/v1/users/me', actor=actor, schema='User')
page = client.call('GET', f"/api/v1/chats/{chat['id']}/messages", actor=actor, schema='MessagePage')['json']
assert page['items'] == [message]
print(f'PASS {service} recovered in {time.monotonic() - start:.2f}s: same session and message', flush=True)
if __name__ == '__main__':
main()
+135
View File
@@ -0,0 +1,135 @@
import os
import subprocess
import sys
import time
import unittest
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
from unittest.mock import patch
from urllib.parse import quote
from uuid import uuid4
from redis.exceptions import ConnectionError
from sqlalchemy import select
from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm import Session
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests'))
from client import Client
from scripts.cache_experiment import make_store, stale_race
from src.auth import sessions
from src.cache import HistoryCache, history_cache
from src.database import create_db_engine
from src.errors import APIError
from src.models import Chat
class CacheTests(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.engine = create_db_engine()
@classmethod
def tearDownClass(cls):
cls.engine.dispose()
def setUp(self):
self.client = Client('http://127.0.0.1:8080')
self.actor = self.client.user('cache')
self.chat = self.client.call('POST', '/api/v1/chats', 201, actor=self.actor,
body={'title': 'Cache', 'member_ids': [self.actor['id']]}, schema='Chat')['json']
self.prefix = 'test-history:' + uuid4().hex
self.cache = HistoryCache(sessions.client, prefix=self.prefix)
self.reader = make_store(self.engine, self.cache)
self.writer = make_store(self.engine, history_cache)
def tearDown(self):
for key in sessions.client.scan_iter(match=self.prefix + ':*'):
sessions.client.delete(key)
def test_late_fill_cannot_restore_old_page(self):
result = stale_race(self.reader, self.writer, self.actor['id'], self.chat['id'])
self.assertFalse(result['stale'])
def test_hit_still_checks_membership_and_cache_failure_falls_back(self):
args = (self.chat['id'], self.actor['id'], 100, None)
self.writer.create_message(self.chat['id'], self.actor['id'], 'hello')
page = self.reader.list_messages(*args)
with patch.object(self.reader, '_load_messages', side_effect=AssertionError('Cache miss')):
self.assertEqual(self.reader.list_messages(*args), page)
with self.assertRaises(APIError) as error:
self.reader.list_messages(self.chat['id'], str(uuid4()), 100, None)
self.assertEqual(error.exception.status_code, 404)
with patch.object(self.cache.client, 'get', side_effect=ConnectionError('unavailable')):
self.assertEqual(self.reader.list_messages(*args), page)
self.cache.client.delete(self.cache.key(self.chat['id'], 1, 100, None))
with patch.object(self.cache.client, 'set', side_effect=ConnectionError('unavailable')):
self.assertEqual(self.reader.list_messages(*args), page)
def test_version_rolls_back_with_failed_message(self):
with self.assertRaises(IntegrityError) as error:
self.writer.create_message(self.chat['id'], self.actor['id'], '')
self.assertEqual(error.exception.orig.sqlstate, '23514')
with Session(self.engine) as session:
self.assertEqual(session.get(Chat, self.chat['id']).history_version, 0)
def test_concurrent_writers_do_not_lose_version_increments(self):
self.assertEqual(self.reader.list_messages(self.chat['id'], self.actor['id'], 100, None)['items'], [])
def write(n):
return self.writer.create_message(self.chat['id'], self.actor['id'], f'message {n}')
with ThreadPoolExecutor(max_workers=4) as executor:
messages = list(executor.map(write, range(8)))
with Session(self.engine) as session:
self.assertEqual(session.get(Chat, self.chat['id']).history_version, 8)
page = self.reader.list_messages(self.chat['id'], self.actor['id'], 100, None)
self.assertEqual({item['id'] for item in page['items']}, {item['id'] for item in messages})
def test_http_read_after_write_and_cursor_pages(self):
path = f"/api/v1/chats/{self.chat['id']}/messages"
def read(query=''):
return self.client.call('GET', path + query, actor=self.actor, schema='MessagePage')['json']
self.assertEqual(read()['items'], [])
first = self.client.message(self.actor, self.chat, 'one')['json']
self.assertEqual(read()['items'], [first])
self.client.message(self.actor, self.chat, 'two')
page = read('?limit=1')
cursor = quote(page['next_cursor'], safe='')
tail = read('?limit=1&cursor=' + cursor)
self.assertIsNone(tail['next_cursor'])
third = self.client.message(self.actor, self.chat, 'three')['json']
tail = read('?limit=1&cursor=' + cursor)
self.assertIsNotNone(tail['next_cursor'])
self.assertEqual(read('?limit=1&cursor=' + quote(tail['next_cursor'], safe=''))['items'], [third])
self.assertEqual(len(read()['items']), 3)
with Session(self.engine) as session:
version = session.scalar(select(Chat.history_version).where(Chat.id == self.chat['id']))
key = history_cache.key(self.chat['id'], version, 50, None)
self.assertGreater(sessions.client.ttl(key), 0)
self.assertLessEqual(sessions.client.ttl(key), history_cache.ttl)
def test_read_after_write_through_another_api_process(self):
process = subprocess.Popen([sys.executable, '-m', 'src'],
env={**os.environ, 'APP_HOST': '127.0.0.1', 'APP_PORT': '8082'},
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
try:
replica = Client('http://127.0.0.1:8082')
for _ in range(100):
try:
replica.call('GET', '/health/ready')
break
except AssertionError:
self.assertIsNone(process.poll())
time.sleep(0.1)
else:
self.fail('Second API did not become ready')
path = f"/api/v1/chats/{self.chat['id']}/messages"
self.assertEqual(replica.call('GET', path, actor=self.actor, schema='MessagePage')['json']['items'], [])
message = self.client.message(self.actor, self.chat, 'written through first API')['json']
self.assertEqual(replica.call('GET', path, actor=self.actor, schema='MessagePage')['json']['items'], [message])
finally:
process.terminate()
process.wait(timeout=10)
if __name__ == '__main__':
unittest.main()
+38
View File
@@ -0,0 +1,38 @@
import unittest
from uuid import uuid4
from sqlalchemy import create_engine, inspect, select
from sqlalchemy.schema import CreateSchema, DropSchema
from scripts.cache_experiment import NoCache, make_store
from src.database import create_db_engine, migrate
from src.models import SchemaMigration
class MigrationTests(unittest.TestCase):
def test_migrate_empty_schema_and_preserve_orm_data_on_repeat(self):
engine = create_db_engine()
schema = 'orm_test_' + uuid4().hex
isolated = create_engine(engine.url, connect_args={
'connect_timeout': 2, 'options': f'-c search_path={schema} -c statement_timeout=3000'})
try:
with engine.begin() as conn:
conn.execute(CreateSchema(schema))
migrate(isolated)
store = make_store(isolated, NoCache())
user = store.create_user('migration_user', 'Migration', 'Synthetic-password-123')
chat = store.create_chat(user['id'], 'Migration', [user['id']])
message = store.create_message(chat['id'], user['id'], 'must survive migration repeat')
migrate(isolated)
self.assertEqual(store.list_messages(chat['id'], user['id'], 100, None)['items'], [message])
with isolated.connect() as conn:
self.assertEqual(set(conn.scalars(select(SchemaMigration.version))),
{'001_initial.sql', '002_history_version.sql'})
indexes = {index['name'] for index in inspect(isolated).get_indexes('messages')}
self.assertIn('messages_history_idx', indexes)
self.assertEqual(len(inspect(isolated).get_foreign_keys('messages')), 2)
finally:
isolated.dispose()
with engine.begin() as conn:
conn.execute(DropSchema(schema, cascade=True, if_exists=True))
engine.dispose()
+127
View File
@@ -0,0 +1,127 @@
"""Run inside the API container: python -m unittest discover -s student-tests -v."""
import base64
import os
import subprocess
import sys
import time
import unittest
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
from uuid import uuid4
from sqlalchemy import func, select
from sqlalchemy.orm import sessionmaker
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests'))
from client import Client
from src.auth import sessions
from src.database import create_db_engine, migrate
from src.models import User, Chat, ChatMember, SchemaMigration
class Lab2Tests(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.engine = create_db_engine()
cls.sessions = sessionmaker(cls.engine)
@classmethod
def tearDownClass(cls):
cls.engine.dispose()
def setUp(self):
self.client = Client('http://127.0.0.1:8080')
def test_registration_race(self):
body = {'username': 'race_' + uuid4().hex[:12], 'display_name': 'Race',
'password': 'Synthetic-password-123'}
def register(_):
return Client().call('POST', '/api/v1/users', [201, 409], body=body)['status']
with ThreadPoolExecutor(max_workers=4) as executor:
codes = list(executor.map(register, range(4)))
self.assertEqual(sorted(codes), [201, 409, 409, 409])
with self.sessions() as session:
count = session.scalar(select(func.count()).select_from(User).where(User.username == body['username']))
self.assertEqual(count, 1)
def test_chat_rollback(self):
actor = self.client.user('rollback')
title = 'rollback_' + uuid4().hex
self.client.call('POST', '/api/v1/chats', 400, actor=actor,
body={'title': title, 'member_ids': [actor['id'], str(uuid4())]})
with self.sessions() as session:
self.assertEqual(session.scalar(select(func.count()).select_from(Chat).where(Chat.title == title)), 0)
self.assertEqual(session.scalar(select(func.count()).select_from(ChatMember).where(
ChatMember.user_id == actor['id'])), 0)
def test_password_logout_and_identity(self):
actor = self.client.user('auth')
credential = base64.b64encode((actor['user']['username'] + ':Wrong-password-123').encode()).decode()
self.client.call('POST', '/api/v1/auth/sessions', 401,
headers={'Authorization': 'Basic ' + credential})
self.client.call('GET', '/api/v1/users/me', 401, headers={'Authorization': 'Basic ' + credential})
self.client.call('GET', '/api/v1/users/me', 401, headers={'X-User-Id': actor['id']})
me = self.client.call('GET', '/api/v1/users/me', actor=actor,
headers={'X-User-Id': str(uuid4())}, schema='User')['json']
self.assertEqual(me['id'], actor['id'])
ttl = sessions.client.ttl(sessions.key(actor['token']))
self.assertGreater(ttl, 0)
self.assertLessEqual(ttl, 3600)
self.client.call('DELETE', '/api/v1/auth/sessions/current', 204, actor=actor)
self.client.call('GET', '/api/v1/users/me', 401, actor=actor)
self.client.call('DELETE', '/api/v1/auth/sessions/current', 401, actor=actor)
self.assertIsNone(sessions.client.get(sessions.key(actor['token'])))
with self.sessions() as session:
encoded = session.get(User, actor['id']).password_hash
self.assertTrue(encoded.startswith('$argon2id$'))
self.assertNotIn(actor['registration']['password'], encoded)
def test_unicode_password(self):
username = 'unicode_' + uuid4().hex[:12]
password = '🙂:я' * 40
self.client.call('POST', '/api/v1/users', 201,
body={'username': username, 'display_name': 'Unicode', 'password': password})
credential = base64.b64encode((username + ':' + password).encode()).decode()
result = self.client.call('POST', '/api/v1/auth/sessions', 201,
headers={'Authorization': 'Basic ' + credential}, schema='Session')['json']
self.assertEqual(result['user']['username'], username)
def test_migrations_repeat(self):
with self.sessions() as session:
before = session.scalar(select(func.count()).select_from(User))
migrate(self.engine)
migrate(self.engine)
with self.sessions() as session:
self.assertEqual(session.scalar(select(func.count()).select_from(User)), before)
versions = set(session.scalars(select(SchemaMigration.version)))
expected = {path.name for path in (Path(__file__).resolve().parents[1] / 'migrations').glob('*.sql')}
self.assertEqual(versions, expected)
def test_expiry_short_ttl(self):
process = subprocess.Popen(
[sys.executable, '-m', 'src'],
env={**os.environ, 'SESSION_TTL_SECONDS': '2', 'APP_PORT': '8081', 'APP_HOST': '127.0.0.1'},
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
)
try:
client = Client('http://127.0.0.1:8081')
for _ in range(100):
try:
client.call('GET', '/health/ready')
break
except AssertionError:
self.assertIsNone(process.poll(), 'Short-TTL API exited')
time.sleep(0.1)
else:
self.fail('Short-TTL API did not become ready')
actor = client.user('expiry')
client.call('GET', '/api/v1/users/me', actor=actor, schema='User')
time.sleep(2.2)
client.call('GET', '/api/v1/users/me', 401, actor=actor)
self.assertIsNone(sessions.client.get(sessions.key(actor['token'])))
finally:
process.terminate()
process.wait(timeout=10)
if __name__ == '__main__':
unittest.main()