Commit d85abf0a authored by ale's avatar ale

Fix zone comparison function

parent 33ba1e70
......@@ -27,13 +27,22 @@ def _is_ip4(s):
return _is_ip(s, socket.AF_INET)
_COMMENTS_RE = re.compile(r';[^\n]*$')
_SPACES_RE = re.compile(r'\s+')
_NOSERIAL_RE = re.compile(r'^\s*(\d+)\s*;\s*Serial$', re.MULTILINE)
_NOSERIAL_RE = re.compile(r'^\s*(\d+)\s*;\s*Serial$')
def _zonecmp(a, b):
"""Check if two zones are equal, ignoring Serial and whitespaces."""
return (_SPACES_RE.sub(' ', _NOSERIAL_RE.sub('', a)) ==
_SPACES_RE.sub(' ', _NOSERIAL_RE.sub('', b)))
def zonecmp(a, b):
"""Check if two zones are equal.
Ignores SOA serial number, comments, and whitespace.
"""
def _lines(z):
return filter(None, [
_COMMENTS_RE.sub('', _SPACES_RE.sub(' ', _NOSERIAL_RE.sub('', x)))
for x in z.split('\n')])
aa = ''.join(_lines(a))
bb = ''.join(_lines(b))
return aa == bb
def _parse_record(value):
......@@ -207,7 +216,7 @@ class ZoneWriter(object):
def write_if_changed(self, data, dst):
if os.path.exists(dst):
with open(dst, 'r') as fd:
if _zonecmp(fd.read(), data):
if zonecmp(fd.read(), data):
return False
if not self.dry_run:
with open(dst, 'w') as fd:
......
......@@ -87,6 +87,29 @@ class ZoneParserTest(unittest.TestCase):
sorted(result[0][1]['www']),
sorted(TEST_CONFIG['FRONTENDS']))
def test_zone_output(self):
self.zp.load(_loadyaml(TEST_DATA))
result = list(self.zp.render())
zone_data = result[0][2]
expected_data = '''; Comments should be ignored
$TTL 3600
@ IN SOA localhost. hostmaster.autistici.org. (
1521885904 ; Serial
43200
3600
2419200
3600 )
IN A 82.221.99.153
IN A 82.94.249.234
onion IN TXT "blahblah.onion."
www IN A 82.221.99.153
www IN A 82.94.249.234
'''
self.assertTrue(
zonecmp(zone_data, expected_data),
'Bad zone data: got: %s, expected: %s' % (
zone_data, expected_data))
def test_merge(self):
self.zp.load(_loadyaml(TEST_DATA_2))
result = list(self.zp.render())
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment