revert get_random_mac behavior from review 400408

Commit If2539f94b5479f0d6afa64c973082cbe8c5309ac made get_random_mac
more versatile. However, in the process it introduced incompatibilities
with the current behavior of randomizing mac as outlined in the
respective bug report.

This patch reverts the logic of get_random_mac back to its original
behavior for backwards compatibility as we suspect no one is relying
on the new behavior from If2539f94b5479f0d6afa64c973082cbe8c5309ac.

Change-Id: I047ff3e17c19fc80a47d3c73ee955103a71d2b30
Closes-Bug: #1720046
This commit is contained in:
Boden R 2017-10-05 11:00:14 -06:00
parent 772a0e612c
commit 5deda57e17
3 changed files with 21 additions and 30 deletions

View File

@ -33,34 +33,16 @@ class TestGetHostname(base.BaseTestCase):
class TestGetRandomMac(base.BaseTestCase):
def test_full_prefix_does_nothing(self):
mac = net.get_random_mac(['aa', 'bb', 'cc', 'dd', 'ee', 'ff'])
self.assertEqual('aa:bb:cc:dd:ee:ff', mac)
@mock.patch.object(random, 'randint', side_effect=[0x11])
def test_5_octets_prefix_replaces_1_part(self, mock_rnd):
mac = net.get_random_mac(['aa', 'bb', 'cc', 'dd', 'ee', '00'])
self.assertEqual('aa:bb:cc:dd:ee:11', mac)
mock_rnd.assert_called_with(0x00, 0xff)
@mock.patch.object(random, 'randint',
side_effect=[0x01, 0x02, 0x03, 0x04, 0x05])
def test_1_octets_prefix_replaces_5_parts(self, mock_rnd):
mac = net.get_random_mac(['aa', '00', '00', '00', '00', '00'])
self.assertEqual('aa:01:02:03:04:05', mac)
@mock.patch.object(random, 'randint', return_value=0xa2)
def test_first_4_octets_unchanged(self, mock_rnd):
mac = net.get_random_mac(['aa', 'bb', '00', 'dd', 'ee', 'ff'])
self.assertEqual('aa:bb:00:dd:a2:a2', mac)
mock_rnd.assert_called_with(0x00, 0xff)
@mock.patch.object(random, 'randint', return_value=0xa2)
def test_no_prefix_replaces_all_parts(self, mock_rnd):
mac = net.get_random_mac(['00', '00', '00', '00', '00', '00'])
self.assertEqual('a2:a2:a2:a2:a2:a2', mac)
def test_first_4th_octet_generated(self, mock_rnd):
mac = net.get_random_mac(['aa', 'bb', 'cc', '00', 'ee', 'ff'])
self.assertEqual('aa:bb:cc:a2:a2:a2', mac)
mock_rnd.assert_called_with(0x00, 0xff)

View File

@ -28,16 +28,19 @@ def get_hostname():
def get_random_mac(base_mac):
"""Get a random MAC address string of the specified base format.
Any part that is '00' will be randomized
The first 3 octets will remain unchanged. If the 4th octet is not
00, it will also be used. The others will be randomly generated.
:param base_mac: Base MAC address represented by an array of 6 strings/int
:returns: The MAC address string.
"""
return ':'.join(
"{:02x}".format(random.randint(0x00, 0xff))if p == '00' else p
for p in base_mac
)
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])
def is_port_trusted(port):

View File

@ -0,0 +1,6 @@
---
fixes:
- Bug `1720046 <https://bugs.launchpad.net/neutron/+bug/1720046>`_ is fixed
by reverting the logic of ``neutron_lib.utils.net.get_random_mac`` to its
original behavior from commit `If2539f94b5479f0d6afa64c973082cbe8c5309ac
<https://review.openstack.org/#/c/400408/>`_.