forked from mtusi2026/mtusi2026lab5
solution
This commit is contained in:
@@ -0,0 +1 @@
|
||||
"""Команды управления учебным стендом."""
|
||||
+59
-35
@@ -1,5 +1,6 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Offline structural checks; full schema validation is a separate target."""
|
||||
|
||||
import ast
|
||||
import json
|
||||
from pathlib import Path
|
||||
@@ -7,48 +8,71 @@ import re
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
def main():
|
||||
lab = json.loads((ROOT/'lab.json').read_text())['number']
|
||||
spec = json.loads((ROOT/'contracts/openapi.json').read_text())
|
||||
assert spec['openapi']=='3.1.0'
|
||||
assert spec['info']['version']=='1.%d.0'%lab
|
||||
lab = json.loads((ROOT / "lab.json").read_text())["number"]
|
||||
spec = json.loads((ROOT / "contracts/openapi.json").read_text(encoding="utf-8"))
|
||||
assert spec["openapi"] == "3.1.0"
|
||||
assert spec["info"]["version"] == "1.%d.0" % lab
|
||||
operation_ids = set()
|
||||
|
||||
def walk(value):
|
||||
if isinstance(value,dict):
|
||||
if '$ref' in value:
|
||||
pointer = value['$ref']
|
||||
assert pointer.startswith('#/'), 'unexpected external reference'
|
||||
if isinstance(value, dict):
|
||||
if "$ref" in value:
|
||||
pointer = value["$ref"]
|
||||
assert pointer.startswith("#/"), "unexpected external reference"
|
||||
target = spec
|
||||
for part in pointer[2:].split('/'):
|
||||
target = target[part.replace('~1','/').replace('~0','~')]
|
||||
for part in pointer[2:].split("/"):
|
||||
target = target[part.replace("~1", "/").replace("~0", "~")]
|
||||
for v in value.values():
|
||||
walk(v)
|
||||
elif isinstance(value,list):
|
||||
elif isinstance(value, list):
|
||||
for v in value:
|
||||
walk(v)
|
||||
walk(spec)
|
||||
for route,methods in spec['paths'].items():
|
||||
placeholders = set(re.findall(r'\{([^}]+)\}',route))
|
||||
for method,op in methods.items():
|
||||
assert op['operationId'] not in operation_ids, 'duplicate operationId'
|
||||
operation_ids.add(op['operationId'])
|
||||
params = {p['name'] for p in op.get('parameters',[]) if p['in']=='path' and p.get('required')}
|
||||
assert placeholders==params, 'path parameter mismatch: '+route
|
||||
for response in op['responses'].values():
|
||||
assert 'X-Request-Id' in response['headers']
|
||||
if lab>=3:
|
||||
assert 'X-Instance-Id' in response['headers']
|
||||
assert ('/api/v1/auth/sessions' in spec['paths']) == (lab>=2)
|
||||
assert ('/api/v1/attachments/{attachment_id}' in spec['paths']) == (lab>=4)
|
||||
for file in list((ROOT/'tests').glob('*.py'))+list((ROOT/'scripts').glob('*.py')):
|
||||
ast.parse(file.read_text(),filename=str(file))
|
||||
for file in ROOT.rglob('*.json'):
|
||||
if any(part in {'.git','.venv','node_modules','vendor','output'} for part in file.parts):
|
||||
continue
|
||||
json.loads(file.read_text())
|
||||
for required in ('README.md','COURSE.md','CONTRIBUTING.md','REPORT.md','contracts/README.md','tests/smoke.py','tests/README.md'):
|
||||
assert (ROOT/required).is_file(), 'missing '+required
|
||||
print('PASS: offline skeleton structure, local refs and Python syntax; application NOT tested.')
|
||||
|
||||
if __name__=='__main__':
|
||||
walk(spec)
|
||||
for route, methods in spec["paths"].items():
|
||||
placeholders = set(re.findall(r"\{([^}]+)\}", route))
|
||||
for method, op in methods.items():
|
||||
assert op["operationId"] not in operation_ids, "duplicate operationId"
|
||||
operation_ids.add(op["operationId"])
|
||||
params = {
|
||||
p["name"]
|
||||
for p in op.get("parameters", [])
|
||||
if p["in"] == "path" and p.get("required")
|
||||
}
|
||||
assert placeholders == params, "path parameter mismatch: " + route
|
||||
for response in op["responses"].values():
|
||||
assert "X-Request-Id" in response["headers"]
|
||||
if lab >= 3:
|
||||
assert "X-Instance-Id" in response["headers"]
|
||||
assert ("/api/v1/auth/sessions" in spec["paths"]) == (lab >= 2)
|
||||
assert ("/api/v1/attachments/{attachment_id}" in spec["paths"]) == (lab >= 4)
|
||||
for file in list((ROOT / "tests").glob("*.py")) + list(
|
||||
(ROOT / "scripts").glob("*.py")
|
||||
):
|
||||
ast.parse(file.read_text(), filename=str(file))
|
||||
for file in ROOT.rglob("*.json"):
|
||||
if any(
|
||||
part in {".git", ".venv", "node_modules", "vendor", "output"}
|
||||
for part in file.parts
|
||||
):
|
||||
continue
|
||||
json.loads(file.read_text(encoding="utf-8"))
|
||||
for required in (
|
||||
"README.md",
|
||||
"COURSE.md",
|
||||
"CONTRIBUTING.md",
|
||||
"REPORT.md",
|
||||
"contracts/README.md",
|
||||
"tests/smoke.py",
|
||||
"tests/README.md",
|
||||
):
|
||||
assert (ROOT / required).is_file(), "missing " + required
|
||||
print(
|
||||
"PASS: offline skeleton structure, local refs and Python syntax; application NOT tested."
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
@@ -0,0 +1,429 @@
|
||||
"""Fault/recovery очереди на явно выбранном Compose-стенде; только синтетические данные."""
|
||||
|
||||
import argparse
|
||||
from datetime import datetime
|
||||
import hashlib
|
||||
from io import BytesIO
|
||||
import json
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import time
|
||||
from uuid import UUID, uuid4
|
||||
|
||||
from PIL import Image
|
||||
|
||||
from scripts.check_outbox_recovery import COMPOSE, ROOT, compose, require_compose
|
||||
from scripts.deployment import ComposeProject
|
||||
from tests.client import Client, check
|
||||
|
||||
PROBE = """
|
||||
import asyncio, hashlib, json
|
||||
from uuid import UUID
|
||||
from sqlalchemy import select
|
||||
from app.core.config import Settings
|
||||
from app.core.database import create_db_engine, create_session_factory
|
||||
from app.models.job import AttachmentJob
|
||||
from app.models.attachment import Attachment
|
||||
from app.storage.s3 import create_s3_storage
|
||||
async def main():
|
||||
settings=Settings(); engine=create_db_engine(settings); storage=create_s3_storage(settings.s3)
|
||||
try:
|
||||
async with create_session_factory(engine)() as session:
|
||||
job, att=(await session.execute(select(AttachmentJob,Attachment).join(Attachment,Attachment.id==AttachmentJob.attachment_id).where(Attachment.id==UUID(ATTACHMENT_ID)))).one()
|
||||
result=dict(job_id=str(job.id),status=att.status,attempt=job.attempt,published_attempt=job.published_attempt,limit=job.attempt_limit,updated_at=att.updated_at,next_attempt_at=job.next_attempt_at,last_error_code=job.last_error_code,permanent=job.last_error_permanent)
|
||||
objects=[]
|
||||
for prefix in ('originals/'+UUID(ATTACHMENT_ID).hex+'/', 'variants/'+UUID(ATTACHMENT_ID).hex+'/'):
|
||||
entries=await asyncio.to_thread(lambda: list(storage._client.list_objects(storage.settings.bucket,prefix=prefix,recursive=True)))
|
||||
for obj in entries:
|
||||
stream=await storage.open_object(obj.object_name); checksum=hashlib.sha256()
|
||||
try:
|
||||
async for chunk in stream: checksum.update(chunk)
|
||||
finally: await stream.aclose()
|
||||
objects.append(dict(name=obj.object_name.split('/')[-1],size=obj.size,sha256=checksum.hexdigest(),last_modified=obj.last_modified))
|
||||
result['objects']=sorted(objects,key=lambda x:x['name']);print(json.dumps(result,default=str))
|
||||
finally: await storage.aclose();await engine.dispose()
|
||||
asyncio.run(main())
|
||||
"""
|
||||
|
||||
|
||||
def probe(attachment_id):
|
||||
code = "ATTACHMENT_ID = " + repr(str(UUID(attachment_id))) + "\n" + PROBE
|
||||
result = subprocess.run(
|
||||
COMPOSE + ["exec", "-T", "api", "python", "-"],
|
||||
input=code,
|
||||
text=True,
|
||||
capture_output=True,
|
||||
cwd=ROOT,
|
||||
timeout=30,
|
||||
)
|
||||
check(result.returncode == 0, "Probe failed; diagnostic output hidden")
|
||||
return json.loads(result.stdout)
|
||||
|
||||
|
||||
def admin(*arguments, expected=0):
|
||||
result = compose(
|
||||
"run",
|
||||
"--rm",
|
||||
"--no-deps",
|
||||
"worker",
|
||||
"python",
|
||||
"-m",
|
||||
"app.core.job_admin",
|
||||
*arguments
|
||||
)
|
||||
check(result.returncode == expected, "Unexpected operator command exit status")
|
||||
return json.loads(result.stdout)
|
||||
|
||||
|
||||
def verify_result(client, actor, attachment, original):
|
||||
ready = client.wait_ready(actor, attachment, wait_seconds=180)
|
||||
check(ready["id"] == attachment["id"], "Attachment identity changed")
|
||||
path = "/api/v1/attachments/" + ready["id"] + "/content"
|
||||
check(
|
||||
client.call("GET", path, actor=actor)["bytes"] == original, "Original changed"
|
||||
)
|
||||
check(
|
||||
{v["name"] for v in ready["variants"]} == {"thumbnail", "crop", "optimized"},
|
||||
"Incomplete pipeline",
|
||||
)
|
||||
for variant in ready["variants"]:
|
||||
data = client.call("GET", path + "?variant=" + variant["name"], actor=actor)[
|
||||
"bytes"
|
||||
]
|
||||
check(
|
||||
hashlib.sha256(data).hexdigest() == variant["sha256"], "Checksum mismatch"
|
||||
)
|
||||
with Image.open(BytesIO(data)) as image:
|
||||
check(
|
||||
image.size == (variant["width"], variant["height"]),
|
||||
"Metadata dimensions mismatch",
|
||||
)
|
||||
check(
|
||||
image.format == ("WEBP" if variant["name"] == "optimized" else "JPEG"),
|
||||
"Wrong format",
|
||||
)
|
||||
check(
|
||||
not image.getexif()
|
||||
and not image.info.get("icc_profile")
|
||||
and not image.info.get("xmp"),
|
||||
"Metadata leaked",
|
||||
)
|
||||
if variant["name"] == "crop":
|
||||
check(image.size == (128, 128), "Wrong crop size")
|
||||
snapshot = probe(ready["id"])
|
||||
check(
|
||||
len(snapshot["objects"]) == 4,
|
||||
"Expected exactly one original and three variants",
|
||||
)
|
||||
return snapshot
|
||||
|
||||
|
||||
def crash(client, actor, chat, mode):
|
||||
name = "messenger-worker-fault-" + uuid4().hex
|
||||
try:
|
||||
require_compose("stop", "worker")
|
||||
attachment, original = client.attachment(actor, chat)
|
||||
result = compose(
|
||||
"run",
|
||||
"--rm",
|
||||
"--no-deps",
|
||||
"--name",
|
||||
name,
|
||||
"worker",
|
||||
"python",
|
||||
"-m",
|
||||
"app.core.job_worker",
|
||||
"--" + mode,
|
||||
attachment["id"],
|
||||
)
|
||||
phase = (
|
||||
"after_object:optimized" if mode == "crash-after-object" else "before_ack"
|
||||
)
|
||||
marker = json.dumps({"fault": phase, "attachment_id": attachment["id"]})
|
||||
check(
|
||||
result.returncode == 137 and marker in result.stdout,
|
||||
"Targeted crash not observed",
|
||||
)
|
||||
before = probe(attachment["id"])
|
||||
check(
|
||||
before["status"]
|
||||
== ("processing" if mode == "crash-after-object" else "ready"),
|
||||
"Unexpected crash state",
|
||||
)
|
||||
check(len(before["objects"]) == 4, "Crash must occur after all four PUTs")
|
||||
finally:
|
||||
subprocess.run(["docker", "rm", "-f", name], capture_output=True, timeout=30)
|
||||
require_compose("start", "worker")
|
||||
after = verify_result(client, actor, attachment, original)
|
||||
if mode == "crash-before-ack":
|
||||
|
||||
wait_queue_empty()
|
||||
after = probe(attachment["id"])
|
||||
check(after == before, "Ready duplicate rewrote result")
|
||||
else:
|
||||
check(
|
||||
after["attempt"] == before["attempt"] + 1,
|
||||
"Lease did not recover the interrupted attempt",
|
||||
)
|
||||
check(
|
||||
[(x["name"], x["sha256"]) for x in before["objects"]]
|
||||
== [(x["name"], x["sha256"]) for x in after["objects"]],
|
||||
"Replay changed object content",
|
||||
)
|
||||
return dict(
|
||||
scenario=mode,
|
||||
attachment_id=attachment["id"],
|
||||
exit_code=137,
|
||||
before=before,
|
||||
after=after,
|
||||
)
|
||||
|
||||
|
||||
def retry_replay(client, actor, chat, permanent):
|
||||
name = "messenger-retry-fault-" + uuid4().hex
|
||||
samples = []
|
||||
configuration = ComposeProject().config["services"]["worker"]["environment"]
|
||||
retry_base = float(configuration["JOB_RETRY_SECONDS"])
|
||||
retry_max = float(configuration["JOB_RETRY_MAX_SECONDS"])
|
||||
try:
|
||||
require_compose("stop", "worker")
|
||||
attachment, original = client.attachment(actor, chat)
|
||||
mode = "fail-permanent" if permanent else "fail-transient"
|
||||
require_compose(
|
||||
"run",
|
||||
"-d",
|
||||
"--no-deps",
|
||||
"--name",
|
||||
name,
|
||||
"worker",
|
||||
"python",
|
||||
"-m",
|
||||
"app.core.job_worker",
|
||||
"--" + mode,
|
||||
attachment["id"],
|
||||
)
|
||||
deadline = time.monotonic() + 180
|
||||
while time.monotonic() < deadline:
|
||||
sample = probe(attachment["id"])
|
||||
if (
|
||||
sample["attempt"]
|
||||
and sample["status"] in {"queued", "failed"}
|
||||
and (not samples or samples[-1]["attempt"] != sample["attempt"])
|
||||
):
|
||||
samples.append(sample)
|
||||
if sample["status"] == "failed":
|
||||
break
|
||||
time.sleep(0.5)
|
||||
check(sample["status"] == "failed", "Retry never reached failed")
|
||||
check(
|
||||
sample["attempt"] == (1 if permanent else sample["limit"]),
|
||||
"Incorrect attempt budget",
|
||||
)
|
||||
check(sample["permanent"] == permanent, "Incorrect error classification")
|
||||
if not permanent:
|
||||
check(len(samples) == sample["limit"], "Missed retry samples")
|
||||
for entry in samples:
|
||||
delay = (
|
||||
datetime.fromisoformat(entry["next_attempt_at"])
|
||||
- datetime.fromisoformat(entry["updated_at"])
|
||||
).total_seconds()
|
||||
ceiling = min(retry_max, retry_base * 2 ** (entry["attempt"] - 1))
|
||||
check(
|
||||
ceiling / 2 <= delay <= ceiling,
|
||||
"Retry delay outside configured jitter bounds",
|
||||
)
|
||||
client.call(
|
||||
"GET",
|
||||
"/api/v1/attachments/" + attachment["id"] + "/content",
|
||||
actor=actor,
|
||||
expected=409,
|
||||
)
|
||||
job_id = sample["job_id"]
|
||||
request_id = str(uuid4())
|
||||
listing = admin("list-failed")
|
||||
check(
|
||||
any(row["job_id"] == job_id for row in listing),
|
||||
"Job missing from failed registry",
|
||||
)
|
||||
subprocess.run(
|
||||
["docker", "rm", "-f", name], capture_output=True, timeout=30, check=True
|
||||
)
|
||||
first = admin(
|
||||
"replay",
|
||||
job_id,
|
||||
"--request-id",
|
||||
request_id,
|
||||
"--actor",
|
||||
"verification",
|
||||
"--reason",
|
||||
"dependency_restored",
|
||||
)
|
||||
check(first["status"] == "queued", "Replay did not queue job")
|
||||
finally:
|
||||
subprocess.run(["docker", "rm", "-f", name], capture_output=True, timeout=30)
|
||||
require_compose("start", "worker")
|
||||
after = verify_result(client, actor, attachment, original)
|
||||
repeat = admin(
|
||||
"replay",
|
||||
job_id,
|
||||
"--request-id",
|
||||
request_id,
|
||||
"--actor",
|
||||
"verification",
|
||||
"--reason",
|
||||
"dependency_restored",
|
||||
)
|
||||
check(repeat["already_applied"], "Replay request id not idempotent")
|
||||
refused = admin(
|
||||
"replay",
|
||||
job_id,
|
||||
"--request-id",
|
||||
str(uuid4()),
|
||||
"--actor",
|
||||
"verification",
|
||||
"--reason",
|
||||
"manual_review",
|
||||
expected=2,
|
||||
)
|
||||
check(refused["error_code"] == "job_not_failed", "Successful job was replayed")
|
||||
check(probe(attachment["id"]) == after, "Operator replay altered ready job")
|
||||
check(len(admin("history", job_id)) == 1, "Incorrect audit count")
|
||||
return dict(
|
||||
scenario=mode,
|
||||
attachment_id=attachment["id"],
|
||||
samples=samples,
|
||||
after=after,
|
||||
replay_idempotent=True,
|
||||
)
|
||||
|
||||
|
||||
def queue_state():
|
||||
output = require_compose(
|
||||
"exec",
|
||||
"-T",
|
||||
"broker",
|
||||
"rabbitmqctl",
|
||||
"list_queues",
|
||||
"--formatter=json",
|
||||
"name",
|
||||
"messages_ready",
|
||||
"messages_unacknowledged",
|
||||
"consumers",
|
||||
"durable",
|
||||
)
|
||||
return next(row for row in json.loads(output) if row["name"] == "attachment-jobs")
|
||||
|
||||
|
||||
def wait_queue_empty():
|
||||
deadline = time.monotonic() + 20
|
||||
while time.monotonic() < deadline:
|
||||
state = queue_state()
|
||||
if state["messages_ready"] == 0 and state["messages_unacknowledged"] == 0:
|
||||
return state
|
||||
time.sleep(0.5)
|
||||
check(False, "RabbitMQ did not acknowledge all completed deliveries")
|
||||
|
||||
|
||||
def persistence(client, actor, chat):
|
||||
try:
|
||||
require_compose("stop", "worker")
|
||||
attachment, original = client.attachment(actor, chat)
|
||||
client.message(actor, chat, "", attachments=[attachment["id"]])
|
||||
client.call(
|
||||
"GET",
|
||||
"/api/v1/attachments/" + attachment["id"] + "/content",
|
||||
actor=actor,
|
||||
expected=409,
|
||||
)
|
||||
time.sleep(2)
|
||||
queued_before = queue_state()
|
||||
check(
|
||||
queued_before["messages_ready"] >= 1 and queued_before["durable"],
|
||||
"No durable queued message before restart",
|
||||
)
|
||||
result = subprocess.run(
|
||||
[str(ROOT / "scripts/restart.sh")],
|
||||
cwd=ROOT,
|
||||
capture_output=True,
|
||||
timeout=180,
|
||||
)
|
||||
check(result.returncode == 0, "Restart hook failed")
|
||||
check(
|
||||
"worker"
|
||||
not in require_compose(
|
||||
"ps", "--status", "running", "--services"
|
||||
).splitlines(),
|
||||
"Restart started deliberately stopped worker",
|
||||
)
|
||||
queued_after = queue_state()
|
||||
check(
|
||||
queued_after["messages_ready"] >= 1 and queued_after["durable"],
|
||||
"Broker lost queued delivery across restart",
|
||||
)
|
||||
client.message(actor, chat, "session and chat survived broker/API restart")
|
||||
finally:
|
||||
require_compose("start", "worker")
|
||||
after = verify_result(client, actor, attachment, original)
|
||||
return dict(
|
||||
scenario="worker_stopped_restart_all",
|
||||
attachment_id=attachment["id"],
|
||||
queue_before=queued_before,
|
||||
queue_after=queued_after,
|
||||
after=after,
|
||||
)
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--base-url", required=True)
|
||||
parser.add_argument("--ca-file")
|
||||
parser.add_argument(
|
||||
"--scenario",
|
||||
choices=[
|
||||
"all",
|
||||
"persistence",
|
||||
"crash-after-object",
|
||||
"crash-before-ack",
|
||||
"fail-transient",
|
||||
"fail-permanent",
|
||||
],
|
||||
default="all",
|
||||
)
|
||||
parser.add_argument("--output", default="evidence/jobs.json")
|
||||
args = parser.parse_args()
|
||||
client = Client(args.base_url, args.ca_file, timeout=40)
|
||||
aliases = {r.instance_id for r in ComposeProject().replicas() if r.running}
|
||||
check(
|
||||
client.call("GET", "/health/live", schema="Health")["headers"].get(
|
||||
"X-Instance-Id"
|
||||
)
|
||||
in aliases,
|
||||
"Wrong Compose project for BASE_URL",
|
||||
)
|
||||
actor, _, _, chat = client.bootstrap()
|
||||
results = []
|
||||
operations = {
|
||||
"persistence": persistence,
|
||||
"crash-after-object": lambda *x: crash(*x, "crash-after-object"),
|
||||
"crash-before-ack": lambda *x: crash(*x, "crash-before-ack"),
|
||||
"fail-transient": lambda *x: retry_replay(*x, False),
|
||||
"fail-permanent": lambda *x: retry_replay(*x, True),
|
||||
}
|
||||
selected = (
|
||||
operations.values() if args.scenario == "all" else [operations[args.scenario]]
|
||||
)
|
||||
for operation in selected:
|
||||
result = operation(client, actor, chat)
|
||||
results.append(result)
|
||||
Path(args.output).parent.mkdir(parents=True, exist_ok=True)
|
||||
Path(args.output).write_text(
|
||||
json.dumps(dict(status="in_progress", results=results), indent=2) + "\n"
|
||||
)
|
||||
print("PASS: " + result["scenario"], flush=True)
|
||||
Path(args.output).write_text(
|
||||
json.dumps(dict(status="passed", results=results), indent=2) + "\n"
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,147 @@
|
||||
"""Сценарий COMMIT → SIGKILL до publish → ready без повторного upload.
|
||||
|
||||
Требуется уже работающий отдельный стенд. Скрипт не выполняется при запуске API.
|
||||
Публичные tests не изменяет, secrets/tokens не печатает и evidence сам не сохраняет.
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import hashlib
|
||||
import json
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
from uuid import uuid4
|
||||
|
||||
from scripts.deployment import ComposeProject
|
||||
from tests.client import Client, check
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
COMPOSE = ["docker", "compose", "--env-file", ".env"]
|
||||
|
||||
|
||||
def compose(*arguments: str, timeout: int = 90) -> subprocess.CompletedProcess:
|
||||
return subprocess.run(
|
||||
COMPOSE + list(arguments),
|
||||
cwd=ROOT,
|
||||
text=True,
|
||||
capture_output=True,
|
||||
timeout=timeout,
|
||||
)
|
||||
|
||||
|
||||
def require_compose(*arguments: str) -> str:
|
||||
result = compose(*arguments)
|
||||
check(result.returncode == 0, "Compose command failed; output hidden")
|
||||
return result.stdout
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Отказ publisher после COMMIT перед RabbitMQ publish"
|
||||
)
|
||||
parser.add_argument("--base-url", required=True)
|
||||
parser.add_argument("--ca-file")
|
||||
args = parser.parse_args()
|
||||
running = set(
|
||||
require_compose("ps", "--status", "running", "--services").splitlines()
|
||||
)
|
||||
check(
|
||||
{"api", "postgres", "minio", "broker", "publisher", "worker"} <= running,
|
||||
"Start the application stack before this scenario",
|
||||
)
|
||||
client = Client(args.base_url, args.ca_file, timeout=40)
|
||||
project = ComposeProject()
|
||||
aliases = {replica.instance_id for replica in project.replicas() if replica.running}
|
||||
live = client.call("GET", "/health/live", schema="Health")
|
||||
check(
|
||||
live["headers"].get("X-Instance-Id") in aliases,
|
||||
"BASE_URL does not belong to the selected Compose project",
|
||||
)
|
||||
alice, _, _, chat = client.bootstrap()
|
||||
attachment = None
|
||||
fault_container = "messenger-outbox-fault-" + uuid4().hex
|
||||
try:
|
||||
require_compose("stop", "publisher")
|
||||
attachment, original = client.attachment(alice, chat)
|
||||
message = client.message(alice, chat, "", attachments=[attachment["id"]])[
|
||||
"json"
|
||||
]
|
||||
client.call(
|
||||
"GET",
|
||||
f"/api/v1/attachments/{attachment['id']}/content",
|
||||
expected=409,
|
||||
actor=alice,
|
||||
)
|
||||
killed = compose(
|
||||
"run",
|
||||
"--rm",
|
||||
"--no-deps",
|
||||
"--name",
|
||||
fault_container,
|
||||
"publisher",
|
||||
"python",
|
||||
"-m",
|
||||
"app.core.outbox_publisher",
|
||||
"--crash-before-publish",
|
||||
attachment["id"],
|
||||
)
|
||||
marker = json.dumps(
|
||||
{"fault": "before_publish", "attachment_id": attachment["id"]}
|
||||
)
|
||||
check(
|
||||
killed.returncode == 137 and marker in killed.stdout,
|
||||
"Expected targeted SIGKILL before publish was not observed",
|
||||
)
|
||||
pending = client.call(
|
||||
"GET",
|
||||
f"/api/v1/attachments/{attachment['id']}",
|
||||
actor=alice,
|
||||
schema="Attachment",
|
||||
)["json"]
|
||||
check(pending["status"] == "queued", "Crash must leave accepted job queued")
|
||||
finally:
|
||||
|
||||
try:
|
||||
subprocess.run(
|
||||
["docker", "rm", "-f", fault_container], capture_output=True, timeout=30
|
||||
)
|
||||
finally:
|
||||
require_compose("start", "publisher")
|
||||
ready = client.wait_ready(alice, attachment, wait_seconds=60)
|
||||
check(ready["id"] == attachment["id"], "Recovered a different attachment")
|
||||
check(
|
||||
{variant["name"] for variant in ready["variants"]}
|
||||
== {"thumbnail", "crop", "optimized"},
|
||||
"Missing required variants",
|
||||
)
|
||||
path = f"/api/v1/attachments/{attachment['id']}/content"
|
||||
check(
|
||||
client.call("GET", path, actor=alice)["bytes"] == original,
|
||||
"Original changed after recovery",
|
||||
)
|
||||
for variant in ready["variants"]:
|
||||
downloaded = client.call(
|
||||
"GET", path + "?variant=" + variant["name"], actor=alice
|
||||
)["bytes"]
|
||||
check(
|
||||
len(downloaded) == variant["size_bytes"]
|
||||
and hashlib.sha256(downloaded).hexdigest() == variant["sha256"],
|
||||
"Recovered variant checksum mismatch",
|
||||
)
|
||||
print(
|
||||
json.dumps(
|
||||
{
|
||||
"scenario": "commit_before_publish",
|
||||
"attachment_id": ready["id"],
|
||||
"message_id": message["id"],
|
||||
"publisher_exit_code": killed.returncode,
|
||||
"status_after_crash": pending["status"],
|
||||
"status_after_restart": ready["status"],
|
||||
"variants": [variant["name"] for variant in ready["variants"]],
|
||||
"reuploaded": False,
|
||||
}
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,152 @@
|
||||
"""Реальный MinIO: повтор bootstrap, private bucket, ограниченные права и отказ."""
|
||||
|
||||
import json
|
||||
import subprocess
|
||||
import time
|
||||
|
||||
from scripts.deployment import ComposeProject
|
||||
from tests.client import Client, arguments, check, run
|
||||
|
||||
PROBE = r"""
|
||||
import asyncio, json, sys
|
||||
from pathlib import Path
|
||||
from uuid import UUID
|
||||
from urllib.request import Request, urlopen
|
||||
from urllib.error import HTTPError, URLError
|
||||
from app.core.config import Settings
|
||||
from app.core.database import create_db_engine, create_session_factory
|
||||
from app.storage.s3 import create_s3_storage
|
||||
from app.models.attachment import Attachment
|
||||
from minio.error import S3Error
|
||||
async def main():
|
||||
settings = Settings()
|
||||
engine = create_db_engine(settings)
|
||||
storage = create_s3_storage(settings.s3)
|
||||
try:
|
||||
async with create_session_factory(engine)() as session:
|
||||
record = await session.get(Attachment, UUID(json.load(sys.stdin)['attachment_id']))
|
||||
key = record.object_key
|
||||
results = {}
|
||||
for method in ('GET', 'PUT'):
|
||||
request = Request(settings.s3.endpoint + '/' + settings.s3.bucket + '/' + key,
|
||||
method=method, data=b'x' if method == 'PUT' else None)
|
||||
try:
|
||||
with urlopen(request, timeout=3) as response:
|
||||
results[method] = response.status
|
||||
except HTTPError as error:
|
||||
results[method] = error.code
|
||||
error.close()
|
||||
try:
|
||||
storage._client.get_bucket_policy(settings.s3.bucket)
|
||||
results['admin_denied'] = False
|
||||
except S3Error as error:
|
||||
results['admin_denied'] = error.code == 'AccessDenied'
|
||||
try:
|
||||
buckets = storage._client.list_buckets()
|
||||
results['other_buckets_denied'] = all(bucket.name == settings.s3.bucket for bucket in buckets)
|
||||
except S3Error as error:
|
||||
results['other_buckets_denied'] = error.code == 'AccessDenied'
|
||||
try:
|
||||
with urlopen(settings.s3.endpoint.replace(':9000', ':9001'), timeout=1):
|
||||
results['console_unreachable'] = False
|
||||
except HTTPError as error:
|
||||
results['console_unreachable'] = False
|
||||
error.close()
|
||||
except URLError:
|
||||
results['console_unreachable'] = True
|
||||
results['temp_originals_absent'] = not any(Path('/tmp').glob('messenger-upload-*'))
|
||||
results['root_files_absent'] = not any(Path('/run/secrets', name).exists()
|
||||
for name in ('minio_root_user', 'minio_root_password'))
|
||||
print(json.dumps(results))
|
||||
finally:
|
||||
await storage.aclose()
|
||||
await engine.dispose()
|
||||
asyncio.run(main())
|
||||
"""
|
||||
|
||||
|
||||
def main():
|
||||
def configure(parser):
|
||||
parser.add_argument(
|
||||
"--with-outage",
|
||||
action="store_true",
|
||||
help="временно остановить MinIO выбранного проекта",
|
||||
)
|
||||
|
||||
args = arguments("MinIO private bucket and failure checks", configure)
|
||||
project = ComposeProject()
|
||||
client = Client(args.base_url, args.ca_file, timeout=40)
|
||||
alice, _, _, chat = client.bootstrap()
|
||||
aliases = {replica.instance_id for replica in project.replicas() if replica.running}
|
||||
live = client.call("GET", "/health/live", schema="Health")
|
||||
check(
|
||||
live["headers"].get("X-Instance-Id") in aliases,
|
||||
"BASE_URL does not belong to the selected Compose project",
|
||||
)
|
||||
attachment, original = client.attachment(alice, chat)
|
||||
attachment = client.wait_ready(alice, attachment)
|
||||
|
||||
for _ in range(2):
|
||||
project.run("run", "--rm", "--no-deps", "minio-bootstrap", timeout=100)
|
||||
replicas = project.replicas()
|
||||
check(bool(replicas), "API not running")
|
||||
result = subprocess.run(
|
||||
["docker", "exec", "-i", replicas[0].id, "python", "-c", PROBE],
|
||||
input=json.dumps({"attachment_id": attachment["id"]}),
|
||||
text=True,
|
||||
capture_output=True,
|
||||
timeout=20,
|
||||
)
|
||||
check(result.returncode == 0, "private bucket probe failed (details hidden)")
|
||||
statuses = json.loads(result.stdout)
|
||||
check(statuses["GET"] == statuses["PUT"] == 403, "anonymous S3 access allowed")
|
||||
check(
|
||||
statuses["admin_denied"]
|
||||
and statuses["other_buckets_denied"]
|
||||
and statuses["root_files_absent"],
|
||||
"API has excessive access",
|
||||
)
|
||||
check(statuses["console_unreachable"], "console reachable from API network")
|
||||
check(statuses["temp_originals_absent"], "API retained temporary originals")
|
||||
check(
|
||||
not project.config["services"]["minio"].get("ports"),
|
||||
"MinIO publishes host ports",
|
||||
)
|
||||
env = project.config["services"]["minio"]["environment"]
|
||||
check(env["MINIO_BROWSER"] == "off", "browser enabled")
|
||||
check(
|
||||
"127.0.0.1:9001" in project.config["services"]["minio"]["command"],
|
||||
"console is not bound to loopback",
|
||||
)
|
||||
path = "/api/v1/attachments/" + attachment["id"] + "/content"
|
||||
check(
|
||||
client.call("GET", path, actor=alice)["bytes"] == original,
|
||||
"bootstrap changed original",
|
||||
)
|
||||
if args.with_outage:
|
||||
try:
|
||||
project.run("stop", "minio")
|
||||
client.call("GET", path, expected=503, actor=alice)
|
||||
finally:
|
||||
project.run("start", "minio")
|
||||
deadline = time.monotonic() + 60
|
||||
while True:
|
||||
try:
|
||||
client.call("GET", "/health/ready", schema="Health")
|
||||
break
|
||||
except Exception:
|
||||
if time.monotonic() >= deadline:
|
||||
raise
|
||||
time.sleep(0.5)
|
||||
check(
|
||||
client.call("GET", path, actor=alice)["bytes"] == original,
|
||||
"original lost after MinIO restart",
|
||||
)
|
||||
print(
|
||||
"PASS: idempotent bootstrap, anonymous GET/PUT denied, restricted API credentials, private console"
|
||||
+ (", MinIO outage/recovery" if args.with_outage else "")
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run(main)
|
||||
@@ -0,0 +1,176 @@
|
||||
"""Проверки внутри Linux-контейнера с отдельным Docker daemon."""
|
||||
|
||||
from datetime import datetime, timezone
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import platform
|
||||
import subprocess
|
||||
import tarfile
|
||||
import time
|
||||
|
||||
ROOT = Path("/workspace")
|
||||
EVIDENCE = ROOT / "evidence"
|
||||
COMPOSE = ["docker", "compose", "--env-file", ".env"]
|
||||
|
||||
|
||||
def step(name, command, timeout=1800):
|
||||
print(name, flush=True)
|
||||
started = time.monotonic()
|
||||
with (EVIDENCE / (name + ".txt")).open("w") as output:
|
||||
output.write("$ " + " ".join(command) + "\n")
|
||||
output.flush()
|
||||
result = subprocess.run(
|
||||
command, cwd=ROOT, stdout=output, stderr=subprocess.STDOUT, timeout=timeout
|
||||
)
|
||||
output.write(
|
||||
f"\nexit_code={result.returncode}; duration_seconds={time.monotonic() - started:.3f}\n"
|
||||
)
|
||||
result.check_returncode()
|
||||
|
||||
|
||||
def main():
|
||||
with tarfile.open("/tmp/source.tar") as archive:
|
||||
archive.extractall(ROOT, filter="data")
|
||||
os.chdir(ROOT)
|
||||
EVIDENCE.mkdir(exist_ok=True)
|
||||
deadline = time.monotonic() + 120
|
||||
while subprocess.run(["docker", "info"], capture_output=True).returncode:
|
||||
if time.monotonic() > deadline:
|
||||
raise RuntimeError("Docker daemon did not start")
|
||||
time.sleep(1)
|
||||
os.environ.update(
|
||||
COMPOSE_PROJECT_NAME="messenger-test",
|
||||
COMPOSE_FILE="compose.yaml:compose.test.yaml",
|
||||
LB_HOST_PORT="28080",
|
||||
DOCKER_TESTS="1",
|
||||
TEST_USE_MIGRATIONS="1",
|
||||
TEST_REDIS_HOST="127.0.0.1",
|
||||
TEST_REDIS_PORT="26379",
|
||||
TEST_REDIS_DB="15",
|
||||
)
|
||||
result = {
|
||||
"status": "failed",
|
||||
"python": platform.python_version(),
|
||||
"platform": platform.platform(),
|
||||
"started_at": datetime.now(timezone.utc).isoformat(),
|
||||
}
|
||||
try:
|
||||
step("00_init", ["python3", "scripts/init_local.py"])
|
||||
secret = (ROOT / "secrets/db_password").read_text()
|
||||
os.environ["TEST_DATABASE_URL"] = (
|
||||
f"postgresql+asyncpg://messenger:{secret}@127.0.0.1:25432/messenger_test"
|
||||
)
|
||||
os.environ["TEST_REDIS_PASSWORD"] = (
|
||||
ROOT / "secrets/redis_password"
|
||||
).read_text()
|
||||
step(
|
||||
"01_black",
|
||||
[
|
||||
"python3",
|
||||
"-m",
|
||||
"black",
|
||||
"--check",
|
||||
"app",
|
||||
"infra",
|
||||
"migrations",
|
||||
"scripts",
|
||||
"student-tests",
|
||||
"tests",
|
||||
],
|
||||
)
|
||||
step("02_contract", ["python3", "scripts/check_contract.py"])
|
||||
step("03_openapi", ["python3", "scripts/validate_openapi.py"])
|
||||
step(
|
||||
"04_build_start",
|
||||
COMPOSE + ["up", "-d", "--build"],
|
||||
timeout=3600,
|
||||
)
|
||||
step(
|
||||
"04_readiness",
|
||||
COMPOSE + ["up", "-d", "--wait", "--wait-timeout", "240", "nginx"],
|
||||
)
|
||||
step(
|
||||
"05_create_test_database",
|
||||
COMPOSE
|
||||
+ [
|
||||
"exec",
|
||||
"-T",
|
||||
"postgres",
|
||||
"createdb",
|
||||
"-U",
|
||||
"messenger",
|
||||
"messenger_test",
|
||||
],
|
||||
)
|
||||
step(
|
||||
"06_pytest",
|
||||
["python3", "-m", "pytest", "-q", "--junitxml=evidence/pytest.xml"],
|
||||
timeout=1800,
|
||||
)
|
||||
base = ["--base-url", "http://127.0.0.1:28080"]
|
||||
step("07_smoke", ["python3", "tests/smoke.py", *base])
|
||||
step(
|
||||
"08_storage", ["python3", "-m", "scripts.check_s3", *base, "--with-outage"]
|
||||
)
|
||||
step("09_outbox", ["python3", "-m", "scripts.check_outbox_recovery", *base])
|
||||
step(
|
||||
"10_jobs",
|
||||
[
|
||||
"python3",
|
||||
"-m",
|
||||
"scripts.check_jobs",
|
||||
*base,
|
||||
"--output",
|
||||
"evidence/jobs.json",
|
||||
],
|
||||
)
|
||||
step(
|
||||
"11_persistence",
|
||||
[
|
||||
"python3",
|
||||
"tests/resilience.py",
|
||||
"--mode",
|
||||
"persistence",
|
||||
"--action-script",
|
||||
"scripts/restart.sh",
|
||||
*base,
|
||||
],
|
||||
)
|
||||
step(
|
||||
"12_failover",
|
||||
[
|
||||
"python3",
|
||||
"tests/resilience.py",
|
||||
"--mode",
|
||||
"failover",
|
||||
"--action-script",
|
||||
"scripts/stop-one.sh",
|
||||
*base,
|
||||
],
|
||||
)
|
||||
step(
|
||||
"13_environment",
|
||||
[
|
||||
"docker",
|
||||
"info",
|
||||
"--format",
|
||||
"CPUs={{.NCPU}} Memory={{.MemTotal}} Server={{.ServerVersion}} Architecture={{.Architecture}}",
|
||||
],
|
||||
)
|
||||
step("13_dependencies", ["python3", "-m", "pip", "freeze"])
|
||||
step("13_images", ["docker", "image", "ls", "--digests"])
|
||||
step("14_resources", COMPOSE + ["ps", "--all", "--format", "json"])
|
||||
result["status"] = "passed"
|
||||
finally:
|
||||
step(
|
||||
"15_cleanup",
|
||||
COMPOSE + ["down", "--volumes", "--remove-orphans"],
|
||||
timeout=180,
|
||||
)
|
||||
result["finished_at"] = datetime.now(timezone.utc).isoformat()
|
||||
(EVIDENCE / "verification.json").write_text(json.dumps(result, indent=2) + "\n")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,68 @@
|
||||
"""Общие операции с репликами текущего Compose-проекта."""
|
||||
|
||||
from dataclasses import dataclass
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Replica:
|
||||
|
||||
id: str
|
||||
image: str
|
||||
instance_id: str
|
||||
running: bool
|
||||
health: str | None
|
||||
|
||||
|
||||
class ComposeProject:
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.command = ["docker", "compose", "--env-file", ".env"]
|
||||
self.config = json.loads(self.run("config", "--format", "json"))
|
||||
self.name = self.config["name"]
|
||||
|
||||
def docker(self, *args: str, timeout: int = 90, env=None) -> str:
|
||||
result = subprocess.run(
|
||||
["docker", *args],
|
||||
cwd=ROOT,
|
||||
text=True,
|
||||
capture_output=True,
|
||||
timeout=timeout,
|
||||
env={**os.environ, **(env or {})},
|
||||
)
|
||||
if result.returncode:
|
||||
raise RuntimeError(result.stderr.strip() or result.stdout.strip())
|
||||
return result.stdout.strip()
|
||||
|
||||
def run(self, *args: str, timeout: int = 90, env=None) -> str:
|
||||
return self.docker(*self.command[1:], *args, timeout=timeout, env=env)
|
||||
|
||||
def replicas(self) -> list[Replica]:
|
||||
ids = self.run("ps", "--all", "--quiet", "api").splitlines()
|
||||
if not ids:
|
||||
return []
|
||||
template = (
|
||||
'{"id":{{json .Id}},"image":{{json .Image}},'
|
||||
'"hostname":{{json .Config.Hostname}},"running":{{json .State.Running}},'
|
||||
'"health":{{if .State.Health}}{{json .State.Health.Status}}{{else}}null{{end}}}'
|
||||
)
|
||||
rows = self.docker("inspect", "--format", template, *ids).splitlines()
|
||||
configured = str(
|
||||
self.config["services"]["api"].get("environment", {}).get("INSTANCE_ID")
|
||||
or ""
|
||||
).strip()
|
||||
return [
|
||||
Replica(
|
||||
row["id"],
|
||||
row["image"],
|
||||
configured or row["hostname"],
|
||||
row["running"],
|
||||
row["health"],
|
||||
)
|
||||
for row in map(json.loads, rows)
|
||||
]
|
||||
@@ -0,0 +1,38 @@
|
||||
"""Создаёт только отсутствующие локальные настройки и файловые секреты."""
|
||||
|
||||
import os
|
||||
from pathlib import Path
|
||||
import secrets
|
||||
|
||||
|
||||
def main() -> None:
|
||||
root = Path(__file__).resolve().parents[1]
|
||||
destination = root / ".env"
|
||||
if not destination.exists():
|
||||
with destination.open("x") as target:
|
||||
target.write((root / ".env.example").read_text())
|
||||
destination.chmod(0o600)
|
||||
directory = root / "secrets"
|
||||
directory.mkdir(mode=0o700, exist_ok=True)
|
||||
for name in (
|
||||
"db_password",
|
||||
"redis_password",
|
||||
"broker_password",
|
||||
"minio_root_user",
|
||||
"minio_root_password",
|
||||
"s3_access_key_id",
|
||||
"s3_secret_access_key",
|
||||
):
|
||||
path = directory / name
|
||||
try:
|
||||
descriptor = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o444)
|
||||
except FileExistsError:
|
||||
continue
|
||||
with os.fdopen(descriptor, "w") as target:
|
||||
target.write(secrets.token_urlsafe(24))
|
||||
print("Создан файл " + str(path.relative_to(root)))
|
||||
print("Настройки готовы; существующие значения сохранены")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,36 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
|
||||
compose() {
|
||||
docker compose --env-file .env "$@"
|
||||
}
|
||||
|
||||
wait_healthy() {
|
||||
for service in "$@"; do
|
||||
attempts=0
|
||||
until states=$(compose ps --format '{{.Health}}' "$service") \
|
||||
&& [ -n "$states" ] \
|
||||
&& [ -z "$(printf '%s\n' "$states" | grep -v '^healthy$')" ]; do
|
||||
attempts=$((attempts + 1))
|
||||
if [ "$attempts" -ge 60 ]; then
|
||||
echo "$service не стал healthy за 60 секунд" >&2
|
||||
exit 1
|
||||
fi
|
||||
sleep 1
|
||||
done
|
||||
done
|
||||
}
|
||||
|
||||
worker_running=$(compose ps --status running --quiet worker)
|
||||
compose stop worker publisher cleanup
|
||||
compose restart postgres redis minio broker
|
||||
wait_healthy postgres redis minio broker
|
||||
compose restart api
|
||||
wait_healthy api
|
||||
compose restart nginx
|
||||
wait_healthy nginx
|
||||
compose start publisher cleanup
|
||||
if [ -n "$worker_running" ]; then
|
||||
compose start worker
|
||||
fi
|
||||
@@ -0,0 +1,30 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
: "${TARGET_INSTANCE_ID:?test provides target API instance alias}"
|
||||
|
||||
case "$TARGET_INSTANCE_ID" in
|
||||
*[!a-zA-Z0-9_.:-]*|'') exit 1 ;;
|
||||
esac
|
||||
|
||||
target=""
|
||||
for container in $(docker compose --env-file .env ps -q api); do
|
||||
hostname=$(docker inspect --format '{{.Config.Hostname}}' "$container")
|
||||
configured=$(docker inspect --format '{{range .Config.Env}}{{if eq (index (split . "=") 0) "INSTANCE_ID"}}{{println .}}{{end}}{{end}}' "$container" \
|
||||
| sed -n 's/^INSTANCE_ID=//p' | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
|
||||
if [ "$TARGET_INSTANCE_ID" = "$hostname" ] || [ "$TARGET_INSTANCE_ID" = "$configured" ]; then
|
||||
if [ -n "$target" ]; then
|
||||
echo 'Instance id matches multiple API containers' >&2
|
||||
exit 1
|
||||
fi
|
||||
target="$container"
|
||||
fi
|
||||
done
|
||||
|
||||
if [ -z "$target" ]; then
|
||||
echo 'Target API instance not found in this Compose project' >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "Stopping API container $target" >&2
|
||||
docker stop --timeout 8 "$target" >/dev/null
|
||||
+189
@@ -0,0 +1,189 @@
|
||||
"""Запускает проверки в одноразовом Docker-окружении и сохраняет evidence."""
|
||||
|
||||
import argparse
|
||||
from datetime import datetime, timezone
|
||||
import io
|
||||
import json
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import tarfile
|
||||
import tempfile
|
||||
from uuid import uuid4
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
EXCLUDED = {
|
||||
".git",
|
||||
".agents",
|
||||
".aws",
|
||||
".codex",
|
||||
".env",
|
||||
"secrets",
|
||||
"evidence",
|
||||
".venv",
|
||||
"venv",
|
||||
"__pycache__",
|
||||
".pytest_cache",
|
||||
".github",
|
||||
"docs",
|
||||
}
|
||||
|
||||
|
||||
def docker(*args, **kwargs):
|
||||
return subprocess.run(["docker", *args], cwd=ROOT, check=True, **kwargs)
|
||||
|
||||
|
||||
def archive_source(destination):
|
||||
with tarfile.open(destination, "w") as archive:
|
||||
for path in sorted(ROOT.rglob("*")):
|
||||
relative = path.relative_to(ROOT)
|
||||
if not path.is_file() or any(part in EXCLUDED for part in relative.parts):
|
||||
continue
|
||||
content = path.read_bytes()
|
||||
if path.suffix in {".py", ".sh", ".conf", ".yaml", ".txt", ".md", ".ini"}:
|
||||
content = content.replace(b"\r\n", b"\n")
|
||||
entry = tarfile.TarInfo(str(relative).replace("\\", "/"))
|
||||
entry.size = len(content)
|
||||
entry.mode = 0o755 if path.suffix == ".sh" else 0o644
|
||||
archive.addfile(entry, io.BytesIO(content))
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--format", action="store_true", help="применить Black и выйти")
|
||||
args = parser.parse_args()
|
||||
project = "messenger-check-" + uuid4().hex[:12]
|
||||
image = project + ":tools"
|
||||
engine = project + "-engine"
|
||||
runner = project + "-runner"
|
||||
workspace = project + "-workspace"
|
||||
data = project + "-data"
|
||||
network = project + "-network"
|
||||
output = ROOT / "evidence" / "lab5"
|
||||
output.mkdir(parents=True, exist_ok=True)
|
||||
summary = {
|
||||
"status": "failed",
|
||||
"project": project,
|
||||
"started_at": datetime.now(timezone.utc).isoformat(),
|
||||
}
|
||||
try:
|
||||
docker("build", "--tag", image, "--file", "infra/testing/Dockerfile", ".")
|
||||
if args.format:
|
||||
docker(
|
||||
"run",
|
||||
"--rm",
|
||||
"--volume",
|
||||
str(ROOT) + ":/workspace",
|
||||
image,
|
||||
"python3",
|
||||
"-m",
|
||||
"black",
|
||||
"app",
|
||||
"infra",
|
||||
"migrations",
|
||||
"scripts",
|
||||
"student-tests",
|
||||
"tests",
|
||||
)
|
||||
summary["status"] = "formatted"
|
||||
return
|
||||
docker("network", "create", network, stdout=subprocess.DEVNULL)
|
||||
for volume in (workspace, data):
|
||||
docker("volume", "create", volume, stdout=subprocess.DEVNULL)
|
||||
docker(
|
||||
"run",
|
||||
"-d",
|
||||
"--privileged",
|
||||
"--name",
|
||||
engine,
|
||||
"--network",
|
||||
network,
|
||||
"--env",
|
||||
"DOCKER_TLS_CERTDIR=",
|
||||
"--volume",
|
||||
workspace + ":/workspace",
|
||||
"--volume",
|
||||
data + ":/var/lib/docker",
|
||||
"docker:29.1.3-dind",
|
||||
"dockerd",
|
||||
"--host=unix:///var/run/docker.sock",
|
||||
"--host=tcp://127.0.0.1:2375",
|
||||
"--tls=false",
|
||||
stdout=subprocess.DEVNULL,
|
||||
)
|
||||
docker(
|
||||
"create",
|
||||
"--name",
|
||||
runner,
|
||||
"--network",
|
||||
"container:" + engine,
|
||||
"--env",
|
||||
"DOCKER_HOST=tcp://127.0.0.1:2375",
|
||||
"--volume",
|
||||
workspace + ":/workspace",
|
||||
image,
|
||||
stdout=subprocess.DEVNULL,
|
||||
)
|
||||
with tempfile.TemporaryDirectory(prefix="messenger-check-") as directory:
|
||||
archive = Path(directory) / "source.tar"
|
||||
archive_source(archive)
|
||||
docker("cp", str(archive), runner + ":/tmp/source.tar")
|
||||
docker("start", runner, stdout=subprocess.DEVNULL)
|
||||
result = subprocess.run(
|
||||
["docker", "wait", runner],
|
||||
cwd=ROOT,
|
||||
text=True,
|
||||
capture_output=True,
|
||||
timeout=7200,
|
||||
)
|
||||
result.check_returncode()
|
||||
docker("logs", runner)
|
||||
docker("cp", runner + ":/workspace/evidence/.", str(output))
|
||||
if int(result.stdout.strip()) != 0:
|
||||
raise RuntimeError("Проверки завершились ошибкой; см. evidence/lab5")
|
||||
summary["status"] = "passed"
|
||||
finally:
|
||||
cleanup = []
|
||||
for command in (
|
||||
["rm", "--force", runner, engine],
|
||||
["volume", "rm", workspace, data],
|
||||
["network", "rm", network],
|
||||
["image", "rm", image],
|
||||
):
|
||||
result = subprocess.run(
|
||||
["docker", *command], cwd=ROOT, text=True, capture_output=True
|
||||
)
|
||||
cleanup.append(
|
||||
{
|
||||
"command": ["docker", *command],
|
||||
"returncode": result.returncode,
|
||||
"output": result.stdout.strip() + result.stderr.strip(),
|
||||
}
|
||||
)
|
||||
remaining = {}
|
||||
for kind in ("container", "volume", "network", "image"):
|
||||
result = subprocess.run(
|
||||
["docker", kind, "ls", "--format", "{{json .}}"],
|
||||
cwd=ROOT,
|
||||
text=True,
|
||||
capture_output=True,
|
||||
)
|
||||
remaining[kind] = [
|
||||
line for line in result.stdout.splitlines() if project in line
|
||||
]
|
||||
if result.returncode:
|
||||
remaining[kind].append("resource audit failed")
|
||||
summary["cleanup"] = cleanup
|
||||
summary["remaining_resources"] = remaining
|
||||
summary["finished_at"] = datetime.now(timezone.utc).isoformat()
|
||||
if any(remaining.values()):
|
||||
summary["status"] = "failed"
|
||||
(output / "run.json").write_text(
|
||||
json.dumps(summary, indent=2, ensure_ascii=False) + "\n", encoding="utf-8"
|
||||
)
|
||||
if summary["status"] == "failed":
|
||||
raise SystemExit(1)
|
||||
print("PASS: проверки завершены, временное окружение удалено")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -1,23 +1,26 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Full OpenAPI and JSON Schema checks; install requirements-dev.txt first."""
|
||||
|
||||
import json
|
||||
from pathlib import Path
|
||||
from openapi_spec_validator import validate
|
||||
from jsonschema import Draft202012Validator
|
||||
|
||||
root = Path(__file__).resolve().parents[1]
|
||||
spec = json.loads((root/'contracts/openapi.json').read_text())
|
||||
spec = json.loads((root / "contracts/openapi.json").read_text(encoding="utf-8"))
|
||||
validate(spec)
|
||||
from referencing import Registry, Resource
|
||||
# The base URI resolves local #/components references inside examples.
|
||||
resource = dict(spec, **{'$schema': 'https://json-schema.org/draft/2020-12/schema'})
|
||||
registry = Registry().with_resource('urn:mtusi:openapi', Resource.from_contents(resource))
|
||||
for name, example in json.loads((root/'contracts/examples.json').read_text()).items():
|
||||
schema = {'$ref': 'urn:mtusi:openapi#/components/schemas/' + name}
|
||||
|
||||
resource = dict(spec, **{"$schema": "https://json-schema.org/draft/2020-12/schema"})
|
||||
registry = Registry().with_resource(
|
||||
"urn:messenger:openapi", Resource.from_contents(resource)
|
||||
)
|
||||
for name, example in json.loads((root / "contracts/examples.json").read_text()).items():
|
||||
schema = {"$ref": "urn:messenger:openapi#/components/schemas/" + name}
|
||||
Draft202012Validator(schema, registry=registry).validate(example)
|
||||
for path in (root/'contracts').glob('*.schema.json'):
|
||||
for path in (root / "contracts").glob("*.schema.json"):
|
||||
schema = json.loads(path.read_text())
|
||||
Draft202012Validator.check_schema(schema)
|
||||
for example in schema.get('examples',[]):
|
||||
for example in schema.get("examples", []):
|
||||
Draft202012Validator(schema).validate(example)
|
||||
print('PASS: full OpenAPI 3.1 and JSON Schema validation; application NOT tested.')
|
||||
print("PASS: full OpenAPI 3.1 and JSON Schema validation; application NOT tested.")
|
||||
|
||||
Reference in New Issue
Block a user