"""Exercise startup/shutdown using fake programs; no Docker or EDA required.""" import os from pathlib import Path import signal import subprocess import sys import tempfile import time import unittest ENTRYPOINT = Path(__file__).resolve().parents[1] / 'docker-entrypoint.sh' REPOSITORY = ENTRYPOINT.parents[1] class ContainerStartup(unittest.TestCase): def setUp(self): self.temporary = tempfile.TemporaryDirectory() self.addCleanup(self.temporary.cleanup) self.root = Path(self.temporary.name) self.bin = self.root / 'bin' self.bin.mkdir() resources = self.root / 'data/resources/references' resources.mkdir(parents=True) (resources / '_index.md').write_text('Test reference index') self.jar = self.root / 'router.jar' self.jar.touch() self.env = { 'PATH': str(self.bin) + os.pathsep + os.defpath, 'HOME': str(self.root / 'home'), 'PYTHONPATH': str(REPOSITORY), 'PCB_SESSION_ROOT': str(self.root / 'data'), 'PCB_CODEX_HOME': str(self.root / 'codex-home'), 'PCB_CODEX_AUTH': 'apikey', 'OPENAI_API_KEY': 'unit-test-api-credential', 'PCB_FREEROUTING_JAR': str(self.jar), 'PCB_CODEX_BIN': str(self.bin / 'codex'), 'PCB_EDA_BIN': str(self.bin / 'lceda-pro'), 'PCB_BLENDER_BIN': str(self.bin / 'blender'), 'DISPLAY': ':99', 'XDG_RUNTIME_DIR': str(self.root / 'xdg'), 'TEST_ROOT': str(self.root), 'APP_EXIT_CODE': '7', } for name in ('codex', 'lceda-pro', 'blender'): self.program(name, 'pass\n') self.program('python', '''import os,sys,time from pathlib import Path if sys.argv[1:2] == ['-c']: os.execv(sys.executable, [sys.executable, *sys.argv[1:]]) Path(os.environ['TEST_ROOT'],'app.pid').write_text(str(os.getpid())) if os.environ.get('APP_WAIT'): while True:time.sleep(1) sys.exit(int(os.environ['APP_EXIT_CODE'])) ''') self.program('Xvfb', '''import os,time from pathlib import Path Path(os.environ['TEST_ROOT'],'xvfb.pid').write_text(str(os.getpid())) while True:time.sleep(1) ''') self.program('xdpyinfo', '''import os,sys from pathlib import Path sys.exit(0 if Path(os.environ['TEST_ROOT'],'xvfb.pid').exists() else 1) ''') def program(self, name, body): path = self.bin / name path.write_text(f'#!{sys.executable}\n' + body) path.chmod(0o755) def command(self): return ['/bin/sh', str(ENTRYPOINT), 'python', '-m', 'uvicorn', 'codex_pcb.app:app'] def assert_process_gone(self, filename): pid = int((self.root / filename).read_text()) with self.assertRaises(ProcessLookupError): os.kill(pid, 0) def test_diagnostic_command_bypasses_runtime_requirements(self): result = subprocess.run(['/bin/sh', str(ENTRYPOINT), '/bin/echo', 'diagnostic'], env={}, capture_output=True, text=True, timeout=5) self.assertEqual(result.returncode, 0) self.assertEqual(result.stdout.strip(), 'diagnostic') def test_missing_eda_fails_before_display_or_app_start(self): (self.bin / 'lceda-pro').unlink() result = subprocess.run(self.command(), env=self.env, cwd=REPOSITORY, capture_output=True, text=True, timeout=5) self.assertEqual(result.returncode, 1) self.assertIn('Mount the complete Linux EDA directory', result.stderr) self.assertNotIn('unit-test-api-credential', result.stderr) self.assertFalse((self.root / 'xvfb.pid').exists()) self.assertFalse((self.root / 'app.pid').exists()) def test_application_exit_preserves_status_and_stops_display(self): result = subprocess.run(self.command(), env=self.env, cwd=REPOSITORY, capture_output=True, text=True, timeout=8) self.assertEqual(result.returncode, 7, result.stderr) self.assert_process_gone('xvfb.pid') self.assert_process_gone('app.pid') self.assertEqual((self.root / 'xdg').stat().st_mode & 0o777, 0o700) def test_shutdown_signal_stops_app_and_display(self): self.env['APP_WAIT'] = '1' process = subprocess.Popen(self.command(), env=self.env, cwd=REPOSITORY, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) try: deadline = time.monotonic() + 5 while not (self.root / 'app.pid').exists() and time.monotonic() < deadline: time.sleep(0.02) self.assertTrue((self.root / 'app.pid').exists()) process.send_signal(signal.SIGTERM) stdout, stderr = process.communicate(timeout=5) self.assertEqual(process.returncode, 143, stderr) self.assert_process_gone('xvfb.pid') self.assert_process_gone('app.pid') finally: if process.poll() is None: process.kill() process.communicate(timeout=5) for filename in ('app.pid', 'xvfb.pid'): if (self.root / filename).exists(): try: os.kill(int((self.root / filename).read_text()), signal.SIGTERM) except ProcessLookupError: pass if __name__ == '__main__': unittest.main()