Commit 98659412 authored by ale's avatar ale

Fix expansion of empty lists in list context

When we are expanding a list in a list evaluation context, and the
list is empty, we'd rather generate no records at all. Previous
behaviour would simply have left the $IDENTIFIER in place unchanged.
parent dda4f7ec
Pipeline #3464 passed with stages
in 1 minute and 28 seconds
......@@ -152,7 +152,10 @@ autistici.org:
```
will result in a zone containing an A record and an MX record for each
entry defined in *SERVERS*.
entry defined in *SERVERS*. Note that in a list context, expanding a
variable which is an empty list will result in zero elements being
generated (i.e., in the above example, if *SERVERS* is empty, no A or
MX records for the top-level zone will be generated).
## Usage
......
......@@ -188,7 +188,7 @@ class ZoneParser(object):
_merge_zones(out, self._resolve_references(
base, self.zones[base]), replace)
self._expand_vars(out)
out = self._expand_vars(out)
out.pop('EXTENDS', None)
out.pop('REPLACES', None)
self.resolved[zone_name] = out
......@@ -202,19 +202,25 @@ class ZoneParser(object):
m = var_re.search(s)
if m:
try:
return self.config[m.group(1)]
return self.config[m.group(1)], True
except KeyError:
raise Error('undefined config variable "%s"' % m.group(1))
return None, False
def _expand_value(value):
var_value = _get_var(value)
if not var_value:
var_value, has_var = _get_var(value)
if not has_var:
print('EXPAND_VALUE(%s) -> not has_var' % value)
return value
elif not var_value:
print('EXPAND_VALUE(%s) -> not var_value' % value)
return []
elif isinstance(var_value, list):
return [var_re.sub(x, value) for x in var_value]
else:
return var_re.sub(var_value, value)
out_zone_data = {}
for key, value in zone_data.iteritems():
if isinstance(value, basestring):
value = _expand_value(value)
......@@ -222,12 +228,17 @@ class ZoneParser(object):
tmp = []
for v in value:
tmpv = _expand_value(v)
if isinstance(tmpv, list):
tmp.extend(tmpv)
else:
tmp.append(tmpv)
if tmpv:
if isinstance(tmpv, list):
tmp.extend(tmpv)
else:
tmp.append(tmpv)
value = tmp
zone_data[key] = value
# Something that expands to an empty value must be
# removed from the zone.
if value:
out_zone_data[key] = value
return out_zone_data
def walk(root_dir):
......
......@@ -99,8 +99,17 @@ autistici.org:
''',
]
TEST_DATA_EMPTY_VAR = [
'''
autistici.org:
banana:
- MX 10 $EMPTY
''',
]
TEST_CONFIG = {
'FRONTENDS': ['82.94.249.234', '82.221.99.153'],
'EMPTY': [],
}
......@@ -175,6 +184,17 @@ www IN A 82.94.249.234
num_ns = len(filter(lambda x: x.startswith('NS '), result[0][1]['_']))
self.assertEquals(num_ns, 2)
def test_expand_empty_variable(self):
self.zp.load(_loadyaml(TEST_DATA_EMPTY_VAR))
result = list(self.zp.render())
self.assertTrue(result)
self.assertEquals('autistici.org', result[0][0])
# The record that expanded an empty list should not be
# present at all in the zone.
self.assertTrue('banana' not in result[0][1],
'Bad zone with empty record:\n%s' % result[0][1])
def test_recursive_extend(self):
self.zp.load(_loadyaml(TEST_DATA_4))
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