Files
rust-discovery/tests/windows_checkpoint_review_database.py
2026-11-15 23:50:29 +01:00

66 lines
3.4 KiB
Python

#!/usr/bin/env python3
"""Human checkpoint recovery contract against an isolated PostgreSQL fixture."""
from pathlib import Path
import subprocess
import time
import uuid
ROOT=Path(__file__).resolve().parents[1]
NAME='rmm-windows-human-review-'+uuid.uuid4().hex[:10]
def command(args, data=None, timeout=120):
result=subprocess.run(args,input=data,capture_output=True,timeout=timeout)
if result.returncode:
raise RuntimeError(result.stderr.decode(errors='replace')[-4000:])
return result.stdout
def main():
command(['docker','run','-d','--network','none','--name',NAME,
'-e','POSTGRES_HOST_AUTH_METHOD=trust','postgres:17.6-bookworm'])
try:
for _ in range(100):
logs=command(['docker','logs',NAME])
ready=subprocess.run(['docker','exec',NAME,'pg_isready','-U','postgres'],capture_output=True)
if b'init process complete' in logs and ready.returncode==0:break
time.sleep(.2)
else:raise RuntimeError('ISOLATED_POSTGRES_NOT_READY')
def sql(raw):
return command(['docker','exec','-i',NAME,'psql','-X','-qAt','-v','ON_ERROR_STOP=1','-U','postgres'],raw.encode())
sql('CREATE ROLE rmm_app LOGIN NOINHERIT NOSUPERUSER NOBYPASSRLS;')
# Exercise the 0016 -> 0017 transition with an existing device record.
files = sorted((ROOT / 'migrations').glob('*.sql'))
upgrade = ROOT / 'migrations/0017_windows_checkpoint_recovery.sql'
for path in files:
if path == upgrade:
break
sql(path.read_text())
marker = '21c7560f-0132-4870-8be9-452fa3f67b56'
sql(f"INSERT INTO assets VALUES('22222222-2222-4222-8222-222222222222','{marker}');")
for path in files[files.index(upgrade):]:
sql(path.read_text())
if sql(f"SELECT count(*) FROM assets WHERE asset_id='{marker}';").strip() != b'1':
raise RuntimeError('Existing record lost during recovery migration')
if sql("SELECT count(*) FROM memberships WHERE 'recovery:confirm'=ANY(permissions);").strip() != b'0':
raise RuntimeError('Migration implicitly granted human recovery permission')
# Both modes need the same observed identity and independent original head.
sql('CREATE DATABASE rmm_echo TEMPLATE postgres;')
output=command(['docker','run','--rm','--network','container:'+NAME,
'--read-only','--tmpfs','/tmp:rw,size=512m','-v',str(ROOT)+':/src:ro',
'-v',str(ROOT/'devstate/windows-server-contract/target')+':/target','-e','CARGO_TARGET_DIR=/target',
'-e','RMM_TEST_REPAIR_ADMIN_URL=postgres://postgres@127.0.0.1/postgres',
'-e','RMM_TEST_ECHO_ADMIN_URL=postgres://postgres@127.0.0.1/rmm_echo',
'-e','RMM_TEST_ECHO_APP_URL=postgres://rmm_app@127.0.0.1/rmm_echo',
'-e','RMM_TEST_REPAIR_APP_URL=postgres://rmm_app@127.0.0.1/postgres',
'--entrypoint','cargo','rust-discovery-windows-tls-full:local',
'test','--locked','--offline','-p','rmm-server','windows_checkpoint_review::tests',
'--','--include-ignored','--nocapture'],timeout=300)
print(output.decode())
print('PASS isolated human review and dev echo scope, signed receipts, idempotence, fixed job/offer/permit and rejection of alternate execution identities.')
finally:
subprocess.run(['docker','rm','-f',NAME],capture_output=True)
if __name__=='__main__':main()