Files
awoooi/apps/api/tests/test_ssh_provider_tools.py
Your Name 8396d37275
All checks were successful
Code Review / ai-code-review (push) Successful in 11s
CD Pipeline / tests (push) Successful in 59s
CD Pipeline / build-and-deploy (push) Successful in 3m20s
CD Pipeline / post-deploy-checks (push) Successful in 1m17s
fix(mcp): harden ssh provider connection params
2026-05-06 21:51:38 +08:00

56 lines
1.7 KiB
Python

import pytest
from src.plugins.mcp.providers.ssh_provider import SSHProvider, _normalize_ssh_host
@pytest.mark.asyncio
async def test_ssh_diagnose_is_registered_read_only_tool():
provider = SSHProvider()
tool_names = {tool.name for tool in await provider.list_tools()}
command = provider._build_command("ssh_diagnose", {})
assert "ssh_diagnose" in tool_names
assert "CPU TOP" in command
assert "df -h" in command
def test_ssh_provider_uses_ollama_user_for_188():
provider = SSHProvider()
assert provider._ssh_user_for_host("192.168.0.188") == "ollama"
assert provider._ssh_user_for_host("192.168.0.110") == "wooo"
@pytest.mark.parametrize(
"raw,expected",
[
("192.168.0.110:9100", "192.168.0.110"),
("wooo@192.168.0.110", "192.168.0.110"),
("ssh://wooo@192.168.0.110:22", "192.168.0.110"),
("192.168.0.188", "192.168.0.188"),
],
)
def test_normalize_ssh_host_strips_exporter_ports_and_users(raw, expected):
assert _normalize_ssh_host(raw) == expected
@pytest.mark.asyncio
async def test_ssh_execute_normalizes_host_before_allowed_check(monkeypatch):
provider = SSHProvider()
captured = {}
async def fake_ssh_exec(host, cmd, timeout, username=None):
captured["host"] = host
captured["timeout"] = timeout
captured["username"] = username
return "ok", ""
monkeypatch.setattr(provider, "_allowed_hosts", lambda: ["192.168.0.110"])
monkeypatch.setattr(provider, "_ssh_exec", fake_ssh_exec)
result = await provider.execute("ssh_diagnose", {"host": "192.168.0.110:9100"})
assert result.success is True
assert captured["host"] == "192.168.0.110"
assert isinstance(captured["timeout"], int)