Files
mtusi2026lab5/student-tests/dependencies.py
T
2026-10-26 18:05:06 +03:00

39 lines
1.9 KiB
Python

"""Отказы Redis и PostgreSQL; старые токены и подтверждения после восстановления."""
import os
from pathlib import Path
import sys
import time
import urllib.request
import urllib.error
sys.path.insert(0,str(Path(__file__).resolve().parents[1]))
sys.path.insert(0,str(Path(__file__).resolve().parents[1]/'tests'))
from client import Client
from scripts.stack import command,wait,save
base=os.getenv('BASE_URL','http://localhost:8080');c=Client(base,timeout=15)
alice,bob,_,chat=c.bootstrap();message=c.message(alice,chat,'До отказов')['json']
att,data=c.attachment(alice,chat);att=c.wait_ready(alice,att)
results={}
try:
command(['docker','compose','stop','cache'])
for _ in range(6):c.call('GET','/api/v1/users/me',actor=alice,schema='User')
c.message(alice,chat,'Без кеша');results['redis']='PASS'
finally:command(['docker','compose','up','-d','--wait','cache'])
try:
command(['docker','compose','stop','db']);time.sleep(4)
codes=[]
for path in ['/health/ready','/api/v1/users/me']:
request=urllib.request.Request(base+path,headers={'Authorization':'Bearer '+alice['token']})
try:
with urllib.request.urlopen(request,timeout=10) as response:codes.append(response.status)
except urllib.error.HTTPError as exc:codes.append(exc.code)
assert codes==[503,503];results['postgres']=codes
finally:
command(['docker','compose','up','-d','--wait','db']);wait(2,base)
page=c.call('GET','/api/v1/chats/'+chat['id']+'/messages',actor=bob,schema='MessagePage')['json']
assert message in page['items']
assert c.call('GET','/api/v1/attachments/'+att['id']+'/content',actor=bob)['bytes']==data
results['old_token_message_original_preserved']=True
save('dependencies.json',results)
print('PASS: Redis fallback, PostgreSQL 503 and recovery, old token/message/original preserved')