aboutsummaryrefslogtreecommitdiffstats
path: root/packages/meshbay-node/tests/test_cli_dispatch.py
diff options
context:
space:
mode:
Diffstat (limited to 'packages/meshbay-node/tests/test_cli_dispatch.py')
-rw-r--r--packages/meshbay-node/tests/test_cli_dispatch.py60
1 files changed, 49 insertions, 11 deletions
diff --git a/packages/meshbay-node/tests/test_cli_dispatch.py b/packages/meshbay-node/tests/test_cli_dispatch.py
index fdcd82e..d9edb17 100644
--- a/packages/meshbay-node/tests/test_cli_dispatch.py
+++ b/packages/meshbay-node/tests/test_cli_dispatch.py
@@ -77,19 +77,21 @@ def stub_daemon(monkeypatch, tmp_path):
import getpass
monkeypatch.setattr(getpass, "getpass", lambda *a, **kw: "test-password")
- # `reload` looks for a real daemon and signals it. Without this the test
- # SIGHUPs whatever node happens to be running on the machine — which it did,
- # once, before this was added. A test must not reach outside itself.
- import os
+ # `reload` and `restart-daemon` shell out to `systemctl --user`. Without
+ # this the test would run that against whatever session bus is actually
+ # available — a test must not reach outside itself, which is exactly what
+ # the pgrep/os.kill version of this fixture existed to prevent before
+ # those commands were rewritten to delegate to systemd.
import subprocess
- signalled: list[int] = []
- monkeypatch.setattr(
- subprocess, "run",
- lambda *a, **k: subprocess.CompletedProcess(a[0], 0, stdout="4242\n",
- stderr=""))
- monkeypatch.setattr(os, "kill", lambda pid, sig: signalled.append(pid))
- calls.append(("_signalled", signalled))
+ systemctl_calls: list[list[str]] = []
+
+ def fake_run(argv, **kw):
+ systemctl_calls.append(argv)
+ return subprocess.CompletedProcess(argv, 0, stdout="", stderr="")
+
+ monkeypatch.setattr(subprocess, "run", fake_run)
+ calls.append(("_systemctl_calls", systemctl_calls))
return calls
@@ -130,3 +132,39 @@ def test_the_verb_list_here_matches_the_parser():
assert not untested, (
f"CLI verbs with no dispatch test: {sorted(untested)} — add them to "
f"VERBS above")
+
+
+@pytest.mark.parametrize("argv,verb", [
+ (["reload"], "reload"),
+ (["restart-daemon"], "restart"),
+])
+def test_lifecycle_commands_delegate_to_systemctl_user(
+ argv, verb, stub_daemon, monkeypatch, capsys):
+ """
+ `reload` and `restart-daemon` must ask systemd to do it, not hunt a PID
+ with pgrep and signal it directly — that pattern-matched a developer's own
+ running node by accident once, which is why it was replaced.
+ """
+ monkeypatch.setattr(sys, "argv", ["meshbay-node", *argv])
+ daemon_mod.main()
+
+ systemctl_calls = dict(stub_daemon)["_systemctl_calls"]
+ assert systemctl_calls == [["systemctl", "--user", verb, "meshbay-node"]]
+ assert not capsys.readouterr().err
+
+
+@pytest.mark.parametrize("argv", [["reload"], ["restart-daemon"]])
+def test_lifecycle_commands_report_systemctl_failure(
+ argv, stub_daemon, monkeypatch, capsys):
+ """A unit that refuses (not installed, not running) must exit non-zero."""
+ import subprocess
+
+ monkeypatch.setattr(
+ subprocess, "run",
+ lambda a, **k: subprocess.CompletedProcess(
+ a, 1, stdout="", stderr="Unit meshbay-node.service not loaded.\n"))
+ monkeypatch.setattr(sys, "argv", ["meshbay-node", *argv])
+ with pytest.raises(SystemExit) as exc:
+ daemon_mod.main()
+ assert exc.value.code == 1
+ assert "not loaded" in capsys.readouterr().out