forked from mtusi2026/mtusi2026lab4
all for lab4
This commit is contained in:
@@ -0,0 +1,249 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import socket
|
||||
import subprocess
|
||||
import time
|
||||
from urllib.parse import urlsplit
|
||||
|
||||
from client import Client, arguments, check, run
|
||||
from upload_abort_check import (
|
||||
FIXTURE,
|
||||
api_containers,
|
||||
command_output,
|
||||
temporary_files,
|
||||
)
|
||||
|
||||
|
||||
def container_running(container):
|
||||
value = command_output([
|
||||
"docker", "inspect",
|
||||
"--format", "{{.State.Running}}",
|
||||
container,
|
||||
])
|
||||
|
||||
return value == "true"
|
||||
|
||||
|
||||
def container_healthy(container):
|
||||
value = command_output([
|
||||
"docker", "inspect",
|
||||
"--format", "{{.State.Health.Status}}",
|
||||
container,
|
||||
])
|
||||
|
||||
return value == "healthy"
|
||||
|
||||
|
||||
def main():
|
||||
args = arguments("API crash during an incomplete upload")
|
||||
|
||||
client = Client(
|
||||
args.base_url,
|
||||
args.ca_file,
|
||||
timeout=30,
|
||||
)
|
||||
|
||||
address = urlsplit(client.base)
|
||||
|
||||
check(
|
||||
address.scheme == "http",
|
||||
"This local test expects HTTP",
|
||||
)
|
||||
|
||||
containers = api_containers()
|
||||
|
||||
check(
|
||||
not temporary_files(containers),
|
||||
"Temporary directories must be empty before the test",
|
||||
)
|
||||
|
||||
alice, bob, eve, chat = client.bootstrap()
|
||||
|
||||
good_attachment, original = client.attachment(alice, chat)
|
||||
|
||||
content = FIXTURE.read_bytes()
|
||||
|
||||
check(
|
||||
len(content) > 32768,
|
||||
"Fixture is too small",
|
||||
)
|
||||
|
||||
path = (
|
||||
address.path.rstrip("/")
|
||||
+ "/api/v1/chats/"
|
||||
+ chat["id"]
|
||||
+ "/attachments?filename=crash-test.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(content)}\r\n"
|
||||
"Connection: close\r\n"
|
||||
"\r\n"
|
||||
).encode("ascii")
|
||||
|
||||
connection = None
|
||||
target = None
|
||||
temporary_path = None
|
||||
|
||||
try:
|
||||
connection = socket.create_connection(
|
||||
(address.hostname, address.port or 80),
|
||||
timeout=10,
|
||||
)
|
||||
|
||||
connection.sendall(headers)
|
||||
connection.sendall(content[:32768])
|
||||
|
||||
print("Partial upload started", flush=True)
|
||||
|
||||
deadline = time.monotonic() + 5
|
||||
|
||||
while True:
|
||||
found = temporary_files(containers)
|
||||
|
||||
if found:
|
||||
check(
|
||||
len(found) == 1,
|
||||
"Expected exactly one active temporary file",
|
||||
)
|
||||
|
||||
target, temporary_path = next(iter(found))
|
||||
break
|
||||
|
||||
check(
|
||||
time.monotonic() < deadline,
|
||||
"Temporary file was not observed",
|
||||
)
|
||||
|
||||
time.sleep(0.2)
|
||||
|
||||
|
||||
command_output([
|
||||
"docker", "exec", target,
|
||||
"touch", "-t", "200001010000",
|
||||
temporary_path,
|
||||
])
|
||||
|
||||
print(
|
||||
"Temporary file found; test timestamp set to the past",
|
||||
flush=True,
|
||||
)
|
||||
|
||||
command_output([
|
||||
"docker", "kill",
|
||||
"--signal", "KILL",
|
||||
target,
|
||||
])
|
||||
|
||||
check(
|
||||
not container_running(target),
|
||||
"Target API container did not stop",
|
||||
)
|
||||
|
||||
connection.close()
|
||||
connection = None
|
||||
|
||||
copied = subprocess.run(
|
||||
[
|
||||
"docker", "cp",
|
||||
target + ":" + temporary_path,
|
||||
"-",
|
||||
],
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE,
|
||||
timeout=10,
|
||||
)
|
||||
|
||||
check(
|
||||
copied.returncode == 0,
|
||||
"Temporary file did not survive the process crash",
|
||||
)
|
||||
|
||||
print(
|
||||
"PASS: temporary file remains after SIGKILL",
|
||||
flush=True,
|
||||
)
|
||||
|
||||
command_output([
|
||||
"docker", "start", target,
|
||||
])
|
||||
|
||||
ready_deadline = time.monotonic() + 120
|
||||
|
||||
while not container_healthy(target):
|
||||
check(
|
||||
container_running(target),
|
||||
"API exited after restart",
|
||||
)
|
||||
|
||||
check(
|
||||
time.monotonic() < ready_deadline,
|
||||
"API did not become healthy after restart",
|
||||
)
|
||||
|
||||
time.sleep(2)
|
||||
|
||||
print(
|
||||
"PASS: the same API container recovered",
|
||||
flush=True,
|
||||
)
|
||||
|
||||
print(
|
||||
"Waiting for temporary-file cleanup...",
|
||||
flush=True,
|
||||
)
|
||||
|
||||
cleanup_deadline = time.monotonic() + 120
|
||||
|
||||
while (target, temporary_path) in temporary_files([target]):
|
||||
check(
|
||||
time.monotonic() < cleanup_deadline,
|
||||
"Old temporary file was not removed",
|
||||
)
|
||||
|
||||
time.sleep(2)
|
||||
|
||||
print(
|
||||
"PASS: stale temporary file removed after API restart",
|
||||
flush=True,
|
||||
)
|
||||
|
||||
downloaded = client.call(
|
||||
"GET",
|
||||
"/api/v1/attachments/"
|
||||
+ good_attachment["id"]
|
||||
+ "/content",
|
||||
actor=bob,
|
||||
)
|
||||
|
||||
check(
|
||||
downloaded["bytes"] == original,
|
||||
"Confirmed attachment was lost or changed",
|
||||
)
|
||||
|
||||
print(
|
||||
"PASS: existing session and confirmed attachment survived",
|
||||
flush=True,
|
||||
)
|
||||
|
||||
client.attachment(alice, chat)
|
||||
|
||||
print("PASS: subsequent upload succeeds", flush=True)
|
||||
print("PASS: API crash scenario completed", flush=True)
|
||||
|
||||
finally:
|
||||
if connection is not None:
|
||||
connection.close()
|
||||
|
||||
if target is not None and not container_running(target):
|
||||
command_output([
|
||||
"docker", "start", target,
|
||||
])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run(main)
|
||||
Reference in New Issue
Block a user