Make the get_random_mac more versatile
The past version was not well documented and could use a little dusting. This new version offers a custom OUI size. This is a random code enhancement. Change-Id: If2539f94b5479f0d6afa64c973082cbe8c5309ac
This commit is contained in:
parent
ee0f5b2ab2
commit
4a78c722aa
@ -11,6 +11,7 @@
|
|||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
|
import random
|
||||||
import socket
|
import socket
|
||||||
|
|
||||||
import mock
|
import mock
|
||||||
@ -31,18 +32,32 @@ class TestGetHostname(base.BaseTestCase):
|
|||||||
|
|
||||||
class TestGetRandomMac(base.BaseTestCase):
|
class TestGetRandomMac(base.BaseTestCase):
|
||||||
|
|
||||||
def _test_get_random_mac(self, base_mac, startswith_len):
|
def test_full_prefix_does_nothing(self):
|
||||||
random_mac = net.get_random_mac(base_mac.split(':'))
|
mac = net.get_random_mac(['aa', 'bb', 'cc', 'dd', 'ee', 'ff'])
|
||||||
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):
|
self.assertEqual('aa:bb:cc:dd:ee:ff', mac)
|
||||||
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):
|
@mock.patch.object(random, 'randint', side_effect=[0x11])
|
||||||
base_mac = 'fa:16:3e:fe:00:00'
|
def test_5_octets_prefix_replaces_1_part(self, mock_rnd):
|
||||||
self._test_get_random_mac(base_mac, 12)
|
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_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)
|
||||||
|
|
||||||
|
mock_rnd.assert_called_with(0x00, 0xff)
|
||||||
|
@ -26,12 +26,13 @@ def get_hostname():
|
|||||||
def get_random_mac(base_mac):
|
def get_random_mac(base_mac):
|
||||||
"""Get a random MAC address string of the specified base format.
|
"""Get a random MAC address string of the specified base format.
|
||||||
|
|
||||||
:param base_mac: The base MAC address format for the MAC address string.
|
Any part that is '00' will be randomized
|
||||||
|
|
||||||
|
:param base_mac: Base MAC address represented by an array of 6 strings/int
|
||||||
:returns: 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),
|
return ':'.join(
|
||||||
random.randint(0x00, 0xff), random.randint(0x00, 0xff)]
|
"{:02x}".format(random.randint(0x00, 0xff))if p == '00' else p
|
||||||
if base_mac[3] != '00':
|
for p in base_mac
|
||||||
mac[3] = int(base_mac[3], 16)
|
)
|
||||||
return ':'.join(["%02x" % x for x in mac])
|
|
||||||
|
Loading…
Reference in New Issue
Block a user