import os
import tempfile
import unittest
from unittest.mock import Mock, patch
from olc.config.properties_loader import OlcConfigManager, OlcConfig
from olc.utils.path_utils import PathUtils
class TestOlcConfigManager(unittest.TestCase):
def setUp(self):
OlcConfigManager._instance = None
def test_singleton_pattern(self):
"""测试OlcConfigManager的单例模式"""
instance1 = OlcConfigManager.get_instance()
instance2 = OlcConfigManager.get_instance()
self.assertIs(instance1, instance2)
def test_initialization(self):
"""测试OlcConfigManager的初始化过程"""
with tempfile.NamedTemporaryFile(mode='w', suffix='.properties', delete=False) as f:
f.write('olc.sdk.domain=test.example.com\n')
f.write('olc.sdk.switch=true\n')
temp_file = f.name
try:
config_manager = OlcConfigManager(temp_file)
self.assertIsNotNone(config_manager)
config = config_manager.get_config()
self.assertIsInstance(config, OlcConfig)
self.assertEqual(config.sdk_config.domain, 'test.example.com')
self.assertEqual(config.sdk_config.switch, True)
finally:
os.unlink(temp_file)
def test_get_config_methods(self):
"""测试配置获取方法"""
with tempfile.NamedTemporaryFile(mode='w', suffix='.properties', delete=False) as f:
f.write('olc.sdk.domain=test.example.com\n')
f.write('olc.sdk.switch=true\n')
f.write('olc.redis.mode=cluster\n')
f.write('olc.redis.timeout=3000\n')
temp_file = f.name
try:
config_manager = OlcConfigManager(temp_file)
config = config_manager.get_config()
self.assertIsInstance(config, OlcConfig)
sdk_config = config_manager.get_sdk_config()
self.assertEqual(sdk_config.domain, 'test.example.com')
self.assertEqual(sdk_config.switch, True)
redis_config = config_manager.get_redis_config()
self.assertEqual(redis_config.mode, 'cluster')
self.assertEqual(redis_config.timeout, 3000)
finally:
os.unlink(temp_file)
def test_nonexistent_config_file(self):
"""测试配置文件不存在的场景"""
nonexistent_file = 'nonexistent_config.properties'
try:
config_manager = OlcConfigManager(nonexistent_file)
self.assertIsNotNone(config_manager)
config = config_manager.get_config()
self.assertIsInstance(config, OlcConfig)
self.assertEqual(config.sdk_config.domain, '')
self.assertEqual(config.sdk_config.switch, True)
self.assertEqual(config.redis_config.mode, 'alone')
finally:
if os.path.exists(nonexistent_file):
os.unlink(nonexistent_file)
@patch('olc.config.properties_loader.PathUtils.get_config_path')
def test_default_config_path(self, mock_get_config_path):
"""测试默认配置路径"""
temp_dir = tempfile.mkdtemp()
try:
mock_get_config_path.return_value = temp_dir + '\\'
default_file_name = 'overload-config.properties'
test_file_path = os.path.join(temp_dir, default_file_name)
with open(test_file_path, 'w') as f:
f.write('olc.sdk.domain=default.example.com\n')
config_manager = OlcConfigManager.get_instance()
config = config_manager.get_config()
self.assertEqual(config.sdk_config.domain, 'default.example.com')
finally:
if os.path.exists(test_file_path):
os.unlink(test_file_path)
if os.path.exists(temp_dir):
os.rmdir(temp_dir)
def test_config_update_callback(self):
"""测试配置变更的监听器功能"""
with tempfile.NamedTemporaryFile(mode='w', suffix='.properties', delete=False) as f:
f.write('olc.sdk.domain=initial.example.com\n')
temp_file = f.name
try:
callback_mock = Mock()
from olc.config.properties_loader import on_config_update
original_callback = on_config_update
try:
import olc.config.properties_loader
olc.config.properties_loader.on_config_update = callback_mock
config_manager = OlcConfigManager(temp_file)
config_manager._config._load_config()
callback_mock.assert_called_once()
finally:
import olc.config.properties_loader
olc.config.properties_loader.on_config_update = original_callback
finally:
os.unlink(temp_file)
def test_invalid_config_values(self):
"""测试无效配置值的处理"""
with tempfile.NamedTemporaryFile(mode='w', suffix='.properties', delete=False) as f:
f.write('olc.sdk.switch=not_a_boolean\n')
f.write('olc.sdk.collector_capacity=not_a_number\n')
temp_file = f.name
try:
config_manager = OlcConfigManager(temp_file)
config = config_manager.get_config()
self.assertIsInstance(config, OlcConfig)
finally:
os.unlink(temp_file)
def test_redis_config_repr(self):
"""测试RedisConfig的__repr__方法(密码脱敏)"""
with tempfile.NamedTemporaryFile(mode='w', suffix='.properties', delete=False) as f:
f.write('olc.redis.password=test_password\n')
temp_file = f.name
try:
config_manager = OlcConfigManager(temp_file)
redis_config = config_manager.get_redis_config()
repr_str = repr(redis_config)
self.assertIn('password=******', repr_str)
self.assertNotIn('test_password', repr_str)
finally:
os.unlink(temp_file)
def test_stop_method(self):
"""测试停止热加载的方法"""
with tempfile.NamedTemporaryFile(mode='w', suffix='.properties', delete=False) as f:
f.write('olc.sdk.domain=test.example.com\n')
temp_file = f.name
try:
config_manager = OlcConfigManager(temp_file)
config_manager._config.stop()
self.assertTrue(True)
finally:
os.unlink(temp_file)
def test_encryption_error_handling(self):
"""测试加密配置的异常处理"""
with tempfile.NamedTemporaryFile(mode='w', suffix='.properties', delete=False) as f:
f.write('olc.redis.password=ENC(encrypted_password)\n')
temp_file = f.name
try:
mock_encryptor = Mock()
mock_encryptor.decrypt.side_effect = Exception("Decryption error")
from olc.config.properties_loader import PropertiesManager
properties_manager = PropertiesManager(temp_file, encryptor=mock_encryptor)
config = properties_manager.get()
self.assertIsInstance(config, OlcConfig)
finally:
os.unlink(temp_file)
def test_new_statistic_config_default_values(self):
"""测试新增统计配置项的默认值"""
with tempfile.NamedTemporaryFile(mode='w', suffix='.properties', delete=False) as f:
temp_file = f.name
try:
config_manager = OlcConfigManager(temp_file)
sdk_config = config_manager.get_sdk_config()
self.assertEqual(sdk_config.statistic_expire, 300)
self.assertEqual(sdk_config.statistic_max_size, 5000000)
self.assertEqual(sdk_config.sub_group_cache_size, 100000)
finally:
os.unlink(temp_file)
def test_new_statistic_config_loading(self):
"""测试新增统计配置项从文件加载"""
with tempfile.NamedTemporaryFile(mode='w', suffix='.properties', delete=False) as f:
f.write('olc.sdk.statistic.expire=120\n')
f.write('olc.sdk.statistic.maxsize=2000\n')
f.write('olc.sdk.subgroup.maxsize=50000\n')
temp_file = f.name
try:
config_manager = OlcConfigManager(temp_file)
sdk_config = config_manager.get_sdk_config()
self.assertEqual(sdk_config.statistic_expire, 120)
self.assertEqual(sdk_config.statistic_max_size, 2000)
self.assertEqual(sdk_config.sub_group_cache_size, 50000)
finally:
os.unlink(temp_file)
if __name__ == '__main__':
print("Running OlcConfigManager tests...")
unittest.main(verbosity=2)