ЛР 2: PostgreSQL, Redis-сессии и проверки на оценку 4
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Create local synthetic credentials once; never print them or replace existing files."""
|
||||
from pathlib import Path
|
||||
import secrets
|
||||
|
||||
root = Path(__file__).resolve().parents[1]
|
||||
folder = root / 'secrets'
|
||||
folder.mkdir(exist_ok=True)
|
||||
password_file = folder / 'db_password'
|
||||
if not password_file.exists():
|
||||
password_file.write_text(secrets.token_hex(24), encoding='utf-8')
|
||||
password = password_file.read_text(encoding='utf-8').strip()
|
||||
values = {
|
||||
'database_url': 'postgresql://messenger:' + password + '@db:5432/messenger',
|
||||
'cache_url': 'redis://redis:6379/0',
|
||||
'cursor_key': secrets.token_hex(32),
|
||||
}
|
||||
for name, value in values.items():
|
||||
target = folder / name
|
||||
if not target.exists():
|
||||
target.write_text(value, encoding='utf-8')
|
||||
if not (root / '.env').exists():
|
||||
(root / '.env').write_text('APP_HOST=0.0.0.0\nAPP_PORT=8080\nAPP_ENV=lab\nSESSION_TTL_SECONDS=3600\nSESSION_STORE=redis\n', encoding='utf-8')
|
||||
print('Local .env and secrets ready (existing values preserved).')
|
||||
@@ -0,0 +1,18 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Separate API container with 2 second TTL; main API configuration is unchanged."""
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
root = Path(__file__).resolve().parents[1]
|
||||
command = ['docker', 'compose', 'run', '-d', '--no-deps', '-e', 'SESSION_TTL_SECONDS=2', 'api']
|
||||
identifier = subprocess.check_output(command, cwd=root, text=True).strip()
|
||||
try:
|
||||
# Start the client in the temporary container after its HTTP server is ready.
|
||||
subprocess.run(['docker', 'exec', identifier, 'python', '-c',
|
||||
"import time,urllib.request; "
|
||||
"\nfor _ in range(50):\n try:\n urllib.request.urlopen('http://localhost:8080/health/ready',timeout=3); break"
|
||||
"\n except OSError: time.sleep(.2)\nelse: raise RuntimeError('API not ready')"], check=True)
|
||||
subprocess.run(['docker', 'exec', identifier, 'python', 'student-tests/test_expiry.py'], check=True)
|
||||
finally:
|
||||
subprocess.run(['docker', 'rm', '-f', identifier], check=True, stdout=subprocess.DEVNULL)
|
||||
@@ -0,0 +1,49 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Check committed sources in a new local clone with fresh secrets and new volumes."""
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
clone = ROOT / 'output' / 'clean-clone'
|
||||
project = 'mtusi-lab2-osokin-clean'
|
||||
env = dict(os.environ, PYTHONIOENCODING='utf-8', BASE_URL='http://127.0.0.1:18082')
|
||||
|
||||
|
||||
def main():
|
||||
if clone.exists():
|
||||
raise SystemExit('output/clean-clone already exists; preserve or move it before repeating this check.')
|
||||
clone.parent.mkdir(exist_ok=True)
|
||||
log = []
|
||||
def run(command, cwd):
|
||||
log.append('$ ' + ' '.join(command).replace(str(ROOT), '<workspace>').replace(sys.executable, 'python'))
|
||||
process = subprocess.run(command, cwd=cwd, env=env, text=True, encoding='utf-8', errors='replace',
|
||||
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
||||
log.append(process.stdout)
|
||||
cleaned = '\n'.join(line.rstrip() for line in '\n'.join(log).splitlines()) + '\n'
|
||||
(ROOT / 'evidence' / 'clean-clone.txt').write_text(cleaned, encoding='utf-8')
|
||||
if process.returncode:
|
||||
raise SystemExit('Clean clone check failed; see evidence/clean-clone.txt')
|
||||
run(['git', 'clone', '--no-hardlinks', str(ROOT), str(clone)], ROOT)
|
||||
assert not (clone / '.env').exists()
|
||||
assert not (clone / 'secrets').exists()
|
||||
run([sys.executable, 'scripts/bootstrap.py'], clone)
|
||||
local_env = clone / '.env'
|
||||
local_env.write_text(local_env.read_text(encoding='utf-8').replace('APP_PORT=8080', 'APP_PORT=18082'), encoding='utf-8')
|
||||
compose = ['docker', 'compose', '-p', project, '--env-file', '.env']
|
||||
try:
|
||||
run(compose + ['build', '--no-cache', '--quiet'], clone)
|
||||
run(compose + ['up', '-d', '--wait'], clone)
|
||||
run([sys.executable, 'scripts/check_contract.py'], clone)
|
||||
run([sys.executable, 'tests/smoke.py', '--base-url', env['BASE_URL']], clone)
|
||||
run(compose + ['exec', '-T', 'api', 'python', 'student-tests/test_database.py'], clone)
|
||||
run(compose + ['logs', '--no-log-prefix', 'migrate'], clone)
|
||||
finally:
|
||||
# Keep even the disposable check's volumes; only stop its isolated containers.
|
||||
run(compose + ['down'], clone)
|
||||
print('PASS: clean local clone; no copied secrets, fresh volumes, no-cache build, smoke/database checks.')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,24 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Human-readable demonstration. Passwords and session tokens stay in memory."""
|
||||
from pathlib import Path
|
||||
import sys
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests'))
|
||||
from client import Client
|
||||
|
||||
c = Client()
|
||||
alice, bob, eve, chat = c.bootstrap()
|
||||
print('1. Readiness 200; registered three users; Basic login returned Bearer sessions.')
|
||||
print('2. Created chat:', chat['id'], '; members: Alice and Bob.')
|
||||
message = c.message(alice, chat, 'Привет, Боб! Это лабораторная 2.')['json']
|
||||
print('3. POST message -> 201; message id:', message['id'])
|
||||
page = c.call('GET', '/api/v1/chats/' + chat['id'] + '/messages', actor=bob, schema='MessagePage')['json']
|
||||
assert page['items'] == [message]
|
||||
print('4. Bob reads Alice\'s committed message -> 200:', message['text'])
|
||||
c.call('GET', '/api/v1/chats/' + chat['id'], 404, actor=eve)
|
||||
print('5. Eve is authenticated but not a chat member -> 404.')
|
||||
c.call('GET', '/api/v1/users/me', 401, headers={'X-User-Id': alice['id']})
|
||||
print('6. X-User-Id without Bearer -> 401.')
|
||||
c.call('DELETE', '/api/v1/auth/sessions/current', 204, actor=alice)
|
||||
c.call('GET', '/api/v1/users/me', 401, actor=alice)
|
||||
print('7. Logout -> 204; previous token -> 401. Demo PASS.')
|
||||
@@ -0,0 +1,36 @@
|
||||
#!/usr/bin/env python3
|
||||
"""One index, 10,000 messages; grade 4 evidence, not a grade 5 comparison."""
|
||||
import json
|
||||
import uuid
|
||||
from messenger.core import Config, HISTORY_SQL, Store
|
||||
|
||||
store = Store(Config.from_env())
|
||||
try:
|
||||
actor = store.create_user(dict(username='plan_' + uuid.uuid4().hex[:20],
|
||||
display_name='Plan fixture', password='synthetic-plan-password'))['id']
|
||||
chat = store.create_chat(actor, dict(title='EXPLAIN 10000', member_ids=[actor]))
|
||||
with store.pool.connection() as conn:
|
||||
conn.execute("INSERT INTO messages(id,chat_id,sender_id,text,created_at) "
|
||||
"SELECT md5(%s || i::text)::uuid, %s, %s, 'fixture ' || i, "
|
||||
"timestamptz '2026-01-01 UTC' + i * interval '1 microsecond' "
|
||||
"FROM generate_series(1,10000) i", (chat['id'], chat['id'], actor))
|
||||
conn.execute('ANALYZE messages')
|
||||
key = conn.execute('SELECT created_at,id FROM messages WHERE chat_id=%s '
|
||||
'ORDER BY created_at,id LIMIT 1 OFFSET 8999', (chat['id'],)).fetchone()
|
||||
plan = conn.execute('EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON) ' + HISTORY_SQL,
|
||||
(chat['id'], key['created_at'], key['id'], 51)).fetchone()['QUERY PLAN']
|
||||
count = conn.execute('SELECT count(*) AS n FROM messages WHERE chat_id=%s', (chat['id'],)).fetchone()['n']
|
||||
# OFFSET is used once to choose a fixture position, never in the API query.
|
||||
assert count == 10000
|
||||
nodes = []
|
||||
def walk(node):
|
||||
nodes.append(node)
|
||||
for child in node.get('Plans', []):
|
||||
walk(child)
|
||||
walk(plan[0]['Plan'])
|
||||
assert any(n.get('Index Name') == 'messages_history' for n in nodes)
|
||||
assert all(n['Node Type'] != 'Seq Scan' for n in nodes)
|
||||
print(json.dumps(dict(messages=count, cursor_position=9000, page_limit=50, fetched_with_lookahead=51,
|
||||
sql=HISTORY_SQL, explain=plan), ensure_ascii=False, indent=2))
|
||||
finally:
|
||||
store.close()
|
||||
@@ -0,0 +1,4 @@
|
||||
@echo off
|
||||
cd /d "%~dp0.."
|
||||
python scripts\restart.py
|
||||
exit /b %errorlevel%
|
||||
@@ -0,0 +1,38 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Restart precisely this lab's API, PostgreSQL and Redis, keeping both volumes."""
|
||||
import json
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
def compose(*args):
|
||||
return subprocess.check_output(['docker', 'compose', '--env-file', '.env', *args], cwd=ROOT, text=True)
|
||||
|
||||
|
||||
def states():
|
||||
result = {}
|
||||
for service in ('api', 'db', 'redis'):
|
||||
identifier = compose('ps', '-q', service).strip()
|
||||
data = json.loads(subprocess.check_output(['docker', 'inspect', identifier], text=True))[0]
|
||||
result[service] = dict(id=identifier, started_at=data['State']['StartedAt'],
|
||||
mounts=[dict(name=m.get('Name'), destination=m['Destination'])
|
||||
for m in data['Mounts'] if m['Type'] == 'volume'])
|
||||
return result
|
||||
|
||||
|
||||
def main():
|
||||
before = states()
|
||||
subprocess.run(['docker', 'compose', '--env-file', '.env', 'restart', 'api', 'db', 'redis'],
|
||||
cwd=ROOT, check=True)
|
||||
after = states()
|
||||
for service in before:
|
||||
assert before[service]['started_at'] != after[service]['started_at'], service + ' did not restart'
|
||||
assert before[service]['mounts'] == after[service]['mounts'], service + ' changed volumes'
|
||||
(ROOT / 'evidence' / 'restart.json').write_text(json.dumps(dict(before=before, after=after), indent=2) + '\n')
|
||||
print('Restarted api/db/redis; volume names unchanged. Evidence: evidence/restart.json')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Executable
+4
@@ -0,0 +1,4 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
exec python3 scripts/restart.py
|
||||
@@ -0,0 +1,74 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Run the grade 4 checks and preserve small sanitized evidence files."""
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import platform
|
||||
import subprocess
|
||||
import sys
|
||||
from datetime import datetime, timezone
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
EVIDENCE = ROOT / 'evidence'
|
||||
env = dict(os.environ, BASE_URL=os.environ.get('BASE_URL', 'http://127.0.0.1:8080'), PYTHONIOENCODING='utf-8')
|
||||
python = sys.executable
|
||||
if hasattr(sys.stdout, 'reconfigure'):
|
||||
sys.stdout.reconfigure(encoding='utf-8')
|
||||
|
||||
|
||||
def run(name, command):
|
||||
print('Running', name, flush=True)
|
||||
process = subprocess.run(command, cwd=ROOT, env=env, text=True, encoding='utf-8', errors='replace',
|
||||
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
||||
log = '$ ' + ' '.join(command).replace(python, 'python') + '\n' + process.stdout
|
||||
log = '\n'.join(line.rstrip() for line in log.splitlines()).rstrip() + '\n'
|
||||
(EVIDENCE / (name + '.txt')).write_text(log, encoding='utf-8')
|
||||
print(process.stdout, end='', flush=True)
|
||||
if process.returncode:
|
||||
raise SystemExit('Check failed: ' + name)
|
||||
|
||||
|
||||
def main():
|
||||
EVIDENCE.mkdir(exist_ok=True)
|
||||
run('check', [python, 'scripts/check_contract.py'])
|
||||
run('compose', ['docker', 'compose', '--env-file', '.env', 'config', '--quiet'])
|
||||
run('smoke', [python, 'tests/smoke.py', '--base-url', env['BASE_URL']])
|
||||
run('own-api', [python, 'student-tests/test_api.py'])
|
||||
run('database', ['docker', 'compose', 'exec', '-T', 'api', 'python', 'student-tests/test_database.py'])
|
||||
run('persistence', [python, 'tests/resilience.py', '--mode', 'persistence', '--action-script',
|
||||
'scripts/restart.cmd' if os.name == 'nt' else 'scripts/restart.sh', '--base-url', env['BASE_URL']])
|
||||
run('expiry', [python, 'scripts/check_expiry.py'])
|
||||
run('outages', [python, 'student-tests/test_outages.py'])
|
||||
run('demo', [python, 'scripts/demo.py'])
|
||||
run('durability-postgres', ['docker', 'compose', 'exec', '-T', 'db', 'psql', '-U', 'messenger',
|
||||
'-d', 'messenger', '-c', 'SHOW fsync;', '-c', 'SHOW synchronous_commit;',
|
||||
'-c', 'SHOW full_page_writes;'])
|
||||
run('durability-redis', ['docker', 'compose', 'exec', '-T', 'redis', 'redis-cli',
|
||||
'CONFIG', 'GET', 'appendonly', 'appendfsync'])
|
||||
output = subprocess.check_output(['docker', 'compose', 'exec', '-T', 'api', 'python',
|
||||
'scripts/explain_history.py'], cwd=ROOT, text=True)
|
||||
plan = json.loads(output)
|
||||
(EVIDENCE / 'explain.json').write_text(json.dumps(plan, ensure_ascii=False, indent=2) + '\n', encoding='utf-8')
|
||||
print('PASS: EXPLAIN on', plan['messages'], 'messages uses messages_history.', flush=True)
|
||||
run('resources', ['docker', 'stats', '--no-stream', '--format',
|
||||
'{{.Name}} CPU={{.CPUPerc}} RAM={{.MemUsage}}',
|
||||
'mtusi-lab2-osokin-api-1', 'mtusi-lab2-osokin-db-1', 'mtusi-lab2-osokin-redis-1'])
|
||||
environment = dict(verified_at_utc=datetime.now(timezone.utc).isoformat(), os=platform.platform(),
|
||||
client_python=platform.python_version())
|
||||
environment['docker'] = json.loads(subprocess.check_output(
|
||||
['docker', 'info', '--format', '{{json .}}'], text=True))
|
||||
# Whitelist machine properties; never persist Docker client config/proxy details.
|
||||
environment['docker'] = {k: environment['docker'][k] for k in
|
||||
('ServerVersion', 'OperatingSystem', 'KernelVersion', 'NCPU', 'MemTotal', 'Architecture')}
|
||||
environment['images'] = {}
|
||||
environment['runtime_python'] = subprocess.check_output(
|
||||
['docker', 'compose', 'exec', '-T', 'api', 'python', '--version'], cwd=ROOT, text=True).strip()
|
||||
for image in ('postgres:17.6-bookworm', 'redis:7.4.5-bookworm'):
|
||||
environment['images'][image] = json.loads(subprocess.check_output(
|
||||
['docker', 'image', 'inspect', image, '--format', '{{json .RepoDigests}}'], text=True))
|
||||
(EVIDENCE / 'environment.json').write_text(json.dumps(environment, indent=2) + '\n', encoding='utf-8')
|
||||
print('PASS: all grade 4 scenarios completed; no grade 5 track was run.')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user