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 <noreply@anthropic.com>
This commit is contained in:
2026-04-29 09:32:51 -04:00
parent 10eac1fda1
commit 2455fe189e
2 changed files with 15 additions and 9 deletions
+2 -1
View File
@@ -520,8 +520,9 @@ class NetworkManager(BaseServiceManager):
zone_file = os.path.join(dns_data, fname) zone_file = os.path.join(dns_data, fname)
with open(zone_file) as f: with open(zone_file) as f:
content = f.read() content = f.read()
# Match name with optional TTL: "name [ttl] IN A value"
content = re.sub( 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', f'{new_name}\\1',
content, flags=re.MULTILINE content, flags=re.MULTILINE
) )
+13 -8
View File
@@ -349,13 +349,16 @@ class TestNetworkManagerApplyCellName(unittest.TestCase):
f.write('domain=cell\n') f.write('domain=cell\n')
with open(os.path.join(self.config_dir, 'ntp', 'chrony.conf'), 'w') as f: with open(os.path.join(self.config_dir, 'ntp', 'chrony.conf'), 'w') as f:
f.write('server pool.ntp.org iburst\n') 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: with open(os.path.join(self.data_dir, 'dns', 'cell.zone'), 'w') as f:
f.write('$ORIGIN cell.\n$TTL 300\n' f.write('$TTL 3600\n'
'@ IN SOA ns1.cell. admin.cell. 2024010101 3600 900 604800 300\n' '@ IN SOA cell. admin.cell. (\n'
'ns1 IN A 172.20.0.3\n' ' 2024010101 ; Serial\n'
'mycell IN A 172.20.0.2\n' ' 3600 ; Refresh\n'
'@ IN A 172.20.0.2\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')) sys.path.insert(0, str(Path(__file__).parent.parent / 'api'))
from network_manager import NetworkManager from network_manager import NetworkManager
self.nm = NetworkManager(self.data_dir, self.config_dir) self.nm = NetworkManager(self.data_dir, self.config_dir)
@@ -368,8 +371,10 @@ class TestNetworkManagerApplyCellName(unittest.TestCase):
mock_run.return_value = MagicMock(returncode=0) mock_run.return_value = MagicMock(returncode=0)
result = self.nm.apply_cell_name('mycell', 'newcell') result = self.nm.apply_cell_name('mycell', 'newcell')
zone = open(os.path.join(self.data_dir, 'dns', 'cell.zone')).read() zone = open(os.path.join(self.data_dir, 'dns', 'cell.zone')).read()
self.assertIn('newcell IN A 172.20.0.2', zone) self.assertIn('newcell', zone)
self.assertNotIn('mycell IN A', 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'])) self.assertIn('cell-dns', ' '.join(result['restarted']))
@patch('subprocess.run') @patch('subprocess.run')