forked from mtusi2026/mtusi2026lab4
51 lines
2.5 KiB
Python
Executable File
51 lines
2.5 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Загрузка -> остановка реплики, ОБСЛУЖИВШЕЙ загрузку -> скачивание через другую.
|
|
|
|
Публичный resilience.py останавливает реплику, принявшую сообщение; сценарий README требует именно
|
|
реплику, принявшую файл, поэтому он проверяется отдельно. Реплику возвращает вызывающий скрипт.
|
|
"""
|
|
import hashlib
|
|
import os
|
|
import subprocess
|
|
import time
|
|
|
|
from common import Client, ROOT, SAMPLE_PNG, arguments, check, run
|
|
|
|
|
|
def main():
|
|
def configure(parser):
|
|
parser.add_argument('--avoid-instance', default='', help='Не останавливать эту реплику: так проверяется каждая по очереди')
|
|
args = arguments('Upload, stop the replica that served it, download via another', configure)
|
|
c = Client(args.base_url, args.ca_file, timeout=10)
|
|
alice, bob, _, chat = c.bootstrap()
|
|
path = '/api/v1/chats/' + chat['id'] + '/attachments?filename=sample.png'
|
|
for _ in range(10):
|
|
response = c.call('POST', path, 201, actor=alice, raw=SAMPLE_PNG, headers={'Content-Type': 'image/png'}, schema='Attachment')
|
|
target = response['headers']['X-Instance-Id']
|
|
if target != args.avoid_instance:
|
|
break
|
|
check(target != args.avoid_instance, 'all uploads were served by the replica to avoid')
|
|
attachment = response['json']
|
|
print('upload served by replica', target, 'status', response['status'])
|
|
|
|
env = dict(os.environ, TARGET_INSTANCE_ID=target)
|
|
subprocess.run([str(ROOT / 'scripts/stop-one.sh')], env=env, check=True, stdout=subprocess.DEVNULL)
|
|
print('replica', target, 'stopped')
|
|
|
|
url = '/api/v1/attachments/' + attachment['id'] + '/content'
|
|
served = set()
|
|
started = time.monotonic()
|
|
for _ in range(5):
|
|
download = c.call('GET', url, actor=bob)
|
|
served.add(download['headers']['X-Instance-Id'])
|
|
check(download['bytes'] == SAMPLE_PNG, 'downloaded bytes differ')
|
|
check(hashlib.sha256(download['bytes']).hexdigest() == attachment['sha256'], 'checksum differs')
|
|
check(target not in served, 'stopped replica still serves downloads')
|
|
print('downloaded 5 times via', sorted(served), 'in %.3fs, SHA-256 matches' % (time.monotonic() - started))
|
|
print('PASS: file survives stop of the replica that uploaded it')
|
|
print('STOPPED=' + target)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
run(main)
|