init
This commit is contained in:
@@ -0,0 +1,276 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Unit tests for NetworkManager class
|
||||
"""
|
||||
|
||||
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))
|
||||
import unittest
|
||||
import tempfile
|
||||
import os
|
||||
import json
|
||||
import shutil
|
||||
from unittest.mock import patch, MagicMock
|
||||
from datetime import datetime
|
||||
|
||||
# Add parent directory to path for imports
|
||||
import sys
|
||||
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
from network_manager import NetworkManager
|
||||
|
||||
class TestNetworkManager(unittest.TestCase):
|
||||
"""Test cases for NetworkManager class"""
|
||||
|
||||
def setUp(self):
|
||||
"""Set up test environment"""
|
||||
self.test_dir = tempfile.mkdtemp()
|
||||
self.data_dir = os.path.join(self.test_dir, 'data')
|
||||
self.config_dir = os.path.join(self.test_dir, 'config')
|
||||
os.makedirs(self.data_dir, exist_ok=True)
|
||||
os.makedirs(self.config_dir, exist_ok=True)
|
||||
|
||||
# Create NetworkManager instance
|
||||
self.network_manager = NetworkManager(self.data_dir, self.config_dir)
|
||||
|
||||
def tearDown(self):
|
||||
"""Clean up test environment"""
|
||||
shutil.rmtree(self.test_dir)
|
||||
|
||||
def test_initialization(self):
|
||||
"""Test NetworkManager initialization"""
|
||||
self.assertEqual(self.network_manager.data_dir, self.data_dir)
|
||||
self.assertEqual(self.network_manager.config_dir, self.config_dir)
|
||||
self.assertTrue(os.path.exists(self.network_manager.dns_zones_dir))
|
||||
self.assertTrue(os.path.exists(os.path.dirname(self.network_manager.dhcp_leases_file)))
|
||||
|
||||
def test_generate_zone_content(self):
|
||||
"""Test DNS zone content generation"""
|
||||
records = [
|
||||
{'name': 'test1', 'type': 'A', 'value': '192.168.1.10', 'ttl': 3600},
|
||||
{'name': 'test2', 'type': 'CNAME', 'value': 'test1', 'ttl': 1800}
|
||||
]
|
||||
|
||||
content = self.network_manager._generate_zone_content('test.cell', records)
|
||||
|
||||
self.assertIn('test.cell', content)
|
||||
self.assertIn('SOA', content)
|
||||
self.assertIn('192.168.1.10', content)
|
||||
self.assertIn('test1', content)
|
||||
self.assertIn('CNAME', content)
|
||||
|
||||
def test_add_dns_record(self):
|
||||
"""Test adding DNS record"""
|
||||
success = self.network_manager.add_dns_record('test.cell', 'test', 'A', '192.168.1.100')
|
||||
self.assertTrue(success)
|
||||
|
||||
# Check if zone file was created
|
||||
zone_file = os.path.join(self.network_manager.dns_zones_dir, 'test.cell.zone')
|
||||
self.assertTrue(os.path.exists(zone_file))
|
||||
|
||||
# Check content
|
||||
with open(zone_file, 'r') as f:
|
||||
content = f.read()
|
||||
self.assertIn('test', content)
|
||||
self.assertIn('192.168.1.100', content)
|
||||
|
||||
def test_remove_dns_record(self):
|
||||
"""Test removing DNS record"""
|
||||
# Add a record first
|
||||
self.network_manager.add_dns_record('test.cell', 'test', 'A', '192.168.1.100')
|
||||
|
||||
# Remove it
|
||||
success = self.network_manager.remove_dns_record('test.cell', 'test', 'A')
|
||||
self.assertTrue(success)
|
||||
|
||||
# Check if record was removed
|
||||
zone_file = os.path.join(self.network_manager.dns_zones_dir, 'test.cell.zone')
|
||||
with open(zone_file, 'r') as f:
|
||||
content = f.read()
|
||||
self.assertNotIn('192.168.1.100', content)
|
||||
|
||||
def test_load_dns_records(self):
|
||||
"""Test loading DNS records from zone file"""
|
||||
# Create a test zone file
|
||||
zone_file = os.path.join(self.network_manager.dns_zones_dir, 'test.cell.zone')
|
||||
content = """$TTL 3600
|
||||
@ IN SOA test.cell. admin.test.cell. (
|
||||
2024010101 ; Serial
|
||||
3600 ; Refresh
|
||||
1800 ; Retry
|
||||
1209600 ; Expire
|
||||
3600 ; Minimum TTL
|
||||
)
|
||||
|
||||
; Name servers
|
||||
@ IN NS test.cell.
|
||||
|
||||
test1 3600 IN A 192.168.1.10
|
||||
test2 1800 IN CNAME test1
|
||||
"""
|
||||
|
||||
with open(zone_file, 'w') as f:
|
||||
f.write(content)
|
||||
|
||||
records = self.network_manager._load_dns_records('test.cell')
|
||||
|
||||
self.assertEqual(len(records), 2)
|
||||
self.assertEqual(records[0]['name'], 'test1')
|
||||
self.assertEqual(records[0]['value'], '192.168.1.10')
|
||||
self.assertEqual(records[1]['name'], 'test2')
|
||||
self.assertEqual(records[1]['type'], 'CNAME')
|
||||
|
||||
def test_get_dhcp_leases(self):
|
||||
"""Test getting DHCP leases"""
|
||||
# Create a test leases file
|
||||
leases_file = self.network_manager.dhcp_leases_file
|
||||
content = """1234567890 aa:bb:cc:dd:ee:ff 192.168.1.100 testhost *
|
||||
1234567891 11:22:33:44:55:66 192.168.1.101 anotherhost *
|
||||
"""
|
||||
|
||||
with open(leases_file, 'w') as f:
|
||||
f.write(content)
|
||||
|
||||
leases = self.network_manager.get_dhcp_leases()
|
||||
|
||||
self.assertEqual(len(leases), 2)
|
||||
self.assertEqual(leases[0]['mac'], 'aa:bb:cc:dd:ee:ff')
|
||||
self.assertEqual(leases[0]['ip'], '192.168.1.100')
|
||||
self.assertEqual(leases[0]['hostname'], 'testhost')
|
||||
self.assertEqual(leases[1]['mac'], '11:22:33:44:55:66')
|
||||
self.assertEqual(leases[1]['ip'], '192.168.1.101')
|
||||
|
||||
def test_add_dhcp_reservation(self):
|
||||
"""Test adding DHCP reservation"""
|
||||
success = self.network_manager.add_dhcp_reservation('aa:bb:cc:dd:ee:ff', '192.168.1.100', 'testhost')
|
||||
self.assertTrue(success)
|
||||
|
||||
# Check if reservation file was created
|
||||
reservation_file = os.path.join(self.config_dir, 'dhcp', 'reservations.conf')
|
||||
self.assertTrue(os.path.exists(reservation_file))
|
||||
|
||||
# Check content
|
||||
with open(reservation_file, 'r') as f:
|
||||
content = f.read()
|
||||
self.assertIn('aa:bb:cc:dd:ee:ff', content)
|
||||
self.assertIn('192.168.1.100', content)
|
||||
self.assertIn('testhost', content)
|
||||
|
||||
def test_remove_dhcp_reservation(self):
|
||||
"""Test removing DHCP reservation"""
|
||||
# Add a reservation first
|
||||
self.network_manager.add_dhcp_reservation('aa:bb:cc:dd:ee:ff', '192.168.1.100', 'testhost')
|
||||
|
||||
# Remove it
|
||||
success = self.network_manager.remove_dhcp_reservation('aa:bb:cc:dd:ee:ff')
|
||||
self.assertTrue(success)
|
||||
|
||||
# Check if reservation was removed
|
||||
reservation_file = os.path.join(self.config_dir, 'dhcp', 'reservations.conf')
|
||||
with open(reservation_file, 'r') as f:
|
||||
content = f.read()
|
||||
self.assertNotIn('aa:bb:cc:dd:ee:ff', content)
|
||||
|
||||
@patch('subprocess.run')
|
||||
def test_get_ntp_status(self, mock_run):
|
||||
"""Test getting NTP status"""
|
||||
# Mock NTP service running
|
||||
mock_run.return_value.stdout = 'cell-ntp\n'
|
||||
mock_run.return_value.returncode = 0
|
||||
|
||||
status = self.network_manager.get_ntp_status()
|
||||
|
||||
self.assertTrue(status['running'])
|
||||
self.assertIn('stats', status)
|
||||
|
||||
@patch('subprocess.run')
|
||||
def test_get_ntp_status_not_running(self, mock_run):
|
||||
"""Test getting NTP status when service is not running"""
|
||||
# Mock NTP service not running
|
||||
mock_run.return_value.stdout = ''
|
||||
mock_run.return_value.returncode = 0
|
||||
|
||||
status = self.network_manager.get_ntp_status()
|
||||
|
||||
self.assertFalse(status['running'])
|
||||
self.assertIn('stats', status)
|
||||
|
||||
@patch('subprocess.run')
|
||||
def test_test_dns_resolution(self, mock_run):
|
||||
"""Test DNS resolution testing"""
|
||||
# Mock successful DNS resolution
|
||||
mock_run.return_value.returncode = 0
|
||||
mock_run.return_value.stdout = 'test.cell -> 192.168.1.100'
|
||||
mock_run.return_value.stderr = ''
|
||||
|
||||
result = self.network_manager.test_dns_resolution('test.cell')
|
||||
|
||||
self.assertTrue(result['success'])
|
||||
self.assertIn('192.168.1.100', result['output'])
|
||||
|
||||
@patch('subprocess.run')
|
||||
def test_test_dns_resolution_failure(self, mock_run):
|
||||
"""Test DNS resolution testing with failure"""
|
||||
# Mock failed DNS resolution
|
||||
mock_run.return_value.returncode = 1
|
||||
mock_run.return_value.stdout = ''
|
||||
mock_run.return_value.stderr = 'NXDOMAIN'
|
||||
|
||||
result = self.network_manager.test_dns_resolution('nonexistent.cell')
|
||||
|
||||
self.assertFalse(result['success'])
|
||||
self.assertIn('NXDOMAIN', result['error'])
|
||||
|
||||
@patch('subprocess.run')
|
||||
def test_test_dhcp_functionality(self, mock_run):
|
||||
"""Test DHCP functionality testing"""
|
||||
# Mock DHCP service running
|
||||
mock_run.return_value.stdout = 'cell-dhcp\n'
|
||||
mock_run.return_value.returncode = 0
|
||||
|
||||
result = self.network_manager.test_dhcp_functionality()
|
||||
|
||||
self.assertTrue(result['running'])
|
||||
self.assertIn('leases_count', result)
|
||||
self.assertIn('leases', result)
|
||||
|
||||
@patch('subprocess.run')
|
||||
def test_test_ntp_functionality(self, mock_run):
|
||||
"""Test NTP functionality testing"""
|
||||
# Mock NTP service running with tracking
|
||||
mock_run.return_value.stdout = 'cell-ntp\n'
|
||||
mock_run.return_value.returncode = 0
|
||||
|
||||
result = self.network_manager.test_ntp_functionality()
|
||||
|
||||
self.assertTrue(result['running'])
|
||||
self.assertIn('ntp_test', result)
|
||||
|
||||
def test_update_dns_zone(self):
|
||||
"""Test updating DNS zone"""
|
||||
records = [
|
||||
{'name': 'test1', 'type': 'A', 'value': '192.168.1.10', 'ttl': 3600},
|
||||
{'name': 'test2', 'type': 'A', 'value': '192.168.1.11', 'ttl': 3600}
|
||||
]
|
||||
|
||||
success = self.network_manager.update_dns_zone('test.cell', records)
|
||||
self.assertTrue(success)
|
||||
|
||||
# Check if zone file was created
|
||||
zone_file = os.path.join(self.network_manager.dns_zones_dir, 'test.cell.zone')
|
||||
self.assertTrue(os.path.exists(zone_file))
|
||||
|
||||
# Check content
|
||||
with open(zone_file, 'r') as f:
|
||||
content = f.read()
|
||||
self.assertIn('test1', content)
|
||||
self.assertIn('test2', content)
|
||||
self.assertIn('192.168.1.10', content)
|
||||
self.assertIn('192.168.1.11', content)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user