59 lines
1.9 KiB
Python
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
|