"""Authorized real systemd stop/start smoke test; no credentials in output.""" import argparse import json import shlex from remote_support import connect, execute REMOTE = r''' import hashlib, json, subprocess, time, urllib.request from pathlib import Path from uuid import uuid4 def service(action): subprocess.run(['systemctl', action, 'matrix-screen-controller.service'], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=45) def status(): with urllib.request.urlopen('http://127.0.0.1:8080/api/status', timeout=3) as response: return json.load(response) data = Path('/var/lib/matrix-screen-controller') paths = [data/'config.json', data/'wifi_config.json', data/'mobile/identity.json'] def fingerprints(): return {str(p.relative_to(data)): hashlib.sha256(p.read_bytes()).hexdigest() if p.exists() else None for p in paths} before = fingerprints() assert before['config.json'] and before['mobile/identity.json'] initial = status() assert not initial['mobile']['connected'], 'Phone must be disconnected before lifecycle test' sentinel = Path('/run/matrix-screen-controller') / ('mobile-lifecycle-' + uuid4().hex) sentinel.write_text('preservation-check', encoding='utf-8') stopped_preserved = False try: service('stop') stopped_preserved = sentinel.exists() finally: service('start') try: current = None for _ in range(30): try: current = status() if current.get('mobile', {}).get('available'): break except Exception: pass time.sleep(1) result = dict(runtime_preserved_on_stop=stopped_preserved, runtime_preserved_after_start=sentinel.exists(), persistent_configuration_unchanged=before == fingerprints(), mobile_available=bool(current and current.get('mobile', {}).get('available')), no_phone_session=bool(current and not current.get('mobile', {}).get('connected'))) print(json.dumps(result)) assert all(result.values()), 'Lifecycle checks failed' finally: sentinel.unlink(missing_ok=True) ''' def main(): parser = argparse.ArgumentParser() parser.add_argument('--restart-service', action='store_true', required=True) parser.parse_args() client, fields = connect() try: code, output, _ = execute(client, 'python3 -c ' + shlex.quote(REMOTE), password=fields['密码'], timeout=150) # Only a small explicit boolean schema is allowed out of this test. lines = [line for line in output.splitlines() if line.startswith('{')] if lines: result = json.loads(lines[-1]) if any(type(value) is not bool for value in result.values()): raise RuntimeError('Unexpected lifecycle output') print(json.dumps(result, ensure_ascii=False)) if code or not lines: raise RuntimeError('Real lifecycle verification failed; raw output withheld') finally: client.close() if __name__ == '__main__': main()