""" 测试各个硬件检测模块 """ import unittest from unittest.mock import patch, MagicMock import sys import os # 添加父目录到路径 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from modules import system_info, cpu, memory, storage, sensors, gpu, log_analyzer class TestSystemInfo(unittest.TestCase): """测试系统信息模块""" @patch('modules.system_info.execute_command') def test_get_os_info(self, mock_exec): mock_exec.return_value = (0, "test-hostname\n", "") result = system_info.get_os_info() self.assertIn("platform", result) self.assertIn("machine", result) def test_get_cpu_info(self): result = system_info.get_cpu_info() self.assertIn("model", result) # 在大多数系统上应该能获取到一些信息 self.assertIsInstance(result["model"], str) def test_get_memory_info(self): result = system_info.get_memory_info() self.assertIn("total_gb", result) self.assertIsInstance(result["total_gb"], (int, float)) def test_get_system_info(self): result = system_info.get_system_info() self.assertIn("status", result) self.assertIn("cpu", result) self.assertIn("memory", result) class TestCPU(unittest.TestCase): """测试 CPU 模块""" def test_get_cpu_details(self): result = cpu.get_cpu_details() self.assertIn("model", result) self.assertIn("cores", result) self.assertIsInstance(result["cores"], int) def test_get_cpu_temperature(self): result = cpu.get_cpu_temperature() self.assertIn("status", result) self.assertIn("sensors", result) def test_get_load_average(self): result = cpu.get_load_average() self.assertIn("1min", result) self.assertIn("5min", result) self.assertIn("15min", result) def test_check_mce_errors(self): result = cpu.check_mce_errors() self.assertIn("count", result) self.assertIn("status", result) class TestMemory(unittest.TestCase): """测试内存模块""" def test_get_memory_summary(self): result = memory.get_memory_summary() self.assertIn("total_bytes", result) self.assertIn("total_gb", result) self.assertIsInstance(result["total_gb"], (int, float)) def test_get_dimm_info(self): result = memory.get_dimm_info() self.assertIsInstance(result, list) def test_check_ecc_status(self): result = memory.check_ecc_status() self.assertIn("supported", result) self.assertIsInstance(result["supported"], bool) def test_check_edac_errors(self): result = memory.check_edac_errors() self.assertIn("total_errors", result) self.assertIsInstance(result["total_errors"], int) class TestStorage(unittest.TestCase): """测试存储模块""" def test_get_storage_devices(self): result = storage.get_storage_devices() self.assertIsInstance(result, list) def test_check_raid_status(self): result = storage.check_raid_status() self.assertIn("arrays", result) self.assertIsInstance(result["arrays"], list) def test_get_io_statistics(self): result = storage.get_io_statistics() self.assertIsInstance(result, dict) class TestSensors(unittest.TestCase): """测试传感器模块""" def test_get_lm_sensors_data(self): result = sensors.get_lm_sensors_data() self.assertIn("available", result) def test_get_thermal_zones(self): result = sensors.get_thermal_zones() self.assertIn("zones", result) self.assertIsInstance(result["zones"], dict) def test_get_power_supply_info(self): result = sensors.get_power_supply_info() self.assertIn("supplies", result) self.assertIsInstance(result["supplies"], list) class TestGPU(unittest.TestCase): """测试 GPU 模块""" def test_check_generic_gpus(self): result = gpu.check_generic_gpus() self.assertIsInstance(result, list) def test_check_gpu_dmesg_errors(self): result = gpu.check_gpu_dmesg_errors() self.assertIsInstance(result, list) class TestLogAnalyzer(unittest.TestCase): """测试日志分析模块""" def test_get_kernel_panic_logs(self): result = log_analyzer.get_kernel_panic_logs() self.assertIsInstance(result, list) def test_get_hardware_error_logs(self): result = log_analyzer.get_hardware_error_logs() self.assertIn("mce_errors", result) self.assertIn("ecc_errors", result) self.assertIn("io_errors", result) def test_summarize_errors(self): test_data = { "dmesg_analysis": { "error_counts": { "cpu_errors": 5, "memory_errors": 3 } }, "journal_analysis": { "error_counts": { "cpu_errors": 2, "memory_errors": 1 } } } result = log_analyzer.summarize_errors(test_data) self.assertEqual(result["cpu_errors"], 7) self.assertEqual(result["memory_errors"], 4) if __name__ == '__main__': unittest.main()