37 lines
1.3 KiB
Python
37 lines
1.3 KiB
Python
"""Local Compose test helpers. SQL uses only synthetic identifiers generated by tests."""
|
|
import subprocess
|
|
import sys
|
|
import time
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT / "tests"))
|
|
from client import Failure, check # noqa: E402
|
|
|
|
|
|
def compose(*args, input_text=None, timeout=40):
|
|
result = subprocess.run(["docker", "compose", *args], cwd=ROOT, input=input_text,
|
|
text=True, capture_output=True, timeout=timeout)
|
|
check(result.returncode == 0, "Compose operation failed (inspect the local stack)")
|
|
return result.stdout.strip()
|
|
|
|
|
|
def sql(statement, service="postgres", database="messenger", timeout=40):
|
|
return compose("exec", "-T", service, "psql", "-X", "-U", "messenger",
|
|
"-d", database, "-v", "ON_ERROR_STOP=1", "-Atq", input_text=statement, timeout=timeout)
|
|
|
|
|
|
def redis(*args):
|
|
return compose("exec", "-T", "redis", "redis-cli", "--raw", *args)
|
|
|
|
|
|
def ready(client):
|
|
deadline = time.monotonic() + 45
|
|
while True:
|
|
try:
|
|
client.call("GET", "/health/ready", schema="Health")
|
|
return
|
|
except Failure:
|
|
check(time.monotonic() < deadline, "stack did not recover within 45 seconds")
|
|
time.sleep(0.5)
|