first commit
This commit is contained in:
189
quick_test.py
Executable file
189
quick_test.py
Executable file
@@ -0,0 +1,189 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
ServerGuard - 快速测试脚本
|
||||
|
||||
用于快速验证各模块是否正常工作,不进行压力测试。
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
|
||||
# 设置日志级别为警告,减少输出
|
||||
import logging
|
||||
logging.basicConfig(level=logging.WARNING)
|
||||
|
||||
def test_imports():
|
||||
"""测试所有模块是否能正常导入"""
|
||||
print("测试模块导入...")
|
||||
modules_to_test = [
|
||||
'utils',
|
||||
'reporter',
|
||||
'modules.system_info',
|
||||
'modules.cpu',
|
||||
'modules.memory',
|
||||
'modules.storage',
|
||||
'modules.sensors',
|
||||
'modules.gpu',
|
||||
'modules.log_analyzer'
|
||||
]
|
||||
|
||||
failed = []
|
||||
for module in modules_to_test:
|
||||
try:
|
||||
__import__(module)
|
||||
print(f" ✓ {module}")
|
||||
except Exception as e:
|
||||
print(f" ✗ {module}: {e}")
|
||||
failed.append(module)
|
||||
|
||||
if failed:
|
||||
print(f"\n有 {len(failed)} 个模块导入失败")
|
||||
return False
|
||||
else:
|
||||
print("\n所有模块导入成功!")
|
||||
return True
|
||||
|
||||
|
||||
def test_basic_functions():
|
||||
"""测试基本功能"""
|
||||
print("\n测试基本功能...")
|
||||
|
||||
from modules import system_info, cpu, memory, storage, sensors, gpu, log_analyzer
|
||||
|
||||
# 返回字典的测试函数
|
||||
dict_tests = [
|
||||
("系统信息", system_info.get_system_info),
|
||||
("CPU 信息", cpu.get_cpu_details),
|
||||
("内存信息", memory.get_memory_summary),
|
||||
("传感器数据", sensors.get_lm_sensors_data),
|
||||
("日志分析", log_analyzer.analyze_logs),
|
||||
]
|
||||
|
||||
# 返回列表的测试函数
|
||||
list_tests = [
|
||||
("存储设备", storage.get_storage_devices),
|
||||
("GPU 信息", gpu.check_generic_gpus),
|
||||
]
|
||||
|
||||
# 测试返回字典的函数
|
||||
for name, func in dict_tests:
|
||||
try:
|
||||
result = func()
|
||||
if isinstance(result, dict):
|
||||
status = result.get("status", "unknown")
|
||||
if status == "error":
|
||||
print(f" ⚠ {name}: 有错误 - {result.get('error', 'Unknown')}")
|
||||
else:
|
||||
print(f" ✓ {name}: 正常")
|
||||
else:
|
||||
print(f" ✓ {name}: 正常 (返回 {type(result).__name__})")
|
||||
except Exception as e:
|
||||
print(f" ✗ {name}: 异常 - {e}")
|
||||
|
||||
# 测试返回列表的函数
|
||||
for name, func in list_tests:
|
||||
try:
|
||||
result = func()
|
||||
if isinstance(result, list):
|
||||
print(f" ✓ {name}: 正常 (找到 {len(result)} 个项目)")
|
||||
else:
|
||||
print(f" ⚠ {name}: 返回类型异常 - {type(result).__name__}")
|
||||
except Exception as e:
|
||||
print(f" ✗ {name}: 异常 - {e}")
|
||||
|
||||
print("\n基本功能测试完成")
|
||||
|
||||
|
||||
def test_utils():
|
||||
"""测试工具函数"""
|
||||
print("\n测试工具函数...")
|
||||
|
||||
from utils import safe_int, safe_float, format_bytes
|
||||
|
||||
# 测试 safe_int
|
||||
assert safe_int("123") == 123
|
||||
assert safe_int("32 GB") == 32
|
||||
assert safe_int("invalid", -1) == -1
|
||||
print(" ✓ safe_int")
|
||||
|
||||
# 测试 safe_float
|
||||
assert safe_float("123.5") == 123.5
|
||||
assert safe_float("2.5GHz") == 2.5
|
||||
print(" ✓ safe_float")
|
||||
|
||||
# 测试 format_bytes
|
||||
assert format_bytes(1024) == "1.00 KB"
|
||||
assert format_bytes(1024**2) == "1.00 MB"
|
||||
print(" ✓ format_bytes")
|
||||
|
||||
print("\n工具函数测试通过")
|
||||
|
||||
|
||||
def test_report_generation():
|
||||
"""测试报告生成"""
|
||||
print("\n测试报告生成...")
|
||||
|
||||
from reporter import ReportGenerator
|
||||
|
||||
generator = ReportGenerator()
|
||||
|
||||
test_data = {
|
||||
"scan_type": "test",
|
||||
"timestamp": "2024-01-01 00:00:00",
|
||||
"modules": {
|
||||
"cpu": {
|
||||
"status": "success",
|
||||
"temperature": {"current_c": 45}
|
||||
},
|
||||
"memory": {
|
||||
"status": "success",
|
||||
"total_gb": 32
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
formats = ['text', 'json', 'html']
|
||||
for fmt in formats:
|
||||
try:
|
||||
report = generator.generate_report(test_data, fmt)
|
||||
print(f" ✓ {fmt.upper()} 格式: {len(report)} 字符")
|
||||
except Exception as e:
|
||||
print(f" ✗ {fmt.upper()} 格式: {e}")
|
||||
|
||||
print("\n报告生成测试完成")
|
||||
|
||||
|
||||
def main():
|
||||
"""主函数"""
|
||||
print("=" * 60)
|
||||
print("ServerGuard 快速测试")
|
||||
print("=" * 60)
|
||||
print()
|
||||
|
||||
# 测试导入
|
||||
if not test_imports():
|
||||
print("\n模块导入测试失败,请检查依赖安装")
|
||||
sys.exit(1)
|
||||
|
||||
# 测试工具函数
|
||||
test_utils()
|
||||
|
||||
# 测试报告生成
|
||||
test_report_generation()
|
||||
|
||||
# 测试基本功能
|
||||
test_basic_functions()
|
||||
|
||||
print()
|
||||
print("=" * 60)
|
||||
print("测试完成!")
|
||||
print("=" * 60)
|
||||
print()
|
||||
print("运行完整诊断命令:")
|
||||
print(" sudo python3 main.py --quick # 快速检测")
|
||||
print(" sudo python3 main.py --full # 全面诊断(含压力测试)")
|
||||
print()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user