From f11707e1807b5c66df8ac632539f81abf92c7a1c Mon Sep 17 00:00:00 2001 From: Slawek Kaplonski Date: Thu, 26 Mar 2020 04:28:16 +0100 Subject: [PATCH] Switch to new engine facade in ML2 unit tests modules Partially-Implements blueprint: enginefacade-switch Change-Id: I4aff27fba6e8dd2e3574ed54dc79f8c75000125d --- .../unit/plugins/ml2/drivers/l2pop/test_db.py | 7 ++-- neutron/tests/unit/plugins/ml2/test_db.py | 2 +- .../tests/unit/plugins/ml2/test_ovo_rpc.py | 3 +- neutron/tests/unit/plugins/ml2/test_plugin.py | 36 +++++++++---------- .../unit/plugins/ml2/test_port_binding.py | 3 +- 5 files changed, 27 insertions(+), 24 deletions(-) diff --git a/neutron/tests/unit/plugins/ml2/drivers/l2pop/test_db.py b/neutron/tests/unit/plugins/ml2/drivers/l2pop/test_db.py index 4ac33caf420..3029a0f2747 100644 --- a/neutron/tests/unit/plugins/ml2/drivers/l2pop/test_db.py +++ b/neutron/tests/unit/plugins/ml2/drivers/l2pop/test_db.py @@ -16,6 +16,7 @@ import netaddr from neutron_lib.api.definitions import portbindings from neutron_lib import constants from neutron_lib import context +from neutron_lib.db import api as db_api from neutron_lib.tests import tools from neutron_lib.utils import net from oslo_utils import uuidutils @@ -53,7 +54,7 @@ class TestL2PopulationDBTestCase(testlib_api.SqlTestCase): network_obj.Network(self.ctx, id=network_id).create() def _create_router(self, distributed=True, ha=False): - with self.ctx.session.begin(subtransactions=True): + with db_api.CONTEXT_WRITER.using(self.ctx): self.ctx.session.add(l3_models.Router(id=TEST_ROUTER_ID)) l3_objs.RouterExtraAttributes( self.ctx, @@ -67,7 +68,7 @@ class TestL2PopulationDBTestCase(testlib_api.SqlTestCase): # Tests should test that host3 is not a HA agent host. helpers.register_l3_agent(HOST_3) helpers.register_ovs_agent(HOST_3, tunneling_ip=HOST_3_TUNNELING_IP) - with self.ctx.session.begin(subtransactions=True): + with db_api.CONTEXT_WRITER.using(self.ctx): network_obj.Network(self.ctx, id=TEST_HA_NETWORK_ID).create() self._create_router(distributed=distributed, ha=True) for state, host in [(constants.HA_ROUTER_STATE_ACTIVE, HOST), @@ -100,7 +101,7 @@ class TestL2PopulationDBTestCase(testlib_api.SqlTestCase): self.assertIsNone(agent) def _setup_port_binding(self, **kwargs): - with self.ctx.session.begin(subtransactions=True): + with db_api.CONTEXT_WRITER.using(self.ctx): mac = netaddr.EUI( net.get_random_mac('fa:16:3e:00:00:00'.split(':')), dialect=netaddr.mac_unix_expanded) diff --git a/neutron/tests/unit/plugins/ml2/test_db.py b/neutron/tests/unit/plugins/ml2/test_db.py index 241b4ef6f5d..3a868da86eb 100644 --- a/neutron/tests/unit/plugins/ml2/test_db.py +++ b/neutron/tests/unit/plugins/ml2/test_db.py @@ -328,7 +328,7 @@ class Ml2DvrDBTestCase(testlib_api.SqlTestCase): return ports def _setup_neutron_router(self): - with self.ctx.session.begin(subtransactions=True): + with db_api.CONTEXT_WRITER.using(self.ctx): router = l3_models.Router() self.ctx.session.add(router) return router diff --git a/neutron/tests/unit/plugins/ml2/test_ovo_rpc.py b/neutron/tests/unit/plugins/ml2/test_ovo_rpc.py index 81b1a2e798e..1a72334885d 100644 --- a/neutron/tests/unit/plugins/ml2/test_ovo_rpc.py +++ b/neutron/tests/unit/plugins/ml2/test_ovo_rpc.py @@ -13,6 +13,7 @@ import mock from neutron_lib import context +from neutron_lib.db import api as db_api from neutron_lib.plugins import directory from neutron.objects import network @@ -93,7 +94,7 @@ class OVOServerRpcInterfaceTestCase(test_plugin.Ml2PluginV2TestCase): def test_transaction_state_error_doesnt_notify(self): # running in a transaction should cause it to skip notification since # fresh reads aren't possible. - with self.ctx.session.begin(): + with db_api.CONTEXT_WRITER.using(self.ctx): self.plugin.create_security_group( self.ctx, {'security_group': {'tenant_id': 'test', 'description': 'desc', diff --git a/neutron/tests/unit/plugins/ml2/test_plugin.py b/neutron/tests/unit/plugins/ml2/test_plugin.py index 4f9df74c768..330345caab6 100644 --- a/neutron/tests/unit/plugins/ml2/test_plugin.py +++ b/neutron/tests/unit/plugins/ml2/test_plugin.py @@ -2394,26 +2394,26 @@ class TestMl2PortBinding(Ml2PluginV2TestCase, self._check_port_binding_profile(port, profile) def test_update_port_binding_host_id_none(self): - with self.port() as port: - plugin = directory.get_plugin() - binding = p_utils.get_port_binding_by_status_and_host( - plugin._get_port(self.context, - port['port']['id']).port_bindings, - constants.ACTIVE) - with self.context.session.begin(subtransactions=True): + with db_api.CONTEXT_WRITER.using(self.context): + with self.port() as port: + plugin = directory.get_plugin() + binding = p_utils.get_port_binding_by_status_and_host( + plugin._get_port(self.context, + port['port']['id']).port_bindings, + constants.ACTIVE) binding.host = 'test' - mech_context = driver_context.PortContext( - plugin, self.context, port['port'], - plugin.get_network(self.context, port['port']['network_id']), - binding, None) - with mock.patch('neutron.plugins.ml2.plugin.Ml2Plugin.' - '_update_port_dict_binding') as update_mock: - attrs = {portbindings.HOST_ID: None} - self.assertEqual('test', binding.host) - with self.context.session.begin(subtransactions=True): + mech_context = driver_context.PortContext( + plugin, self.context, port['port'], + plugin.get_network( + self.context, port['port']['network_id']), + binding, None) + with mock.patch('neutron.plugins.ml2.plugin.Ml2Plugin.' + '_update_port_dict_binding') as update_mock: + attrs = {portbindings.HOST_ID: None} + self.assertEqual('test', binding.host) plugin._process_port_binding(mech_context, attrs) - self.assertTrue(update_mock.mock_calls) - self.assertEqual('', binding.host) + self.assertTrue(update_mock.mock_calls) + self.assertEqual('', binding.host) def test_update_port_binding_host_id_not_changed(self): with self.port() as port: diff --git a/neutron/tests/unit/plugins/ml2/test_port_binding.py b/neutron/tests/unit/plugins/ml2/test_port_binding.py index 39fa94a3a0a..a83f8fc74cf 100644 --- a/neutron/tests/unit/plugins/ml2/test_port_binding.py +++ b/neutron/tests/unit/plugins/ml2/test_port_binding.py @@ -18,6 +18,7 @@ from neutron_lib.api.definitions import portbindings from neutron_lib.api.definitions import portbindings_extended as pbe_ext from neutron_lib import constants as const from neutron_lib import context +from neutron_lib.db import api as db_api from neutron_lib import exceptions from neutron_lib.plugins import directory from neutron_lib.plugins import utils @@ -118,7 +119,7 @@ class PortBindingTestCase(test_plugin.NeutronDbPluginV2TestCase): ctx = context.get_admin_context() with self.port(name='name') as port: # emulating concurrent binding deletion - with ctx.session.begin(): + with db_api.CONTEXT_WRITER.using(ctx): for item in (ctx.session.query(ml2_models.PortBinding). filter_by(port_id=port['port']['id'])): ctx.session.delete(item)