forked from mtusi2026/mtusi2026lab4
ЛР4: MinIO, вложения и reconciler на оценку 5
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
"""Shared operator/test helpers. Never print credentials or HTTP bodies."""
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT/'tests'))
|
||||
from client import Client as PublicClient, check
|
||||
|
||||
|
||||
class Client(PublicClient):
|
||||
"""Use the published IPv4 loopback by default; respect explicit targets."""
|
||||
def __init__(self, base_url=None, ca_file=None, timeout=10):
|
||||
super().__init__(base_url or os.environ.get('BASE_URL') or 'http://127.0.0.1:8080',
|
||||
ca_file=ca_file, timeout=timeout)
|
||||
|
||||
|
||||
def command(args, *, capture=True, timeout=600, input=None):
|
||||
result = subprocess.run(args, cwd=ROOT, input=input, text=True,
|
||||
encoding='utf-8', errors='replace', capture_output=capture,
|
||||
timeout=timeout)
|
||||
if result.returncode:
|
||||
# Child output may contain environment-specific details. Full command
|
||||
# does not contain secrets, but do not echo arbitrary child stderr.
|
||||
raise RuntimeError('command failed: '+str(args[0])+' (exit '+str(result.returncode)+')')
|
||||
return result.stdout.strip() if capture else None
|
||||
|
||||
|
||||
def compose(*args, **kwargs):
|
||||
return command(['docker', 'compose', *args], **kwargs)
|
||||
|
||||
|
||||
def sql(query):
|
||||
return compose('exec', '-T', 'db', 'psql', '-X', '-v', 'ON_ERROR_STOP=1',
|
||||
'-U', 'messenger', '-d', 'messenger', '-At', '-c', query)
|
||||
|
||||
|
||||
def mc(script):
|
||||
setup = ('set -eu\nmc alias set local http://s3:9000 '
|
||||
'"$(cat /run/secrets/s3_root_user)" '
|
||||
'"$(cat /run/secrets/s3_root_password)" >/dev/null\n')
|
||||
return compose('run', '--rm', '--no-deps', '-T', '--entrypoint', '/bin/sh',
|
||||
's3-init', '-c', setup+script)
|
||||
|
||||
|
||||
def reconcile(apply=False, grace=60):
|
||||
args = ['exec', '-T', 'api', '/messenger', 'reconcile', '--grace-seconds', str(grace)]
|
||||
if apply:
|
||||
args.append('--apply')
|
||||
report = json.loads(compose(*args))
|
||||
check(report['complete'], 'incomplete reconciler report')
|
||||
return report
|
||||
|
||||
|
||||
def wait_ready(base=None, seconds=90, count=1):
|
||||
deadline = time.monotonic()+seconds
|
||||
client=Client(base, timeout=2)
|
||||
observed=set()
|
||||
last_error=None
|
||||
while time.monotonic()<deadline:
|
||||
try:
|
||||
response=client.call('GET','/health/ready',schema='Health')
|
||||
observed.add(response['headers']['X-Instance-Id'])
|
||||
if len(observed)>=count:return
|
||||
except (OSError,AssertionError) as exc:
|
||||
last_error=type(exc).__name__
|
||||
observed.clear()
|
||||
time.sleep(.1)
|
||||
raise RuntimeError('stack did not become ready: expected '+str(count)+
|
||||
' distinct replicas, observed '+str(len(observed))+
|
||||
'; last probe '+(last_error or 'HTTP 200'))
|
||||
|
||||
|
||||
def save(name, data):
|
||||
(ROOT/'evidence').mkdir(exist_ok=True)
|
||||
(ROOT/'evidence'/name).write_text(json.dumps(data, ensure_ascii=False, indent=2)+'\n', encoding='utf-8')
|
||||
Reference in New Issue
Block a user