@@ -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+
4379async 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+
4996async def test_cache_service_branches (monkeypatch : pytest .MonkeyPatch ) -> None :
5097 from app .core .redis_crud import RedisCURD
5198 fake = SimpleNamespace ()
0 commit comments