feat(hub): show server CPU memory disk and network status on monitor page
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
"""hub_host_status_lib 单元测试。"""
|
||||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
import unittest
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from hub_host_status_lib import _disk_path, _state, get_host_status
|
||||
|
||||
|
||||
class HubHostStatusLibTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
_state["primed"] = False
|
||||
_state["net_ts"] = 0.0
|
||||
_state["net_sent"] = 0
|
||||
_state["net_recv"] = 0
|
||||
|
||||
def test_disk_path_env_override(self):
|
||||
with patch.dict("os.environ", {"HUB_HOST_DISK_PATH": "/data"}, clear=False):
|
||||
self.assertEqual(_disk_path(), "/data")
|
||||
|
||||
def test_get_host_status_without_psutil(self):
|
||||
import builtins
|
||||
|
||||
real_import = builtins.__import__
|
||||
|
||||
def fake_import(name, globals=None, locals=None, fromlist=(), level=0):
|
||||
if name == "psutil":
|
||||
raise ImportError("no psutil")
|
||||
return real_import(name, globals, locals, fromlist, level)
|
||||
|
||||
with patch("builtins.__import__", side_effect=fake_import):
|
||||
out = get_host_status()
|
||||
self.assertFalse(out.get("ok"))
|
||||
self.assertIn("psutil", out.get("msg", ""))
|
||||
|
||||
def test_get_host_status_payload(self):
|
||||
fake_vm = MagicMock(total=8_000_000_000, used=3_200_000_000, percent=40.0)
|
||||
fake_du = MagicMock(total=100_000_000_000, used=50_000_000_000)
|
||||
fake_net = MagicMock(bytes_sent=1_000_000, bytes_recv=2_000_000)
|
||||
fake_psutil = MagicMock()
|
||||
fake_psutil.cpu_percent.return_value = 12.5
|
||||
fake_psutil.cpu_count.return_value = 4
|
||||
fake_psutil.virtual_memory.return_value = fake_vm
|
||||
fake_psutil.disk_usage.return_value = fake_du
|
||||
fake_psutil.net_io_counters.return_value = fake_net
|
||||
fake_psutil.boot_time.return_value = 1_700_000_000.0
|
||||
with patch.dict(sys.modules, {"psutil": fake_psutil}):
|
||||
out = get_host_status()
|
||||
self.assertTrue(out.get("ok"))
|
||||
self.assertEqual(out["cpu"]["percent"], 12.5)
|
||||
self.assertEqual(out["memory"]["percent"], 40.0)
|
||||
self.assertEqual(out["disk"]["percent"], 50.0)
|
||||
self.assertIn("network", out)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user