Merge "Remove remaining notions of python-neutronclient"

This commit is contained in:
Zuul 2020-03-06 01:01:13 +00:00 committed by Gerrit Code Review
commit 44c0a307b5
13 changed files with 172 additions and 249 deletions

View File

@ -17,12 +17,12 @@ from functools import partial
import ipaddress
import os
from debtcollector import removals
from kuryr.lib import utils
from openstack import connection
from openstack import exceptions as os_exc
from openstack.network.v2 import port as os_port
from openstack.network.v2 import trunk as os_trunk
from openstack import resource as os_resource
from openstack import utils as os_utils
from kuryr_kubernetes import config
@ -40,11 +40,6 @@ def get_network_client():
return _clients[_OPENSTACKSDK].network
@removals.remove
def get_neutron_client():
return _clients[_NEUTRON_CLIENT]
def get_openstacksdk():
return _clients[_OPENSTACKSDK]
@ -66,15 +61,10 @@ def get_compute_client():
def setup_clients():
setup_neutron_client()
setup_kubernetes_client()
setup_openstacksdk()
def setup_neutron_client():
_clients[_NEUTRON_CLIENT] = utils.get_neutron_client()
def setup_kubernetes_client():
if config.CONF.kubernetes.api_root:
api_root = config.CONF.kubernetes.api_root
@ -151,6 +141,11 @@ def handle_neutron_errors(method, *args, **kwargs):
def setup_openstacksdk():
auth_plugin = utils.get_auth_plugin('neutron')
session = utils.get_keystone_session('neutron', auth_plugin)
# TODO(mdulko): To use Neutron's ability to do compare-and-swap updates we
# need to manually add support for inserting If-Match header
# into requests. At the moment we only need it for ports.
# Remove when lower-constraints openstacksdk supports this.
os_port.Port.if_match = os_resource.Header('If-Match')
conn = connection.Connection(
session=session,
region_name=getattr(config.CONF.neutron, 'region_name', None))

View File

