Skip to content

The agent that grows with you

Merge branch 'bs5-c' into hermes/hermes-b802e898

teknium1f1df96b6294d
diff --cc tests/tools/test_bot_desktop_install.py
index 486faf955f5,96bcdc4de5d..40265aa7bc0
--- a/tests/tools/test_bot_desktop_install.py
+++ b/tests/tools/test_bot_desktop_install.py
@@@ -99,29 -101,71 +101,99 @@@ def test_timeout_kills_the_package_mana
pytest.fail("grandchild survived the install timeout")
+def test_passwordless_sudo_runs_the_install_without_asking_for_a_password(monkeypatch):
+ """A host with NOPASSWD sudo must not pop the masked password card — the card blocks the install
+ for up to five minutes on an answer nobody needs to give — and the package command still runs
+ and streams its output."""
+ monkeypatch.setattr(install, "_sudo_nopasswd", lambda: True)
+ spawned: list[list[str]] = []
+
+ class _Proc:
+ pid = 4242
+ stdin = __import__("io").StringIO()
+
+ def __init__(self, argv, **kw):
+ spawned.append(argv)
+ self.stdout = iter(["Reading package lists...\n", "Done\n"])
+
+ def wait(self):
+ return 0
+
+ monkeypatch.setattr(install.subprocess, "Popen", _Proc)
+ monkeypatch.setattr(install.os, "killpg", lambda *a: None)
+ lines: list[str] = []
+ code = install.install_packages(ask_password=lambda: pytest.fail("sudo password asked despite NOPASSWD"),
+ on_line=lines.append)
+ assert code == 0
+ assert spawned and spawned[0][:1] == ["sudo"] and "-S" not in spawned[0]
+ assert "Done" in lines
++
++
+ @pytest.mark.linux_only
+ def test_timeout_returns_and_frees_the_slot_even_when_a_descendant_survives(monkeypatch):
+ """From an unprivileged Hermes, killpg reaches the sudo leader but not a root-owned apt child; that
+ child keeps the pipe's write end open, so draining stdout never sees EOF and the profile slot stays
+ taken forever. The timeout must end the drain and release the slot regardless of what survived.
+ Stand-in for the unkillable root child: a grandchild in its own session holding our stdout."""
+ import subprocess
+ import time
+
+ monkeypatch.setattr(install, "_sudo_nopasswd", lambda: True)
+ monkeypatch.setattr(install, "_TERM_GRACE_SECONDS", 0.2, raising=False)
+ real_popen = subprocess.Popen
+
+ def popen(argv, **kw):
+ if argv[:1] != ["sudo"]:
+ return real_popen(argv, **kw)
+ return real_popen(["sh", "-c", "setsid sleep 30 & echo child $!; wait"], **kw)
+
+ monkeypatch.setattr(install.subprocess, "Popen", popen)
+ lines: list[str] = []
+ started = time.monotonic()
+ worker = threading.Thread(target=lambda: lines.append(
+ f"code {install.install_packages(ask_password=lambda: '', on_line=lines.append, timeout_seconds=0.5)}"))
+ worker.start()
+ worker.join(3.0)
+ survivor = next((int(line.split()[1]) for line in lines if line.startswith("child ")), None)
+ if survivor:
+ subprocess.run(["kill", "-9", str(survivor)], check=False)
+ assert not worker.is_alive(), f"install_packages hung {time.monotonic() - started:.1f}s on a surviving descendant"
+ assert any(line.startswith("code ") and line != "code 0" for line in lines), lines
+ assert any("timed out" in line for line in lines), lines
+ install.release(install.claim()) # slot is free again
+
+
+ def test_root_installs_without_sudo_and_without_asking(monkeypatch):
+ """The official Docker image runs Hermes as uid 0 with no sudo binary: the package manager must be
+ run directly, and the password card must never be raised for a user who already is root."""
+ import subprocess
+
+ monkeypatch.setattr(install.os, "geteuid", lambda: 0)
+ monkeypatch.setattr("shutil.which", lambda name, *a, **k: None)
+ monkeypatch.setattr(runtime, "package_manager", lambda: "apt")
+ monkeypatch.setattr(runtime, "install_command", _REAL_INSTALL_COMMAND)
+ spawned: list[list[str]] = []
+ real_popen = subprocess.Popen
+
+ def popen(argv, **kw):
+ spawned.append(list(argv))
+ return real_popen(["true"], **kw)
+
+ monkeypatch.setattr(install.subprocess, "Popen", popen)
+ code = install.install_packages(ask_password=lambda: pytest.fail("root was asked for a sudo password"),
+ on_line=lambda _l: None)
+ assert code == 0
+ assert spawned and spawned[0][0] == "apt-get", spawned
+
+
+ def test_no_sudo_binary_returns_the_host_command_instead_of_a_password_card(monkeypatch):
+ """Unprivileged with no sudo on the host (minimal containers): a password card would be a dead end,
+ the user needs the exact command to run on the host instead."""
+ monkeypatch.setattr(install.os, "geteuid", lambda: 1000)
+ monkeypatch.setattr("shutil.which", lambda name, *a, **k: None)
+ monkeypatch.setattr(install.subprocess, "Popen", lambda *a, **k: pytest.fail("package manager spawned"))
+ lines: list[str] = []
+ code = install.install_packages(ask_password=lambda: pytest.fail("password card raised without sudo"),
+ on_line=lines.append)
+ assert code == install.NO_SUDO
+ assert any("apt-get install" in line for line in lines), lines