"""Hardware-free generic dispatcher exercise with a second, tiny executable backend.""" import hashlib import json import os import signal import time from pathlib import Path import struct import subprocess import sys import tempfile import unittest ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "python")) from frameyap.model_files import load_backends FAKE = '''#!/usr/bin/env python3 import os, struct, sys read = sys.stdin.buffer.read write = sys.stdout.buffer.write assert len(sys.argv) in (3, 4), 'generic backend received unsupported flags' if os.environ.get('FRAMEYAP_ADVANCED_DEBUG') != '0': raise AssertionError('generic launcher must not receive Redux debug opt-in') if os.environ.get('FAKE_PID_PATH'): import signal signal.signal(signal.SIGTERM, signal.SIG_IGN) open(os.environ['FAKE_PID_PATH'], 'w').write(str(os.getpid())) write(struct.pack(' ' + str(marker) + '\n') install = base + ['--install', '--backend', 'fake', '--installer', str(installer), '--expected-manifest-sha256', old] manifest['model']['source'] = 'https://example.org/changed' source.write_text(json.dumps(manifest)) wrong = subprocess.run(install, capture_output=True, text=True) self.assertNotEqual(wrong.returncode, 0) self.assertFalse(marker.exists(), wrong.stdout) self.assertNotIn('DONE', wrong.stdout) self.assertEqual(json.loads(wrong.stdout), { 'ok': False, 'code': 'manifest_mismatch', 'message': 'selected backend manifest changed since consent'}) missing = subprocess.run(install[:-1] + [hashlib.sha256(source.read_bytes()).hexdigest(), '--installer', str(root / 'absent.sh')], capture_output=True, text=True) self.assertNotEqual(missing.returncode, 0) self.assertEqual(json.loads(missing.stdout)['message'], 'installer missing or unsafe') self.assertFalse(marker.exists()) # Reusing a backend ID cannot silently re-authorize new sources. new = hashlib.sha256(source.read_bytes()).hexdigest() self.assertNotEqual(new, old) self.assertEqual(subprocess.run(install[:-1] + [new], capture_output=True).returncode, 0) self.assertTrue(marker.exists()) passed = marker.read_text().splitlines() self.assertEqual(passed[passed.index('--expected-manifest-sha256') + 1], new) def test_dispatcher_reaps_child_ignoring_term_on_eof_and_native_style_stop(self): with tempfile.TemporaryDirectory() as temp: root = Path(temp) (root / 'assets/backends').mkdir(parents=True) (root / 'bin').mkdir() model = root / 'model' model.mkdir() (model / 'weights.dat').write_bytes(b'fixture') clip = root / 'clip' clip.mkdir(mode=0o700) (clip / 'clip.raw').write_bytes(b'\0' * 12800) fake = root / 'bin/fake' fake.write_text(FAKE) fake.chmod(0o700) manifest = {'schema': 1, 'id': 'fake', 'display_name': 'Fake', 'launcher': {'type': 'executable', 'path': 'bin/fake', 'arguments': ['{model_dir}', '{clip_dir}'], 'protocol': 'frameyap-worker-v1'}, 'model': {'source': 'https://example.org/fake', 'revision': 'test', 'files': [{'path': 'weights.dat', 'size': 7, 'sha256': hashlib.sha256(b'fixture').hexdigest()}]}, 'attribution': 'Fixture', 'license': {'id': 'MIT', 'text': 'Fixture'}, 'requirements': {'cpu': 'None', 'gpu': 'None'}} (root / 'assets/backends/fake.json').write_text(json.dumps(manifest)) args = [sys.executable, str(ROOT / 'python/frameyap/backend_worker.py'), '--backend', 'fake', '--root', str(root), '--python', sys.executable, '--manifest-dir', str(root / 'assets/backends'), '--model', str(model), '--clip-dir', str(clip), '--threads', '2'] for mode in ('eof', 'term'): pidfile = root / ('pid-' + mode) with subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, start_new_session=True, env={**os.environ, 'FAKE_PID_PATH': str(pidfile)}) as child: self.assertEqual(read_frame(child.stdout), b'Y') pid = int(pidfile.read_text()) if mode == 'eof': child.stdin.close() else: child.stdin.write(frame(b'T' + (1).to_bytes(8, 'little'))) child.stdin.flush() child.terminate() try: code = child.wait(timeout=3) self.assertIn(code, (0, 1), child.stderr.read()) for _ in range(100): if not Path('/proc/' + str(pid)).exists(): break time.sleep(0.01) self.assertFalse(Path('/proc/' + str(pid)).exists(), 'owned model still running') finally: # Same dedicated group as native. Kill only this test's processes. try: os.killpg(child.pid, signal.SIGKILL) except ProcessLookupError: pass if __name__ == '__main__': unittest.main()