Files

66 lines
2.7 KiB
Python
Executable File

#!/usr/bin/env python3
"""Run existing database/cache integration tests over verified local TLS.
Requires a bootstrapped stand (make up). Tests create isolated PostgreSQL
fixture databases and use Redis DB7, never production cache DB0.
"""
from __future__ import annotations
import os
from pathlib import Path
import subprocess
import sys
from lab7_environment import OperationError, ROOT
from securityctl import Controller
def main() -> int:
control = Controller()
if not control.credentials_path.exists():
raise OperationError('run make up before operator integration tests')
values = control.credentials()
environment = control.env.process_env.copy()
environment['COMPOSE_FILE'] = str(ROOT / 'docker-compose.local.yaml')
control.env.process_env.update(environment)
control.env.config['COMPOSE_FILE'] = environment['COMPOSE_FILE']
control.env.compose('up', '-d', '--no-build', 'postgres', 'redis', timeout=180)
control.wait_stores()
identity = control.env.identity('operator')
environment.update({
'TEST_POSTGRES_HOST': 'localhost',
'TEST_POSTGRES_PORT': control.env.value('TEST_POSTGRES_PORT', '15432'),
'TEST_POSTGRES_USER': values['postgres']['operator']['username'],
'TEST_POSTGRES_PASSWORD': values['postgres']['operator']['password'],
'TEST_POSTGRES_DATABASE': 'messenger',
'TEST_POSTGRES_SSL_MODE': 'verify-full',
'TEST_REDIS_HOST': 'localhost',
'TEST_REDIS_PORT': control.env.value('TEST_REDIS_PORT', '16379'),
'TEST_REDIS_USERNAME': 'operator',
'TEST_REDIS_PASSWORD': values['redis']['operator']['password'],
'TEST_REDIS_DB': '7',
'TLS_CA_FILE': str(identity / 'ca.crt'),
'TLS_CERT_FILE': str(identity / 'tls.crt'),
'TLS_KEY_FILE': str(identity / 'tls.key'),
'TLS_CRL_FILE': str(identity / 'ca.crl'),
'TLS_VERSION_FILE': str(identity.parent / 'current-version'),
})
result = subprocess.run([
os.environ.get('GO', 'go'), 'test', '-tags=integration', '-p', '1',
'-count=1', '-timeout=15m', './internal/storage/postgres', './internal/storage/redis',
], cwd=ROOT, env=environment, text=True, capture_output=True, timeout=1000)
output = control.env.redact(result.stdout + result.stderr)
destination = ROOT / 'evidence/lab7/integration.txt'
destination.parent.mkdir(parents=True, exist_ok=True)
destination.write_text(output)
print(output, end='')
return result.returncode
if __name__ == '__main__':
try:
raise SystemExit(main())
except (OperationError, OSError, subprocess.TimeoutExpired) as error:
print('FAIL: ' + str(error), file=sys.stderr)
raise SystemExit(1)