@ -31,10 +31,10 @@ class NestedDpdkPodVIFDriver(nested_vif.NestedPodVIFDriver):
# TODO(garyloug): maybe log a warning if the vswitch is not ovs-dpdk?
def request_vif(self, pod, project_id, subnets, security_groups):
neutron = clients.get_neutron_client()
os_net = clients.get_network_client()
compute = clients.get_compute_client()
vm_id = self._get_parent_port(neutron, pod)['device_id']
vm_id = self._get_parent_port(pod).device_id
net_id = utils.get_network_id(subnets)
try:
@ -43,7 +43,7 @@ class NestedDpdkPodVIFDriver(nested_vif.NestedPodVIFDriver):
LOG.warning("Unable to create interface for server %s.",
vm_id)
raise
port = neutron.show_port(result.port_id).get('port')
port = os_net.get_port(result.port_id)
return ovu.neutron_to_osvif_vif_dpdk(port, subnets, pod)
def request_vifs(self, pod, project_id, subnets, security_groups,
@ -52,10 +52,9 @@ class NestedDpdkPodVIFDriver(nested_vif.NestedPodVIFDriver):
raise NotImplementedError()
def release_vif(self, pod, vif, project_id=None, security_groups=None):
neutron = clients.get_neutron_client()
compute = clients.get_compute_client()
vm_id = self._get_parent_port(neutron, pod)['device_id']
vm_id = self._get_parent_port(pod).device_id
LOG.debug("release_vif for vm_id %s %s", vm_id, vif.id)
try:

View File

@ -14,13 +14,14 @@
import threading
from neutronclient.common import exceptions as n_exc
from openstack import exceptions as o_exc
from oslo_config import cfg
from oslo_log import log as logging
from kuryr_kubernetes import clients
from kuryr_kubernetes import config as kuryr_config
from kuryr_kubernetes.controller.drivers import nested_vif
from kuryr_kubernetes.controller.drivers import utils
from kuryr_kubernetes import exceptions as k_exc
from kuryr_kubernetes import os_vif_util as ovu
@ -35,7 +36,7 @@ class NestedMacvlanPodVIFDriver(nested_vif.NestedPodVIFDriver):
self.lock = threading.Lock()
def request_vif(self, pod, project_id, subnets, security_groups):
neutron = clients.get_neutron_client()
os_net = clients.get_network_client()
req = self._get_port_request(pod, project_id, subnets,
security_groups)
attempts = kuryr_config.CONF.pod_vif_nested.rev_update_attempts
@ -44,12 +45,12 @@ class NestedMacvlanPodVIFDriver(nested_vif.NestedPodVIFDriver):
vm_port = self._get_parent_port(pod)
if not container_port:
container_port = neutron.create_port({'port': req}).get('port')
_tag_neutron_port(container_port['id'])
container_port = os_net.create_port(**req)
utils.tag_neutron_resources([container_port])
container_mac = container_port['mac_address']
container_mac = container_port.mac_address
container_ips = frozenset(entry['ip_address'] for entry in
container_port['fixed_ips'])
container_port.fixed_ips)
attempts = self._try_update_port(
attempts, self._add_to_allowed_address_pairs, vm_port,
@ -63,23 +64,23 @@ class NestedMacvlanPodVIFDriver(nested_vif.NestedPodVIFDriver):
raise NotImplementedError()
def release_vif(self, pod, vif, project_id=None, security_groups=None):
neutron = clients.get_neutron_client()
os_net = clients.get_network_client()
attempts = kuryr_config.CONF.pod_vif_nested.rev_update_attempts
while attempts > 0:
container_port = neutron.show_port(vif.id).get('port')
container_port = os_net.get_port(vif.id)
container_mac = container_port['mac_address']
container_mac = container_port.mac_address
container_ips = frozenset(entry['ip_address'] for entry in
container_port['fixed_ips'])
container_port.fixed_ips)
vm_port = self._get_parent_port(pod)
attempts = self._try_update_port(
attempts, self._remove_from_allowed_address_pairs,
vm_port, container_ips, container_mac)
try:
neutron.delete_port(vif.id)
except n_exc.PortNotFoundClient:
os_net.delete_port(vif.id, ignore_missing=False)
except o_exc.ResourceNotFound:
LOG.warning("Unable to release port %s as it no longer exists.",
vif.id)
@ -97,10 +98,10 @@ class NestedMacvlanPodVIFDriver(nested_vif.NestedPodVIFDriver):
raise k_exc.IntegrityError(
"Cannot add pair from the "
"allowed_address_pairs of port %s: missing IP address",
port['id'])
port.id)
mac = mac_address if mac_address else port['mac_address']
address_pairs = port['allowed_address_pairs']
mac = mac_address if mac_address else port.mac_address
address_pairs = port.allowed_address_pairs
# look for duplicates or near-matches
for pair in address_pairs:
@ -121,8 +122,8 @@ class NestedMacvlanPodVIFDriver(nested_vif.NestedPodVIFDriver):
address_pairs.append({'ip_address': ip, 'mac_address': mac})
self._update_port_address_pairs(
port['id'], address_pairs,
revision_number=port['revision_number'])
port.id, address_pairs,
revision_number=port.revision_number)
LOG.debug("Added allowed_address_pair %s %s" %
(str(ip_addresses,), mac_address))
@ -133,10 +134,10 @@ class NestedMacvlanPodVIFDriver(nested_vif.NestedPodVIFDriver):
raise k_exc.IntegrityError(
"Cannot remove pair from the "
"allowed_address_pairs of port %s: missing IP address",
port['id'])
port.id)
mac = mac_address if mac_address else port['mac_address']
address_pairs = port['allowed_address_pairs']
mac = mac_address if mac_address else port.mac_address
address_pairs = port.allowed_address_pairs
updated = False
for ip in ip_addresses:
@ -150,18 +151,15 @@ class NestedMacvlanPodVIFDriver(nested_vif.NestedPodVIFDriver):
if updated:
self._update_port_address_pairs(
port['id'],
port.id,
address_pairs,
revision_number=port['revision_number'])
revision_number=port.revision_number)
def _update_port_address_pairs(self, port_id, address_pairs,
revision_number=None):
neutron = clients.get_neutron_client()
neutron.update_port(
port_id,
{'port': {'allowed_address_pairs': address_pairs}},
revision_number=revision_number
)
os_net = clients.get_network_client()
os_net.update_port(port_id, allowed_address_pairs=address_pairs,
if_match=f'revision_number={revision_number}')
def _try_update_port(self, attempts, f,
vm_port, container_ips, container_mac):
@ -169,7 +167,7 @@ class NestedMacvlanPodVIFDriver(nested_vif.NestedPodVIFDriver):
with self.lock:
f(vm_port, container_ips, container_mac)
attempts = 0
except n_exc.NeutronClientException:
except o_exc.SDKException:
attempts -= 1
if attempts == 0:
LOG.exception("Error happened during updating port %s",
@ -177,17 +175,3 @@ class NestedMacvlanPodVIFDriver(nested_vif.NestedPodVIFDriver):
raise
return attempts
def _tag_neutron_port(res_id):
tags = CONF.neutron_defaults.resource_tags
if not tags:
return
neutron = clients.get_neutron_client()
try:
neutron.replace_tag('ports', res_id, body={"tags": tags})
except n_exc.NeutronClientException:
LOG.warning("Failed to tag port %s with %s. Ignoring, but this is "
"still unexpected.", res_id, tags, exc_info=True)

View File

@ -13,7 +13,6 @@
# License for the specific language governing permissions and limitations
# under the License.
from neutronclient.common import exceptions as n_exc
from openstack import exceptions as os_exc
from oslo_config import cfg as oslo_cfg
from oslo_log import log as logging
@ -131,7 +130,7 @@ class VIFHandler(k8s_base.ResourceEventHandler):
try:
self._drv_vif_pool.activate_vif(pod, vif)
changed = True
except n_exc.PortNotFoundClient:
except os_exc.ResourceNotFound:
LOG.debug("Port not found, possibly already "
"deleted. No need to activate it")
finally:

View File

@ -16,7 +16,6 @@
import itertools
import time
from neutronclient.common import exceptions as n_exc
from openstack import exceptions as os_exc
from oslo_log import log as logging
from oslo_utils import excutils
@ -78,12 +77,6 @@ class Retry(base.EventHandler):
try:
self._handler(event)
break
except n_exc.OverQuotaClient:
# NOTE(gryf): this exception handling should be removed after
# nested_macvlan_driver convertion to OpenStackSDK.
with excutils.save_and_reraise_exception() as ex:
if self._sleep(deadline, attempt, ex.value):
ex.reraise = False
except os_exc.ConflictException as ex:
if ex.details.startswith('Quota exceeded for resources'):
with excutils.save_and_reraise_exception() as ex:

View File

@ -90,7 +90,7 @@ def _fake_vifs_string(dictionary=None):
def get_port_obj(port_id='07cfe856-11cc-43d9-9200-ff4dc02d3620',
device_owner='compute:kuryr', ip_address=None,
vif_details=None):
vif_details=None, **kwargs):
fixed_ips = [{'subnet_id': 'e1942bb1-5f51-4646-9885-365b66215592',
'ip_address': '10.10.0.5'},
@ -138,4 +138,5 @@ def get_port_obj(port_id='07cfe856-11cc-43d9-9200-ff4dc02d3620',
'tags': [],
'trunk_details': None,
'updated_at': u'2019-12-04T15:06:09Z'}
port_data.update(kwargs)
return os_port.Port(**port_data)

View File

@ -16,7 +16,6 @@
import mock
import munch
from neutronclient.common import exceptions as n_exc
from openstack import exceptions as os_exc
from openstack.load_balancer.v2 import listener as o_lis
from openstack.load_balancer.v2 import load_balancer as o_lb
@ -110,7 +109,7 @@ class TestLBaaSv2Driver(test_base.TestCase):
d_lbaasv2.LBaaSv2Driver.get_octavia_version(None))
def test_ensure_loadbalancer(self):
neutron = self.useFixture(k_fix.MockNeutronClient()).client
os_net = self.useFixture(k_fix.MockNetworkClient()).client
cls = d_lbaasv2.LBaaSv2Driver
m_driver = mock.Mock(spec=d_lbaasv2.LBaaSv2Driver)
expected_resp = obj_lbaas.LBaaSLoadBalancer(
@ -123,7 +122,7 @@ class TestLBaaSv2Driver(test_base.TestCase):
lb_name = 'just_a_name'
m_driver._ensure.return_value = expected_resp
neutron.update_port = mock.Mock()
os_net.update_port = mock.Mock()
resp = cls.ensure_loadbalancer(m_driver, lb_name, project_id,
subnet_id, ip, sg_ids, 'ClusterIP')
m_driver._ensure.assert_called_once_with(mock.ANY,
@ -135,7 +134,7 @@ class TestLBaaSv2Driver(test_base.TestCase):
self.assertEqual(subnet_id, req.subnet_id)
self.assertEqual(ip, str(req.ip))
self.assertEqual(expected_resp, resp)
neutron.update_port.assert_not_called()
os_net.update_port.assert_not_called()
def test_ensure_loadbalancer_not_ready(self):
cls = d_lbaasv2.LBaaSv2Driver
@ -611,10 +610,10 @@ class TestLBaaSv2Driver(test_base.TestCase):
'loadbalancer_id': pool.loadbalancer_id,
'protocol': pool.protocol,
'lb_algorithm': lb_algorithm}
lbaas.create_pool.side_effect = n_exc.StateInvalidClient
lbaas.create_pool.side_effect = os_exc.BadRequestException
self.assertRaises(n_exc.StateInvalidClient, cls._create_pool, m_driver,
pool)
self.assertRaises(os_exc.BadRequestException, cls._create_pool,
m_driver, pool)
lbaas.create_pool.assert_called_once_with(**req)
def test_find_pool_by_listener(self):

View File

@ -20,7 +20,6 @@ from kuryr_kubernetes.controller.drivers import nested_dpdk_vif
from kuryr_kubernetes.tests import base as test_base
from kuryr_kubernetes.tests.unit import kuryr_fixtures as k_fix
from neutronclient.common import exceptions as ntron_exc
from openstack import exceptions as o_exc
@ -33,7 +32,7 @@ class TestNestedDpdkVIFDriver(test_base.TestCase):
def test_request_vif(self, m_get_network_id, m_to_vif):
cls = nested_dpdk_vif.NestedDpdkPodVIFDriver
m_driver = mock.Mock(spec=cls)
neutron = self.useFixture(k_fix.MockNeutronClient()).client
os_net = self.useFixture(k_fix.MockNetworkClient()).client
compute = self.useFixture(k_fix.MockComputeClient()).client
pod = mock.sentinel.pod
@ -49,22 +48,22 @@ class TestNestedDpdkVIFDriver(test_base.TestCase):
vif = mock.Mock()
result = mock.Mock()
parent_port.__getitem__.return_value = vm_id
parent_port.device_id = vm_id
result.port_id = port_id
compute.create_server_interface.return_value = result
m_to_vif.return_value = vif
m_driver._get_parent_port.return_value = parent_port
m_get_network_id.return_value = net_id
neutron.show_port.return_value.get.return_value = port
os_net.get_port.return_value = port
self.assertEqual(vif, cls.request_vif(m_driver, pod, project_id,
subnets, security_groups))
m_driver._get_parent_port.assert_called_once_with(neutron, pod)
m_driver._get_parent_port.assert_called_once_with(pod)
m_get_network_id.assert_called_once_with(subnets)
compute.create_server_interface.assert_called_once_with(
vm_id, net_id=net_id)
neutron.show_port.assert_called_once_with(result.port_id)
os_net.get_port.assert_called_once_with(result.port_id)
m_to_vif.assert_called_once_with(port, subnets, pod)
@mock.patch(
@ -73,7 +72,7 @@ class TestNestedDpdkVIFDriver(test_base.TestCase):
def test_request_vif_parent_not_found(self, m_get_network_id, m_to_vif):
cls = nested_dpdk_vif.NestedDpdkPodVIFDriver
m_driver = mock.Mock(spec=cls)
neutron = self.useFixture(k_fix.MockNeutronClient()).client
os_net = self.useFixture(k_fix.MockNetworkClient()).client
compute = self.useFixture(k_fix.MockComputeClient()).client
pod = mock.sentinel.pod
@ -94,17 +93,17 @@ class TestNestedDpdkVIFDriver(test_base.TestCase):
compute.create_server_interface.return_value = result
m_to_vif.return_value = vif
m_driver._get_parent_port.side_effect = \
ntron_exc.NeutronClientException
o_exc.SDKException
m_get_network_id.return_value = net_id
neutron.show_port.return_value.get.return_value = port
os_net.get_port.return_value = port
self.assertRaises(ntron_exc.NeutronClientException, cls.request_vif,
self.assertRaises(o_exc.SDKException, cls.request_vif,
m_driver, pod, project_id, subnets, security_groups)
m_driver._get_parent_port.assert_called_once_with(neutron, pod)
m_driver._get_parent_port.assert_called_once_with(pod)
m_get_network_id.assert_not_called()
compute.create_server_interface.assert_not_called()
neutron.show_port.assert_not_called()
os_net.get_port.assert_not_called()
m_to_vif.assert_not_called()
@mock.patch(
@ -113,7 +112,7 @@ class TestNestedDpdkVIFDriver(test_base.TestCase):
def test_request_vif_attach_failed(self, m_get_network_id, m_to_vif):
cls = nested_dpdk_vif.NestedDpdkPodVIFDriver
m_driver = mock.Mock(spec=cls)
neutron = self.useFixture(k_fix.MockNeutronClient()).client
os_net = self.useFixture(k_fix.MockNetworkClient()).client
compute = self.useFixture(k_fix.MockComputeClient()).client
pod = mock.sentinel.pod
@ -129,28 +128,27 @@ class TestNestedDpdkVIFDriver(test_base.TestCase):
vif = mock.Mock()
result = mock.Mock()
parent_port.__getitem__.return_value = vm_id
parent_port.device_id = vm_id
result.port_id = port_id
m_to_vif.return_value = vif
m_driver._get_parent_port.return_value = parent_port
m_get_network_id.return_value = net_id
neutron.show_port.return_value.get.return_value = port
os_net.get_port.return_value = port
compute.create_server_interface.side_effect = o_exc.SDKException
self.assertRaises(o_exc.SDKException, cls.request_vif,
m_driver, pod, project_id, subnets, security_groups)
m_driver._get_parent_port.assert_called_once_with(neutron, pod)
m_driver._get_parent_port.assert_called_once_with(pod)
m_get_network_id.assert_called_once_with(subnets)
compute.create_server_interface.assert_called_once_with(
vm_id, net_id=net_id)
neutron.show_port.assert_not_called()
os_net.get_port.assert_not_called()
m_to_vif.assert_not_called()
def test_release_vif(self):
cls = nested_dpdk_vif.NestedDpdkPodVIFDriver
m_driver = mock.Mock(spec=cls)
neutron = self.useFixture(k_fix.MockNeutronClient()).client
compute = self.useFixture(k_fix.MockComputeClient()).client
port_id = mock.sentinel.port_id
@ -160,20 +158,19 @@ class TestNestedDpdkVIFDriver(test_base.TestCase):
vm_id = mock.sentinel.vm_id
vm_port = mock.MagicMock()
vm_port.__getitem__.return_value = vm_id
vm_port.device_id = vm_id
m_driver._get_parent_port.return_value = vm_port
cls.release_vif(m_driver, pod, vif)
m_driver._get_parent_port.assert_called_once_with(neutron, pod)
m_driver._get_parent_port.assert_called_once_with(pod)
compute.delete_server_interface.assert_called_once_with(
vif.id, server=vm_id)
def test_release_parent_not_found(self):
cls = nested_dpdk_vif.NestedDpdkPodVIFDriver
m_driver = mock.Mock(spec=cls)
neutron = self.useFixture(k_fix.MockNeutronClient()).client
compute = self.useFixture(k_fix.MockComputeClient()).client
pod = mock.sentinel.pod
@ -185,18 +182,17 @@ class TestNestedDpdkVIFDriver(test_base.TestCase):
parent_port.__getitem__.return_value = vm_id
m_driver._get_parent_port.side_effect = \
ntron_exc.NeutronClientException
o_exc.SDKException
self.assertRaises(ntron_exc.NeutronClientException, cls.release_vif,
self.assertRaises(o_exc.SDKException, cls.release_vif,
m_driver, pod, vif)
m_driver._get_parent_port.assert_called_once_with(neutron, pod)
m_driver._get_parent_port.assert_called_once_with(pod)
compute.delete_server_interface.assert_not_called()
def test_release_detach_failed(self):
cls = nested_dpdk_vif.NestedDpdkPodVIFDriver
m_driver = mock.Mock(spec=cls)
neutron = self.useFixture(k_fix.MockNeutronClient()).client
compute = self.useFixture(k_fix.MockComputeClient()).client
pod = mock.sentinel.pod
@ -205,7 +201,7 @@ class TestNestedDpdkVIFDriver(test_base.TestCase):
vm_id = mock.sentinel.parent_port_id
parent_port = mock.MagicMock()
parent_port.__getitem__.return_value = vm_id
parent_port.device_id = vm_id
compute.delete_server_interface.side_effect = o_exc.SDKException
@ -214,7 +210,7 @@ class TestNestedDpdkVIFDriver(test_base.TestCase):
self.assertRaises(o_exc.SDKException, cls.release_vif,
m_driver, pod, vif)
m_driver._get_parent_port.assert_called_once_with(neutron, pod)
m_driver._get_parent_port.assert_called_once_with(pod)
compute.delete_server_interface.assert_called_once_with(
vif.id, server=vm_id)

View File

@ -15,11 +15,12 @@ import mock
import threading
from kuryr.lib import utils as lib_utils
from neutronclient.common import exceptions as n_exc
from openstack import exceptions as o_exc
from kuryr_kubernetes.controller.drivers import nested_macvlan_vif
from kuryr_kubernetes import exceptions as k_exc
from kuryr_kubernetes.tests import base as test_base
from kuryr_kubernetes.tests import fake
from kuryr_kubernetes.tests.unit import kuryr_fixtures as k_fix
@ -31,7 +32,7 @@ class TestNestedMacvlanPodVIFDriver(test_base.TestCase):
def test_request_vif(self, m_to_vif):
cls = nested_macvlan_vif.NestedMacvlanPodVIFDriver
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
@ -39,51 +40,51 @@ class TestNestedMacvlanPodVIFDriver(test_base.TestCase):
security_groups = mock.sentinel.security_groups
container_mac = mock.sentinel.mac_address
container_ip = mock.sentinel.ip_address
container_port = self._get_fake_port(mac_address=container_mac,
ip_address=container_ip)
container_port = fake.get_port_obj(mac_address=container_mac,
ip_address=container_ip)
vif = mock.Mock()
port_request = mock.sentinel.port_request
vm_port = self._get_fake_port()
port_request = {'foo': mock.sentinel.port_request}
vm_port = fake.get_port_obj()
m_to_vif.return_value = vif
m_driver._get_port_request.return_value = port_request
m_driver._get_parent_port.return_value = vm_port
m_driver._try_update_port.return_value = 0
m_driver.lock = mock.MagicMock(spec=threading.Lock())
neutron.create_port.return_value = container_port
os_net.create_port.return_value = container_port
self.assertEqual(vif, cls.request_vif(m_driver, pod, project_id,
subnets, security_groups))
m_driver._get_port_request.assert_called_once_with(
pod, project_id, subnets, security_groups)
neutron.create_port.assert_called_once_with({'port': port_request})
os_net.create_port.assert_called_once_with(**port_request)
m_driver._get_parent_port.assert_called_once_with(pod)
m_driver._try_update_port.assert_called_once()
m_to_vif.assert_called_once_with(container_port['port'], subnets)
m_to_vif.assert_called_once_with(container_port, subnets)
@mock.patch(
'kuryr_kubernetes.os_vif_util.neutron_to_osvif_vif_nested_macvlan')
def test_request_vif_port_create_failed(self, m_to_vif):
cls = nested_macvlan_vif.NestedMacvlanPodVIFDriver
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
subnets = mock.sentinel.subnets
security_groups = mock.sentinel.security_groups
port_request = mock.sentinel.port_request
port_request = {'foo': mock.sentinel.port_request}
m_driver._get_port_request.return_value = port_request
neutron.create_port.side_effect = n_exc.NeutronClientException
os_net.create_port.side_effect = o_exc.SDKException
self.assertRaises(n_exc.NeutronClientException, cls.request_vif,
self.assertRaises(o_exc.SDKException, cls.request_vif,
m_driver, pod, project_id, subnets, security_groups)
m_driver._get_port_request.assert_called_once_with(
pod, project_id, subnets, security_groups)
neutron.create_port.assert_called_once_with({'port': port_request})
os_net.create_port.assert_called_once_with(**port_request)
m_driver._try_update_port.assert_not_called()
m_to_vif.assert_not_called()
@ -92,7 +93,7 @@ class TestNestedMacvlanPodVIFDriver(test_base.TestCase):
def test_request_vif_parent_not_found(self, m_to_vif):
cls = nested_macvlan_vif.NestedMacvlanPodVIFDriver
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
@ -100,20 +101,20 @@ class TestNestedMacvlanPodVIFDriver(test_base.TestCase):
security_groups = mock.sentinel.security_groups
container_mac = mock.sentinel.mac_address
container_ip = mock.sentinel.ip_address
container_port = self._get_fake_port(mac_address=container_mac,
ip_address=container_ip)
container_port = fake.get_port_obj(mac_address=container_mac,
ip_address=container_ip)
port_request = mock.sentinel.port_request
m_driver._get_port_request.return_value = port_request
m_driver.lock = mock.MagicMock(spec=threading.Lock())
neutron.create_port.return_value = container_port
m_driver._get_parent_port.side_effect = n_exc.NeutronClientException
os_net.create_port.return_value = container_port
m_driver._get_parent_port.side_effect = o_exc.SDKException
self.assertRaises(n_exc.NeutronClientException, cls.request_vif,
self.assertRaises(o_exc.SDKException, cls.request_vif,
m_driver, pod, project_id, subnets, security_groups)
m_driver._get_port_request.assert_called_once_with(
pod, project_id, subnets, security_groups)
neutron.create_port.assert_not_called()
os_net.create_port.assert_not_called()
m_driver._get_parent_port.assert_called_once_with(pod)
m_driver._try_update_port.assert_not_called()
m_to_vif.assert_not_called()
@ -121,7 +122,7 @@ class TestNestedMacvlanPodVIFDriver(test_base.TestCase):
def test_release_vif(self):
cls = nested_macvlan_vif.NestedMacvlanPodVIFDriver
m_driver = mock.Mock(spec=cls)
neutron = self.useFixture(k_fix.MockNeutronClient()).client
os_net = self.useFixture(k_fix.MockNetworkClient()).client
port_id = lib_utils.get_hash()
pod = mock.sentinel.pod
@ -130,42 +131,44 @@ class TestNestedMacvlanPodVIFDriver(test_base.TestCase):
container_mac = mock.sentinel.mac_address
container_ip = mock.sentinel.ip_address
container_port = self._get_fake_port(port_id, container_ip,
container_mac)
neutron.show_port.return_value = container_port
container_port = fake.get_port_obj(
port_id=port_id, ip_address=container_ip,
mac_address=container_mac)
os_net.get_port.return_value = container_port
vm_port = self._get_fake_port()
vm_port = fake.get_port_obj()
m_driver._get_parent_port.return_value = vm_port
m_driver._try_update_port.return_value = 0
m_driver.lock = mock.MagicMock(spec=threading.Lock())
cls.release_vif(m_driver, pod, vif)
neutron.show_port.assert_called_once_with(port_id)
os_net.get_port.assert_called_once_with(port_id)
m_driver._get_parent_port.assert_called_once_with(pod)
m_driver._try_update_port.assert_called_once()
neutron.delete_port.assert_called_once_with(vif.id)
os_net.delete_port.assert_called_once_with(vif.id,
ignore_missing=False)
def test_release_vif_not_found(self):
cls = nested_macvlan_vif.NestedMacvlanPodVIFDriver
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
vif = mock.Mock()
vif.id = lib_utils.get_hash()
neutron.show_port.side_effect = n_exc.PortNotFoundClient
os_net.get_port.side_effect = o_exc.NotFoundException
self.assertRaises(n_exc.PortNotFoundClient, cls.release_vif,
self.assertRaises(o_exc.NotFoundException, cls.release_vif,
m_driver, pod, vif)
m_driver._remove_from_allowed_address_pairs.assert_not_called()
neutron.delete_port.assert_not_called()
os_net.delete_port.assert_not_called()
def test_release_vif_parent_not_found(self):
cls = nested_macvlan_vif.NestedMacvlanPodVIFDriver
m_driver = mock.Mock(spec=cls)
neutron = self.useFixture(k_fix.MockNeutronClient()).client
os_net = self.useFixture(k_fix.MockNetworkClient()).client
port_id = lib_utils.get_hash()
pod = mock.sentinel.pod
@ -174,26 +177,27 @@ class TestNestedMacvlanPodVIFDriver(test_base.TestCase):
container_mac = mock.sentinel.mac_address
container_ip = mock.sentinel.ip_address
container_port = self._get_fake_port(port_id, container_ip,
container_mac)
neutron.show_port.return_value = container_port
container_port = fake.get_port_obj(
port_id=port_id, ip_address=container_ip,
mac_address=container_mac)
os_net.get_port.return_value = container_port
m_driver.lock = mock.MagicMock(spec=threading.Lock())
m_driver._get_parent_port.side_effect = n_exc.NeutronClientException
m_driver._get_parent_port.side_effect = o_exc.SDKException
self.assertRaises(n_exc.NeutronClientException, cls.release_vif,
self.assertRaises(o_exc.SDKException, cls.release_vif,
m_driver, pod, vif)
neutron.show_port.assert_called_with(port_id)
self.assertEqual(neutron.show_port.call_count, 1)
os_net.get_port.assert_called_with(port_id)
self.assertEqual(os_net.get_port.call_count, 1)
m_driver._get_parent_port.assert_called_with(pod)
self.assertEqual(m_driver._get_parent_port.call_count, 1)
m_driver._remove_from_allowed_address_pairs.assert_not_called()
neutron.delete_port.assert_not_called()
os_net.delete_port.assert_not_called()
def test_release_vif_delete_failed(self):
cls = nested_macvlan_vif.NestedMacvlanPodVIFDriver
m_driver = mock.Mock(spec=cls)
neutron = self.useFixture(k_fix.MockNeutronClient()).client
os_net = self.useFixture(k_fix.MockNetworkClient()).client
port_id = lib_utils.get_hash()
pod = mock.sentinel.pod
@ -202,22 +206,24 @@ class TestNestedMacvlanPodVIFDriver(test_base.TestCase):
container_mac = mock.sentinel.mac_address
container_ip = mock.sentinel.ip_addresses
container_port = self._get_fake_port(port_id, container_ip,
container_mac)
neutron.show_port.return_value = container_port
neutron.delete_port.side_effect = n_exc.PortNotFoundClient
container_port = fake.get_port_obj(
port_id=port_id, ip_address=container_ip,
mac_address=container_mac)
os_net.get_port.return_value = container_port
os_net.delete_port.side_effect = o_exc.NotFoundException
vm_port = self._get_fake_port()
vm_port = fake.get_port_obj()
m_driver._get_parent_port.return_value = vm_port
m_driver._try_update_port.return_value = 0
m_driver.lock = mock.MagicMock(spec=threading.Lock())
cls.release_vif(m_driver, pod, vif)
neutron.show_port.assert_called_once_with(port_id)
os_net.get_port.assert_called_once_with(port_id)
m_driver._get_parent_port.assert_called_once_with(pod)
m_driver._try_update_port.assert_called_once()
neutron.delete_port.assert_called_once_with(vif.id)
os_net.delete_port.assert_called_once_with(vif.id,
ignore_missing=False)
@ddt.data((False), (True))
def test_activate_vif(self, active_value):
@ -235,10 +241,10 @@ class TestNestedMacvlanPodVIFDriver(test_base.TestCase):
def test_add_to_allowed_address_pairs(self, m_mac):
cls = nested_macvlan_vif.NestedMacvlanPodVIFDriver
m_driver = mock.Mock(spec=cls)
self.useFixture(k_fix.MockNeutronClient()).client
self.useFixture(k_fix.MockNetworkClient()).client
port_id = lib_utils.get_hash()
vm_port = self._get_fake_port(port_id)['port']
vm_port = fake.get_port_obj(port_id)
mac_addr = 'fa:16:3e:1b:30:00' if m_mac else vm_port['mac_address']
address_pairs = [
@ -259,15 +265,15 @@ class TestNestedMacvlanPodVIFDriver(test_base.TestCase):
frozenset([ip_addr]), m_mac)
m_driver._update_port_address_pairs.assert_called_once_with(
port_id, address_pairs, revision_number=1)
port_id, address_pairs, revision_number=9)
def test_add_to_allowed_address_pairs_no_ip_addresses(self):
cls = nested_macvlan_vif.NestedMacvlanPodVIFDriver
m_driver = mock.Mock(spec=cls)
self.useFixture(k_fix.MockNeutronClient()).client
self.useFixture(k_fix.MockNetworkClient()).client
port_id = lib_utils.get_hash()
vm_port = self._get_fake_port(port_id)['port']
vm_port = fake.get_port_obj(port_id)
self.assertRaises(k_exc.IntegrityError,
cls._add_to_allowed_address_pairs, m_driver,
@ -276,10 +282,10 @@ class TestNestedMacvlanPodVIFDriver(test_base.TestCase):
def test_add_to_allowed_address_pairs_same_ip(self):
cls = nested_macvlan_vif.NestedMacvlanPodVIFDriver
m_driver = mock.Mock(spec=cls)
self.useFixture(k_fix.MockNeutronClient()).client
self.useFixture(k_fix.MockNetworkClient()).client
port_id = lib_utils.get_hash()
vm_port = self._get_fake_port(port_id)['port']
vm_port = fake.get_port_obj(port_id)
address_pairs = [
{'ip_address': '10.0.0.30',
'mac_address': 'fa:16:3e:1b:30:00'},
@ -296,15 +302,15 @@ class TestNestedMacvlanPodVIFDriver(test_base.TestCase):
frozenset([ip_addr]), mac_addr)
m_driver._update_port_address_pairs.assert_called_once_with(
port_id, address_pairs, revision_number=1)
port_id, address_pairs, revision_number=9)
def test_add_to_allowed_address_pairs_already_present(self):
cls = nested_macvlan_vif.NestedMacvlanPodVIFDriver
m_driver = mock.Mock(spec=cls)
self.useFixture(k_fix.MockNeutronClient()).client
self.useFixture(k_fix.MockNetworkClient()).client
port_id = lib_utils.get_hash()
vm_port = self._get_fake_port(port_id)['port']
vm_port = fake.get_port_obj(port_id)
address_pairs = [
{'ip_address': '10.0.0.30',
'mac_address': 'fa:16:3e:1b:30:00'},
@ -324,10 +330,10 @@ class TestNestedMacvlanPodVIFDriver(test_base.TestCase):
def test_remove_from_allowed_address_pairs(self, m_mac):
cls = nested_macvlan_vif.NestedMacvlanPodVIFDriver
m_driver = mock.Mock(spec=cls)
self.useFixture(k_fix.MockNeutronClient()).client
self.useFixture(k_fix.MockNetworkClient()).client
port_id = lib_utils.get_hash()
vm_port = self._get_fake_port(port_id)['port']
vm_port = fake.get_port_obj(port_id)
mac_addr = 'fa:16:3e:1b:30:00' if m_mac else vm_port['mac_address']
address_pairs = [
@ -348,15 +354,15 @@ class TestNestedMacvlanPodVIFDriver(test_base.TestCase):
m_driver, vm_port, frozenset([ip_addr]), m_mac)
m_driver._update_port_address_pairs.assert_called_once_with(
port_id, address_pairs, revision_number=1)
port_id, address_pairs, revision_number=9)
def test_remove_from_allowed_address_pairs_no_ip_addresses(self):
cls = nested_macvlan_vif.NestedMacvlanPodVIFDriver
m_driver = mock.Mock(spec=cls)
self.useFixture(k_fix.MockNeutronClient()).client
self.useFixture(k_fix.MockNetworkClient()).client
port_id = lib_utils.get_hash()
vm_port = self._get_fake_port(port_id)['port']
vm_port = fake.get_port_obj(port_id)
self.assertRaises(k_exc.IntegrityError,
cls._remove_from_allowed_address_pairs, m_driver,
@ -366,10 +372,10 @@ class TestNestedMacvlanPodVIFDriver(test_base.TestCase):
def test_remove_from_allowed_address_pairs_missing(self, m_mac):
cls = nested_macvlan_vif.NestedMacvlanPodVIFDriver
m_driver = mock.Mock(spec=cls)
self.useFixture(k_fix.MockNeutronClient()).client
self.useFixture(k_fix.MockNetworkClient()).client
port_id = lib_utils.get_hash()
vm_port = self._get_fake_port(port_id)['port']
vm_port = fake.get_port_obj(port_id)
mac_addr = 'fa:16:3e:1b:30:00' if m_mac else vm_port['mac_address']
address_pairs = [
@ -388,16 +394,16 @@ class TestNestedMacvlanPodVIFDriver(test_base.TestCase):
m_driver, vm_port, frozenset(ip_addr), m_mac)
m_driver._update_port_address_pairs.assert_called_once_with(
port_id, address_pairs, revision_number=1)
port_id, address_pairs, revision_number=9)
@ddt.data((None), ('fa:16:3e:71:cb:80'))
def test_remove_from_allowed_address_pairs_no_update(self, m_mac):
cls = nested_macvlan_vif.NestedMacvlanPodVIFDriver
m_driver = mock.Mock(spec=cls)
self.useFixture(k_fix.MockNeutronClient()).client
self.useFixture(k_fix.MockNetworkClient()).client
port_id = lib_utils.get_hash()
vm_port = self._get_fake_port(port_id)['port']
vm_port = fake.get_port_obj(port_id)
mac_addr = 'fa:16:3e:1b:30:00' if m_mac else vm_port['mac_address']
address_pairs = [
@ -418,36 +424,36 @@ class TestNestedMacvlanPodVIFDriver(test_base.TestCase):
def test_update_port_address_pairs(self):
cls = nested_macvlan_vif.NestedMacvlanPodVIFDriver
m_driver = mock.Mock(spec=cls)
neutron = self.useFixture(k_fix.MockNeutronClient()).client
os_net = self.useFixture(k_fix.MockNetworkClient()).client
port_id = lib_utils.get_hash()
pairs = mock.sentinel.allowed_address_pairs
cls._update_port_address_pairs(m_driver, port_id, pairs,
revision_number=1)
revision_number=9)
neutron.update_port.assert_called_with(
os_net.update_port.assert_called_with(
port_id,
{'port': {'allowed_address_pairs': pairs}},
revision_number=1)
allowed_address_pairs=pairs,
if_match='revision_number=9')
def test_update_port_address_pairs_failure(self):
cls = nested_macvlan_vif.NestedMacvlanPodVIFDriver
m_driver = mock.Mock(spec=cls)
neutron = self.useFixture(k_fix.MockNeutronClient()).client
os_net = self.useFixture(k_fix.MockNetworkClient()).client
port_id = lib_utils.get_hash()
pairs = mock.sentinel.allowed_address_pairs
neutron.update_port.side_effect = n_exc.NeutronClientException
os_net.update_port.side_effect = o_exc.SDKException
self.assertRaises(n_exc.NeutronClientException,
self.assertRaises(o_exc.SDKException,
cls._update_port_address_pairs, m_driver,
port_id, pairs, revision_number=1)
port_id, pairs, revision_number=9)
neutron.update_port.assert_called_with(
os_net.update_port.assert_called_with(
port_id,
{'port': {'allowed_address_pairs': pairs}},
revision_number=1)
allowed_address_pairs=pairs,
if_match='revision_number=9')
@mock.patch('kuryr_kubernetes.controller.drivers.nested_macvlan_vif.'
'NestedMacvlanPodVIFDriver._add_to_allowed_address_pairs')
@ -455,10 +461,10 @@ class TestNestedMacvlanPodVIFDriver(test_base.TestCase):
cls = nested_macvlan_vif.NestedMacvlanPodVIFDriver
m_driver = mock.Mock(spec=cls)
m_driver.lock = mock.MagicMock(spec=threading.Lock())
self.useFixture(k_fix.MockNeutronClient()).client
self.useFixture(k_fix.MockNetworkClient()).client
port_id = lib_utils.get_hash()
vm_port = self._get_fake_port(port_id)['port']
vm_port = fake.get_port_obj(port_id)
mac_addr = 'fa:16:3e:1b:30:00'
address_pairs = [
@ -482,10 +488,10 @@ class TestNestedMacvlanPodVIFDriver(test_base.TestCase):
cls = nested_macvlan_vif.NestedMacvlanPodVIFDriver
m_driver = mock.Mock(spec=cls)
m_driver.lock = mock.MagicMock(spec=threading.Lock())
self.useFixture(k_fix.MockNeutronClient()).client
self.useFixture(k_fix.MockNetworkClient()).client
port_id = lib_utils.get_hash()
vm_port = self._get_fake_port(port_id)['port']
vm_port = fake.get_port_obj(port_id)
mac_addr = 'fa:16:3e:1b:30:00'
address_pairs = [
@ -498,45 +504,8 @@ class TestNestedMacvlanPodVIFDriver(test_base.TestCase):
ip_addr = ['10.0.0.29']
aaapf_mock.side_effect = n_exc.NeutronClientException
self.assertRaises(n_exc.NeutronClientException,
aaapf_mock.side_effect = o_exc.SDKException
self.assertRaises(o_exc.SDKException,
cls._try_update_port, m_driver, 1,
cls._add_to_allowed_address_pairs,
vm_port, frozenset(ip_addr), mac_addr)
# TODO(garyloug) consider exending and moving to a parent class
def _get_fake_port(self, port_id=None, ip_address=None, mac_address=None):
fake_port = {
'port': {
"mac_address": "fa:16:3e:20:57:c4",
"fixed_ips": [],
"id": "07b21ebf-b105-4720-9f2e-95670c4032e4",
"allowed_address_pairs": [],
"revision_number": 1
}
}
if port_id:
fake_port['port']['id'] = port_id
if ip_address:
fake_port['port']['fixed_ips'].append({
"subnet_id": lib_utils.get_hash(),
"ip_address": ip_address
})
if mac_address:
fake_port['port']['mac_address'] = mac_address
return fake_port
def _get_fake_ports(self, ip_address, mac_address):
fake_port = self._get_fake_port(ip_address=ip_address,
mac_address=mac_address)
fake_port = fake_port['port']
fake_ports = {
'ports': [
fake_port
]
}
return fake_ports

View File

@ -119,10 +119,10 @@ class TestFipPubIpDriver(test_base.TestCase):
res_id = mock.sentinel.res_id
vip_port_id = mock.sentinel.vip_port_id
neutron = self.useFixture(k_fix.MockNeutronClient()).client
neutron.update_ip.side_effect = os_exc.ConflictException
neutron.get_ip.return_value = munch.Munch({'id': res_id,
'port_id': vip_port_id})
os_net = self.useFixture(k_fix.MockNetworkClient()).client
os_net.update_ip.side_effect = os_exc.ConflictException
os_net.get_ip.return_value = munch.Munch({'id': res_id,
'port_id': vip_port_id})
self.assertIsNone(driver.associate(res_id, vip_port_id))
def test_associate_conflict_incorrect(self):

View File

@ -15,7 +15,7 @@
import mock
from neutronclient.common import exceptions as n_exc
from openstack import exceptions as o_exc
from kuryr_kubernetes.controller.drivers import base as drivers
from kuryr_kubernetes.controller.drivers import vif_pool
@ -153,9 +153,9 @@ class TestNamespaceHandler(test_base.TestCase):
self._get_net_crd_id.return_value = None
self._get_net_crd.return_value = None
self._create_namespace_network.side_effect = (
n_exc.NeutronClientException)
o_exc.SDKException)
self.assertRaises(n_exc.NeutronClientException,
self.assertRaises(o_exc.SDKException,
namespace.NamespaceHandler.on_present,
self._handler, self._namespace)
@ -226,9 +226,9 @@ class TestNamespaceHandler(test_base.TestCase):
self._add_kuryrnet_crd.return_value = net_crd
self._set_net_crd.side_effect = k_exc.K8sClientException
self._rollback_network_resources.side_effect = (
n_exc.NeutronClientException)
o_exc.SDKException)
self.assertRaises(n_exc.NeutronClientException,
self.assertRaises(o_exc.SDKException,
namespace.NamespaceHandler.on_present,
self._handler, self._namespace)

View File

@ -27,14 +27,6 @@ class MockK8sClient(fixtures.Fixture):
lambda: self.client))
class MockNeutronClient(fixtures.Fixture):
def _setUp(self):
self.client = mock.Mock()
self.useFixture(fixtures.MockPatch(
'kuryr_kubernetes.clients.get_neutron_client',
lambda: self.client))
class MockLBaaSClient(fixtures.Fixture):
def _setUp(self):
self.client = mock.Mock()

View File

@ -24,11 +24,9 @@ class TestK8sClient(test_base.TestCase):
@mock.patch('openstack.connection.Connection')
@mock.patch('kuryr_kubernetes.config.CONF')
@mock.patch('kuryr_kubernetes.k8s_client.K8sClient')
@mock.patch('kuryr.lib.utils.get_neutron_client')
def test_setup_clients(self, m_neutron, m_k8s, m_cfg, m_openstack):
def test_setup_clients(self, m_k8s, m_cfg, m_openstack):
k8s_api_root = 'http://127.0.0.1:1234'
neutron_mock = mock.Mock()
openstacksdk_mock = mock.Mock()
openstacksdk_mock.load_balancer = mock.Mock()
openstacksdk_mock.network = mock.Mock()
@ -36,7 +34,6 @@ class TestK8sClient(test_base.TestCase):
k8s_dummy = object()
m_cfg.kubernetes.api_root = k8s_api_root
m_neutron.return_value = neutron_mock
m_k8s.return_value = k8s_dummy
m_openstack.return_value = openstacksdk_mock
@ -44,7 +41,6 @@ class TestK8sClient(test_base.TestCase):
m_k8s.assert_called_with(k8s_api_root)
self.assertIs(k8s_dummy, clients.get_kubernetes_client())
self.assertIs(neutron_mock, clients.get_neutron_client())
self.assertIs(openstacksdk_mock, clients.get_openstacksdk())
self.assertIs(openstacksdk_mock.load_balancer,
clients.get_loadbalancer_client())