From ee0f5b2ab27c828cfedb771735d237a968423da2 Mon Sep 17 00:00:00 2001 From: Trevor McCasland Date: Fri, 28 Oct 2016 02:27:15 -0500 Subject: [PATCH] Move get_random_mac into neutron-lib get_random_mac is used by networking-ovn, tap-as-a-service, neutron-vpnaas, and vmware-nsx, so we should move it into the neutron-lib repository. Change-Id: I391300e6f1d04b2413084bccdbacb038071c4509 --- neutron_lib/tests/unit/utils/test_net.py | 19 +++++++++++++++++++ neutron_lib/utils/net.py | 15 +++++++++++++++ .../move-get-random-mac-98f47d81cb34483d.yaml | 4 ++++ 3 files changed, 38 insertions(+) create mode 100644 releasenotes/notes/move-get-random-mac-98f47d81cb34483d.yaml diff --git a/neutron_lib/tests/unit/utils/test_net.py b/neutron_lib/tests/unit/utils/test_net.py index f832cbed8..d051765fa 100644 --- a/neutron_lib/tests/unit/utils/test_net.py +++ b/neutron_lib/tests/unit/utils/test_net.py @@ -27,3 +27,22 @@ class TestGetHostname(base.BaseTestCase): self.assertEqual('fake-host-name', net.get_hostname()) mock_gethostname.assert_called_once_with() + + +class TestGetRandomMac(base.BaseTestCase): + + def _test_get_random_mac(self, base_mac, startswith_len): + random_mac = net.get_random_mac(base_mac.split(':')) + self.assertEqual(base_mac[:startswith_len], + random_mac[:startswith_len]) + self.assertEqual(len(base_mac), len(random_mac)) + for i in random_mac.split(':'): + int(i, 16) + + def test_get_random_mac_with_three_octets(self): + base_mac = 'fa:16:3e:00:00:00' + self._test_get_random_mac(base_mac, 9) + + def test_get_random_mac_with_four_octets(self): + base_mac = 'fa:16:3e:fe:00:00' + self._test_get_random_mac(base_mac, 12) diff --git a/neutron_lib/utils/net.py b/neutron_lib/utils/net.py index 2749907dc..3dd5167f4 100644 --- a/neutron_lib/utils/net.py +++ b/neutron_lib/utils/net.py @@ -11,6 +11,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import random import socket @@ -20,3 +21,17 @@ def get_hostname(): :returns: The hostname of the system. """ return socket.gethostname() + + +def get_random_mac(base_mac): + """Get a random MAC address string of the specified base format. + + :param base_mac: The base MAC address format for the MAC address string. + :returns: The MAC address string. + """ + mac = [int(base_mac[0], 16), int(base_mac[1], 16), + int(base_mac[2], 16), random.randint(0x00, 0xff), + random.randint(0x00, 0xff), random.randint(0x00, 0xff)] + if base_mac[3] != '00': + mac[3] = int(base_mac[3], 16) + return ':'.join(["%02x" % x for x in mac]) diff --git a/releasenotes/notes/move-get-random-mac-98f47d81cb34483d.yaml b/releasenotes/notes/move-get-random-mac-98f47d81cb34483d.yaml new file mode 100644 index 000000000..c213235b3 --- /dev/null +++ b/releasenotes/notes/move-get-random-mac-98f47d81cb34483d.yaml @@ -0,0 +1,4 @@ +--- +features: + - The ``get_random_mac`` utility function from ``neutron.common.utils`` is + now in ``neutron_lib.utils.net`` with the same name, ``get_random_mac``.