askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
sandbox_signals.py · 1.3 KB · 37 lines
| 1 | """Signal a user-code worker from a test, whoever it runs as. |
| 2 | |
| 3 | Where there is no root launcher (macOS, CI) the worker is this process's own |
| 4 | user and ``os.kill`` is enough. Under the real setup the worker is the |
| 5 | sandbox user's and ``os.kill`` is refused; the Linux lane |
| 6 | (scripts/test_linux_sandbox.sh) then offers a root helper on the socket |
| 7 | named by DBBASIC_TEST_SIGNAL_SOCKET (tests/linux_sandbox/signal_helper.py). |
| 8 | Without it the test skips, as it did before the lane existed. |
| 9 | """ |
| 10 | from __future__ import annotations |
| 11 | |
| 12 | import json |
| 13 | import os |
| 14 | import signal |
| 15 | import socket |
| 16 | |
| 17 | import pytest |
| 18 | |
| 19 | SOCKET_ENV = "DBBASIC_TEST_SIGNAL_SOCKET" |
| 20 | |
| 21 | |
| 22 | def signal_worker(pid: int, sig: int = signal.SIGKILL) -> None: |
| 23 | try: |
| 24 | os.kill(pid, sig) |
| 25 | return |
| 26 | except PermissionError: |
| 27 | pass |
| 28 | path = os.environ.get(SOCKET_ENV) |
| 29 | if not path: |
| 30 | pytest.skip("the worker runs as the sandbox user and this process cannot signal it; " |
| 31 | "scripts/test_linux_sandbox.sh runs this test with a root helper") |
| 32 | with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as conn: |
| 33 | conn.settimeout(5) |
| 34 | conn.connect(path) |
| 35 | conn.sendall(json.dumps({"pid": pid, "signal": signal.Signals(sig).name}).encode()) |
| 36 | reply = conn.recv(256).decode() |
| 37 | assert reply == "ok", f"the signal helper answered {reply!r} for pid {pid}" |