From 4916fe115fea151b36f6d63d45ea1dbe8e7a003f Mon Sep 17 00:00:00 2001 From: Erik Olof Gunnar Andersson Date: Wed, 11 Mar 2020 18:33:26 -0700 Subject: [PATCH] Fix issue with neutron_api tests There is a potential race condition when running the unit tests with how eventlet is loaded. This patch changes the neutron api implementation to use GreenThreadPoolExecutor to make sure that it does not fail depending on when eventlet applies it's monkey patching. Change-Id: I051d3ad8cd5af9b52d90dc28ebe99a3f0c8a17f2 --- designate/network_api/neutron.py | 3 +- designate/tests/test_network_api/__init__.py | 0 .../tests/test_network_api/test_neutron.py | 52 ------- .../tests/unit/network_api/test_neutron.py | 140 ++++++++++++++++++ 4 files changed, 142 insertions(+), 53 deletions(-) delete mode 100644 designate/tests/test_network_api/__init__.py delete mode 100644 designate/tests/test_network_api/test_neutron.py create mode 100644 designate/tests/unit/network_api/test_neutron.py diff --git a/designate/network_api/neutron.py b/designate/network_api/neutron.py index 91e5bc962..111da5a27 100644 --- a/designate/network_api/neutron.py +++ b/designate/network_api/neutron.py @@ -15,6 +15,7 @@ # # Copied partially from nova import concurrent.futures +import futurist from neutronclient.common import exceptions as neutron_exceptions from neutronclient.v2_0 import client as clientv20 from oslo_config import cfg @@ -66,7 +67,7 @@ def list_floatingips(self, context, region=None): ) floating_ips = [] - with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor: + with futurist.GreenThreadPoolExecutor(max_workers=5) as executor: executors = [ executor.submit( self._get_floating_ips, diff --git a/designate/tests/test_network_api/__init__.py b/designate/tests/test_network_api/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/designate/tests/test_network_api/test_neutron.py b/designate/tests/test_network_api/test_neutron.py deleted file mode 100644 index 21de801f5..000000000 --- a/designate/tests/test_network_api/test_neutron.py +++ /dev/null @@ -1,52 +0,0 @@ -# Copyright 2013 Hewlett-Packard Development Company, L.P. -# -# Author: Endre Karlson -# -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. -from neutronclient.v2_0 import client as clientv20 -from neutronclient.common import exceptions as neutron_exceptions -from oslo_config import cfg -from mock import patch -import testtools - -from designate import exceptions -from designate.network_api import get_network_api -from designate.tests import TestCase - - -cfg.CONF.import_group('network_api:neutron', 'designate.network_api.neutron') - - -class NeutronAPITest(TestCase): - def setUp(self): - super(NeutronAPITest, self).setUp() - self.config(endpoints=['RegionOne|http://localhost:9696'], - group='network_api:neutron') - self.api = get_network_api('neutron') - - @patch.object(clientv20.Client, 'list_floatingips', - side_effect=neutron_exceptions.Unauthorized) - def test_unauthorized_returns_empty(self, _): - context = self.get_context(project_id='a', auth_token='test') - - fips = self.api.list_floatingips(context) - self.assertEqual(0, len(fips)) - - @patch.object(clientv20.Client, 'list_floatingips', - side_effect=neutron_exceptions.NeutronException) - def test_communication_failure(self, _): - context = self.get_context(project_id='a', auth_token='test') - - with testtools.ExpectedException( - exceptions.NeutronCommunicationFailure): - self.api.list_floatingips(context) diff --git a/designate/tests/unit/network_api/test_neutron.py b/designate/tests/unit/network_api/test_neutron.py new file mode 100644 index 000000000..149790059 --- /dev/null +++ b/designate/tests/unit/network_api/test_neutron.py @@ -0,0 +1,140 @@ +# Copyright 2013 Hewlett-Packard Development Company, L.P. +# +# Author: Endre Karlson +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +import mock +from neutronclient.common import exceptions as neutron_exceptions +from neutronclient.v2_0 import client as clientv20 +from oslo_config import cfg +from oslo_config import fixture as cfg_fixture +import oslotest.base + +from designate import context +from designate import exceptions +from designate.network_api import get_network_api +from designate.network_api import neutron + +CONF = cfg.CONF + + +class NeutronNetworkAPITest(oslotest.base.BaseTestCase): + def setUp(self): + super(NeutronNetworkAPITest, self).setUp() + self.useFixture(cfg_fixture.Config(CONF)) + + CONF.set_override( + 'endpoints', ['RegionOne|http://localhost:9696'], + 'network_api:neutron' + ) + + self.api = get_network_api('neutron') + self.context = context.DesignateContext( + user_id='12345', project_id='54321', + ) + + @mock.patch.object(clientv20, 'Client') + def test_get_client(self, mock_client): + neutron.get_client(self.context, 'http://localhost:9696') + + kwargs = mock_client.call_args.kwargs + + self.assertIn('endpoint_url', kwargs) + self.assertIn('timeout', kwargs) + self.assertIn('insecure', kwargs) + self.assertIn('ca_cert', kwargs) + + self.assertNotIn('token', kwargs) + self.assertNotIn('username', kwargs) + + self.assertEqual('http://localhost:9696', kwargs['endpoint_url']) + + @mock.patch.object(clientv20, 'Client') + def test_get_client_using_token(self, mock_client): + self.context = context.DesignateContext( + user_id='12345', project_id='54321', auth_token='token', + ) + + neutron.get_client(self.context, 'http://localhost:9696') + + kwargs = mock_client.call_args.kwargs + + self.assertIn('token', kwargs) + self.assertIn('auth_strategy', kwargs) + self.assertNotIn('username', kwargs) + + self.assertEqual('http://localhost:9696', kwargs['endpoint_url']) + self.assertEqual(kwargs['token'], self.context.auth_token) + + @mock.patch.object(clientv20, 'Client') + def test_get_client_using_admin(self, mock_client): + CONF.set_override( + 'admin_username', 'test', + 'network_api:neutron' + ) + + neutron.get_client(self.context, 'http://localhost:9696') + + kwargs = mock_client.call_args.kwargs + + self.assertIn('auth_strategy', kwargs) + self.assertIn('username', kwargs) + self.assertIn('project_name', kwargs) + self.assertIn('password', kwargs) + self.assertIn('auth_url', kwargs) + self.assertNotIn('token', kwargs) + + self.assertEqual('http://localhost:9696', kwargs['endpoint_url']) + self.assertEqual( + kwargs['username'], CONF['network_api:neutron'].admin_username + ) + + @mock.patch.object(neutron, 'get_client') + def test_list_floatingips(self, get_client): + driver = mock.Mock() + driver.list_floatingips.return_value = {'floatingips': [ + { + 'id': '123', + 'floating_ip_address': '192.168.0.100', + 'region': 'RegionOne' + }, + { + 'id': '456', + 'floating_ip_address': '192.168.0.200', + 'region': 'RegionOne' + }, + ]} + get_client.return_value = driver + + self.assertEqual(2, len(self.api.list_floatingips(self.context))) + + @mock.patch.object(neutron, 'get_client') + def test_list_floatingips_unauthorized(self, get_client): + driver = mock.Mock() + driver.list_floatingips.side_effect = neutron_exceptions.Unauthorized + get_client.return_value = driver + + self.assertEqual(0, len(self.api.list_floatingips(self.context))) + + @mock.patch.object(neutron, 'get_client') + def test_list_floatingips_communication_failure(self, get_client): + driver = mock.Mock() + driver.list_floatingips.side_effect = ( + neutron_exceptions.NeutronException + ) + get_client.return_value = driver + + self.assertRaises( + exceptions.NeutronCommunicationFailure, + self.api.list_floatingips, self.context + )