Files

50 lines
2.5 KiB
Python

"""Optional Docker-backed test harness; deployment itself remains native Linux/systemd."""
import argparse
import json
import os
import pathlib
import secrets
import subprocess
import time
parser = argparse.ArgumentParser()
parser.add_argument('--reference', action='store_true', help='Run the preserved B-local A baseline')
parser.add_argument('--image', default='postgres:18.6')
args = parser.parse_args()
repository = pathlib.Path(__file__).resolve().parents[1]
cwd = repository / 'Reference/ArchitectureA' if args.reference else repository
evidence = repository / 'ImplementationPlan/evidence'
evidence.mkdir(exist_ok=True)
label = 'reference' if args.reference else 'b'
name = 'healthapp-b-tests-' + secrets.token_hex(4)
password = secrets.token_hex(32)
container_env = dict(os.environ, POSTGRES_PASSWORD=password)
subprocess.run(['docker', 'run', '--rm', '-d', '--name', name, '-p', '127.0.0.1::5432',
'-e', 'POSTGRES_PASSWORD', args.image], env=container_env,
stdout=subprocess.DEVNULL, check=True)
try:
for attempt in range(60):
if subprocess.run(['docker', 'exec', name, 'pg_isready', '-U', 'postgres'],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).returncode == 0:
break
time.sleep(.5)
else:
raise RuntimeError('Disposable database did not start.')
ports = json.loads(subprocess.check_output(
['docker', 'inspect', '--format', '{{json .NetworkSettings.Ports}}', name], text=True))
port = ports['5432/tcp'][0]['HostPort']
env = dict(os.environ, TEST_POSTGRES_CONNECTION='Host=127.0.0.1;Port=' + port +
';Database=postgres;Username=postgres;Password=' + password)
dotnet = os.environ.get('HEALTHAPP_DOTNET', 'dotnet')
for stage, command in [('build', ['build', 'HealthApp.slnx', '-c', 'Release']),
('tests', ['test', 'HealthApp.slnx', '-c', 'Release', '--no-build',
'--logger', 'trx', '--results-directory', str(evidence / (label + '-trx'))])]:
result = subprocess.run([dotnet, *command], cwd=cwd, env=env, capture_output=True, text=True)
output = (result.stdout + result.stderr).replace(password, '[redacted]')
(evidence / (label + '-' + stage + '.log')).write_text(output)
print(output[-5000:])
if result.returncode:
raise SystemExit(result.returncode)
finally:
subprocess.run(['docker', 'stop', name], stdout=subprocess.DEVNULL, check=True)