summaryrefslogtreecommitdiff
path: root/test/daemon.py
diff options
context:
space:
mode:
Diffstat (limited to 'test/daemon.py')
-rw-r--r--test/daemon.py79
1 files changed, 79 insertions, 0 deletions
diff --git a/test/daemon.py b/test/daemon.py
index fb4f3013..d9ca7153 100644
--- a/test/daemon.py
+++ b/test/daemon.py
@@ -452,6 +452,82 @@ def clients(binary):
shutil.rmtree(root, ignore_errors=True)
+def manage(binary):
+ """`pardes --ps`, `--kill=NAME` and `--kill-all`: two detached sessions
+ and their shells listed, one session killed outright and its shell
+ listed as no one's, the other ended by name with its shell, a name no
+ session has refused, the kept shell ended by --kill-all, and the daemon
+ gone after it."""
+ scratch = Path(__file__).resolve().parent.parent.parent / '.scratch' / 'daemon' / 'dmn'
+ scratch.mkdir(parents=True, exist_ok=True)
+ root = Path(tempfile.mkdtemp(prefix='rm-', 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=shutil.which('bash') or '/bin/bash', PARDES_NOTIME='1')
+ (root / 'config' / 'pardes').mkdir(parents=True)
+ (root / 'config' / 'pardes' / 'init').write_text('Shell bash\nMessageLinger 0\n')
+
+ def cli(*args):
+ return subprocess.run([binary, *args], env=env, cwd=root, capture_output=True, timeout=30)
+
+ started = []
+ try:
+ sessions, shells = {}, {}
+ for name in ('one', 'two'):
+ (root / f'{name}.txt').write_text(f'{name}\n')
+ started.append(subprocess.Popen([binary, f'--detach={name}', str(root / f'{name}.txt')], env=env, cwd=root,
+ stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL,
+ stderr=subprocess.DEVNULL, start_new_session=True).pid)
+ sock = root / f'pardes-9p-{name}.sock'
+ wait_for(f'session {name}\'s 9P socket', sock.exists)
+ with Client(sock) as c:
+ status = dict(line.split(maxsplit=1) for line in c.read('/status').decode().splitlines())
+ sessions[name] = int(status['pid'])
+ started.append(sessions[name])
+ c.write('/pane/1/exec', b'Tty\n')
+ wait_for(f'session {name}\'s shell', lambda: terminals(c))
+ shell = terminals(c)[0]
+ wait_for(f'session {name}\'s shell answers', lambda: run(c, shell, b'true\n') == b'exit 0\n')
+ shells[name] = int(run(c, shell, b'echo $$\n').split(b'\n')[1])
+ started.append(shells[name])
+ daemon = [p for p in processes(root) if cmdline(p)[1:2] == [b'--daemon']][0]
+ started.append(daemon)
+ out = cli('--ps')
+ assert out.returncode == 0, out
+ listed = out.stdout.decode()
+ assert f'pardes daemon {daemon}' in listed, listed
+ for name in ('one', 'two'):
+ assert f'session {name} {sessions[name]} (tty, detached)' in listed, listed
+ assert f'shell {shells[name]}' in listed, listed
+ os.kill(sessions['two'], signal.SIGKILL)
+ wait_for('session two gone', lambda: not alive(sessions['two']))
+ wait_for('its shell no session\'s', lambda: 'no session holds' in cli('--ps').stdout.decode())
+ listed = cli('--ps').stdout.decode()
+ assert 'session two' not in listed and listed.index(f'shell {shells["two"]}') > listed.index('no session holds'), listed
+ assert alive(shells['two'])
+ print('manage: --ps listed two sessions and their shells, then a killed one\'s shell as no one\'s')
+ refused = cli('--kill=nobody')
+ assert refused.returncode == 1 and b"no session called 'nobody'" in refused.stderr, refused
+ killed = cli('--kill=one')
+ assert killed.returncode == 0, killed
+ assert not alive(sessions['one'])
+ wait_for('session one\'s shell hung up with it', lambda: not alive(shells['one']))
+ assert alive(shells['two']) and 'session one' not in cli('--ps').stdout.decode()
+ assert cli('--kill-all').returncode == 0
+ wait_for('the kept shell ended', lambda: not alive(shells['two']))
+ wait_for('the daemon gone once it keeps nothing', lambda: not alive(daemon))
+ none = cli('--ps')
+ assert none.returncode == 1 and b'no pardes daemon is running' in none.stderr, none
+ print('manage: --kill=one ended it and its shell, --kill-all the kept shell, a name nobody has refused; the daemon left')
+ finally:
+ for pid in started + processes(root):
+ try:
+ os.kill(pid, signal.SIGKILL)
+ except (ProcessLookupError, PermissionError):
+ pass
+ shutil.rmtree(root, ignore_errors=True)
+
+
def joins(path):
"""A replica client's joins (PARDES_REPLICA_STATS, a line a link): how
many blobs each listed, how many it held already from the core it left
@@ -660,6 +736,7 @@ def main(binary, cycles):
replicas(root, binary, session, daemon)
clients(binary)
far(binary)
+ manage(binary)
shell = terminals(client)[0]
shell_pid = int(run(client, shell, b'echo $$\n').split(b'\n')[1])
started.append(shell_pid)
@@ -1260,6 +1337,8 @@ if __name__ == '__main__':
upgrades(Path(sys.argv[2]).resolve(), Path(sys.argv[3]).resolve())
elif sys.argv[1:2] == ['--clients']:
clients(sys.argv[2])
+ elif sys.argv[1:2] == ['--manage']:
+ manage(sys.argv[2])
elif sys.argv[1:2] == ['--far']:
far(sys.argv[2])
elif sys.argv[1:2] == ['--ping']: