Files
2026-11-06 18:54:28 +03:00

70 lines
2.1 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Исходный persistence-сценарий плюс доказательство рестарта всех трёх сервисов."""
import argparse
import json
from pathlib import Path
from runtime_config import base_url
import subprocess
import sys
ROOT = Path(__file__).resolve().parents[1]
def states():
result = {}
for service in ("api", "postgres", "redis"):
ids = subprocess.check_output(
["docker", "compose", "ps", "-q", service], cwd=ROOT, text=True
).split()
if not ids:
raise RuntimeError("Не запущен сервис " + service)
result[service] = {}
for container in ids:
state = json.loads(
subprocess.check_output(
["docker", "inspect", "--format", "{{json .State}}", container],
cwd=ROOT,
text=True,
)
)
result[service][container] = {
"StartedAt": state["StartedAt"],
"Pid": state["Pid"],
}
return result
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--base-url", default=base_url())
parser.add_argument("--action-script", required=True)
parser.add_argument("--ca-file")
args = parser.parse_args()
before = states()
print("ДО: " + json.dumps(before), flush=True)
command = [
sys.executable,
str(ROOT / "tests/resilience.py"),
"--mode",
"persistence",
"--action-script",
args.action_script,
"--base-url",
args.base_url,
]
if args.ca_file:
command += ["--ca-file", args.ca_file]
subprocess.run(command, cwd=ROOT, check=True)
after = states()
print("ПОСЛЕ: " + json.dumps(after), flush=True)
for service in before:
if before[service] == after[service]:
raise AssertionError("Сервис не перезапущен: " + service)
print(
"PASS: StartedAt изменился у API, PostgreSQL и Redis; прежний токен и данные сохранены"
)
if __name__ == "__main__":
main()