Merge "Fix _validate_mac_address method"

This commit is contained in:
Jenkins 2014-04-08 06:05:09 +00:00 committed by Gerrit Code Review
commit cb5381fb65
2 changed files with 18 additions and 6 deletions

View File

@ -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):

View File

@ -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)