Files
Nexus/tests/test_psutil_install.py
T
Nexus Agent 8092000880
Nexus CI/CD / test (push) Waiting to run
Nexus CI/CD / e2e (push) Blocked by required conditions
Nexus CI/CD / deploy (push) Blocked by required conditions
Nexus Pre-commit Checks / quick-check (push) Waiting to run
fix(watch): psutil SSH 安装 apt 走 sudo
非 root 用户 apt-get 未加 sudo 导致 Permission denied;补充 run_root 与中文错误提示。
2026-06-12 04:09:10 +08:00

59 lines
1.9 KiB
Python

"""Tests for SSH psutil install helper."""
from unittest.mock import AsyncMock, patch
import pytest
from server.utils.psutil_install import build_psutil_install_cmd, install_psutil_via_ssh
def test_build_psutil_install_cmd_root():
cmd = build_psutil_install_cmd(ssh_user="root")
assert "run_root" in cmd
assert "pip install --user -q psutil" in cmd
assert "python3-psutil" in cmd
assert "run_root apt-get" in cmd
def test_build_psutil_install_cmd_non_root_wraps_sudo():
cmd = build_psutil_install_cmd(ssh_user="deploy")
assert "sudoers.d/nexus-agent" in cmd
assert "run_root apt-get" in cmd
assert 'sudo "$@"' in cmd
@pytest.mark.asyncio
async def test_install_psutil_via_ssh_success():
server = type("S", (), {"username": "root", "domain": "h.example.com", "port": 22})()
with patch(
"server.utils.psutil_install.exec_ssh_command",
new_callable=AsyncMock,
return_value={"exit_code": 0, "stdout": "psutil_ok\n", "stderr": ""},
):
result = await install_psutil_via_ssh(server)
assert result["success"] is True
assert result["message"] == "psutil 已就绪"
@pytest.mark.asyncio
async def test_install_psutil_via_ssh_failure():
server = type("S", (), {"username": "root", "domain": "h.example.com", "port": 22})()
with patch(
"server.utils.psutil_install.exec_ssh_command",
new_callable=AsyncMock,
return_value={"exit_code": 1, "stdout": "", "stderr": "permission denied"},
):
result = await install_psutil_via_ssh(server)
assert result["success"] is False
assert result["message"]
def test_psutil_install_error_zh_apt_lock():
from server.utils.psutil_install import _psutil_install_error_zh
msg = _psutil_install_error_zh(
"E: Could not open lock file /var/lib/apt/lists/lock - open (13: Permission denied)",
"",
)
assert "免密 sudo" in msg or "root" in msg