"""Guarded acceptance runner for the retained disposable DB; never uses .env.""" import argparse import json import os from pathlib import Path import subprocess import sys import time ROOT = Path(__file__).resolve().parents[3] sys.path.insert(0, str(ROOT)) from sqlalchemy import create_engine, text from sqlalchemy.engine import URL from tests.support.postgres import validated_test_url NAME = 'rag-local-s2s-postgres-test' IDENTITY = 'e5da8c342ed59fc48a49856bfd76010feda45e4a221f640d34aa330a5cd8a7da' IMAGE = 'sha256:7b822b0aac60967beb1ea5e576b8602c94c300a157d187f385ae3e0da199b90a' VOLUME = '9f19da047744737e9bc010ba69c2f22e347f898939d1d42f7bbbb97c4591a3b6' def inspect(name): return json.loads(subprocess.check_output(['docker', 'inspect', name]))[0] def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('--suite', choices=['local', 'expanded'], required=True) args = parser.parse_args() row, operational = inspect(NAME), inspect('rag-postgres') values = dict(item.split('=', 1) for item in row['Config']['Env'] if '=' in item) expected_ports = {'5432/tcp': [{'HostIp': '127.0.0.1', 'HostPort': '55439'}]} if not (row['Id'] == IDENTITY and row['Image'] == IMAGE and row['HostConfig']['PortBindings'] == expected_ports and values.get('POSTGRES_DB') == 'rag_local_s2s_test' and values.get('POSTGRES_USER') == 'local_s2s_test' and row['State']['Status'] == 'exited' and len(row['Mounts']) == 1 and row['Mounts'][0].get('Name') == VOLUME and all(m.get('Name') != VOLUME for m in operational['Mounts']) and values.get('POSTGRES_PASSWORD')): raise SystemExit('DISPOSABLE_DATABASE_IDENTITY_MISMATCH') url = validated_test_url(URL.create('postgresql+psycopg', username=values['POSTGRES_USER'], password=values['POSTGRES_PASSWORD'], host='127.0.0.1', port=55439, database=values['POSTGRES_DB'])) print(json.dumps(dict(stage='verified-container', container_id=IDENTITY, database=url.database, host=url.host, port=url.port, volume=VOLUME)), flush=True) started = False try: subprocess.run(['docker', 'start', IDENTITY], check=True, stdout=subprocess.DEVNULL) started = True for attempt in range(30): ready = subprocess.run(['docker', 'exec', IDENTITY, 'pg_isready', '-U', 'local_s2s_test', '-d', 'rag_local_s2s_test'], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) if ready.returncode == 0: break time.sleep(1) else: raise SystemExit('DISPOSABLE_DATABASE_STARTUP_DEADLINE') engine = create_engine(url, hide_parameters=True) try: with engine.connect() as conn: actual = tuple(conn.execute(text('SELECT current_database(), current_user')).one()) if actual != ('rag_local_s2s_test', 'local_s2s_test'): raise SystemExit('DISPOSABLE_DATABASE_SERVER_MISMATCH') finally: engine.dispose() env = os.environ.copy() env['TEST_DATABASE_URL'] = url.render_as_string(hide_password=False) tests = ['tests/integration/test_local_s2s_phase2.py'] if args.suite == 'expanded': tests.append('tests/integration/test_phase2_postgres.py') command = [sys.executable, '-m', 'pytest', *tests, '-m', 'postgres', '--require-postgres', '-q', '--tb=short', '-p', 'no:cacheprovider'] print(json.dumps(dict(stage='acceptance', suite=args.suite, schemas='test_; fixture creates/drops', require_postgres=True)), flush=True) return subprocess.run(command, cwd=ROOT, env=env, timeout=600).returncode finally: if started: stopped = subprocess.run(['docker', 'stop', '--time', '5', IDENTITY], stdout=subprocess.DEVNULL) print(json.dumps(dict(stage='test-container-restored', stopped=stopped.returncode == 0, status=inspect(NAME)['State']['Status'])), flush=True) if stopped.returncode: raise SystemExit('DISPOSABLE_DATABASE_STOP_FAILED') if __name__ == '__main__': raise SystemExit(main())