forked from mtusi2026/mtusi2026lab6
55 lines
2.3 KiB
Python
55 lines
2.3 KiB
Python
"""Stop the actual upload replica and check original bytes after failover and restart."""
|
|
from hashlib import sha256
|
|
import os
|
|
from pathlib import Path
|
|
import subprocess
|
|
import sys
|
|
import time
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT / 'tests'))
|
|
from client import Client, arguments, check, run
|
|
|
|
|
|
def main():
|
|
args = arguments(__doc__)
|
|
client = Client(args.base_url, args.ca_file, timeout=55)
|
|
alice, bob, _, chat = client.bootstrap()
|
|
path = f"/api/v1/chats/{chat['id']}/attachments?filename=sample.png"
|
|
original = (ROOT / 'tests/fixtures/sample.png').read_bytes()
|
|
reply = client.call('POST', path, 201, actor=alice, raw=original,
|
|
headers={'Content-Type': 'image/png'}, schema='Attachment')
|
|
aid = reply['json']['id']
|
|
upload_instance = reply['headers']['X-Instance-Id']
|
|
digest = sha256(original).hexdigest()
|
|
content = '/api/v1/attachments/' + aid + '/content'
|
|
text = client.message(alice, chat, '', attachments=[aid])['json']
|
|
|
|
def verify():
|
|
received = client.call('GET', content, actor=bob)
|
|
check(received['bytes'] == original and sha256(received['bytes']).hexdigest() == digest,
|
|
'original changed')
|
|
history = client.call('GET', f"/api/v1/chats/{chat['id']}/messages",
|
|
actor=bob, schema='MessagePage')['json']
|
|
check(text in history['items'], 'message attachment link lost')
|
|
return received['headers']['X-Instance-Id']
|
|
|
|
start = time.monotonic()
|
|
try:
|
|
subprocess.run(['scripts/stop-one.sh'], cwd=ROOT, check=True, timeout=30,
|
|
env={**os.environ, 'TARGET_INSTANCE_ID': upload_instance})
|
|
instance = verify()
|
|
check(instance != upload_instance, 'download was served by the stopped uploader')
|
|
print(f'PASS: {upload_instance} -> {instance}, SHA-256 {digest}, {time.monotonic()-start:.2f}s')
|
|
finally:
|
|
subprocess.run([sys.executable, 'scripts/manage.py', 'restore'], cwd=ROOT, check=True, timeout=100)
|
|
|
|
start = time.monotonic()
|
|
subprocess.run(['scripts/restart.sh'], cwd=ROOT, check=True, timeout=100)
|
|
instance = verify()
|
|
print(f'PASS: after full restart via {instance}, SHA-256 {digest}, {time.monotonic()-start:.2f}s')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
run(main)
|