Сделана лаба 4
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Negative attachment checks required for the level-4 assessment."""
|
||||
import hashlib
|
||||
from pathlib import Path
|
||||
import uuid
|
||||
|
||||
from client import Client, arguments, check, run
|
||||
|
||||
|
||||
def main():
|
||||
args = arguments("lab4 level-4 attachment checks")
|
||||
client = Client(args.base_url, args.ca_file)
|
||||
alice, bob, eve, chat = client.bootstrap()
|
||||
second_chat = client.call(
|
||||
"POST",
|
||||
"/api/v1/chats",
|
||||
201,
|
||||
actor=alice,
|
||||
body={"title": "Другой чат", "member_ids": [alice["id"], bob["id"]]},
|
||||
schema="Chat",
|
||||
)["json"]
|
||||
sample = Path(__file__).with_name("fixtures").joinpath("sample.png").read_bytes()
|
||||
|
||||
client.call(
|
||||
"POST",
|
||||
f"/api/v1/chats/{chat['id']}/attachments?filename=large.png",
|
||||
413,
|
||||
actor=alice,
|
||||
raw=b"x" * (10 * 1024 * 1024 + 1),
|
||||
headers={"Content-Type": "image/png"},
|
||||
)
|
||||
client.call(
|
||||
"POST",
|
||||
f"/api/v1/chats/{chat['id']}/attachments?filename=broken.png",
|
||||
415,
|
||||
actor=alice,
|
||||
raw=b"not an image",
|
||||
headers={"Content-Type": "image/png"},
|
||||
)
|
||||
client.call(
|
||||
"POST",
|
||||
f"/api/v1/chats/{chat['id']}/attachments?filename=spoof.jpg",
|
||||
415,
|
||||
actor=alice,
|
||||
raw=sample,
|
||||
headers={"Content-Type": "image/jpeg"},
|
||||
)
|
||||
client.call(
|
||||
"POST",
|
||||
f"/api/v1/chats/{chat['id']}/attachments?filename=..%2Fsecret.png",
|
||||
400,
|
||||
actor=alice,
|
||||
raw=sample,
|
||||
headers={"Content-Type": "image/png"},
|
||||
)
|
||||
attachment, content = client.attachment(alice, chat)
|
||||
check(hashlib.sha256(content).hexdigest() == attachment["sha256"], "fixture checksum changed")
|
||||
client.call(
|
||||
"GET",
|
||||
f"/api/v1/attachments/{attachment['id']}",
|
||||
404,
|
||||
actor=eve,
|
||||
)
|
||||
client.call(
|
||||
"POST",
|
||||
f"/api/v1/chats/{second_chat['id']}/messages",
|
||||
404,
|
||||
actor=alice,
|
||||
body={"text": "wrong chat", "attachment_ids": [attachment["id"]]},
|
||||
headers={"Idempotency-Key": uuid.uuid4().hex},
|
||||
)
|
||||
print("PASS: lab4 level-4 attachment validation and access checks")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run(main)
|
||||
@@ -0,0 +1,71 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Run ten concurrent bounded uploads and sample API RSS via Docker."""
|
||||
import concurrent.futures
|
||||
import subprocess
|
||||
import threading
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
from client import Client, arguments, check, run
|
||||
|
||||
|
||||
def rss_mb() -> float:
|
||||
output = subprocess.check_output(
|
||||
["docker", "stats", "--no-stream", "--format", "{{.Name}} {{.MemUsage}}"],
|
||||
text=True,
|
||||
)
|
||||
values = []
|
||||
for line in output.splitlines():
|
||||
if "api1" not in line and "api2" not in line:
|
||||
continue
|
||||
used = line.split()[1].split("/")[0]
|
||||
factor = {"B": 1 / 1024 / 1024, "KiB": 1 / 1024, "MiB": 1, "GiB": 1024}
|
||||
values.append(float(used[:-3]) * factor[used[-3:]])
|
||||
return max(values, default=0.0)
|
||||
|
||||
|
||||
def main():
|
||||
args = arguments("lab4 concurrent bounded upload experiment")
|
||||
client = Client(args.base_url, args.ca_file)
|
||||
alice, _, _, chat = client.bootstrap()
|
||||
content = Path(__file__).with_name("fixtures").joinpath("sample.png").read_bytes()
|
||||
stop = threading.Event()
|
||||
samples = [rss_mb()]
|
||||
|
||||
def sample():
|
||||
while not stop.is_set():
|
||||
try:
|
||||
samples.append(rss_mb())
|
||||
except (OSError, ValueError, subprocess.SubprocessError):
|
||||
pass
|
||||
time.sleep(0.1)
|
||||
|
||||
sampler = threading.Thread(target=sample, daemon=True)
|
||||
sampler.start()
|
||||
started = time.monotonic()
|
||||
with concurrent.futures.ThreadPoolExecutor(max_workers=10) as pool:
|
||||
futures = [
|
||||
pool.submit(
|
||||
client.call,
|
||||
"POST",
|
||||
f"/api/v1/chats/{chat['id']}/attachments?filename=load-{index}.png",
|
||||
201,
|
||||
None,
|
||||
alice,
|
||||
{"Content-Type": "image/png"},
|
||||
content,
|
||||
"Attachment",
|
||||
)
|
||||
for index in range(10)
|
||||
]
|
||||
[future.result() for future in futures]
|
||||
elapsed = time.monotonic() - started
|
||||
stop.set()
|
||||
sampler.join(timeout=1)
|
||||
samples.append(rss_mb())
|
||||
check(len(samples) > 0, "RSS sampling did not produce data")
|
||||
print(f"uploads=10 concurrency=10 elapsed_seconds={elapsed:.3f} peak_api_rss_mb={max(samples):.1f}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run(main)
|
||||
Reference in New Issue
Block a user