forked from mtusi2026/mtusi2026lab4
273 lines
6.2 KiB
Python
273 lines
6.2 KiB
Python
#!/usr/bin/env python3
|
|
|
|
import hashlib
|
|
import re
|
|
import socket
|
|
import subprocess
|
|
import time
|
|
from pathlib import Path
|
|
from urllib.parse import urlsplit
|
|
|
|
from client import Client, arguments, check, run
|
|
|
|
|
|
FIXTURE = (
|
|
Path(__file__).resolve().parent
|
|
/ "fixtures"
|
|
/ "sample.png"
|
|
)
|
|
|
|
|
|
def command_output(command):
|
|
result = subprocess.run(
|
|
command,
|
|
stdout=subprocess.PIPE,
|
|
stderr=subprocess.PIPE,
|
|
text=True,
|
|
timeout=10,
|
|
)
|
|
|
|
check(
|
|
result.returncode == 0,
|
|
"Docker command failed; check that API containers are running",
|
|
)
|
|
|
|
return result.stdout.strip()
|
|
|
|
|
|
def api_containers():
|
|
output = command_output([
|
|
"docker", "compose", "ps", "-q", "api",
|
|
])
|
|
|
|
containers = output.splitlines()
|
|
|
|
check(
|
|
len(containers) == 2,
|
|
"This test requires exactly two running API replicas",
|
|
)
|
|
|
|
for container in containers:
|
|
check(
|
|
re.fullmatch(r"[a-f0-9]{12,64}", container) is not None,
|
|
"Unexpected container ID",
|
|
)
|
|
|
|
return containers
|
|
|
|
|
|
def temporary_files(containers):
|
|
result = set()
|
|
|
|
for container in containers:
|
|
output = command_output([
|
|
"docker", "exec", container,
|
|
"sh", "-ec",
|
|
'''
|
|
directory="${UPLOAD_TEMP_DIR:-/tmp/messenger-uploads}"
|
|
|
|
if [ -d "$directory" ]; then
|
|
find "$directory" \
|
|
-maxdepth 1 \
|
|
-type f \
|
|
-name 'upload-*.tmp' \
|
|
-print
|
|
fi
|
|
''',
|
|
])
|
|
|
|
for filename in output.splitlines():
|
|
if filename:
|
|
result.add((container, filename))
|
|
|
|
return result
|
|
|
|
|
|
def wait_for_empty_directory(containers, seconds=25):
|
|
deadline = time.monotonic() + seconds
|
|
|
|
while True:
|
|
remaining = temporary_files(containers)
|
|
|
|
if not remaining:
|
|
return
|
|
|
|
check(
|
|
time.monotonic() < deadline,
|
|
"Temporary upload files were not removed",
|
|
)
|
|
|
|
time.sleep(0.5)
|
|
|
|
|
|
def main():
|
|
args = arguments("Interrupted upload cleanup check")
|
|
|
|
client = Client(
|
|
args.base_url,
|
|
args.ca_file,
|
|
timeout=30,
|
|
)
|
|
|
|
address = urlsplit(client.base)
|
|
|
|
check(
|
|
address.scheme == "http",
|
|
"This local test expects an HTTP endpoint",
|
|
)
|
|
|
|
containers = api_containers()
|
|
|
|
check(
|
|
not temporary_files(containers),
|
|
"Temporary directory is not empty. "
|
|
"Finish other uploads and inspect leftover files first",
|
|
)
|
|
|
|
alice, bob, eve, chat = client.bootstrap()
|
|
|
|
original = FIXTURE.read_bytes()
|
|
|
|
check(
|
|
len(original) > 32768,
|
|
"Fixture must be larger than the partial upload",
|
|
)
|
|
|
|
path = (
|
|
address.path.rstrip("/")
|
|
+ "/api/v1/chats/"
|
|
+ chat["id"]
|
|
+ "/attachments?filename=interrupted.png"
|
|
)
|
|
|
|
headers = (
|
|
f"POST {path} HTTP/1.1\r\n"
|
|
f"Host: {address.netloc}\r\n"
|
|
f"Authorization: Bearer {alice['token']}\r\n"
|
|
"Content-Type: image/png\r\n"
|
|
f"Content-Length: {len(original)}\r\n"
|
|
"Connection: close\r\n"
|
|
"\r\n"
|
|
).encode("ascii")
|
|
|
|
connection = socket.create_connection(
|
|
(address.hostname, address.port or 80),
|
|
timeout=10,
|
|
)
|
|
|
|
observed = False
|
|
|
|
try:
|
|
connection.sendall(headers)
|
|
connection.sendall(original[:32768])
|
|
|
|
print(
|
|
"Partial body sent; waiting for a temporary file",
|
|
flush=True,
|
|
)
|
|
|
|
deadline = time.monotonic() + 5
|
|
|
|
while time.monotonic() < deadline:
|
|
if temporary_files(containers):
|
|
observed = True
|
|
break
|
|
|
|
time.sleep(0.2)
|
|
|
|
check(
|
|
observed,
|
|
"Temporary file was not observed. "
|
|
"Check API logs and proxy timeouts",
|
|
)
|
|
|
|
print(
|
|
"PASS: API started receiving the incomplete upload",
|
|
flush=True,
|
|
)
|
|
|
|
finally:
|
|
# Закрываем соединение до передачи остальных байтов.
|
|
try:
|
|
connection.shutdown(socket.SHUT_RDWR)
|
|
except OSError:
|
|
pass
|
|
|
|
connection.close()
|
|
|
|
print("Client connection closed before upload completion", flush=True)
|
|
|
|
wait_for_empty_directory(containers)
|
|
|
|
print(
|
|
"PASS: temporary file removed after interrupted upload",
|
|
flush=True,
|
|
)
|
|
|
|
# Проверяем, что разрешение UploadLimiter не осталось занятым.
|
|
# Последовательными запросами через LB наблюдаем обе реплики.
|
|
instances = set()
|
|
last_attachment = None
|
|
|
|
for _ in range(20):
|
|
response = client.call(
|
|
"POST",
|
|
"/api/v1/chats/" + chat["id"]
|
|
+ "/attachments?filename=after-abort.png",
|
|
201,
|
|
actor=alice,
|
|
raw=original,
|
|
headers={"Content-Type": "image/png"},
|
|
schema="Attachment",
|
|
)
|
|
|
|
attachment = response["json"]
|
|
|
|
check(
|
|
attachment["status"] == "ready",
|
|
"Upload did not become ready",
|
|
)
|
|
|
|
check(
|
|
attachment["sha256"]
|
|
== hashlib.sha256(original).hexdigest(),
|
|
"Uploaded checksum mismatch",
|
|
)
|
|
|
|
instances.add(response["headers"]["X-Instance-Id"])
|
|
last_attachment = attachment
|
|
|
|
if len(instances) >= 2:
|
|
break
|
|
|
|
check(
|
|
len(instances) == 2,
|
|
"Successful uploads were not observed on both API replicas",
|
|
)
|
|
|
|
print(
|
|
"PASS: both API replicas accept uploads after cancellation",
|
|
flush=True,
|
|
)
|
|
|
|
downloaded = client.call(
|
|
"GET",
|
|
"/api/v1/attachments/"
|
|
+ last_attachment["id"]
|
|
+ "/content",
|
|
actor=bob,
|
|
)
|
|
|
|
check(
|
|
downloaded["bytes"] == original,
|
|
"Downloaded file differs from the original",
|
|
)
|
|
|
|
wait_for_empty_directory(containers)
|
|
|
|
print("PASS: subsequent upload is intact", flush=True)
|
|
print("PASS: interrupted upload scenario completed", flush=True)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
run(main) |