autool/test_config_loader.py
2026-06-17 19:44:18 +08:00

92 lines
3.3 KiB
Python

#!/usr/bin/env python3
"""测试配置加载功能"""
import sys
from pathlib import Path
# 添加项目根目录到 sys.path
ROOT = Path(__file__).resolve().parent
sys.path.insert(0, str(ROOT))
from config_loader import load_config
def test_config_loading():
"""测试配置加载"""
print("=" * 60)
print("Testing Config Loader")
print("=" * 60)
# 检查 PyYAML
try:
import yaml
print("\n✓ PyYAML is installed")
except ImportError:
print("\n✗ PyYAML not installed (will use JSON fallback)")
# 获取当前环境
current_env = get_config_env()
print(f"\nCurrent environment: {current_env}")
# 加载配置
try:
config = load_config()
print("\n✓ Config loaded successfully")
# 判断配置格式
if 'environment' in config:
print("\n📋 Config Format: YAML (new structured format)")
print("\nKey Configuration:")
print(f" • Environment: {config.get('environment', {}).get('name')}")
print(f" • Redis Host: {config.get('redis', {}).get('host')}")
print(f" • Redis DB: {config.get('redis', {}).get('db')}")
print(f" • Channel Namespace: {config.get('redis', {}).get('channel_namespace')}")
print(f" • Timeout: {config.get('execution', {}).get('timeout')}s")
print(f" • Block Timeout: {config.get('execution', {}).get('block_timeout')}s")
print(f" • Is Emulator: {config.get('device', {}).get('is_emulator')}")
# 测试环境变量展开
notifications = config.get('notifications', {})
if notifications:
print("\n🔔 Notification Configuration:")
wechat = notifications.get('wechat', {})
wecom = notifications.get('wecom', {})
if wechat:
print(" WeChat tokens:")
for key, value in wechat.items():
if value.startswith('${'):
print(f"{key}: {value} (⚠️ env var not set)")
else:
print(f"{key}: ***{value[-4:]} (✓ resolved)")
if wecom:
print(" WeCom tokens:")
for key, value in wecom.items():
if value.startswith('${'):
print(f"{key}: {value} (⚠️ env var not set)")
else:
print(f"{key}: {value[:8]}... (✓ resolved)")
else:
print("\n📋 Config Format: JSON (legacy flat format)")
print("\nKey Configuration:")
print(f" • Timeout: {config.get('TIMEOUT')}s")
print(f" • Redis Host: {config.get('CONTROL_REDIS_HOST')}")
print(f" • Redis DB: {config.get('CONTROL_REDIS_DB')}")
print(f" • Is Emulator: {config.get('IS_EMULATOR')}")
print("\n" + "=" * 60)
print("✓ All tests passed")
print("=" * 60)
except Exception as e:
import traceback
print(f"\n✗ Config load failed: {e}")
print("\nTraceback:")
traceback.print_exc()
sys.exit(1)
if __name__ == "__main__":
test_config_loading()