Update nested_vlan_vif driver to use OpenStackSDK.
Implements: blueprint switch-to-openstacksdk Change-Id: Id35f1e16c5cb3df25d79828b99bb096ed19d72d5
This commit is contained in:
parent
c6ddf4f521
commit
c55e3fbeec
|
@ -16,7 +16,7 @@ from time import sleep
|
|||
from kuryr.lib import constants as kl_const
|
||||
from kuryr.lib import exceptions as kl_exc
|
||||
from kuryr.lib import segmentation_type_drivers as seg_driver
|
||||
from neutronclient.common import exceptions as n_exc
|
||||
from openstack import exceptions as os_exc
|
||||
from oslo_log import log as logging
|
||||
|
||||
from kuryr_kubernetes import clients
|
||||
|
@ -38,14 +38,14 @@ class NestedVlanPodVIFDriver(nested_vif.NestedPodVIFDriver):
|
|||
"""Manages ports for nested-containers using VLANs to provide VIFs."""
|
||||
|
||||
def request_vif(self, pod, project_id, subnets, security_groups):
|
||||
neutron = clients.get_neutron_client()
|
||||
os_net = clients.get_network_client()
|
||||
parent_port = self._get_parent_port(pod)
|
||||
trunk_id = self._get_trunk_id(parent_port)
|
||||
|
||||
rq = self._get_port_request(pod, project_id, subnets, security_groups)
|
||||
port = neutron.create_port(rq).get('port')
|
||||
utils.tag_neutron_resources('ports', [port['id']])
|
||||
vlan_id = self._add_subport(trunk_id, port['id'])
|
||||
port = os_net.create_port(**rq)
|
||||
utils.tag_neutron_resources('ports', [port.id])
|
||||
vlan_id = self._add_subport(trunk_id, port.id)
|
||||
|
||||
return ovu.neutron_to_osvif_vif_nested_vlan(port, subnets, vlan_id)
|
||||
|
||||
|
@ -59,12 +59,12 @@ class NestedVlanPodVIFDriver(nested_vif.NestedPodVIFDriver):
|
|||
If not enough vlan ids are available for all the subports to create,
|
||||
it creates as much as available vlan ids.
|
||||
|
||||
Note the neutron trunk_add_subports is an atomic operation that will
|
||||
Note the os_net add_trunk_subports is an atomic operation that will
|
||||
either attach all or none of the subports. Therefore, if there is a
|
||||
vlan id collision, all the created ports will be deleted and the
|
||||
exception is raised.
|
||||
"""
|
||||
neutron = clients.get_neutron_client()
|
||||
os_net = clients.get_network_client()
|
||||
if trunk_ip:
|
||||
parent_port = self._get_parent_port_by_host_ip(trunk_ip)
|
||||
else:
|
||||
|
@ -81,28 +81,27 @@ class NestedVlanPodVIFDriver(nested_vif.NestedPodVIFDriver):
|
|||
|
||||
bulk_port_rq = {'ports': [port_rq] * len(subports_info)}
|
||||
try:
|
||||
ports = neutron.create_port(bulk_port_rq).get('ports')
|
||||
except n_exc.NeutronClientException:
|
||||
ports = list(os_net.create_ports(bulk_port_rq))
|
||||
except os_exc.SDKException:
|
||||
LOG.exception("Error creating bulk ports: %s", bulk_port_rq)
|
||||
raise
|
||||
utils.tag_neutron_resources('ports', [port['id'] for port in ports])
|
||||
utils.tag_neutron_resources('ports', [port.id for port in ports])
|
||||
|
||||
for index, port in enumerate(ports):
|
||||
subports_info[index]['port_id'] = port['id']
|
||||
|
||||
try:
|
||||
try:
|
||||
neutron.trunk_add_subports(trunk_id,
|
||||
{'sub_ports': subports_info})
|
||||
except n_exc.Conflict:
|
||||
os_net.add_trunk_subports(trunk_id, subports_info)
|
||||
except os_exc.ConflictException:
|
||||
LOG.error("vlan ids already in use on trunk")
|
||||
for port in ports:
|
||||
neutron.delete_port(port['id'])
|
||||
os_net.delete_port(port.id)
|
||||
return []
|
||||
except n_exc.NeutronClientException:
|
||||
except os_exc.SDKException:
|
||||
LOG.exception("Error happened during subport addition to trunk")
|
||||
for port in ports:
|
||||
neutron.delete_port(port['id'])
|
||||
os_net.delete_port(port.id)
|
||||
return []
|
||||
|
||||
vifs = []
|
||||
|
@ -113,16 +112,12 @@ class NestedVlanPodVIFDriver(nested_vif.NestedPodVIFDriver):
|
|||
return vifs
|
||||
|
||||
def release_vif(self, pod, vif, project_id=None, security_groups=None):
|
||||
neutron = clients.get_neutron_client()
|
||||
os_net = clients.get_network_client()
|
||||
parent_port = self._get_parent_port(pod)
|
||||
trunk_id = self._get_trunk_id(parent_port)
|
||||
self._remove_subport(trunk_id, vif.id)
|
||||
self._release_vlan_id(vif.vlan_id)
|
||||
try:
|
||||
neutron.delete_port(vif.id)
|
||||
except n_exc.PortNotFoundClient:
|
||||
LOG.debug('Unable to release port %s as it no longer exists.',
|
||||
vif.id)
|
||||
os_net.delete_port(vif.id)
|
||||
|
||||
def _get_port_request(self, pod, project_id, subnets, security_groups,
|
||||
unbound=False):
|
||||
|
@ -142,7 +137,7 @@ class NestedVlanPodVIFDriver(nested_vif.NestedPodVIFDriver):
|
|||
if security_groups:
|
||||
port_req_body['security_groups'] = security_groups
|
||||
|
||||
return {'port': port_req_body}
|
||||
return port_req_body
|
||||
|
||||
def _create_subports_info(self, pod, project_id, subnets,
|
||||
security_groups, trunk_id, num_ports,
|
||||
|
@ -151,7 +146,7 @@ class NestedVlanPodVIFDriver(nested_vif.NestedPodVIFDriver):
|
|||
|
||||
in_use_vlan_ids = self._get_in_use_vlan_ids_set(trunk_id)
|
||||
port_rq = self._get_port_request(pod, project_id, subnets,
|
||||
security_groups, unbound)['port']
|
||||
security_groups, unbound)
|
||||
for i in range(num_ports):
|
||||
try:
|
||||
vlan_id = seg_driver.allocate_segmentation_id(in_use_vlan_ids)
|
||||
|
@ -169,7 +164,7 @@ class NestedVlanPodVIFDriver(nested_vif.NestedPodVIFDriver):
|
|||
def _get_trunk_id(self, port):
|
||||
try:
|
||||
return port['trunk_details']['trunk_id']
|
||||
except KeyError:
|
||||
except (KeyError, TypeError):
|
||||
LOG.error("Neutron port is missing trunk details. "
|
||||
"Please ensure that k8s node port is associated "
|
||||
"with a Neutron vlan trunk")
|
||||
|
@ -186,12 +181,12 @@ class NestedVlanPodVIFDriver(nested_vif.NestedPodVIFDriver):
|
|||
"""
|
||||
# TODO(vikasc): Better approach for retrying in case of
|
||||
# vlan-id conflict.
|
||||
neutron = clients.get_neutron_client()
|
||||
os_net = clients.get_network_client()
|
||||
retry_count = 1
|
||||
while True:
|
||||
try:
|
||||
vlan_id = self._get_vlan_id(trunk_id)
|
||||
except n_exc.NeutronClientException:
|
||||
except os_exc.SDKException:
|
||||
LOG.error("Getting VlanID for subport on "
|
||||
"trunk %s failed!!", trunk_id)
|
||||
raise
|
||||
|
@ -199,9 +194,8 @@ class NestedVlanPodVIFDriver(nested_vif.NestedPodVIFDriver):
|
|||
'port_id': subport,
|
||||
'segmentation_type': 'vlan'}]
|
||||
try:
|
||||
neutron.trunk_add_subports(trunk_id,
|
||||
{'sub_ports': subport})
|
||||
except n_exc.Conflict:
|
||||
os_net.add_trunk_subports(trunk_id, subport)
|
||||
except os_exc.ConflictException:
|
||||
if retry_count < DEFAULT_MAX_RETRY_COUNT:
|
||||
LOG.error("vlanid already in use on trunk, "
|
||||
"%s. Retrying...", trunk_id)
|
||||
|
@ -213,21 +207,20 @@ class NestedVlanPodVIFDriver(nested_vif.NestedPodVIFDriver):
|
|||
"MAX retry count reached. Failed to add subport")
|
||||
raise
|
||||
|
||||
except n_exc.NeutronClientException:
|
||||
except os_exc.SDKException:
|
||||
LOG.exception("Error happened during subport "
|
||||
"addition to trunk %s", trunk_id)
|
||||
raise
|
||||
return vlan_id
|
||||
|
||||
def _remove_subports(self, trunk_id, subports_id):
|
||||
neutron = clients.get_neutron_client()
|
||||
os_net = clients.get_network_client()
|
||||
subports_body = []
|
||||
for subport_id in set(subports_id):
|
||||
subports_body.append({'port_id': subport_id})
|
||||
try:
|
||||
neutron.trunk_remove_subports(trunk_id,
|
||||
{'sub_ports': subports_body})
|
||||
except n_exc.NeutronClientException:
|
||||
os_net.delete_trunk_subports(trunk_id, subports_body)
|
||||
except os_exc.SDKException:
|
||||
LOG.exception("Error happened during subport removal from "
|
||||
"trunk %s", trunk_id)
|
||||
raise
|
||||
|
@ -244,9 +237,9 @@ class NestedVlanPodVIFDriver(nested_vif.NestedPodVIFDriver):
|
|||
|
||||
def _get_in_use_vlan_ids_set(self, trunk_id):
|
||||
vlan_ids = set()
|
||||
neutron = clients.get_neutron_client()
|
||||
trunk = neutron.show_trunk(trunk_id)
|
||||
for port in trunk['trunk']['sub_ports']:
|
||||
os_net = clients.get_network_client()
|
||||
trunk = os_net.get_trunk(trunk_id)
|
||||
for port in trunk.sub_ports:
|
||||
vlan_ids.add(port['segmentation_id'])
|
||||
|
||||
return vlan_ids
|
||||
|
|
|
@ -11,10 +11,11 @@
|
|||
# under the License.
|
||||
|
||||
import mock
|
||||
import munch
|
||||
|
||||
from kuryr.lib import constants as kl_const
|
||||
from kuryr.lib import exceptions as kl_exc
|
||||
from neutronclient.common import exceptions as n_exc
|
||||
from openstack import exceptions as os_exc
|
||||
from oslo_config import cfg as oslo_cfg
|
||||
|
||||
from kuryr_kubernetes import constants
|
||||
|
@ -31,7 +32,7 @@ class TestNestedVlanPodVIFDriver(test_base.TestCase):
|
|||
def test_request_vif(self, m_to_vif):
|
||||
cls = nested_vlan_vif.NestedVlanPodVIFDriver
|
||||
m_driver = mock.Mock(spec=cls)
|
||||
neutron = self.useFixture(k_fix.MockNeutronClient()).client
|
||||
os_net = self.useFixture(k_fix.MockNetworkClient()).client
|
||||
|
||||
pod = mock.sentinel.pod
|
||||
project_id = mock.sentinel.project_id
|
||||
|
@ -41,9 +42,12 @@ class TestNestedVlanPodVIFDriver(test_base.TestCase):
|
|||
parent_port = mock.sentinel.parent_port
|
||||
trunk_id = mock.sentinel.trunk_id
|
||||
port_id = mock.sentinel.port_id
|
||||
port = mock.MagicMock()
|
||||
port.__getitem__.return_value = port_id
|
||||
port_request = mock.sentinel.port_request
|
||||
port = munch.Munch({'id': port_id})
|
||||
port_request = {'project_id': project_id,
|
||||
'name': mock.sentinel.name,
|
||||
'network_id': mock.sentinel.network_id,
|
||||
'fixed_ips': mock.sentinel.fixed_ips,
|
||||
'admin_state_up': True}
|
||||
vlan_id = mock.sentinel.vlan_id
|
||||
|
||||
vif = mock.Mock()
|
||||
|
@ -53,8 +57,8 @@ class TestNestedVlanPodVIFDriver(test_base.TestCase):
|
|||
m_driver._get_trunk_id.return_value = trunk_id
|
||||
m_driver._get_port_request.return_value = port_request
|
||||
m_driver._add_subport.return_value = vlan_id
|
||||
neutron.list_ports.return_value = {'ports': [parent_port]}
|
||||
neutron.create_port.return_value = {'port': port}
|
||||
os_net.ports.return_value = (p for p in [parent_port])
|
||||
os_net.create_port.return_value = port
|
||||
|
||||
self.assertEqual(vif, cls.request_vif(m_driver, pod, project_id,
|
||||
subnets, security_groups))
|
||||
|
@ -63,7 +67,7 @@ class TestNestedVlanPodVIFDriver(test_base.TestCase):
|
|||
m_driver._get_trunk_id.assert_called_once_with(parent_port)
|
||||
m_driver._get_port_request.assert_called_once_with(
|
||||
pod, project_id, subnets, security_groups)
|
||||
neutron.create_port.assert_called_once_with(port_request)
|
||||
os_net.create_port.assert_called_once_with(**port_request)
|
||||
m_driver._add_subport.assert_called_once_with(trunk_id, port_id)
|
||||
m_to_vif.assert_called_once_with(port, subnets, vlan_id)
|
||||
|
||||
|
@ -72,7 +76,7 @@ class TestNestedVlanPodVIFDriver(test_base.TestCase):
|
|||
def test_request_vifs(self, m_to_vif):
|
||||
cls = nested_vlan_vif.NestedVlanPodVIFDriver
|
||||
m_driver = mock.Mock(spec=cls)
|
||||
neutron = self.useFixture(k_fix.MockNeutronClient()).client
|
||||
os_net = self.useFixture(k_fix.MockNetworkClient()).client
|
||||
|
||||
pod = mock.sentinel.pod
|
||||
project_id = mock.sentinel.project_id
|
||||
|
@ -89,7 +93,7 @@ class TestNestedVlanPodVIFDriver(test_base.TestCase):
|
|||
{'segmentation_id': 2,
|
||||
'port_id': '',
|
||||
'segmentation_type': 'vlan'}]
|
||||
port = {'id': mock.sentinel.id}
|
||||
port = munch.Munch({'id': mock.sentinel.id})
|
||||
vif = mock.sentinel.vif
|
||||
|
||||
bulk_rq = {'ports': [port_request for _ in range(len(subports_info))]}
|
||||
|
@ -98,7 +102,7 @@ class TestNestedVlanPodVIFDriver(test_base.TestCase):
|
|||
m_driver._get_trunk_id.return_value = trunk_id
|
||||
m_driver._create_subports_info.return_value = (port_request,
|
||||
subports_info)
|
||||
neutron.create_port.return_value = {'ports': [port, port]}
|
||||
os_net.create_ports.return_value = (p for p in [port, port])
|
||||
m_to_vif.return_value = vif
|
||||
|
||||
self.assertEqual([vif, vif], cls.request_vifs(
|
||||
|
@ -109,10 +113,10 @@ class TestNestedVlanPodVIFDriver(test_base.TestCase):
|
|||
m_driver._create_subports_info.assert_called_once_with(
|
||||
pod, project_id, subnets, security_groups, trunk_id, num_ports,
|
||||
unbound=True)
|
||||
neutron.create_port.assert_called_once_with(bulk_rq)
|
||||
neutron.trunk_add_subports.assert_called_once_with(
|
||||
trunk_id, {'sub_ports': subports_info})
|
||||
neutron.delete_port.assert_not_called()
|
||||
os_net.create_ports.assert_called_once_with(bulk_rq)
|
||||
os_net.add_trunk_subports.assert_called_once_with(trunk_id,
|
||||
subports_info)
|
||||
os_net.delete_port.assert_not_called()
|
||||
|
||||
calls = [mock.call(port, subnets, info['segmentation_id'])
|
||||
for info in subports_info]
|
||||
|
@ -121,7 +125,7 @@ class TestNestedVlanPodVIFDriver(test_base.TestCase):
|
|||
def test_request_vifs_no_vlans(self):
|
||||
cls = nested_vlan_vif.NestedVlanPodVIFDriver
|
||||
m_driver = mock.Mock(spec=cls)
|
||||
self.useFixture(k_fix.MockNeutronClient()).client
|
||||
self.useFixture(k_fix.MockNetworkClient()).client
|
||||
|
||||
pod = mock.sentinel.pod
|
||||
project_id = mock.sentinel.project_id
|
||||
|
@ -152,7 +156,7 @@ class TestNestedVlanPodVIFDriver(test_base.TestCase):
|
|||
def test_request_vifs_bulk_creation_exception(self):
|
||||
cls = nested_vlan_vif.NestedVlanPodVIFDriver
|
||||
m_driver = mock.Mock(spec=cls)
|
||||
neutron = self.useFixture(k_fix.MockNeutronClient()).client
|
||||
os_net = self.useFixture(k_fix.MockNetworkClient()).client
|
||||
|
||||
pod = mock.sentinel.pod
|
||||
project_id = mock.sentinel.project_id
|
||||
|
@ -176,10 +180,10 @@ class TestNestedVlanPodVIFDriver(test_base.TestCase):
|
|||
m_driver._get_trunk_id.return_value = trunk_id
|
||||
m_driver._create_subports_info.return_value = (port_request,
|
||||
subports_info)
|
||||
neutron.create_port.side_effect = n_exc.NeutronClientException
|
||||
os_net.create_ports.side_effect = os_exc.SDKException
|
||||
|
||||
self.assertRaises(
|
||||
n_exc.NeutronClientException, cls.request_vifs,
|
||||
os_exc.SDKException, cls.request_vifs,
|
||||
m_driver, pod, project_id, subnets, security_groups, num_ports)
|
||||
|
||||
m_driver._get_parent_port.assert_called_once_with(pod)
|
||||
|
@ -187,12 +191,12 @@ class TestNestedVlanPodVIFDriver(test_base.TestCase):
|
|||
m_driver._create_subports_info.assert_called_once_with(
|
||||
pod, project_id, subnets, security_groups,
|
||||
trunk_id, num_ports, unbound=True)
|
||||
neutron.create_port.assert_called_once_with(bulk_rq)
|
||||
os_net.create_ports.assert_called_once_with(bulk_rq)
|
||||
|
||||
def test_request_vifs_trunk_subports_conflict(self):
|
||||
cls = nested_vlan_vif.NestedVlanPodVIFDriver
|
||||
m_driver = mock.Mock(spec=cls)
|
||||
neutron = self.useFixture(k_fix.MockNeutronClient()).client
|
||||
os_net = self.useFixture(k_fix.MockNetworkClient()).client
|
||||
|
||||
pod = mock.sentinel.pod
|
||||
project_id = mock.sentinel.project_id
|
||||
|
@ -209,7 +213,7 @@ class TestNestedVlanPodVIFDriver(test_base.TestCase):
|
|||
{'segmentation_id': 2,
|
||||
'port_id': '',
|
||||
'segmentation_type': 'vlan'}]
|
||||
port = {'id': mock.sentinel.id}
|
||||
port = munch.Munch({'id': mock.sentinel.id})
|
||||
|
||||
bulk_rq = {'ports': [port_request for _ in range(len(subports_info))]}
|
||||
|
||||
|
@ -217,8 +221,8 @@ class TestNestedVlanPodVIFDriver(test_base.TestCase):
|
|||
m_driver._get_trunk_id.return_value = trunk_id
|
||||
m_driver._create_subports_info.return_value = (port_request,
|
||||
subports_info)
|
||||
neutron.create_port.return_value = {'ports': [port, port]}
|
||||
neutron.trunk_add_subports.side_effect = n_exc.Conflict
|
||||
os_net.create_ports.return_value = (p for p in [port, port])
|
||||
os_net.add_trunk_subports.side_effect = os_exc.ConflictException
|
||||
|
||||
self.assertEqual([], cls.request_vifs(m_driver, pod, project_id,
|
||||
subnets, security_groups, num_ports))
|
||||
|
@ -228,15 +232,15 @@ class TestNestedVlanPodVIFDriver(test_base.TestCase):
|
|||
m_driver._create_subports_info.assert_called_once_with(
|
||||
pod, project_id, subnets, security_groups,
|
||||
trunk_id, num_ports, unbound=True)
|
||||
neutron.create_port.assert_called_once_with(bulk_rq)
|
||||
neutron.trunk_add_subports.assert_called_once_with(
|
||||
trunk_id, {'sub_ports': subports_info})
|
||||
neutron.delete_port.assert_called_with(port['id'])
|
||||
os_net.create_ports.assert_called_once_with(bulk_rq)
|
||||
os_net.add_trunk_subports.assert_called_once_with(trunk_id,
|
||||
subports_info)
|
||||
os_net.delete_port.assert_called_with(port['id'])
|
||||
|
||||
def test_request_vifs_trunk_subports_exception(self):
|
||||
cls = nested_vlan_vif.NestedVlanPodVIFDriver
|
||||
m_driver = mock.Mock(spec=cls)
|
||||
neutron = self.useFixture(k_fix.MockNeutronClient()).client
|
||||
os_net = self.useFixture(k_fix.MockNetworkClient()).client
|
||||
|
||||
pod = mock.sentinel.pod
|
||||
project_id = mock.sentinel.project_id
|
||||
|
@ -253,7 +257,7 @@ class TestNestedVlanPodVIFDriver(test_base.TestCase):
|
|||
{'segmentation_id': 2,
|
||||
'port_id': '',
|
||||
'segmentation_type': 'vlan'}]
|
||||
port = {'id': mock.sentinel.id}
|
||||
port = munch.Munch({'id': mock.sentinel.id})
|
||||
|
||||
bulk_rq = {'ports': [port_request for _ in range(len(subports_info))]}
|
||||
|
||||
|
@ -261,8 +265,8 @@ class TestNestedVlanPodVIFDriver(test_base.TestCase):
|
|||
m_driver._get_trunk_id.return_value = trunk_id
|
||||
m_driver._create_subports_info.return_value = (port_request,
|
||||
subports_info)
|
||||
neutron.create_port.return_value = {'ports': [port, port]}
|
||||
neutron.trunk_add_subports.side_effect = n_exc.NeutronClientException
|
||||
os_net.create_ports.return_value = (p for p in [port, port])
|
||||
os_net.add_trunk_subports.side_effect = os_exc.SDKException
|
||||
|
||||
self.assertEqual([], cls.request_vifs(m_driver, pod, project_id,
|
||||
subnets, security_groups, num_ports))
|
||||
|
@ -272,15 +276,15 @@ class TestNestedVlanPodVIFDriver(test_base.TestCase):
|
|||
m_driver._create_subports_info.assert_called_once_with(
|
||||
pod, project_id, subnets, security_groups,
|
||||
trunk_id, num_ports, unbound=True)
|
||||
neutron.create_port.assert_called_once_with(bulk_rq)
|
||||
neutron.trunk_add_subports.assert_called_once_with(
|
||||
trunk_id, {'sub_ports': subports_info})
|
||||
neutron.delete_port.assert_called_with(port['id'])
|
||||
os_net.create_ports.assert_called_once_with(bulk_rq)
|
||||
os_net.add_trunk_subports.assert_called_once_with(trunk_id,
|
||||
subports_info)
|
||||
os_net.delete_port.assert_called_with(port['id'])
|
||||
|
||||
def test_release_vif(self):
|
||||
cls = nested_vlan_vif.NestedVlanPodVIFDriver
|
||||
m_driver = mock.Mock(spec=cls)
|
||||
neutron = self.useFixture(k_fix.MockNeutronClient()).client
|
||||
os_net = self.useFixture(k_fix.MockNetworkClient()).client
|
||||
parent_port = mock.sentinel.parent_port
|
||||
trunk_id = mock.sentinel.trunk_id
|
||||
|
||||
|
@ -294,12 +298,12 @@ class TestNestedVlanPodVIFDriver(test_base.TestCase):
|
|||
m_driver._get_parent_port.assert_called_once_with(pod)
|
||||
m_driver._get_trunk_id.assert_called_once_with(parent_port)
|
||||
m_driver._remove_subport.assert_called_once_with(trunk_id, vif.id)
|
||||
neutron.delete_port.assert_called_once_with(vif.id)
|
||||
os_net.delete_port.assert_called_once_with(vif.id)
|
||||
|
||||
def test_release_vif_not_found(self):
|
||||
cls = nested_vlan_vif.NestedVlanPodVIFDriver
|
||||
m_driver = mock.Mock(spec=cls)
|
||||
neutron = self.useFixture(k_fix.MockNeutronClient()).client
|
||||
os_net = self.useFixture(k_fix.MockNetworkClient()).client
|
||||
parent_port = mock.sentinel.parent_port
|
||||
trunk_id = mock.sentinel.trunk_id
|
||||
|
||||
|
@ -310,14 +314,13 @@ class TestNestedVlanPodVIFDriver(test_base.TestCase):
|
|||
vif = mock.Mock()
|
||||
m_driver._port_vlan_mapping = {vif.id: vlan_id}
|
||||
self.assertTrue(vif.id in m_driver._port_vlan_mapping)
|
||||
neutron.delete_port.side_effect = n_exc.PortNotFoundClient
|
||||
|
||||
cls.release_vif(m_driver, pod, vif)
|
||||
|
||||
m_driver._get_parent_port.assert_called_once_with(pod)
|
||||
m_driver._get_trunk_id.assert_called_once_with(parent_port)
|
||||
m_driver._remove_subport.assert_called_once_with(trunk_id, vif.id)
|
||||
neutron.delete_port.assert_called_once_with(vif.id)
|
||||
os_net.delete_port.assert_called_once_with(vif.id)
|
||||
|
||||
def _test_get_port_request(self, m_to_fips, security_groups,
|
||||
m_get_network_id, m_get_port_name,
|
||||
|
@ -340,18 +343,18 @@ class TestNestedVlanPodVIFDriver(test_base.TestCase):
|
|||
True,
|
||||
group='kubernetes')
|
||||
|
||||
expected = {'port': {'project_id': project_id,
|
||||
'name': port_name,
|
||||
'network_id': network_id,
|
||||
'fixed_ips': fixed_ips,
|
||||
'device_owner': kl_const.DEVICE_OWNER,
|
||||
'admin_state_up': True}}
|
||||
expected = {'project_id': project_id,
|
||||
'name': port_name,
|
||||
'network_id': network_id,
|
||||
'fixed_ips': fixed_ips,
|
||||
'device_owner': kl_const.DEVICE_OWNER,
|
||||
'admin_state_up': True}
|
||||
|
||||
if security_groups:
|
||||
expected['port']['security_groups'] = security_groups
|
||||
expected['security_groups'] = security_groups
|
||||
|
||||
if unbound:
|
||||
expected['port']['name'] = constants.KURYR_PORT_NAME
|
||||
expected['name'] = constants.KURYR_PORT_NAME
|
||||
|
||||
ret = cls._get_port_request(m_driver, pod, project_id, subnets,
|
||||
security_groups, unbound)
|
||||
|
@ -411,7 +414,7 @@ class TestNestedVlanPodVIFDriver(test_base.TestCase):
|
|||
for i in range(num_ports)]
|
||||
|
||||
m_driver._get_in_use_vlan_ids_set.return_value = in_use_vlan
|
||||
m_driver._get_port_request.return_value = {'port': port}
|
||||
m_driver._get_port_request.return_value = port
|
||||
m_allocate_seg_id.side_effect = [2, 3]
|
||||
|
||||
port_res, subports_res = cls._create_subports_info(
|
||||
|
@ -444,7 +447,7 @@ class TestNestedVlanPodVIFDriver(test_base.TestCase):
|
|||
'segmentation_type': 'vlan'}]
|
||||
|
||||
m_driver._get_in_use_vlan_ids_set.return_value = in_use_vlan
|
||||
m_driver._get_port_request.return_value = {'port': port}
|
||||
m_driver._get_port_request.return_value = port
|
||||
m_allocate_seg_id.side_effect = [
|
||||
2, kl_exc.SegmentationIdAllocationFailure
|
||||
]
|
||||
|
@ -476,7 +479,7 @@ class TestNestedVlanPodVIFDriver(test_base.TestCase):
|
|||
port = mock.sentinel.port
|
||||
|
||||
m_driver._get_in_use_vlan_ids_set.return_value = in_use_vlan
|
||||
m_driver._get_port_request.return_value = {'port': port}
|
||||
m_driver._get_port_request.return_value = port
|
||||
m_allocate_seg_id.side_effect = kl_exc.SegmentationIdAllocationFailure
|
||||
|
||||
port_res, subports_res = cls._create_subports_info(
|
||||
|
@ -510,7 +513,7 @@ class TestNestedVlanPodVIFDriver(test_base.TestCase):
|
|||
def test_add_subport(self):
|
||||
cls = nested_vlan_vif.NestedVlanPodVIFDriver
|
||||
m_driver = mock.Mock(spec=cls)
|
||||
neutron = self.useFixture(k_fix.MockNeutronClient()).client
|
||||
os_net = self.useFixture(k_fix.MockNetworkClient()).client
|
||||
trunk_id = mock.sentinel.trunk_id
|
||||
subport = mock.sentinel.subport
|
||||
vlan_id = mock.sentinel.vlan_id
|
||||
|
@ -522,26 +525,26 @@ class TestNestedVlanPodVIFDriver(test_base.TestCase):
|
|||
self.assertEqual(vlan_id, cls._add_subport(m_driver, trunk_id,
|
||||
subport))
|
||||
m_driver._get_vlan_id.assert_called_once_with(trunk_id)
|
||||
neutron.trunk_add_subports.assert_called_once_with(
|
||||
trunk_id, {'sub_ports': subport_dict})
|
||||
os_net.add_trunk_subports.assert_called_once_with(trunk_id,
|
||||
subport_dict)
|
||||
|
||||
def test_add_subport_get_vlanid_failure(self):
|
||||
cls = nested_vlan_vif.NestedVlanPodVIFDriver
|
||||
m_driver = mock.Mock(spec=cls)
|
||||
self.useFixture(k_fix.MockNeutronClient()).client
|
||||
self.useFixture(k_fix.MockNetworkClient()).client
|
||||
trunk_id = mock.sentinel.trunk_id
|
||||
subport = mock.sentinel.subport
|
||||
m_driver._get_vlan_id.side_effect = n_exc.NeutronClientException
|
||||
m_driver._get_vlan_id.side_effect = os_exc.SDKException
|
||||
nested_vlan_vif.DEFAULT_MAX_RETRY_COUNT = 1
|
||||
self.assertRaises(n_exc.NeutronClientException, cls._add_subport,
|
||||
m_driver, trunk_id, subport)
|
||||
self.assertRaises(os_exc.SDKException, cls._add_subport, m_driver,
|
||||
trunk_id, subport)
|
||||
|
||||
m_driver._get_vlan_id.assert_called_once_with(trunk_id)
|
||||
|
||||
def test_add_subport_with_vlan_id_conflict(self):
|
||||
cls = nested_vlan_vif.NestedVlanPodVIFDriver
|
||||
m_driver = mock.Mock(spec=cls)
|
||||
neutron = self.useFixture(k_fix.MockNeutronClient()).client
|
||||
os_net = self.useFixture(k_fix.MockNetworkClient()).client
|
||||
trunk_id = mock.sentinel.trunk_id
|
||||
subport = mock.sentinel.subport
|
||||
vlan_id = mock.sentinel.vlan_id
|
||||
|
@ -549,37 +552,37 @@ class TestNestedVlanPodVIFDriver(test_base.TestCase):
|
|||
subport_dict = [{'segmentation_id': vlan_id,
|
||||
'port_id': subport,
|
||||
'segmentation_type': 'vlan'}]
|
||||
neutron.trunk_add_subports.side_effect = n_exc.Conflict
|
||||
os_net.add_trunk_subports.side_effect = os_exc.ConflictException
|
||||
nested_vlan_vif.DEFAULT_MAX_RETRY_COUNT = 1
|
||||
self.assertRaises(n_exc.Conflict, cls._add_subport, m_driver,
|
||||
self.assertRaises(os_exc.ConflictException, cls._add_subport, m_driver,
|
||||
trunk_id, subport)
|
||||
|
||||
neutron.trunk_add_subports.assert_called_once_with(
|
||||
trunk_id, {'sub_ports': subport_dict})
|
||||
os_net.add_trunk_subports.assert_called_once_with(trunk_id,
|
||||
subport_dict)
|
||||
|
||||
def test__remove_subports(self):
|
||||
cls = nested_vlan_vif.NestedVlanPodVIFDriver
|
||||
m_driver = mock.Mock(spec=cls)
|
||||
neutron = self.useFixture(k_fix.MockNeutronClient()).client
|
||||
os_net = self.useFixture(k_fix.MockNetworkClient()).client
|
||||
trunk_id = mock.sentinel.trunk_id
|
||||
subport_id = mock.sentinel.subport_id
|
||||
subportid_dict = [{'port_id': subport_id}]
|
||||
cls._remove_subports(m_driver, trunk_id, [subport_id])
|
||||
|
||||
neutron.trunk_remove_subports.assert_called_once_with(
|
||||
trunk_id, {'sub_ports': subportid_dict})
|
||||
os_net.delete_trunk_subports.assert_called_once_with(trunk_id,
|
||||
subportid_dict)
|
||||
|
||||
def test__remove_subports_duplicate(self):
|
||||
cls = nested_vlan_vif.NestedVlanPodVIFDriver
|
||||
m_driver = mock.Mock(spec=cls)
|
||||
neutron = self.useFixture(k_fix.MockNeutronClient()).client
|
||||
os_net = self.useFixture(k_fix.MockNetworkClient()).client
|
||||
trunk_id = mock.sentinel.trunk_id
|
||||
subport_id = mock.sentinel.subport_id
|
||||
subportid_dict = [{'port_id': subport_id}]
|
||||
cls._remove_subports(m_driver, trunk_id, [subport_id, subport_id])
|
||||
|
||||
neutron.trunk_remove_subports.assert_called_once_with(
|
||||
trunk_id, {'sub_ports': subportid_dict})
|
||||
os_net.delete_trunk_subports.assert_called_once_with(trunk_id,
|
||||
subportid_dict)
|
||||
|
||||
@mock.patch('kuryr.lib.segmentation_type_drivers.allocate_segmentation_id')
|
||||
def test_get_vlan_id(self, mock_alloc_seg_id):
|
||||
|
@ -617,18 +620,14 @@ class TestNestedVlanPodVIFDriver(test_base.TestCase):
|
|||
def test_get_in_use_vlan_ids_set(self):
|
||||
cls = nested_vlan_vif.NestedVlanPodVIFDriver
|
||||
m_driver = mock.Mock(spec=cls)
|
||||
neutron = self.useFixture(k_fix.MockNeutronClient()).client
|
||||
os_net = self.useFixture(k_fix.MockNetworkClient()).client
|
||||
|
||||
vlan_ids = set()
|
||||
trunk_id = mock.sentinel.trunk_id
|
||||
vlan_ids.add('100')
|
||||
|
||||
port = mock.MagicMock()
|
||||
port.__getitem__.return_value = '100'
|
||||
trunk_obj = mock.MagicMock()
|
||||
trunk_obj.__getitem__.return_value = [port]
|
||||
trunk = mock.MagicMock()
|
||||
trunk.__getitem__.return_value = trunk_obj
|
||||
neutron.show_trunk.return_value = trunk
|
||||
port = munch.Munch({'segmentation_id': '100'})
|
||||
trunk_obj = munch.Munch({'sub_ports': [port]})
|
||||
os_net.get_trunk.return_value = trunk_obj
|
||||
self.assertEqual(vlan_ids,
|
||||
cls._get_in_use_vlan_ids_set(m_driver, trunk_id))
|
||||
|
|
Loading…
Reference in New Issue