Forward the lab 4 foundation and migrate the lab 5 pipeline to SQLAlchemy
This commit is contained in:
@@ -23,6 +23,19 @@ def compose(*args, stdin=None):
|
||||
return subprocess.run(cmd, cwd=ROOT, input=stdin, capture_output=True, text=True, check=False)
|
||||
|
||||
|
||||
def containers(service):
|
||||
ids = compose("ps", "-aq", service).stdout.split()
|
||||
fields = ("hostname", "status", "exit_code", "started_at", "finished_at")
|
||||
template = (
|
||||
"{{.Config.Hostname}} {{.State.Status}} {{.State.ExitCode}}"
|
||||
" {{.State.StartedAt}} {{.State.FinishedAt}}"
|
||||
)
|
||||
out = subprocess.run(
|
||||
["docker", "inspect", "-f", template, *ids], capture_output=True, text=True, check=False
|
||||
)
|
||||
return [dict(zip(fields, line.split())) for line in out.stdout.splitlines()]
|
||||
|
||||
|
||||
def upload(c, actor, chat, expected=202):
|
||||
path = f"/api/v1/chats/{chat['id']}/attachments?filename=sample.png"
|
||||
schema = "Attachment" if expected == 202 else None
|
||||
@@ -90,6 +103,10 @@ def failover_upload(c, args):
|
||||
recovered = time.monotonic() - started
|
||||
served = got["headers"].get("X-Instance-Id")
|
||||
check(served != target, "download served by the stopped replica")
|
||||
during = containers(args.service)
|
||||
states = {item["hostname"]: item["status"] for item in during}
|
||||
check(states.get(target) == "exited", f"target {target} is not exited: {states}")
|
||||
check(states.get(served) == "running", f"neighbour {served} is not running: {states}")
|
||||
digest = hashlib.sha256(got["bytes"]).hexdigest()
|
||||
check(
|
||||
digest == att["sha256"] == hashlib.sha256(FIXTURE).hexdigest(),
|
||||
@@ -104,7 +121,9 @@ def failover_upload(c, args):
|
||||
"sha256_equal": True,
|
||||
"hook_seconds": round(hook_seconds, 3),
|
||||
"download_ok_after_seconds": round(recovered, 3),
|
||||
"replicas_at_download": during,
|
||||
"restored": args.restore,
|
||||
"replicas_after_restore": containers(args.service) if args.restore else None,
|
||||
}
|
||||
|
||||
|
||||
@@ -124,6 +143,7 @@ def s3_outage(c, args):
|
||||
"text_message": timed(lambda: c.message(alice, chat, "S3 недоступен, текст работает")),
|
||||
"health_ready": timed(lambda: c.call("GET", "/health/ready", [200, 503])),
|
||||
}
|
||||
stopped = containers(args.s3_service)
|
||||
finally:
|
||||
started = time.monotonic()
|
||||
compose("up", "-d", "--wait", "--no-deps", "--no-recreate", args.s3_service)
|
||||
@@ -131,8 +151,11 @@ def s3_outage(c, args):
|
||||
got = wait(lambda: content(c, bob, att), 30)
|
||||
check(got["bytes"] == FIXTURE, "attachment changed across S3 outage")
|
||||
expected = {"upload": 503, "download": 503, "text_message": 201}
|
||||
check(all(during[k]["status"] == v for k, v in expected.items()), f"S3 outage: {during}")
|
||||
check(all(during[k]["seconds"] < 6 for k in ("upload", "download")), "S3 timeout exceeded")
|
||||
return {
|
||||
"scenario": "s3-outage",
|
||||
"s3_container_during_outage": stopped,
|
||||
"during_outage": during,
|
||||
"expected": expected,
|
||||
"as_expected": all(during[k]["status"] == v for k, v in expected.items()),
|
||||
@@ -180,6 +203,8 @@ def abort_upload(c, args):
|
||||
)
|
||||
rows = out.stdout.split()
|
||||
counts = dict(zip(rows[::2], (int(x) for x in rows[1::2])))
|
||||
check(out.returncode == 0, "cannot inspect aborted upload")
|
||||
check(counts.get("ready", 0) == 0, "aborted upload became ready")
|
||||
alive = c.call("GET", "/api/v1/users/me", actor=alice, schema="User")["status"]
|
||||
return {
|
||||
"scenario": "abort-upload",
|
||||
@@ -216,10 +241,17 @@ def main():
|
||||
"s3-outage": s3_outage,
|
||||
"abort-upload": abort_upload,
|
||||
}[args.scenario]
|
||||
commit = subprocess.run(
|
||||
["git", "rev-parse", "HEAD"], cwd=ROOT, capture_output=True, check=False
|
||||
)
|
||||
stamp = {
|
||||
"date": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
|
||||
"commit": commit.stdout.decode().strip(),
|
||||
}
|
||||
try:
|
||||
report = {"ok": True, **run(c, args)}
|
||||
report = {"ok": True, **stamp, **run(c, args)}
|
||||
except Failure as exc:
|
||||
report = {"scenario": args.scenario, "ok": False, "error": str(exc)}
|
||||
report = {"scenario": args.scenario, "ok": False, **stamp, "error": str(exc)}
|
||||
text = json.dumps(report, ensure_ascii=False, indent=2)
|
||||
if args.output:
|
||||
Path(args.output).write_text(text + "\n")
|
||||
|
||||
Reference in New Issue
Block a user