init
This commit is contained in:
@@ -0,0 +1,226 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Tests for ConfigManager
|
||||
"""
|
||||
|
||||
import unittest
|
||||
import json
|
||||
import tempfile
|
||||
import os
|
||||
import shutil
|
||||
from unittest.mock import Mock, patch, MagicMock
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
# Add api directory to path
|
||||
api_dir = Path(__file__).parent.parent / 'api'
|
||||
sys.path.insert(0, str(api_dir))
|
||||
|
||||
from config_manager import ConfigManager
|
||||
|
||||
class TestConfigManager(unittest.TestCase):
|
||||
"""Test the configuration manager functionality"""
|
||||
|
||||
def setUp(self):
|
||||
self.temp_dir = tempfile.mkdtemp()
|
||||
self.config_file = os.path.join(self.temp_dir, 'cell_config.json')
|
||||
self.data_dir = os.path.join(self.temp_dir, 'data')
|
||||
os.makedirs(self.data_dir, exist_ok=True)
|
||||
self.config_manager = ConfigManager(self.config_file, self.data_dir)
|
||||
|
||||
def tearDown(self):
|
||||
shutil.rmtree(self.temp_dir)
|
||||
|
||||
def test_initialization(self):
|
||||
"""Test config manager initialization"""
|
||||
self.assertTrue(os.path.exists(self.config_file))
|
||||
self.assertTrue(os.path.exists(self.data_dir))
|
||||
self.assertTrue(os.path.exists(self.config_manager.backup_dir))
|
||||
self.assertIsNotNone(self.config_manager.service_schemas)
|
||||
|
||||
def test_get_service_config(self):
|
||||
"""Test getting service configuration"""
|
||||
# Test with non-existent service
|
||||
with self.assertRaises(ValueError):
|
||||
self.config_manager.get_service_config('nonexistent_service')
|
||||
|
||||
# Test with valid service
|
||||
config = self.config_manager.get_service_config('network')
|
||||
self.assertEqual(config, {})
|
||||
|
||||
def test_update_service_config(self):
|
||||
"""Test updating service configuration"""
|
||||
test_config = {
|
||||
'dns_port': 53,
|
||||
'dhcp_range': '10.0.0.100-10.0.0.200',
|
||||
'ntp_servers': ['pool.ntp.org']
|
||||
}
|
||||
|
||||
success = self.config_manager.update_service_config('network', test_config)
|
||||
self.assertTrue(success)
|
||||
|
||||
# Verify config was saved
|
||||
config = self.config_manager.get_service_config('network')
|
||||
self.assertEqual(config['dns_port'], 53)
|
||||
self.assertEqual(config['dhcp_range'], '10.0.0.100-10.0.0.200')
|
||||
self.assertEqual(config['ntp_servers'], ['pool.ntp.org'])
|
||||
|
||||
def test_validate_config(self):
|
||||
"""Test configuration validation"""
|
||||
# Test valid config
|
||||
valid_config = {
|
||||
'dns_port': 53,
|
||||
'dhcp_range': '10.0.0.100-10.0.0.200',
|
||||
'ntp_servers': ['pool.ntp.org']
|
||||
}
|
||||
validation = self.config_manager.validate_config('network', valid_config)
|
||||
self.assertTrue(validation['valid'])
|
||||
self.assertEqual(len(validation['errors']), 0)
|
||||
|
||||
# Test invalid config (missing required field)
|
||||
invalid_config = {
|
||||
'dns_port': 53,
|
||||
'ntp_servers': ['pool.ntp.org']
|
||||
# Missing dhcp_range
|
||||
}
|
||||
validation = self.config_manager.validate_config('network', invalid_config)
|
||||
self.assertFalse(validation['valid'])
|
||||
self.assertGreater(len(validation['errors']), 0)
|
||||
|
||||
def test_backup_and_restore(self):
|
||||
"""Test backup and restore functionality"""
|
||||
# Create some test configs
|
||||
test_config = {
|
||||
'dns_port': 53,
|
||||
'dhcp_range': '10.0.0.100-10.0.0.200',
|
||||
'ntp_servers': ['pool.ntp.org']
|
||||
}
|
||||
self.config_manager.update_service_config('network', test_config)
|
||||
|
||||
# Create backup
|
||||
backup_id = self.config_manager.backup_config()
|
||||
self.assertIsNotNone(backup_id)
|
||||
|
||||
# List backups
|
||||
backups = self.config_manager.list_backups()
|
||||
self.assertIsInstance(backups, list)
|
||||
self.assertGreater(len(backups), 0)
|
||||
|
||||
# Modify config
|
||||
modified_config = {
|
||||
'dns_port': 5353,
|
||||
'dhcp_range': '10.0.0.100-10.0.0.200',
|
||||
'ntp_servers': ['pool.ntp.org']
|
||||
}
|
||||
self.config_manager.update_service_config('network', modified_config)
|
||||
|
||||
# Restore backup
|
||||
success = self.config_manager.restore_config(backup_id)
|
||||
self.assertTrue(success)
|
||||
|
||||
# Verify restoration
|
||||
config = self.config_manager.get_service_config('network')
|
||||
self.assertEqual(config['dns_port'], 53) # Should be restored value
|
||||
|
||||
def test_export_import_config(self):
|
||||
"""Test export and import functionality"""
|
||||
# Create test configs
|
||||
test_configs = {
|
||||
'network': {
|
||||
'dns_port': 53,
|
||||
'dhcp_range': '10.0.0.100-10.0.0.200',
|
||||
'ntp_servers': ['pool.ntp.org']
|
||||
},
|
||||
'wireguard': {
|
||||
'port': 51820,
|
||||
'private_key': 'test_key',
|
||||
'address': '10.0.0.1/24'
|
||||
}
|
||||
}
|
||||
|
||||
for service, config in test_configs.items():
|
||||
self.config_manager.update_service_config(service, config)
|
||||
|
||||
# Export config
|
||||
exported = self.config_manager.export_config()
|
||||
self.assertIsInstance(exported, str)
|
||||
|
||||
# Import config
|
||||
success = self.config_manager.import_config(exported)
|
||||
self.assertTrue(success)
|
||||
|
||||
# Verify import
|
||||
for service, expected_config in test_configs.items():
|
||||
config = self.config_manager.get_service_config(service)
|
||||
for key, value in expected_config.items():
|
||||
self.assertEqual(config[key], value)
|
||||
|
||||
def test_get_all_configs(self):
|
||||
"""Test getting all configurations"""
|
||||
# Create some test configs
|
||||
test_configs = {
|
||||
'network': {'dns_port': 53, 'dhcp_range': '10.0.0.100-10.0.0.200', 'ntp_servers': ['pool.ntp.org']},
|
||||
'wireguard': {'port': 51820}
|
||||
}
|
||||
|
||||
for service, config in test_configs.items():
|
||||
self.config_manager.update_service_config(service, config)
|
||||
|
||||
all_configs = self.config_manager.get_all_configs()
|
||||
self.assertIn('network', all_configs)
|
||||
self.assertIn('wireguard', all_configs)
|
||||
self.assertEqual(all_configs['network']['dns_port'], 53)
|
||||
|
||||
def test_get_config_summary(self):
|
||||
"""Test getting configuration summary"""
|
||||
# Create some test configs
|
||||
test_configs = {
|
||||
'network': {'dns_port': 53, 'dhcp_range': '10.0.0.100-10.0.0.200', 'ntp_servers': ['pool.ntp.org']},
|
||||
'wireguard': {'port': 51820}
|
||||
}
|
||||
|
||||
for service, config in test_configs.items():
|
||||
self.config_manager.update_service_config(service, config)
|
||||
|
||||
summary = self.config_manager.get_config_summary()
|
||||
self.assertIn('total_services', summary)
|
||||
self.assertIn('configured_services', summary)
|
||||
self.assertIn('backup_count', summary)
|
||||
|
||||
def test_get_config_hash(self):
|
||||
"""Test getting configuration hash"""
|
||||
test_config = {'dns_port': 53, 'dhcp_range': '10.0.0.100-10.0.0.200', 'ntp_servers': ['pool.ntp.org']}
|
||||
self.config_manager.update_service_config('network', test_config)
|
||||
|
||||
hash1 = self.config_manager.get_config_hash('network')
|
||||
self.assertIsInstance(hash1, str)
|
||||
self.assertGreater(len(hash1), 0)
|
||||
|
||||
# Update config and get new hash
|
||||
test_config['dns_port'] = 5353
|
||||
self.config_manager.update_service_config('network', test_config)
|
||||
|
||||
hash2 = self.config_manager.get_config_hash('network')
|
||||
self.assertNotEqual(hash1, hash2)
|
||||
|
||||
def test_has_config_changed(self):
|
||||
"""Test checking if configuration has changed"""
|
||||
test_config = {'dns_port': 53, 'dhcp_range': '10.0.0.100-10.0.0.200', 'ntp_servers': ['pool.ntp.org']}
|
||||
self.config_manager.update_service_config('network', test_config)
|
||||
|
||||
original_hash = self.config_manager.get_config_hash('network')
|
||||
|
||||
# Check if changed (should be False since we just set it)
|
||||
changed = self.config_manager.has_config_changed('network', original_hash)
|
||||
self.assertFalse(changed)
|
||||
|
||||
# Update config
|
||||
test_config['dns_port'] = 5353
|
||||
self.config_manager.update_service_config('network', test_config)
|
||||
|
||||
# Check if changed (should be True)
|
||||
changed = self.config_manager.has_config_changed('network', original_hash)
|
||||
self.assertTrue(changed)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user