diff --git a/neutron/api/v2/attributes.py b/neutron/api/v2/attributes.py index d6842f21cb..512255f0ca 100644 --- a/neutron/api/v2/attributes.py +++ b/neutron/api/v2/attributes.py @@ -267,11 +267,10 @@ def _validate_subnet(data, valid_values=None): msg = None try: net = netaddr.IPNetwork(_validate_no_whitespace(data)) - cidr = str(net.cidr) - if (cidr != data): + if ('/' not in data or net.network != net.ip): msg = _("'%(data)s' isn't a recognized IP subnet cidr," " '%(cidr)s' is recommended") % {"data": data, - "cidr": cidr} + "cidr": net.cidr} else: return except Exception: diff --git a/neutron/tests/unit/test_attributes.py b/neutron/tests/unit/test_attributes.py index a763b54d17..800c1f18c9 100644 --- a/neutron/tests/unit/test_attributes.py +++ b/neutron/tests/unit/test_attributes.py @@ -397,6 +397,36 @@ class TestAttributes(base.BaseTestCase): None) self.assertIsNone(msg) + # Valid - uncompressed ipv6 address + cidr = "fe80:0:0:0:0:0:0:0/128" + msg = attributes._validate_subnet(cidr, + None) + self.assertIsNone(msg) + + # Valid - ipv6 address with multiple consecutive zero + cidr = "2001:0db8:0:0:1::1/128" + msg = attributes._validate_subnet(cidr, + None) + self.assertIsNone(msg) + + # Valid - ipv6 address with multiple consecutive zero + cidr = "2001:0db8::1:0:0:1/128" + msg = attributes._validate_subnet(cidr, + None) + self.assertIsNone(msg) + + # Valid - ipv6 address with multiple consecutive zero + cidr = "2001::0:1:0:0:1100/120" + msg = attributes._validate_subnet(cidr, + None) + self.assertIsNone(msg) + + # Valid - abbreviated ipv4 address + cidr = "10/24" + msg = attributes._validate_subnet(cidr, + None) + self.assertIsNone(msg) + # Invalid - IPv4 missing mask cidr = "10.0.2.0" msg = attributes._validate_subnet(cidr, diff --git a/neutron/tests/unit/test_extension_firewall.py b/neutron/tests/unit/test_extension_firewall.py index 7550a706fe..486f20f03b 100644 --- a/neutron/tests/unit/test_extension_firewall.py +++ b/neutron/tests/unit/test_extension_firewall.py @@ -531,10 +531,7 @@ class TestFirewallAttributeValidators(base.BaseTestCase): # Valid - IPv6 with final octets cidr = "fe80::0/24" msg = firewall._validate_ip_or_subnet_or_none(cidr, None) - self.assertEqual(msg, ("'%s' is not a valid IP address and " - "'%s' isn't a recognized IP subnet cidr," - " 'fe80::/24' is recommended") % (cidr, - cidr)) + self.assertIsNone(msg) cidr = "fe80::" msg = firewall._validate_ip_or_subnet_or_none(cidr, None)