import os import subprocess import time import uuid from pathlib import Path from client import Failure, check ROOT = Path(__file__).resolve().parent.parent SAMPLE = (ROOT / 'tests/fixtures/sample.png').read_bytes() def compose(*args, env=None, check_rc=True): full = dict(os.environ, **(env or {})) return subprocess.run(['docker', 'compose', '--env-file', '.env', *args], cwd=ROOT, check=check_rc, capture_output=True, text=True, env=full) def psql(sql): return compose('exec', '-T', 'postgres', 'psql', '-U', 'messenger', '-d', 'messenger', '-Atc', sql).stdout.strip() def broker(*args): cmd = 'valkey-cli --no-auth-warning -a "$(sed -n "s/^requirepass //p" /run/secrets/broker_conf)" ' + ' '.join("'%s'" % a for a in args) return compose('exec', '-T', 'broker', 'sh', '-c', cmd).stdout.strip() def logs(service, since): return compose('logs', '--no-color', '--since', since, service).stdout def now_iso(): return time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime()) def upload(c, actor, chat, data=SAMPLE, content_type='image/png', filename=None, expected=202): filename = filename or 'f-' + uuid.uuid4().hex[:12] + '.png' return c.call('POST', '/api/v1/chats/' + chat['id'] + '/attachments?filename=' + filename, expected, actor=actor, raw=data, headers={'Content-Type': content_type}, schema='Attachment' if expected == 202 else None) def wait_status(c, actor, att_id, statuses, timeout=120): deadline = time.monotonic() + timeout while time.monotonic() < deadline: try: att = c.call('GET', '/api/v1/attachments/' + att_id, actor=actor, schema='Attachment')['json'] if att['status'] in statuses: return att except Failure: pass time.sleep(0.5) raise Failure('attachment %s did not reach %s' % (att_id, statuses)) def wait_api(c, timeout=90): deadline = time.monotonic() + timeout while time.monotonic() < deadline: try: c.call('GET', '/health/ready') return except Failure: time.sleep(1) raise Failure('api did not become ready') def wait_log(service, since, needle, timeout=60): deadline = time.monotonic() + timeout while time.monotonic() < deadline: if needle in logs(service, since): return time.sleep(1) raise Failure('%s did not log %r' % (service, needle)) def job_row(att_id): out = psql("select status, attempt, coalesce(last_error, '') from jobs where attachment_id='%s'" % att_id) check(out, 'job not found') status, attempt, error = out.split('|', 2) return status, int(attempt), error def variant_count(att_id): return int(psql("select count(*) from attachment_variants where attachment_id='%s'" % att_id)) def restore_worker(): compose('up', '-d', '--force-recreate', '--no-deps', 'worker', env={'WORKER_FAULT_POINT': ''}) def restore_api(c): compose('up', '-d', '--force-recreate', '--no-deps', '--wait', 'api', env={'API_FAULT_POINT': ''}) compose('restart', 'lb') wait_api(c)