diff options
Diffstat (limited to 'test')
| -rw-r--r-- | test/daemon.py | 322 | ||||
| -rw-r--r-- | test/e2e_harness.zig | 4 | ||||
| -rw-r--r-- | test/fs.py | 40 |
3 files changed, 357 insertions, 9 deletions
diff --git a/test/daemon.py b/test/daemon.py new file mode 100644 index 00000000..bf3d237c --- /dev/null +++ b/test/daemon.py @@ -0,0 +1,322 @@ +#!/usr/bin/env python3 +"""The per-user daemon end to end (docs/next-daemon.md, phase 3), under a +private HOME and runtime directory: + +- a terminal's session detached (the stub ends, exit 0) and attached again + from another terminal, N times; +- a session killed outright: its shells live on, held by the daemon, and + another session adopts them as terminal panes; +- nothing left behind: no session, shell, daemon or socket. + + python3 -B test/daemon.py <pardes> [cycles] + python3 -B test/daemon.py --gui <pardes-gui> [cycles] (a hidden window; + needs a Wayland display, and a pardes beside it for the refusal) +""" +import fcntl +import os +import shutil +import signal +import struct +import subprocess +import sys +import tempfile +import termios +import threading +import time +from pathlib import Path + +sys.path.insert(0, str(Path(__file__).parent)) +from ninep import Client # noqa: E402 + + +def wait_for(what, check, timeout=10): + deadline = time.monotonic() + timeout + while True: + got = check() + if got: + return got + if time.monotonic() > deadline: + raise TimeoutError(what) + time.sleep(.01) + + +class Term: + """A terminal: a pty whose other end is read and kept, its frame + requests answered (CSI 5 n), as a terminal emulator does.""" + + def __init__(self, rows=24, cols=80): + self.master, self.slave = os.openpty() + fcntl.ioctl(self.slave, termios.TIOCSWINSZ, struct.pack('HHHH', rows, cols, 0, 0)) + self.out = bytearray() + self.lock = threading.Lock() + self.reader = threading.Thread(target=self.drain, daemon=True) + self.reader.start() + + def drain(self): + while True: + try: + got = os.read(self.master, 65536) + except OSError: + return + if not got: + return + with self.lock: + self.out += got + if b'\x1b[5n' in got: + try: + os.write(self.master, b'\x1b[0n') + except OSError: + return + + def spawn(self, argv, env): + slave = self.slave + return subprocess.Popen(argv, env=env, stdin=slave, stdout=slave, stderr=slave, + start_new_session=True, + preexec_fn=lambda: fcntl.ioctl(0, termios.TIOCSCTTY, 0)) + + def text(self): + with self.lock: + return bytes(self.out) + + def close(self): + os.close(self.slave) + os.close(self.master) + self.reader.join(timeout=2) + + +def run(client, serial, line): + """pty/run: a line in, `exit N` and its output out.""" + fid = client.open(f'/pane/{serial}/pty/run', 2) + try: + client.rpc(118, struct.pack('<IQI', fid, 0, len(line)) + line) + return client.read_fid(fid) + finally: + client.close(fid) + + +def processes(root): + """The pids whose environment is this run's (its runtime directory).""" + found = [] + mark = f'XDG_RUNTIME_DIR={root}'.encode() + for entry in os.listdir('/proc'): + if not entry.isdigit(): + continue + try: + environ = open(f'/proc/{entry}/environ', 'rb').read().split(b'\0') + except OSError: + continue + if mark in environ: + found.append(int(entry)) + return found + + +def cmdline(pid): + try: + return open(f'/proc/{pid}/cmdline', 'rb').read().split(b'\0') + except OSError: + return [] + + +def alive(pid): + try: + os.kill(pid, 0) + except ProcessLookupError: + return False + try: + state = open(f'/proc/{pid}/stat').read().rsplit(')', 1)[1].split()[0] + except OSError: + return False + return state != 'Z' + + +def fds(pid): + try: + return len(os.listdir(f'/proc/{pid}/fd')) + except OSError: + return -1 + + +def terminals(client): + return [int(row.split()[0]) for row in client.read('/index').splitlines() if row.split()[1] == b'term'] + + +def main(binary, cycles): + scratch = Path(__file__).resolve().parent.parent.parent / '.scratch' / 'daemon' / 'dmn' + scratch.mkdir(parents=True, exist_ok=True) + root = Path(tempfile.mkdtemp(prefix='rt-', dir=scratch)) + env = {k: v for k, v in os.environ.items() if not k.startswith('PARDES_') and 'PAGER' not in k} + env.update(HOME=str(root), XDG_RUNTIME_DIR=str(root), XDG_CONFIG_HOME=str(root / 'config'), + TERM='xterm-256color', SHELL='/bin/bash', PARDES_NOTIME='1') + (root / 'config' / 'pardes').mkdir(parents=True) + (root / 'config' / 'pardes' / 'init').write_text('Shell bash\nMessageLinger 0\n') + daemon_sock = root / 'pardes-daemon.sock' + started = [] + try: + term = Term() + stub = term.spawn([binary, '--tty', '--9p=work'], env) + started.append(stub.pid) + sock = root / 'pardes-9p-work.sock' + wait_for('the session\'s 9P socket', sock.exists) + client = Client(sock) + status = dict(line.split(maxsplit=1) for line in client.read('/status').decode().splitlines()) + session = int(status['pid']) + started.append(session) + assert session != stub.pid, 'the session is forked off its stub' + assert daemon_sock.exists(), list(root.iterdir()) + daemons = [p for p in processes(root) if cmdline(p)[1:2] == [b'--daemon']] + assert len(daemons) == 1, [cmdline(p) for p in processes(root)] + daemon = daemons[0] + started.append(daemon) + shell = terminals(client)[0] + shell_pid = int(run(client, shell, b'echo $$\n').split(b'\n')[1]) + started.append(shell_pid) + baseline = None + for cycle in range(cycles): + client.write(f'/pane/{shell}/exec', b'Detach\n') + assert stub.wait(timeout=10) == 0, stub.returncode + said = term.text() + assert b'detached from \'work\'' in said, said[-400:] + assert b'\x1b[?1049l' in said, 'the terminal is put back' + term.close() + assert alive(session) and alive(shell_pid) + assert run(client, shell, b'echo still\n').startswith(b'exit 0\nstill'), 'the shell runs on, detached' + term = Term(30, 100) + stub = term.spawn([binary, '--attach=work'], env) + wait_for('the attached terminal\'s first frame', lambda: b'\x1b[?1049h' in term.text()) + wait_for('the session sized to its new terminal', + lambda: client.screen()['cols'] == 100 and client.screen()['rows'] == 30) + now = (fds(session), fds(daemon), len(list(root.iterdir())), len(processes(root))) + if baseline is None: + baseline = now + assert now == baseline, (cycle, now, baseline) + print(f'daemon: {cycles} detach/attach cycles, fds/sockets/processes level: {baseline}') + + # Killed outright: its shells live on, their masters the daemon's. + os.kill(session, signal.SIGKILL) + stub.wait(timeout=10) + client.socket.close() + term.close() + wait_for('the killed session gone', lambda: not alive(session)) + time.sleep(.3) + assert alive(shell_pid), 'a killed session\'s shell is not hung up' + assert alive(daemon) + term = Term() + stub = term.spawn([binary, '--tty', '--9p=fresh'], env) + started.append(stub.pid) + sock = root / 'pardes-9p-fresh.sock' + wait_for('the second session', sock.exists) + client = Client(sock) + status = dict(line.split(maxsplit=1) for line in client.read('/status').decode().splitlines()) + session = int(status['pid']) + started.append(session) + before = set(terminals(client)) + client.write(f'/pane/{min(before)}/exec', b'Adopt\n') + adopted = wait_for('the adopted pane', lambda: set(terminals(client)) - before) + pane = adopted.pop() + said = run(client, pane, b'echo $$\n') + assert said.split(b'\n')[1] == str(shell_pid).encode(), said + print(f'daemon: shell {shell_pid} outlived kill -9 of its session and was adopted') + + # The end: the session quits, its shells hang up, the daemon goes. + client.write(f'/pane/{pane}/exec', b'Exit\n') + stub.wait(timeout=10) + client.socket.close() + term.close() + wait_for('the adopted shell hung up', lambda: not alive(shell_pid)) + wait_for('the daemon gone once idle', lambda: not alive(daemon), timeout=5) + left = [p for p in processes(root) if alive(p)] + assert not left, [cmdline(p) for p in left] + sockets = [p.name for p in root.iterdir() if p.is_socket()] + assert not sockets, sockets + print('daemon: nothing left behind') + finally: + for pid in started + processes(root): + try: + os.kill(pid, signal.SIGKILL) + except OSError: + pass + shutil.rmtree(root, ignore_errors=True) + + +def sized(client): + """Whether the session takes a `size`: only with no window or terminal.""" + try: + client.write('/ctl', b'size 100 30\n') + return True + except OSError: + return False + + +def gui(binary, cycles): + """A window's Detach closes it and keeps the process; `pardes-gui + --attach` opens it again. Test mode: a hidden window, its stdin the + test feed.""" + scratch = Path(__file__).resolve().parent.parent.parent / '.scratch' / 'daemon' / 'dmn' + root = Path(tempfile.mkdtemp(prefix='rtg-', dir=scratch)) + env = {k: v for k, v in os.environ.items() if not k.startswith('PARDES_') and 'PAGER' not in k} + display = Path(os.environ.get('WAYLAND_DISPLAY', 'wayland-0')) + if not display.is_absolute(): + display = Path(os.environ.get('XDG_RUNTIME_DIR', f'/run/user/{os.getuid()}')) / display + (root / 'caps').mkdir() + env.update(HOME=str(root), XDG_RUNTIME_DIR=str(root), XDG_CONFIG_HOME=str(root / 'config'), + SHELL='/bin/bash', PARDES_NOTIME='1', PARDES_TEST='1', PARDES_TEST_CAPTURE_DIR=str(root / 'caps'), + SDL_VIDEODRIVER='wayland', WAYLAND_DISPLAY=str(display), PARDES_DAEMON='on') + (root / 'config' / 'pardes').mkdir(parents=True) + (root / 'config' / 'pardes' / 'init').write_text('Shell bash\nMessageLinger 0\n') + feed_r, feed_w = os.pipe() + proc = subprocess.Popen([binary, '--9p=win'], env=env, cwd=root, stdin=feed_r, + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, start_new_session=True) + os.close(feed_r) + try: + sock = root / 'pardes-9p-win.sock' + wait_for('the window session', sock.exists, timeout=20) + client = Client(sock) + shell = terminals(client)[0] + assert not sized(client), 'a window owns the size' + baseline = None + for cycle in range(cycles): + client.write(f'/pane/{shell}/exec', b'Detach\n') + wait_for('the window closed', lambda: sized(client)) + assert proc.poll() is None, 'the process carries on with no window' + attach = subprocess.run([binary, '--attach=win'], env=env, cwd=root, timeout=10, + stdout=subprocess.PIPE, stderr=subprocess.PIPE) + assert attach.returncode == 0, attach.stderr + wait_for('the window open again', lambda: not sized(client)) + now = (fds(proc.pid), len(processes(root))) + baseline = baseline or now + assert now == baseline, (cycle, now, baseline) + print(f'daemon: {cycles} window detach/attach cycles, fds/processes level: {baseline}') + # Another kind is refused in words, and so is a name nobody has. + client.write(f'/pane/{shell}/exec', b'Detach\n') + wait_for('the window closed', lambda: sized(client)) + term = Term() + tty_attach = term.spawn([binary.replace('pardes-gui', 'pardes'), '--attach=win'], env) + assert tty_attach.wait(timeout=10) == 1 + said = term.text() + term.close() + assert b'is a window' in said, said + nobody = subprocess.run([binary, '--attach=nobody'], env=env, cwd=root, timeout=10, + stdout=subprocess.PIPE, stderr=subprocess.PIPE) + assert nobody.returncode == 1 and b"no session called 'nobody'" in nobody.stderr, nobody.stderr + # Detached and told to end: it ends. + client.write(f'/pane/{shell}/exec', b'Exit\n') + proc.wait(timeout=10) + client.socket.close() + wait_for('the daemon gone once idle', lambda: not [p for p in processes(root) if alive(p)], timeout=5) + print('daemon: window session ended, nothing left behind') + finally: + os.close(feed_w) + for pid in [proc.pid] + processes(root): + try: + os.kill(pid, signal.SIGKILL) + except OSError: + pass + shutil.rmtree(root, ignore_errors=True) + + +if __name__ == '__main__': + if '--gui' in sys.argv: + args = [a for a in sys.argv[1:] if a != '--gui'] + gui(args[0], int(args[1]) if len(args) > 1 else 50) + else: + main(sys.argv[1], int(sys.argv[2]) if len(sys.argv) > 2 else 50) diff --git a/test/e2e_harness.zig b/test/e2e_harness.zig index a2010c19..7e29476b 100644 --- a/test/e2e_harness.zig +++ b/test/e2e_harness.zig @@ -15,6 +15,7 @@ extern "c" fn forkpty( extern "c" fn execvp(file: [*:0]const u8, argv: [*:null]const ?[*:0]const u8) c_int; extern "c" fn _exit(status: c_int) noreturn; extern "c" fn unsetenv(name: [*:0]const u8) c_int; +extern "c" fn setenv(name: [*:0]const u8, value: [*:0]const u8, overwrite: c_int) c_int; /// Monotonic milliseconds; the harness's only clock (std.time lost /// milliTimestamp in 0.16). @@ -96,6 +97,9 @@ pub const Harness = struct { if (pid == 0) { for ([_][*:0]const u8{ "PARDES_PID", "PARDES_9P", "PARDES_PANE" }) |name| if (unsetenv(name) != 0) _exit(126); + // The app is the process this kills and the terminal it hangs + // up (deinit): no daemon to fork it off and keep it (daemon.zig). + if (setenv("PARDES_DAEMON", "off", 1) != 0) _exit(126); _ = execvp(exe, argv.ptr); _exit(127); } @@ -47,6 +47,7 @@ def session(binary, root, name, *options, socket_name=None, file=None, tty=False preexec = lambda: fcntl.ioctl(0, termios.TIOCSCTTY, 0) if launch is not None: args = [binary, *launch] + existing = set(root.glob('pardes-9p-*.sock')) child = subprocess.Popen(args, cwd=root, env=env, stdin=slave, stdout=slave if tty else subprocess.DEVNULL, stderr=subprocess.PIPE, start_new_session=True, @@ -55,7 +56,9 @@ def session(binary, root, name, *options, socket_name=None, file=None, tty=False # Borrowed handles for input-driving probes, valid inside this context. terminal.update(pid=child.pid, input_fd=master) if launch is not None and socket_name is None: - address = root / f'pardes-9p-{child.pid}.sock' + # Named by the session's pid: a terminal's session is forked + # off the process launched, which stays as its stub. + address = None if slave is not None: os.close(slave) slave = None @@ -68,7 +71,11 @@ def session(binary, root, name, *options, socket_name=None, file=None, tty=False reader = threading.Thread(target=drain, daemon=True) reader.start() deadline = time.monotonic() + 10 - while not address.exists(): + while address is None or not address.exists(): + if address is None: + address = next(iter(set(root.glob('pardes-9p-*.sock')) - existing), None) + if address is not None: + continue if child.poll() is not None: raise AssertionError((args, child.returncode, child.stderr.read().decode())) if time.monotonic() > deadline: @@ -984,19 +991,24 @@ def python_repl_runs_one_line(binary): def signal_cleans_up(binary): - """A detached session told to stop by a signal unlinks its sockets.""" + """A detached session told to stop by a signal unlinks its sockets. A + hangup is a terminal's end, not a session's: the daemon keeps it.""" for sig in [signal.SIGTERM, signal.SIGINT, signal.SIGHUP]: with tempfile.TemporaryDirectory(prefix='pardes-sig-') as directory: root = Path(directory) seen = {} with session(binary, root, 'sig', terminal=seen) as (client, address): - detached = root / 'pardes-detached-sig.sock' - assert address.exists() and detached.exists() + assert address.exists() + if sig == signal.SIGHUP: + os.kill(seen['pid'], sig) + time.sleep(.3) + assert address.exists() and client.read('/index'), 'a hangup ended a kept session' + sig = signal.SIGTERM os.kill(seen['pid'], sig) deadline = time.monotonic() + 5 - while (address.exists() or detached.exists()) and time.monotonic() < deadline: + while address.exists() and time.monotonic() < deadline: time.sleep(.01) - assert not address.exists() and not detached.exists(), (sig, list(root.iterdir())) + assert not address.exists(), (sig, list(root.iterdir())) def odd_paths(binary): @@ -1885,6 +1897,7 @@ def test(binary, quic=False): cooked = termios.tcgetattr(slave) env = {k: v for k, v in os.environ.items() if not k.startswith('PARDES_')} env.update(HOME=str(root), XDG_RUNTIME_DIR=str(root), TERM='xterm-256color', PARDES_NOTIME='1') + existing = set(root.glob('pardes-9p-*.sock')) killed = subprocess.Popen([binary, '--tty', 'x.txt'], cwd=root, env=env, stdin=slave, stdout=slave, stderr=subprocess.DEVNULL, start_new_session=True, @@ -1897,12 +1910,21 @@ def test(binary, quic=False): if select.select([master], [], [], .1)[0]: seen += os.read(master, 65536) time.sleep(.3) - own_socket = root / f'pardes-9p-{killed.pid}.sock' - assert own_socket.exists() + # The session's own, named by its pid: forked off the + # launched process, which stays as its stub. + own_socket, = set(root.glob('pardes-9p-*.sock')) - existing assert not dead_socket.exists() assert live_socket.exists() killed.send_signal(sig) assert killed.wait(timeout=5) == -sig + if sig == signal.SIGHUP: + # A hangup is the terminal's end: the session carries on + # detached (the daemon keeps it) until told to end. + assert own_socket.exists(), own_socket + os.kill(int(own_socket.name[len('pardes-9p-'):-len('.sock')]), signal.SIGTERM) + deadline = time.monotonic() + 5 + while own_socket.exists() and time.monotonic() < deadline: + time.sleep(.01) assert not own_socket.exists(), own_socket while select.select([master], [], [], .2)[0]: try: |
