From 7da36840989c29d27903283785fdc7b8723b5ecc Mon Sep 17 00:00:00 2001 From: longqianzhao Date: Sat, 8 Dec 2018 00:23:37 +0800 Subject: [PATCH] Modify the judgment method of CIDR and Add utests The routing table could not add information, because the original method could not identify the invalid CIDR. This patch modify the judgement method. It will help the router working normally. Co-Authored-By: Allain Legacy Change-Id: I4db2e35303492aeda7e14cf9d525cc9b4a54ac36 Closes-Bug: #1805991 Story: 2004567 --- neutron_lib/api/validators/__init__.py | 26 ++++++++++++++++++- .../unit/api/validators/test_validators.py | 25 ++++++++++++++++++ .../add-validate-cidr-848c9171dcbcf57c.yaml | 7 +++++ 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 releasenotes/notes/add-validate-cidr-848c9171dcbcf57c.yaml diff --git a/neutron_lib/api/validators/__init__.py b/neutron_lib/api/validators/__init__.py index fa9a608d4..3d7c614cc 100644 --- a/neutron_lib/api/validators/__init__.py +++ b/neutron_lib/api/validators/__init__.py @@ -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. diff --git a/neutron_lib/tests/unit/api/validators/test_validators.py b/neutron_lib/tests/unit/api/validators/test_validators.py index 62c43ec45..3bfa17802 100644 --- a/neutron_lib/tests/unit/api/validators/test_validators.py +++ b/neutron_lib/tests/unit/api/validators/test_validators.py @@ -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) diff --git a/releasenotes/notes/add-validate-cidr-848c9171dcbcf57c.yaml b/releasenotes/notes/add-validate-cidr-848c9171dcbcf57c.yaml new file mode 100644 index 000000000..e18e3b744 --- /dev/null +++ b/releasenotes/notes/add-validate-cidr-848c9171dcbcf57c.yaml @@ -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.