Merge "Refactor to create _populate_mac_address"

This commit is contained in:
Jenkins 2016-06-22 17:42:26 +00:00 committed by Gerrit Code Review
commit f283bc4449
2 changed files with 40 additions and 6 deletions

View File

@ -196,12 +196,8 @@ class API(base_api.NetworkAPI):
port_req_body['port']['tenant_id'] = instance.project_id
if security_group_ids:
port_req_body['port']['security_groups'] = security_group_ids
if available_macs is not None:
if not available_macs:
raise exception.PortNotFree(
instance=instance.uuid)
mac_address = available_macs.pop()
port_req_body['port']['mac_address'] = mac_address
mac_address = self._populate_mac_address(
instance, port_req_body, available_macs)
if dhcp_opts is not None:
port_req_body['port']['extra_dhcp_opts'] = dhcp_opts
port = port_client.create_port(port_req_body)
@ -247,6 +243,16 @@ class API(base_api.NetworkAPI):
LOG.exception(_LE('Neutron error creating port on network %s'),
network_id, instance=instance)
@staticmethod
def _populate_mac_address(instance, port_req_body, available_macs):
if available_macs is not None:
if not available_macs:
raise exception.PortNotFree(
instance=instance.uuid)
mac_address = available_macs.pop()
port_req_body['port']['mac_address'] = mac_address
return mac_address
def _check_external_network_attach(self, context, nets):
"""Check if attaching to external network is permitted."""
if not soft_external_network_attach_authorize(context):

View File

@ -4217,6 +4217,34 @@ class TestNeutronv2ExtraDhcpOpts(TestNeutronv2Base):
self._allocate_for_instance(1, dhcp_options=dhcp_opts)
class TestAllocateForInstanceHelpers(test.NoDBTestCase):
def test_populate_mac_address_skip_if_none(self):
api = neutronapi.API()
port_req_body = {}
api._populate_mac_address(None, port_req_body, None)
self.assertEqual({}, port_req_body)
def test_populate_mac_address_raise_if_empty(self):
api = neutronapi.API()
port_req_body = {}
instance = objects.Instance(uuid=uuids.instance)
self.assertRaises(exception.PortNotFree,
api._populate_mac_address,
instance, port_req_body, [])
def test_populate_mac_address_adds_last(self):
api = neutronapi.API()
port_req_body = {'port': {"foo": "bar"}}
api._populate_mac_address(None, port_req_body, ["a", "b"])
expected_port = {"foo": "bar", "mac_address": "b"}
self.assertEqual(expected_port, port_req_body["port"])
class TestNeutronv2NeutronHostnameDNS(TestNeutronv2Base):
def setUp(self):
super(TestNeutronv2NeutronHostnameDNS, self).setUp()