Merge "Use function is_valid_mac from oslo.utils"
This commit is contained in:
commit
66c4f5502b
|
@ -27,6 +27,7 @@ import tempfile
|
||||||
|
|
||||||
from oslo_concurrency import processutils
|
from oslo_concurrency import processutils
|
||||||
from oslo_log import log as logging
|
from oslo_log import log as logging
|
||||||
|
from oslo_utils import netutils
|
||||||
import six
|
import six
|
||||||
|
|
||||||
from magnum.common import exception
|
from magnum.common import exception
|
||||||
|
@ -100,22 +101,6 @@ def trycmd(*args, **kwargs):
|
||||||
return processutils.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):
|
def validate_and_normalize_mac(address):
|
||||||
"""Validate a MAC address and return normalized form.
|
"""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.
|
: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)
|
raise exception.InvalidMAC(mac=address)
|
||||||
return address.lower()
|
return address.lower()
|
||||||
|
|
||||||
|
|
|
@ -21,6 +21,7 @@ import tempfile
|
||||||
|
|
||||||
import mock
|
import mock
|
||||||
from oslo_concurrency import processutils
|
from oslo_concurrency import processutils
|
||||||
|
from oslo_utils import netutils
|
||||||
|
|
||||||
from magnum.common import exception
|
from magnum.common import exception
|
||||||
from magnum.common import utils
|
from magnum.common import utils
|
||||||
|
@ -183,26 +184,15 @@ grep foo
|
||||||
utils.execute('foo', run_as_root=False)
|
utils.execute('foo', run_as_root=False)
|
||||||
execute_mock.assert_called_once_with('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):
|
def test_validate_and_normalize_mac(self):
|
||||||
mac = 'AA:BB:CC:DD:EE:FF'
|
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
|
m_mock.return_value = True
|
||||||
self.assertEqual(mac.lower(),
|
self.assertEqual(mac.lower(),
|
||||||
utils.validate_and_normalize_mac(mac))
|
utils.validate_and_normalize_mac(mac))
|
||||||
|
|
||||||
def test_validate_and_normalize_mac_invalid_format(self):
|
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
|
m_mock.return_value = False
|
||||||
self.assertRaises(exception.InvalidMAC,
|
self.assertRaises(exception.InvalidMAC,
|
||||||
utils.validate_and_normalize_mac, 'invalid-mac')
|
utils.validate_and_normalize_mac, 'invalid-mac')
|
||||||
|
|
Loading…
Reference in New Issue