[+] add project
This commit is contained in:
Executable
+121
@@ -0,0 +1,121 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Concurrent idempotency and replica-distribution checks."""
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from pathlib import Path
|
||||
import uuid
|
||||
|
||||
from client import Client, Failure, arguments, check, run
|
||||
from image_fixture import solid_gray_png
|
||||
|
||||
|
||||
def main():
|
||||
args = arguments("Concurrent shared-idempotency and API-replica check")
|
||||
client = Client(args.base_url, args.ca_file, timeout=5)
|
||||
alice, bob, eve, chat = client.bootstrap()
|
||||
key = uuid.uuid4().hex
|
||||
body = {"text": "parallel-idempotency-" + key}
|
||||
|
||||
def send(_):
|
||||
return Client(args.base_url, args.ca_file, timeout=5).call(
|
||||
"POST",
|
||||
"/api/v1/chats/" + chat["id"] + "/messages",
|
||||
201,
|
||||
body=body,
|
||||
actor=alice,
|
||||
headers={"Idempotency-Key": key},
|
||||
schema="Message",
|
||||
)
|
||||
|
||||
with ThreadPoolExecutor(max_workers=12) as executor:
|
||||
responses = list(executor.map(send, range(12)))
|
||||
messages = [response["json"] for response in responses]
|
||||
check(all(message == messages[0] for message in messages), "concurrent replay differed")
|
||||
|
||||
conflict = client.call(
|
||||
"POST",
|
||||
"/api/v1/chats/" + chat["id"] + "/messages",
|
||||
409,
|
||||
body={"text": "different body"},
|
||||
actor=alice,
|
||||
headers={"Idempotency-Key": key},
|
||||
)
|
||||
check(conflict["status"] == 409, "same key with a different body was accepted")
|
||||
|
||||
instances = {
|
||||
client.call("GET", "/api/v1/users/me", actor=alice, schema="User")["headers"][
|
||||
"X-Instance-Id"
|
||||
]
|
||||
for _ in range(80)
|
||||
}
|
||||
check(len(instances) >= 2, "load balancer did not expose two API instances")
|
||||
|
||||
upload_path = "/api/v1/chats/" + chat["id"] + "/attachments"
|
||||
fixture = (Path(__file__).parent / "fixtures" / "sample.png").read_bytes()
|
||||
client.call(
|
||||
"POST",
|
||||
upload_path + "?filename=too-large.png",
|
||||
413,
|
||||
actor=alice,
|
||||
raw=b"x" * (10 * 1024 * 1024 + 1),
|
||||
headers={"Content-Type": "image/png"},
|
||||
)
|
||||
client.call(
|
||||
"POST",
|
||||
upload_path + "?filename=broken.png",
|
||||
415,
|
||||
actor=alice,
|
||||
raw=b"\x89PNG\r\n\x1a\nnot-an-image",
|
||||
headers={"Content-Type": "image/png"},
|
||||
)
|
||||
client.call(
|
||||
"POST",
|
||||
upload_path + "?filename=mime.jpg",
|
||||
415,
|
||||
actor=alice,
|
||||
raw=fixture,
|
||||
headers={"Content-Type": "image/jpeg"},
|
||||
)
|
||||
client.call(
|
||||
"POST",
|
||||
upload_path + "?filename=pixels.png",
|
||||
413,
|
||||
actor=alice,
|
||||
raw=solid_gray_png(6401, 6251),
|
||||
headers={"Content-Type": "image/png"},
|
||||
)
|
||||
client.call(
|
||||
"POST",
|
||||
upload_path + "?filename=..%2Fprivate.png",
|
||||
400,
|
||||
actor=alice,
|
||||
raw=fixture,
|
||||
headers={"Content-Type": "image/png"},
|
||||
)
|
||||
attachment, _ = client.attachment(alice, chat)
|
||||
other_chat = client.call(
|
||||
"POST",
|
||||
"/api/v1/chats",
|
||||
201,
|
||||
actor=alice,
|
||||
body={"title": "Другой чат", "member_ids": [alice["id"], bob["id"]]},
|
||||
schema="Chat",
|
||||
)["json"]
|
||||
client.call(
|
||||
"POST",
|
||||
"/api/v1/chats/" + other_chat["id"] + "/messages",
|
||||
404,
|
||||
actor=alice,
|
||||
body={"text": "", "attachment_ids": [attachment["id"]]},
|
||||
headers={"Idempotency-Key": uuid.uuid4().hex},
|
||||
)
|
||||
for path in (
|
||||
"/api/v1/attachments/" + attachment["id"],
|
||||
"/api/v1/attachments/" + attachment["id"] + "/content",
|
||||
):
|
||||
client.call("GET", path, 404, actor=eve)
|
||||
print("PASS: concurrent idempotency returned one message; observed %d API replicas" % len(instances))
|
||||
print("PASS: level-4 attachment error, pixel limit, chat binding and outsider checks.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run(main)
|
||||
Reference in New Issue
Block a user