Merge "Modify the judgment method of CIDR and Add utests"

This commit is contained in:
Zuul 2019-01-04 17:02:52 +00:00 committed by Gerrit Code Review
commit d1d6b4e7bc
3 changed files with 57 additions and 1 deletions

View File

@ -599,7 +599,7 @@ def validate_hostroutes(data, valid_values=None):
msg = _verify_dict_keys(expected_keys, hostroute)
if msg:
return msg
msg = validate_subnet(hostroute['destination'])
msg = validate_route_cidr(hostroute['destination'])
if msg:
return msg
msg = validate_ip_address(hostroute['nexthop'])
@ -664,6 +664,30 @@ def validate_subnet(data, valid_values=None):
return msg
def validate_route_cidr(data, valid_values=None):
"""Validate data is a proper CIDR string.
:param data: The data to validate.
:param valid_values: Not used!
:returns: None if data is valid CIDR. Otherwise a human
readable message as to why data is invalid.
"""
msg = None
try:
net = netaddr.IPNetwork(validate_no_whitespace(data))
if '/' not in data or (net.network != net.ip):
msg = _("'%(data)s' is not a recognized CIDR,"
" '%(cidr)s' is recommended") % {"data": data,
"cidr": net.cidr}
else:
return
except Exception:
msg = _("'%s' is not a valid CIDR") % data
if msg:
LOG.debug(msg)
return msg
def validate_subnet_or_none(data, valid_values=None):
"""Validate data is a valid subnet address string or None.

View File

@ -723,6 +723,31 @@ class TestAttributeValidation(base.BaseTestCase):
def test_validate_subnet(self):
self._test_validate_subnet(validators.validate_subnet)
def test_validate_route_cidr(self):
# Valid - CIDR
cidr = "10.0.0.0/8"
msg = validators.validate_route_cidr(cidr, None)
self.assertIsNone(msg)
# Valid - CIDR
cidr = "192.168.1.1/32"
msg = validators.validate_route_cidr(cidr, None)
self.assertIsNone(msg)
# Invalid - CIDR
cidr = "192.168.1.1/8"
msg = validators.validate_route_cidr(cidr, None)
error = _("'%(data)s' is not a recognized CIDR,"
" '%(cidr)s' is recommended") % {"data": cidr,
"cidr": "192.0.0.0/8"}
self.assertEqual(error, msg)
# Invalid - CIDR format error
cidr = 'invalid'
msg = validators.validate_route_cidr(cidr, None)
error = "'%s' is not a valid CIDR" % cidr
self.assertEqual(error, msg)
def test_validate_subnet_or_none(self):
self._test_validate_subnet(validators.validate_subnet_or_none,
allow_none=True)

View File

@ -0,0 +1,7 @@
---
features:
- |
A new function named ``validate_route_cidr`` was introduced
which is used to validate if ``destination`` of ``routes``
and ``destination`` of ``host_routes`` is a network address
of a destination subnet or an IP address of a destination.