diff --git a/neutron/api/v2/attributes.py b/neutron/api/v2/attributes.py index f5a23298b..852263ce0 100644 --- a/neutron/api/v2/attributes.py +++ b/neutron/api/v2/attributes.py @@ -153,12 +153,19 @@ def _validate_no_whitespace(data): def _validate_mac_address(data, valid_values=None): + valid_mac = False try: - netaddr.EUI(_validate_no_whitespace(data)) + valid_mac = netaddr.valid_mac(_validate_no_whitespace(data)) except Exception: - msg = _("'%s' is not a valid MAC address") % data - LOG.debug(msg) - return msg + pass + finally: + # TODO(arosen): The code in this file should be refactored + # so it catches the correct exceptions. _validate_no_whitespace + # raises AttributeError if data is None. + if valid_mac is False: + msg = _("'%s' is not a valid MAC address") % data + LOG.debug(msg) + return msg def _validate_mac_address_or_none(data, valid_values=None): diff --git a/neutron/tests/unit/test_attributes.py b/neutron/tests/unit/test_attributes.py index 53edc2693..0282cdc0c 100644 --- a/neutron/tests/unit/test_attributes.py +++ b/neutron/tests/unit/test_attributes.py @@ -176,14 +176,19 @@ class TestAttributes(base.BaseTestCase): mac_addr = "ffa:16:3e:4f:00:00" msg = validator(mac_addr) - self.assertEqual(msg, "'%s' is not a valid MAC address" % mac_addr) + err_msg = "'%s' is not a valid MAC address" + self.assertEqual(msg, err_msg % mac_addr) + + mac_addr = "123" + msg = validator(mac_addr) + self.assertEqual(msg, err_msg % mac_addr) mac_addr = None msg = validator(mac_addr) if allow_none: self.assertIsNone(msg) else: - self.assertEqual(msg, "'None' is not a valid MAC address") + self.assertEqual(msg, err_msg % mac_addr) def test_validate_mac_address(self): self._test_validate_mac_address(attributes._validate_mac_address)