Files
mtusi2026lab3/student-tests/test_dependencies.py
T

44 lines
2.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Проверка ограниченного отказа зависимостей без удаления учебного тома."""
from pathlib import Path
import subprocess
import sys
import time
import urllib.error
import urllib.request
sys.path.insert(0,str(Path(__file__).resolve().parents[1]/'tests'))
from client import Client, check
def compose(*args):
subprocess.run(['docker','compose',*args],check=True,stdout=subprocess.DEVNULL)
c=Client()
alice=c.user('dependency')
try:
compose('stop','redis')
cache_started=time.monotonic()
check(c.call('GET','/api/v1/users/me',actor=alice,schema='User')['json']['id']==alice['id'],'нет fallback к PostgreSQL')
check(time.monotonic()-cache_started<2,'кэш задержал fallback более чем на 2 секунды')
print('PASS: Redis остановлен, действующая сессия и профиль доступны из PostgreSQL за',round(time.monotonic()-cache_started,3),'с')
finally:
compose('start','redis')
try:
compose('stop','db')
started=time.monotonic()
req=urllib.request.Request('http://localhost:8080/api/v1/users/me',headers={'Authorization':'Bearer '+alice['token']})
try:
urllib.request.urlopen(req,timeout=8)
except urllib.error.HTTPError as error:
check(error.code==503,'при недоступной БД должен быть ограниченный отказ 503')
error.close()
else:
raise AssertionError('запрос доверен при недоступной БД')
check(time.monotonic()-started<8,'отказ не ограничен таймаутом')
print('PASS: PostgreSQL остановлен; закрытый отказ 503 за',round(time.monotonic()-started,3),'с')
finally:
compose('start','db')
subprocess.run([sys.executable,'scripts/wait_ready.py','--replicas','2'],check=True)
check(c.call('GET','/api/v1/users/me',actor=alice,schema='User')['json']['id']==alice['id'],'прежняя сессия не восстановилась')
print('PASS: прежняя сессия сохранилась после восстановления БД')