обновила тесты на оценку 5
This commit is contained in:
@@ -25,6 +25,7 @@ import sys
|
||||
import time
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
import base64
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tests"))
|
||||
from client import Client, Failure, arguments, check, run # noqa: E402
|
||||
@@ -325,14 +326,21 @@ def step_backup_restore(dsn: str):
|
||||
def step_rpo(dsn: str, base_url: str, ca_file):
|
||||
print("=== 5. RPO: запись во время pg_dump ===")
|
||||
c = Client(base_url, ca_file)
|
||||
|
||||
# Данные и сессии готовим заранее, до дампа.
|
||||
alice, bob, eve, chat = c.bootstrap()
|
||||
|
||||
# Контроль: сессия должна быть валидна, иначе 401 — это не RPO.
|
||||
me = c.call("GET", "/api/v1/users/me", 200, actor=alice, schema="User")
|
||||
check(me["status"] == 200, "сессия должна быть валидна перед RPO-тестом")
|
||||
|
||||
marker = "rpo-" + uuid.uuid4().hex
|
||||
# Запускаем pg_dump
|
||||
out_file = EVIDENCE / "lab2_track2_rpo.dump"
|
||||
if out_file.exists():
|
||||
out_file.unlink()
|
||||
|
||||
# pg_dump: тот же приём, что в шаге 4.
|
||||
proc = subprocess.Popen(
|
||||
["docker", "run", "--rm",
|
||||
"--add-host=host.docker.internal:host-gateway",
|
||||
@@ -341,21 +349,34 @@ def step_rpo(dsn: str, base_url: str, ca_file):
|
||||
"pg_dump", "-Fc", "-f", "/out/lab2_track2_rpo.dump", dsn],
|
||||
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
|
||||
)
|
||||
time.sleep(1.0) # гарантируем, что снапшот дампа уже снят
|
||||
written = c.message(alice, chat, marker)["json"]
|
||||
time.sleep(1.0) # снапшот уже снят
|
||||
|
||||
# Свежий логин прямо перед записью: не зависим от TTL старой сессии.
|
||||
cred = base64.b64encode(
|
||||
(alice["registration"]["username"] + ":" +
|
||||
alice["registration"]["password"]).encode()
|
||||
).decode()
|
||||
fresh = c.call("POST", "/api/v1/auth/sessions", 201,
|
||||
headers={"Authorization": "Basic " + cred},
|
||||
schema="Session")["json"]
|
||||
alice_for_write = {"id": alice["id"], "user": alice["user"], "token": fresh["token"]}
|
||||
|
||||
written = c.message(alice_for_write, chat, marker)["json"]
|
||||
code = proc.wait(timeout=300)
|
||||
check(code == 0, "pg_dump для RPO упал")
|
||||
|
||||
# restore во временный контейнер
|
||||
# Временный restore-контейнер: публикуем порт 55433 на хост.
|
||||
restore_container = "mtusi-pg-rpo"
|
||||
subprocess.run(["docker", "rm", "-f", restore_container], capture_output=True)
|
||||
subprocess.run(
|
||||
["docker", "run", "-d", "--name", restore_container,
|
||||
"-e", "POSTGRES_PASSWORD=restore", "-e", "POSTGRES_USER=postgres",
|
||||
"-e", "POSTGRES_DB=high_load_messenger",
|
||||
"-p", "127.0.0.1:55433:5432",
|
||||
"postgres:18-alpine"],
|
||||
capture_output=True, text=True,
|
||||
)
|
||||
|
||||
deadline = time.monotonic() + 60
|
||||
while time.monotonic() < deadline:
|
||||
c2 = subprocess.run(["docker", "exec", restore_container, "pg_isready", "-U", "postgres"],
|
||||
@@ -363,14 +384,17 @@ def step_rpo(dsn: str, base_url: str, ca_file):
|
||||
if c2.returncode == 0:
|
||||
break
|
||||
time.sleep(1)
|
||||
else:
|
||||
raise Failure("restore container для RPO не поднялся")
|
||||
|
||||
# pg_restore идёт в опубликованный порт через host.docker.internal.
|
||||
r = subprocess.run(
|
||||
["docker", "run", "--rm",
|
||||
"--add-host=host.docker.internal:host-gateway",
|
||||
"--network", "host",
|
||||
"-v", f"{EVIDENCE}:/in",
|
||||
"postgres:18-alpine",
|
||||
"pg_restore", "-d", "postgresql://postgres:restore@127.0.0.1:5432/high_load_messenger",
|
||||
"pg_restore", "-d",
|
||||
"postgresql://postgres:restore@host.docker.internal:55433/high_load_messenger",
|
||||
"--no-owner", "--no-privileges", "/in/lab2_track2_rpo.dump"],
|
||||
capture_output=True, text=True, timeout=600,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user