Use function is_valid_mac from oslo.utils
Oslo.utils 3.17 provides same function, just use it. Change-Id: I5bdcafaa9942e63c6383ffc399204378b2ae67c8
This commit is contained in:
parent
68af88a737
commit
024d3d2089
|
@ -27,6 +27,7 @@ import tempfile
|
|||
|
||||
from oslo_concurrency import processutils
|
||||
from oslo_log import log as logging
|
||||
from oslo_utils import netutils
|
||||
import six
|
||||
|
||||
from magnum.common import exception
|
||||
|
@ -100,22 +101,6 @@ def trycmd(*args, **kwargs):
|
|||
return processutils.trycmd(*args, **kwargs)
|
||||
|
||||
|
||||
def is_valid_mac(address):
|
||||
"""Verify the format of a MAC address.
|
||||
|
||||
Check if a MAC address is valid and contains six octets. Accepts
|
||||
colon-separated format only.
|
||||
|
||||
:param address: MAC address to be validated.
|
||||
:returns: True if valid. False if not.
|
||||
|
||||
"""
|
||||
m = "[0-9a-f]{2}(:[0-9a-f]{2}){5}$"
|
||||
if isinstance(address, six.string_types) and re.match(m, address.lower()):
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def validate_and_normalize_mac(address):
|
||||
"""Validate a MAC address and return normalized form.
|
||||
|
||||
|
@ -127,7 +112,7 @@ def validate_and_normalize_mac(address):
|
|||
:raises: InvalidMAC If the MAC address is not valid.
|
||||
|
||||
"""
|
||||
if not is_valid_mac(address):
|
||||
if not netutils.is_valid_mac(address):
|
||||
raise exception.InvalidMAC(mac=address)
|
||||
return address.lower()
|
||||
|
||||
|
|
|
@ -21,6 +21,7 @@ import tempfile
|
|||
|
||||
import mock
|
||||
from oslo_concurrency import processutils
|
||||
from oslo_utils import netutils
|
||||
|
||||
from magnum.common import exception
|
||||
from magnum.common import utils
|
||||
|
@ -183,26 +184,15 @@ grep foo
|
|||
utils.execute('foo', run_as_root=False)
|
||||
execute_mock.assert_called_once_with('foo', run_as_root=False)
|
||||
|
||||
def test_is_valid_mac(self):
|
||||
self.assertTrue(utils.is_valid_mac("52:54:00:cf:2d:31"))
|
||||
self.assertTrue(utils.is_valid_mac(u"52:54:00:cf:2d:31"))
|
||||
self.assertFalse(utils.is_valid_mac("127.0.0.1"))
|
||||
self.assertFalse(utils.is_valid_mac("not:a:mac:address"))
|
||||
self.assertFalse(utils.is_valid_mac("52-54-00-cf-2d-31"))
|
||||
self.assertFalse(utils.is_valid_mac("aa bb cc dd ee ff"))
|
||||
self.assertTrue(utils.is_valid_mac("AA:BB:CC:DD:EE:FF"))
|
||||
self.assertFalse(utils.is_valid_mac("AA BB CC DD EE FF"))
|
||||
self.assertFalse(utils.is_valid_mac("AA-BB-CC-DD-EE-FF"))
|
||||
|
||||
def test_validate_and_normalize_mac(self):
|
||||
mac = 'AA:BB:CC:DD:EE:FF'
|
||||
with mock.patch.object(utils, 'is_valid_mac') as m_mock:
|
||||
with mock.patch.object(netutils, 'is_valid_mac') as m_mock:
|
||||
m_mock.return_value = True
|
||||
self.assertEqual(mac.lower(),
|
||||
utils.validate_and_normalize_mac(mac))
|
||||
|
||||
def test_validate_and_normalize_mac_invalid_format(self):
|
||||
with mock.patch.object(utils, 'is_valid_mac') as m_mock:
|
||||
with mock.patch.object(netutils, 'is_valid_mac') as m_mock:
|
||||
m_mock.return_value = False
|
||||
self.assertRaises(exception.InvalidMAC,
|
||||
utils.validate_and_normalize_mac, 'invalid-mac')
|
||||
|
|
Loading…
Reference in New Issue