From 2455fe189edab7a6a191f2dd64c3649ecd079f19 Mon Sep 17 00:00:00 2001 From: Dmitrii Iurco Date: Wed, 29 Apr 2026 09:32:51 -0400 Subject: [PATCH] fix: apply_cell_name regex now matches zone files with TTL field _generate_zone_content writes records as "name TTL IN A value" but the regex only matched "name IN A value" (no TTL), so renaming the cell never updated the DNS hostname record. Updated regex to make TTL optional. Also fixed the unit test zone fixture to use the actual generated format. Co-Authored-By: Claude Sonnet 4.6 --- api/network_manager.py | 3 ++- tests/test_config_manager.py | 21 +++++++++++++-------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/api/network_manager.py b/api/network_manager.py index 5370cb6..5cdaf6d 100644 --- a/api/network_manager.py +++ b/api/network_manager.py @@ -520,8 +520,9 @@ class NetworkManager(BaseServiceManager): zone_file = os.path.join(dns_data, fname) with open(zone_file) as f: content = f.read() + # Match name with optional TTL: "name [ttl] IN A value" content = re.sub( - rf'^{re.escape(old_name)}(\s+IN\s+A\s+)', + rf'^{re.escape(old_name)}(\s+(?:\d+\s+)?IN\s+A\s+)', f'{new_name}\\1', content, flags=re.MULTILINE ) diff --git a/tests/test_config_manager.py b/tests/test_config_manager.py index 472758a..20f2574 100644 --- a/tests/test_config_manager.py +++ b/tests/test_config_manager.py @@ -349,13 +349,16 @@ class TestNetworkManagerApplyCellName(unittest.TestCase): f.write('domain=cell\n') with open(os.path.join(self.config_dir, 'ntp', 'chrony.conf'), 'w') as f: f.write('server pool.ntp.org iburst\n') - # Create a zone file with old cell name + # Create a zone file matching _generate_zone_content format (name TTL IN type value) with open(os.path.join(self.data_dir, 'dns', 'cell.zone'), 'w') as f: - f.write('$ORIGIN cell.\n$TTL 300\n' - '@ IN SOA ns1.cell. admin.cell. 2024010101 3600 900 604800 300\n' - 'ns1 IN A 172.20.0.3\n' - 'mycell IN A 172.20.0.2\n' - '@ IN A 172.20.0.2\n') + f.write('$TTL 3600\n' + '@ IN SOA cell. admin.cell. (\n' + ' 2024010101 ; Serial\n' + ' 3600 ; Refresh\n' + ' )\n\n' + 'ns1 3600 IN A 172.20.0.3\n' + 'mycell 3600 IN A 172.20.0.2\n' + '@ 3600 IN A 172.20.0.2\n') sys.path.insert(0, str(Path(__file__).parent.parent / 'api')) from network_manager import NetworkManager self.nm = NetworkManager(self.data_dir, self.config_dir) @@ -368,8 +371,10 @@ class TestNetworkManagerApplyCellName(unittest.TestCase): mock_run.return_value = MagicMock(returncode=0) result = self.nm.apply_cell_name('mycell', 'newcell') zone = open(os.path.join(self.data_dir, 'dns', 'cell.zone')).read() - self.assertIn('newcell IN A 172.20.0.2', zone) - self.assertNotIn('mycell IN A', zone) + self.assertIn('newcell', zone) + self.assertIn('172.20.0.2', zone) + # old name must be gone from A records (the @ record still contains 172.20.0.2) + self.assertNotIn('mycell 3600 IN A', zone) self.assertIn('cell-dns', ' '.join(result['restarted'])) @patch('subprocess.run')