use neutron_lib's get_random_mac

Neutron-lib 1.1.0 is now out and contains the get_random_mac
definition[1]. This patch moves neutron references over to
the neutron-lib version.

NeutronLibImpact

[1] ee0f5b2ab27c828cfedb771735d237a968423da2

Change-Id: I28a2a1d85a85461f7a4344b86d18da7f68066c95
This commit is contained in:
Trevor McCasland 2017-03-07 09:04:49 -06:00
parent 97db8aab07
commit ae3b344be3
14 changed files with 53 additions and 40 deletions

View File

@ -36,6 +36,7 @@ from eventlet.green import subprocess
import netaddr
from neutron_lib import constants as n_const
from neutron_lib.utils import helpers
from neutron_lib.utils import net
from oslo_concurrency import lockutils
from oslo_config import cfg
from oslo_db import exception as db_exc
@ -107,13 +108,13 @@ def log_opt_values(log):
cfg.CONF.log_opt_values(log, logging.DEBUG)
@removals.remove(
message="Use get_random_mac from neutron_lib.utils.net",
version="Pike",
removal_version="Queens"
)
def get_random_mac(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])
return net.get_random_mac(base_mac)
def get_dhcp_agent_device_id(network_id, host):

View File

@ -18,6 +18,7 @@ import functools
from neutron_lib.api import validators
from neutron_lib import constants
from neutron_lib import exceptions as n_exc
from neutron_lib.utils import net
from oslo_config import cfg
from oslo_log import log as logging
from sqlalchemy.orm import exc
@ -25,7 +26,6 @@ from sqlalchemy.orm import exc
from neutron.api.v2 import attributes
from neutron.common import constants as n_const
from neutron.common import exceptions
from neutron.common import utils
from neutron.db import _utils as db_utils
from neutron.db import common_db_mixin
from neutron.db import models_v2
@ -81,7 +81,7 @@ class DbBasePluginCommon(common_db_mixin.CommonDbMixin):
@staticmethod
def _generate_mac():
return utils.get_random_mac(cfg.CONF.base_mac.split(':'))
return net.get_random_mac(cfg.CONF.base_mac.split(':'))
def _is_mac_in_use(self, context, network_id, mac_address):
return bool(context.session.query(models_v2.Port).

View File

@ -16,6 +16,7 @@
from neutron_lib import constants
from neutron_lib import exceptions as n_exc
from neutron_lib.plugins import directory
from neutron_lib.utils import net
from oslo_config import cfg
from oslo_db import exception as db_exc
from oslo_log import helpers as log_helpers
@ -109,7 +110,7 @@ class DVRDbMixin(ext_dvr.DVRMacAddressPluginBase):
@db_api.retry_if_session_inactive()
def _create_dvr_mac_address_retry(self, context, host, base_mac):
with db_api.context_manager.writer.using(context):
mac_address = utils.get_random_mac(base_mac)
mac_address = net.get_random_mac(base_mac)
dvr_mac_binding = dvr_models.DistributedVirtualRouterMacAddress(
host=host, mac_address=mac_address)
context.session.add(dvr_mac_binding)

View File

@ -19,6 +19,7 @@ import random
import eventlet
import mock
from neutron_lib import constants as n_const
from neutron_lib.utils import net
from oslo_config import cfg
from oslo_utils import uuidutils
@ -169,7 +170,7 @@ class OVSAgentTestFramework(base.BaseOVSLinuxTestCase):
def _create_test_port_dict(self):
return {'id': uuidutils.generate_uuid(),
'mac_address': utils.get_random_mac(
'mac_address': net.get_random_mac(
'fa:16:3e:00:00:00'.split(':')),
'fixed_ips': [{
'ip_address': '10.%d.%d.%d' % (

View File

@ -14,6 +14,7 @@
import functools
from neutron_lib.utils import net
from oslo_config import cfg
from oslo_utils import uuidutils
import testtools
@ -30,7 +31,7 @@ from neutron.tests.functional import base
class InterfaceDriverTestCaseMixin(object):
def _test_mtu_set_after_action(self, device_name, br_name, namespace,
action=None):
mac_address = utils.get_random_mac('fa:16:3e:00:00:00'.split(':'))
mac_address = net.get_random_mac('fa:16:3e:00:00:00'.split(':'))
plug = functools.partial(
self.interface.plug,
@ -92,7 +93,7 @@ class OVSInterfaceDriverTestCase(linux_base.BaseOVSLinuxTestCase,
def test_plug_succeeds(self):
device_name = utils.get_rand_name()
mac_address = utils.get_random_mac('fa:16:3e:00:00:00'.split(':'))
mac_address = net.get_random_mac('fa:16:3e:00:00:00'.split(':'))
namespace = self.useFixture(net_helpers.NamespaceFixture()).name
self.assertFalse(self.bridge.get_port_name_list())

View File

@ -17,6 +17,7 @@ import collections
import netaddr
from neutron_lib import constants
from neutron_lib.utils import net
from oslo_config import cfg
from oslo_log import log as logging
from oslo_utils import importutils
@ -57,7 +58,7 @@ class IpLibTestFramework(functional_base.BaseSudoTestCase):
return Device(name or utils.get_rand_name(),
ip_cidrs or ["%s/24" % TEST_IP],
mac_address or
utils.get_random_mac('fa:16:3e:00:00:00'.split(':')),
net.get_random_mac('fa:16:3e:00:00:00'.split(':')),
namespace or utils.get_rand_name())
def _safe_delete_device(self, device):
@ -232,7 +233,7 @@ class IpLibTestCase(IpLibTestFramework):
attr = self.generate_device_details(
ip_cidrs=["%s/24" % TEST_IP, "fd00::1/64"]
)
mac_address = utils.get_random_mac('fa:16:3e:00:00:00'.split(':'))
mac_address = net.get_random_mac('fa:16:3e:00:00:00'.split(':'))
device = self.manage_device(attr)
device.neigh.add(TEST_IP_NEIGH, mac_address)
@ -265,7 +266,7 @@ class IpLibTestCase(IpLibTestFramework):
attr = self.generate_device_details(
ip_cidrs=["%s/24" % TEST_IP, "fd00::1/64"]
)
mac_address = utils.get_random_mac('fa:16:3e:00:00:00'.split(':'))
mac_address = net.get_random_mac('fa:16:3e:00:00:00'.split(':'))
device = self.manage_device(attr)
# trying to delete a non-existent entry shouldn't raise an error

View File

@ -13,8 +13,8 @@
# License for the specific language governing permissions and limitations
# under the License.
from neutron.common import utils
from neutron_lib import constants
from neutron_lib.utils import net
from neutron.plugins.ml2.drivers.linuxbridge.agent import arp_protect
from neutron.tests.common import machine_fixtures
@ -72,7 +72,7 @@ class LinuxBridgeARPSpoofTestCase(functional_base.BaseSudoTestCase):
# make sure a large number of allowed address pairs works
for i in range(100000):
port['allowed_address_pairs'].append(
{'mac_address': utils.get_random_mac(
{'mac_address': net.get_random_mac(
'fa:16:3e:00:00:00'.split(':')),
'ip_address': '10.10.10.10'})
self._add_arp_protection(self.source, ['1.2.2.2'], port)

View File

@ -17,6 +17,7 @@ import mock
from neutron_lib import constants as n_consts
from neutron_lib.utils import helpers
from neutron_lib.utils import net
from oslo_utils import uuidutils
from neutron.agent.common import ovs_lib
@ -50,8 +51,7 @@ class OVSDBHandlerTestCase(base.OVSAgentTestFramework):
trunk_id = uuidutils.generate_uuid()
self.trunk_dict = {
'id': trunk_id,
'mac_address': common_utils.get_random_mac(
'fa:16:3e:00:00:00'.split(':')),
'mac_address': net.get_random_mac('fa:16:3e:00:00:00'.split(':')),
'sub_ports': []}
self.trunk_port_name = generate_tap_device_name()
self.trunk_br = trunk_manager.TrunkBridge(trunk_id)

View File

@ -15,11 +15,11 @@
import mock
from neutron_lib.utils import net
from oslo_log import log as logging
from oslo_utils import uuidutils
import testtools
from neutron.common import utils as common_utils
from neutron.services.trunk.drivers.openvswitch.agent import trunk_manager
from neutron.services.trunk.drivers.openvswitch import utils
from neutron.tests.common import conn_testers
@ -42,7 +42,7 @@ class TrunkParentPortTestCase(base.BaseSudoTestCase):
super(TrunkParentPortTestCase, self).setUp()
trunk_id = uuidutils.generate_uuid()
port_id = uuidutils.generate_uuid()
port_mac = common_utils.get_random_mac('fa:16:3e:00:00:00'.split(':'))
port_mac = net.get_random_mac('fa:16:3e:00:00:00'.split(':'))
self.trunk = trunk_manager.TrunkParentPort(trunk_id, port_id, port_mac)
self.trunk.bridge = self.useFixture(
net_helpers.OVSTrunkBridgeFixture(
@ -95,7 +95,7 @@ class SubPortTestCase(base.BaseSudoTestCase):
super(SubPortTestCase, self).setUp()
trunk_id = uuidutils.generate_uuid()
port_id = uuidutils.generate_uuid()
port_mac = common_utils.get_random_mac('fa:16:3e:00:00:00'.split(':'))
port_mac = net.get_random_mac('fa:16:3e:00:00:00'.split(':'))
trunk_bridge_name = utils.gen_trunk_br_name(trunk_id)
trunk_bridge = self.useFixture(
net_helpers.OVSTrunkBridgeFixture(trunk_bridge_name)).bridge
@ -174,9 +174,8 @@ class TrunkManagerTestCase(base.BaseSudoTestCase):
"""
vlan_net1 = helpers.get_not_used_vlan(self.tester.bridge, VLAN_RANGE)
vlan_net2 = helpers.get_not_used_vlan(self.tester.bridge, VLAN_RANGE)
trunk_mac = common_utils.get_random_mac('fa:16:3e:00:00:00'.split(':'))
sub_port_mac = common_utils.get_random_mac(
'fa:16:3e:00:00:00'.split(':'))
trunk_mac = net.get_random_mac('fa:16:3e:00:00:00'.split(':'))
sub_port_mac = net.get_random_mac('fa:16:3e:00:00:00'.split(':'))
sub_port_segmentation_id = helpers.get_not_used_vlan(
self.tester.bridge, VLAN_RANGE)
LOG.debug("Using %(n1)d vlan tag as local vlan ID for net1 and %(n2)d "

View File

@ -21,11 +21,13 @@ import random
import time
import warnings
from debtcollector import removals
import fixtures
import mock
import netaddr
from neutron_lib import constants
from neutron_lib.utils import helpers
from neutron_lib.utils import net
from oslo_utils import netutils
from oslo_utils import timeutils
import six
@ -254,17 +256,20 @@ def get_random_cidr(version=4):
return '2001:db8:%x::/%d' % (random.getrandbits(16), 64)
@removals.remove(
message="Use get_random_mac from neutron_lib.utils.net",
version="Pike",
removal_version="Queens"
)
def get_random_mac():
"""Generate a random mac address starting with fe:16:3e"""
mac = [0xfe, 0x16, 0x3e,
random.randint(0x00, 0xff),
random.randint(0x00, 0xff),
random.randint(0x00, 0xff)]
return ':'.join(map(lambda x: "%02x" % x, mac))
return net.get_random_mac(['fe', '16', '3e', '00', '00', '00'])
def get_random_EUI():
return netaddr.EUI(get_random_mac())
return netaddr.EUI(
net.get_random_mac(['fe', '16', '3e', '00', '00', '00'])
)
def get_random_ip_network(version=4):
@ -278,8 +283,10 @@ def get_random_ip_address(version=4):
random.randint(3, 254))
return netaddr.IPAddress(ip_string)
else:
ip = netutils.get_ipv6_addr_by_EUI64('2001:db8::/64',
get_random_mac())
ip = netutils.get_ipv6_addr_by_EUI64(
'2001:db8::/64',
net.get_random_mac(['fe', '16', '3e', '00', '00', '00'])
)
return ip

View File

@ -26,6 +26,7 @@ from neutron_lib import context
from neutron_lib import exceptions as lib_exc
from neutron_lib.plugins import directory
from neutron_lib.utils import helpers
from neutron_lib.utils import net
from oslo_concurrency import lockutils
from oslo_config import cfg
from oslo_utils import importutils
@ -126,7 +127,7 @@ class NeutronDbPluginV2TestCase(testlib_api.WebTestCase):
for key, default in six.iteritems(service_plugins or {})]
)
cfg.CONF.set_override('base_mac', "12:34:56:78:90:ab")
cfg.CONF.set_override('base_mac', "12:34:56:78:00:00")
cfg.CONF.set_override('max_dns_nameservers', 2)
cfg.CONF.set_override('max_subnet_host_routes', 2)
self.api = router.APIRouter()
@ -1729,7 +1730,7 @@ fixed_ips=ip_address%%3D%s&fixed_ips=ip_address%%3D%s&fixed_ips=subnet_id%%3D%s
# simulate duplicate mac generation to make sure DBDuplicate is retried
responses = ['12:34:56:78:00:00', '12:34:56:78:00:00',
'12:34:56:78:00:01']
with mock.patch('neutron.common.utils.get_random_mac',
with mock.patch.object(net, 'get_random_mac',
side_effect=responses) as grand_mac:
with self.subnet() as s:
with self.port(subnet=s) as p1, self.port(subnet=s) as p2:

View File

@ -64,7 +64,7 @@ class DvrDbMixinTestCase(test_plugin.Ml2PluginV2TestCase):
def test__create_dvr_mac_address_success(self):
entry = {'host': 'foo_host', 'mac_address': '00:11:22:33:44:55:66'}
with mock.patch.object(dvr_mac_db.utils, 'get_random_mac') as f:
with mock.patch.object(dvr_mac_db.net, 'get_random_mac') as f:
f.return_value = entry['mac_address']
expected = self.mixin._create_dvr_mac_address(
self.ctx, entry['host'])
@ -75,7 +75,7 @@ class DvrDbMixinTestCase(test_plugin.Ml2PluginV2TestCase):
mock.patch('neutron.db.api._retry_db_errors.max_retries',
new=2).start()
self._create_dvr_mac_entry('foo_host_1', 'non_unique_mac')
with mock.patch.object(dvr_mac_db.utils, 'get_random_mac') as f:
with mock.patch.object(dvr_mac_db.net, 'get_random_mac') as f:
f.return_value = 'non_unique_mac'
self.assertRaises(dvr.MacAddressGenerationFailure,
self.mixin._create_dvr_mac_address,

View File

@ -16,6 +16,7 @@ import itertools
import random
from neutron_lib import constants as const
from neutron_lib.utils import net
from oslo_serialization import jsonutils
from neutron.common import constants
@ -118,7 +119,7 @@ class MACAddressFieldTest(test_base.BaseTestCase, TestField):
'XXXX', 'ypp', 'g3:vvv',
# the field type is strict and does not allow to pass strings, even
# if they represent a valid MAC address
tools.get_random_mac(),
net.get_random_mac('fe:16:3e:00:00:00'.split(':')),
]
self.to_primitive_values = ((a1, str(a2))
for a1, a2 in self.coerce_good_values)

View File

@ -14,10 +14,10 @@
from neutron_lib import constants
from neutron_lib import context
from neutron_lib.utils import net
from oslo_utils import uuidutils
from neutron.common import constants as n_const
from neutron.common import utils
from neutron.db.models import l3 as l3_models
from neutron.db.models import l3_attrs
from neutron.db.models import l3ha as l3ha_model
@ -96,7 +96,7 @@ class TestL2PopulationDBTestCase(testlib_api.SqlTestCase):
def _setup_port_binding(self, **kwargs):
with self.ctx.session.begin(subtransactions=True):
mac = utils.get_random_mac('fa:16:3e:00:00:00'.split(':'))
mac = net.get_random_mac('fa:16:3e:00:00:00'.split(':'))
port_id = uuidutils.generate_uuid()
network_id = kwargs.get('network_id', TEST_NETWORK_ID)
device_owner = kwargs.get('device_owner', '')