Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.

9 downloads

dbbasic-object-server/tests/sandbox_signals.py
Install on DigitalOcean Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-object-server.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-object-server.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-object-server.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

Daniel Quellhorst Changelog for this batch. 65ef78d 3 hours, 19 minutes ago History
sandbox_signals.py · 1.3 KB · 37 lines
1"""Signal a user-code worker from a test, whoever it runs as.
2
3Where there is no root launcher (macOS, CI) the worker is this process's own
4user and ``os.kill`` is enough. Under the real setup the worker is the
5sandbox user's and ``os.kill`` is refused; the Linux lane
6(scripts/test_linux_sandbox.sh) then offers a root helper on the socket
7named by DBBASIC_TEST_SIGNAL_SOCKET (tests/linux_sandbox/signal_helper.py).
8Without it the test skips, as it did before the lane existed.
9"""
10from __future__ import annotations
11
12import json
13import os
14import signal
15import socket
16
17import pytest
18
19SOCKET_ENV = "DBBASIC_TEST_SIGNAL_SOCKET"
20
21
22def 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}"