Skip to content

Commit 37fd0cb

Browse files
author
zhangtao
committed
test: 监控服务覆盖提升到100%
1 parent 3110f6a commit 37fd0cb

2 files changed

Lines changed: 49 additions & 1 deletion

File tree

‎backend/tests/test_monitor_services.py‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,59 @@ def test_server_run_time() -> None:
4040
assert ServerService._calculate_run_time(time.time() - 3661) .startswith("0天1小时1分钟")
4141

4242

43+
def test_server_local_ip_success(monkeypatch: pytest.MonkeyPatch) -> None:
44+
import socket
45+
class GoodSocket:
46+
def __enter__(self): return self
47+
def __exit__(self, *a): return False
48+
def connect(self, addr): return None
49+
def getsockname(self): return ("10.0.0.2", 1)
50+
monkeypatch.setattr(socket, "socket", lambda *a, **k: GoodSocket())
51+
assert ServerService._get_local_ip() == "10.0.0.2"
52+
53+
54+
def test_server_local_ip_fallbacks(monkeypatch: pytest.MonkeyPatch) -> None:
55+
import socket
56+
class BadSocket:
57+
def __enter__(self): return self
58+
def __exit__(self, *a): return False
59+
def connect(self, addr): raise OSError("no route")
60+
monkeypatch.setattr(socket, "socket", lambda *a, **k: BadSocket())
61+
monkeypatch.setattr(socket, "gethostbyname", lambda name: "10.0.0.1")
62+
assert ServerService._get_local_ip() == "10.0.0.1"
63+
monkeypatch.setattr(socket, "gethostbyname", lambda name: (_ for _ in ()).throw(OSError()))
64+
assert ServerService._get_local_ip() == "127.0.0.1"
65+
66+
67+
def test_server_python_info(monkeypatch: pytest.MonkeyPatch) -> None:
68+
class Proc:
69+
def name(self): return "python"
70+
def create_time(self): return 1.0
71+
def exe(self): return "/usr/bin/python"
72+
def memory_info(self): return SimpleNamespace(rss=10)
73+
monkeypatch.setattr("app.modules.monitor.server.service.psutil.Process", lambda: Proc())
74+
monkeypatch.setattr("app.modules.monitor.server.service.psutil.virtual_memory", lambda: SimpleNamespace(available=100))
75+
info = ServerService._get_python_info()
76+
assert info.name == "python" and info.memory_used == "10.0B"
77+
78+
4379
async def test_server_monitor_delegates(monkeypatch: pytest.MonkeyPatch) -> None:
4480
sentinel = object()
4581
monkeypatch.setattr(ServerService, "_collect_monitor_info", staticmethod(lambda: sentinel))
4682
assert await ServerService.get_server_monitor_info() is sentinel
4783

4884

85+
def test_server_collect_info(monkeypatch: pytest.MonkeyPatch) -> None:
86+
from app.modules.monitor.server.schema import CpuInfoSchema, MemoryInfoSchema, PyInfoSchema, SysInfoSchema
87+
monkeypatch.setattr(ServerService, "_get_cpu_info", staticmethod(lambda: CpuInfoSchema(cpu_num=1, used=0, sys=0, free=100)))
88+
monkeypatch.setattr(ServerService, "_get_memory_info", staticmethod(lambda: MemoryInfoSchema(total="1", used="0", free="1", usage=0)))
89+
monkeypatch.setattr(ServerService, "_get_system_info", staticmethod(lambda: SysInfoSchema(computer_ip="x", computer_name="x", os_arch="x", os_name="x", user_dir="x")))
90+
monkeypatch.setattr(ServerService, "_get_python_info", staticmethod(lambda: PyInfoSchema(name="x", version="x", start_time="x", run_time="x", home="x", memory_total="x", memory_used="x", memory_free="x", memory_usage=0)))
91+
monkeypatch.setattr(ServerService, "_get_disk_info", staticmethod(lambda: []))
92+
result = ServerService._collect_monitor_info()
93+
assert result.disks == []
94+
95+
4996
async def test_cache_service_branches(monkeypatch: pytest.MonkeyPatch) -> None:
5097
from app.core.redis_crud import RedisCURD
5198
fake = SimpleNamespace()

‎docs/COVERAGE-PLAN.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ D 包含四个独立目标:
3333
- `test_transfer_engine_helpers.py`:本地大小/文件树/目录准备/临时清理/目录判断/步骤正交,transfer engine 26%;
3434
- `test_monitor_services.py`:缓存服务 100%、服务器监控 75%;
3535
- `test_workflow_graph_service.py`:画布拆分、去重、源目标派生、非法连线,workflow service 39%;
36-
- `test_online_service.py`:在线列表/搜索/清理/趋势,online service 81%;修复搜索参数把 str 当 tuple、趋势少算今天的问题。
36+
- `test_online_service.py`:在线列表/搜索/清理/趋势,online service 81%;修复搜索参数把 str 当 tuple、趋势少算今天的问题;
37+
- 监控服务补充本地 IP 成功/降级、Python 进程信息、汇总调用,server/cache service 均 100%。
3738

3839
## 外部协议范围(2026-11-05 确认)
3940

0 commit comments

Comments
 (0)