From 0ecceacb3ec4527f66573f7f093ebd6175d84fed Mon Sep 17 00:00:00 2001 From: Nate Johnston Date: Wed, 24 Oct 2018 13:44:18 -0400 Subject: [PATCH] Fix random_mac_generator to make proper EUI64s The initial implementation of random_mac_generator created MAC addresses that were one octet too long - the MAC addresses would have seven octets instead of the proper EUI64-standard six. This resolved testing issues in the consuming patch in Neutron (see the change referenced in the Needed-By line). A test is added that replicates the conditions of the failure. In the failed tests, the base mac '12:34:56:78' was instead parsed so it became '12:34:56:7:8'; when the code went to add 2 octets to the result, there were too many octets to form a valid EUI64 MAC address. Implements: blueprint speed-up-neutron-bulk-creation Needed-By: https://review.openstack.org/584061 Change-Id: I9a2c40709db9204c7686500deac9ee8e8471b1bc --- neutron_lib/tests/unit/utils/test_net.py | 8 ++++++++ neutron_lib/utils/net.py | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/neutron_lib/tests/unit/utils/test_net.py b/neutron_lib/tests/unit/utils/test_net.py index 0d88e57e9..0a8c36721 100644 --- a/neutron_lib/tests/unit/utils/test_net.py +++ b/neutron_lib/tests/unit/utils/test_net.py @@ -77,6 +77,14 @@ class TestRandomMacGenerator(base.BaseTestCase): self.assertEqual(['aa:bb:00:a2:a2:a2'], list(generator)) mock_rnd.assert_called_with(8) + @mock.patch.object(random, 'getrandbits', return_value=0xa2) + def test_short_supplied_mac(self, mock_rnd): + mac_base = '12:34:56:78' + mac = mac_base.split(':') + generator = itertools.islice(net.random_mac_generator(mac), 1) + self.assertEqual(['12:34:56:78:a2:a2'], list(generator)) + mock_rnd.assert_called_with(8) + class TestPortDeviceOwner(base.BaseTestCase): diff --git a/neutron_lib/utils/net.py b/neutron_lib/utils/net.py index 9c5f36357..33870e24d 100644 --- a/neutron_lib/utils/net.py +++ b/neutron_lib/utils/net.py @@ -56,7 +56,7 @@ def random_mac_generator(base_mac): fixed = list(base_mac[0:3]) to_generate = 3 if base_mac[3] != '00': - fixed += base_mac[3] + fixed = list(base_mac[0:4]) to_generate = 2 beginning = ':'.join(fixed) + ':'