#!/usr/bin/env python3 """Explicit disposable-container-only test of the root helper CLI and /run state.""" import json import os from pathlib import Path import subprocess import sys import uuid from test_disconnected_state import CONNECTED, DISCONNECTED assert os.geteuid() == 0 and os.environ.get('MC_DISPOSABLE_STATE_TEST') == '1', ( 'Run only in a disposable container with MC_DISPOSABLE_STATE_TEST=1') helper = (Path(sys.argv[1]) if len(sys.argv) > 1 else Path(__file__).resolve().parents[1] / 'refactored/managed-container.d/applications/postgresql/promotion_state.py') resource = 'mc-disposable-' + uuid.uuid4().hex args = ['python3', str(helper), resource, 'test-owner', 'test.service', 'test-container', '300'] def run(obs, expected): result = subprocess.run(args, input=json.dumps(obs), text=True, capture_output=True, timeout=3) assert result.returncode == 0 and result.stdout.strip() == expected, result run(DISCONNECTED, '-INFINITY') run(CONNECTED, '100') files = list(Path('/run/managed-container/promotion').glob('*.json')) record_path = next(p for p in files if json.loads(p.read_text())['context'][0] == resource) record = json.loads(record_path.read_text()) assert record['replay'] == CONNECTED['replay'] and record['flush'] == CONNECTED['flush'] for path, mode in [(record_path, 0o600), (record_path.parent, 0o700), (record_path.parent.parent, 0o700)]: info = path.stat() assert (info.st_uid, info.st_gid, info.st_mode & 0o777) == (0, 0, mode), path run(DISCONNECTED, '50') assert json.loads(record_path.read_text()) == record # Disconnection never renews evidence. record_path.write_text('{malformed') run(DISCONNECTED, '-INFINITY') assert not record_path.exists() run(CONNECTED, '100') run(dict(DISCONNECTED, identity=['999', '/new', '1789201000.123456']), '-INFINITY') assert not record_path.exists() print('PASS root CLI: fixed /run location, ownership/modes, persistence, disconnect, malformed and replaced data')