forked from xhgit/webup-cloud
51 lines
2.4 KiB
Python
51 lines
2.4 KiB
Python
"""Restart only the dedicated test PostgreSQL and compare persisted business data."""
|
|
import json
|
|
import os
|
|
import pathlib
|
|
import subprocess
|
|
import time
|
|
|
|
if os.environ.get('WSL_DISTRO_NAME') != 'WebUp-Local' or os.geteuid() != 0:
|
|
raise SystemExit('Dedicated local environment required.')
|
|
name = 'webup-test-postgres'
|
|
|
|
|
|
def docker(*args):
|
|
return subprocess.check_output(['docker', *args], text=True, timeout=60)
|
|
|
|
|
|
metadata = json.loads(docker('container', 'inspect', name))[0]
|
|
if metadata['Config'].get('Labels', {}).get('webup.local-test') != 'true':
|
|
raise SystemExit('Refusing to restart an unowned container.')
|
|
tables = ('users', 'wallets', 'wallet_ledger', 'ai_tasks', 'ai_usage_records')
|
|
|
|
|
|
def fingerprint():
|
|
result = {}
|
|
for table in tables:
|
|
key = 'user_id' if table == 'wallets' else 'id'
|
|
query = "SELECT count(*) || ':' || md5(COALESCE(jsonb_agg(to_jsonb(t) ORDER BY " + key + ")::text,'[]')) FROM " + table + ' t'
|
|
result[table] = docker('exec', name, 'psql', '-XAt', '-v', 'ON_ERROR_STOP=1', '-U', 'webup_test', '-d', 'webup_integration', '-c', query).strip()
|
|
result['migrations'] = docker('exec', name, 'psql', '-XAt', '-v', 'ON_ERROR_STOP=1', '-U', 'webup_test', '-d', 'webup_integration', '-c', 'SELECT count(*) FROM flyway_schema_history WHERE success').strip()
|
|
return result
|
|
|
|
|
|
before = fingerprint()
|
|
if int(before['users'].split(':')[0]) == 0 or int(before['wallet_ledger'].split(':')[0]) == 0:
|
|
raise SystemExit('Run the integration tests first; an empty database is not a persistence check.')
|
|
docker('restart', '--time', '20', name)
|
|
for attempt in range(30):
|
|
ready = subprocess.run(['docker', 'exec', name, 'pg_isready', '-U', 'webup_test', '-d', 'webup_integration'], capture_output=True, timeout=5)
|
|
if ready.returncode == 0:
|
|
break
|
|
time.sleep(1)
|
|
else:
|
|
raise SystemExit('PostgreSQL failed to recover after restart.')
|
|
after = fingerprint()
|
|
if before != after:
|
|
raise SystemExit('Persisted database fingerprints changed after restart.')
|
|
report = {'passed': True, 'checkedAtUnix': time.time(), 'database': 'webup_integration', 'fingerprints': after}
|
|
target = pathlib.Path(__file__).resolve().parents[2] / '.runtime/local-cluster/postgres-persistence.json'
|
|
target.write_text(json.dumps(report, indent=2))
|
|
print('PASS PostgreSQL restart preserved users, wallets, ledger, task / usage records and migration history.')
|