Files
mtusi2026lab6/scripts/lab5/stand.py
T
ilya.tyunin 6889e77ab4 lab6: базовый коммит из lab5 (0790599)
Перенесено дерево lab5 целиком: NestJS-приложение (API + worker,
RabbitMQ, outbox, S3), Dockerfile, compose.yaml, infra/{haproxy,minio,
rabbitmq}, стендовые скрипты, scripts/lab4, scripts/lab5,
evidence/lab4, evidence/lab5 и contracts/extensions.openapi.json.

Задание lab6 оставлено своим: README, REPORT, COURSE, lab.json,
contracts/{openapi,infrastructure,jobs.schema,attachment-states,
examples,observability}, tests, compose.example.yaml, infra/otel,
.github/pull_request_template.md.

Адаптации при переносе:
- docker-проект mtusi-lab5 -> mtusi-lab6 и тег образа
  mtusi-messenger:lab6 (compose.yaml, scripts/stop-one.sh, .env.example,
  infra/README.md), чтобы тома и контейнеры не пересекались с lab5;
- Makefile: цели lab5 сохранены целиком (skeleton lab6 не отличается
  от skeleton lab5);
- .env.example: переменные приложения и стенда lab5 + блок lab6
  (OTEL_*) из skeleton.
2026-11-09 16:44:21 +03:00

181 lines
5.4 KiB
Python

"""Shared stdlib-only helpers for the lab5 demo and fault suite."""
import hashlib
import json
import subprocess
import sys
import time
from pathlib import Path
ROOT = Path(__file__).resolve().parents[2]
sys.path.insert(0, str(ROOT / 'tests'))
from client import Client, Failure, check # noqa: E402
ENV_FILE = '.env'
COMPOSE = ['docker', 'compose', '--env-file', ENV_FILE]
def compose(*args, stdin=None, check_result=True):
result = subprocess.run(
COMPOSE + list(args),
cwd=ROOT,
input=stdin,
text=True,
capture_output=True,
)
if check_result and result.returncode != 0:
detail = (result.stderr or result.stdout).strip().splitlines()
raise Failure('compose ' + ' '.join(args) + ' failed: ' +
(detail[-1] if detail else 'unknown error'))
return result
def docker(*args, check_result=True):
result = subprocess.run(
['docker'] + list(args), cwd=ROOT, text=True, capture_output=True,
)
if check_result and result.returncode != 0:
detail = (result.stderr or result.stdout).strip().splitlines()
raise Failure('docker ' + ' '.join(args) + ' failed: ' +
(detail[-1] if detail else 'unknown error'))
return result
def cli(*args, stdin=None):
"""Run jobs.cli in the API container; returns (exit_code, parsed_json)."""
for _ in range(5):
result = compose(
'exec', '-T', 'api', 'node', 'dist/jobs/jobs.cli.js', *args,
stdin=stdin, check_result=False,
)
text = result.stdout.strip()
if text:
try:
return result.returncode, json.loads(text)
except ValueError:
pass
time.sleep(1)
raise Failure('jobs.cli did not return JSON for ' + ' '.join(args))
def set_fault(key, value):
compose('exec', '-T', 'redis', 'redis-cli', 'SET', key, value)
def clear_fault(key):
compose('exec', '-T', 'redis', 'redis-cli', 'DEL', key)
def clear_faults():
script = (
"local keys=redis.call('keys',ARGV[1]) "
"for i=1,#keys do redis.call('del',keys[i]) end return #keys"
)
compose('exec', '-T', 'redis', 'redis-cli', 'EVAL', script, '0', 'fault:*')
def rabbit_queues():
result = compose('exec', '-T', 'broker', 'rabbitmqctl', '-q',
'-p', 'messenger', 'list_queues',
'name', 'messages', 'type')
queues = {}
for line in result.stdout.splitlines():
parts = line.split()
if len(parts) >= 2 and parts[0] not in ('name', 'Timeout:'):
try:
queues[parts[0]] = {'messages': int(parts[1]),
'type': parts[2] if len(parts) > 2 else ''}
except ValueError:
continue
return queues
def wait_for(predicate, timeout, description='condition', interval=0.5):
deadline = time.monotonic() + timeout
last = None
while time.monotonic() < deadline:
last = predicate()
if last:
return last
time.sleep(interval)
raise Failure('timed out waiting for ' + description)
def upload(client, actor, chat, data, filename='sample.png',
content_type='image/png'):
result = client.call(
'POST',
'/api/v1/chats/' + chat['id'] + '/attachments?filename=' + filename,
202,
actor=actor,
raw=data,
headers={'Content-Type': content_type},
schema='Attachment',
)
check(result['json']['status'] == 'queued', 'upload did not answer queued')
return result['json']
def metadata(client, actor, attachment_id):
return client.call('GET', '/api/v1/attachments/' + attachment_id,
actor=actor, schema='Attachment')['json']
def wait_ready(client, actor, attachment_id, timeout=60,
allow_failed=False):
deadline = time.monotonic() + timeout
while time.monotonic() < deadline:
meta = metadata(client, actor, attachment_id)
if meta['status'] == 'failed':
if allow_failed:
return meta
raise Failure('attachment failed: ' +
str(meta.get('error_code')))
if meta['status'] == 'ready':
return meta
time.sleep(0.2)
raise Failure('attachment did not become ready in ' + str(timeout) + 's')
def sha256(data):
return hashlib.sha256(data).hexdigest()
def variant_sha(meta):
return {item['name']: item['sha256'] for item in meta['variants']}
def sample_bytes():
return (ROOT / 'tests/fixtures/sample.png').read_bytes()
def worker_env(name):
result = compose('exec', '-T', 'worker', 'printenv', name,
check_result=False)
return result.stdout.strip()
def worker_start_count():
result = compose('logs', '--no-color', 'worker', check_result=False)
return result.stdout.count('worker ready:')
def worker_memory_peak(containers):
names = [line for line in containers.splitlines() if line.strip()]
if not names:
return 'n/a'
result = subprocess.run(
['docker', 'stats', '--no-stream', '--format',
'{{.Name}} {{.MemUsage}}'] + names,
text=True, capture_output=True,
)
return result.stdout.strip()
def print_step(number, text):
print('[' + str(number) + '] ' + text, flush=True)
def new_client(base_url, ca_file=None):
return Client(base_url, ca_file)