From 716af601730402859f101758ac6928b450fa8ac1 Mon Sep 17 00:00:00 2001 From: Salvatore Orlando Date: Fri, 23 Aug 2013 03:37:57 -0700 Subject: [PATCH] Separate l3 db tests for l3 agent into their own test case Bug 1215871 This patch does a simple refactoring of test_l3_plugin, pushing out tests aimed at validating the interactions of the l3 agent with the server. These tests explicitly use TestL3NatPlugin, whereas all the other tests use a configurable plugin, which might be specificed by a child class. This might lead to confusion and possibly errors in unit tests for child classes, especially those not using the l3 agent - for which running these test is also superfluous. Change-Id: Ia9ed320ea775fc548e6a5b711c67c9f3b0ae2f6d --- neutron/tests/unit/test_l3_plugin.py | 162 ++++++++++++++------------- 1 file changed, 83 insertions(+), 79 deletions(-) diff --git a/neutron/tests/unit/test_l3_plugin.py b/neutron/tests/unit/test_l3_plugin.py index 3ce117cd1..14c9b2212 100644 --- a/neutron/tests/unit/test_l3_plugin.py +++ b/neutron/tests/unit/test_l3_plugin.py @@ -1568,65 +1568,29 @@ class L3NatDBTestCase(L3NatTestCaseBase): self.assertEqual(ext_net['network'][l3.EXTERNAL], True) - def _test_notify_op_agent(self, target_func, *args): - l3_rpc_agent_api_str = ( - 'neutron.api.rpc.agentnotifiers.l3_rpc_agent_api.L3AgentNotifyAPI') - oldNotify = l3_rpc_agent_api.L3AgentNotify - try: - with mock.patch(l3_rpc_agent_api_str) as notifyApi: - l3_rpc_agent_api.L3AgentNotify = notifyApi - kargs = [item for item in args] - kargs.append(notifyApi) - target_func(*kargs) - except Exception: - l3_rpc_agent_api.L3AgentNotify = oldNotify - raise - else: - l3_rpc_agent_api.L3AgentNotify = oldNotify - - def _test_router_gateway_op_agent(self, notifyApi): + def test_router_delete_subnet_inuse_returns_409(self): with self.router() as r: with self.subnet() as s: - self._set_net_external(s['subnet']['network_id']) - self._add_external_gateway_to_router( - r['router']['id'], - s['subnet']['network_id']) - self._remove_external_gateway_from_router( - r['router']['id'], - s['subnet']['network_id']) - self.assertEqual( - 2, notifyApi.routers_updated.call_count) - - def test_router_gateway_op_agent(self): - self._test_notify_op_agent(self._test_router_gateway_op_agent) - - def _test_interfaces_op_agent(self, r, notifyApi): - with self.port(no_delete=True) as p: - self._router_interface_action('add', - r['router']['id'], - None, - p['port']['id']) - # clean-up - self._router_interface_action('remove', - r['router']['id'], - None, - p['port']['id']) - self.assertEqual(2, notifyApi.routers_updated.call_count) + self._router_interface_action('add', + r['router']['id'], + s['subnet']['id'], + None) + # subnet cannot be delete as it's attached to a router + self._delete('subnets', s['subnet']['id'], + expected_code=exc.HTTPConflict.code) + # remove interface so test can exit without errors + self._router_interface_action('remove', + r['router']['id'], + s['subnet']['id'], + None) - def test_interfaces_op_agent(self): - with self.router() as r: - self._test_notify_op_agent( - self._test_interfaces_op_agent, r) - def _test_floatingips_op_agent(self, notifyApi): - with self.floatingip_with_assoc(): - pass - # add gateway, add interface, associate, deletion of floatingip, - # delete gateway, delete interface - self.assertEqual(6, notifyApi.routers_updated.call_count) +class L3AgentDbTestCase(L3NatTestCaseBase): + """Unit tests for methods called by the L3 agent.""" - def test_floatingips_op_agent(self): - self._test_notify_op_agent(self._test_floatingips_op_agent) + def setUp(self): + self.plugin = TestL3NatPlugin() + super(L3AgentDbTestCase, self).setUp() def test_l3_agent_routers_query_interfaces(self): with self.router() as r: @@ -1636,9 +1600,8 @@ class L3NatDBTestCase(L3NatTestCaseBase): None, p['port']['id']) - plugin = TestL3NatPlugin() - routers = plugin.get_sync_data(context.get_admin_context(), - None) + routers = self.plugin.get_sync_data( + context.get_admin_context(), None) self.assertEqual(1, len(routers)) interfaces = routers[0][l3_constants.INTERFACE_KEY] self.assertEqual(1, len(interfaces)) @@ -1666,10 +1629,9 @@ class L3NatDBTestCase(L3NatTestCaseBase): 'subnet_id': subnet['subnet']['id']}, {'ip_address': '9.0.1.5', 'subnet_id': subnet['subnet']['id']}]}} - plugin = TestL3NatPlugin() ctx = context.get_admin_context() - plugin.update_port(ctx, p['port']['id'], port) - routers = plugin.get_sync_data(ctx, None) + self.plugin.update_port(ctx, p['port']['id'], port) + routers = self.plugin.get_sync_data(ctx, None) self.assertEqual(1, len(routers)) interfaces = routers[0].get(l3_constants.INTERFACE_KEY, []) self.assertEqual(1, len(interfaces)) @@ -1686,9 +1648,8 @@ class L3NatDBTestCase(L3NatTestCaseBase): self._add_external_gateway_to_router( r['router']['id'], s['subnet']['network_id']) - plugin = TestL3NatPlugin() - routers = plugin.get_sync_data(context.get_admin_context(), - [r['router']['id']]) + routers = self.plugin.get_sync_data( + context.get_admin_context(), [r['router']['id']]) self.assertEqual(1, len(routers)) gw_port = routers[0]['gw_port'] self.assertEqual(s['subnet']['id'], gw_port['subnet']['id']) @@ -1698,9 +1659,8 @@ class L3NatDBTestCase(L3NatTestCaseBase): def test_l3_agent_routers_query_floatingips(self): with self.floatingip_with_assoc() as fip: - plugin = TestL3NatPlugin() - routers = plugin.get_sync_data(context.get_admin_context(), - [fip['floatingip']['router_id']]) + routers = self.plugin.get_sync_data( + context.get_admin_context(), [fip['floatingip']['router_id']]) self.assertEqual(1, len(routers)) floatingips = routers[0][l3_constants.FLOATINGIP_KEY] self.assertEqual(1, len(floatingips)) @@ -1711,21 +1671,65 @@ class L3NatDBTestCase(L3NatTestCaseBase): self.assertTrue(floatingips[0]['fixed_ip_address'] is not None) self.assertTrue(floatingips[0]['router_id'] is not None) - def test_router_delete_subnet_inuse_returns_409(self): + def _test_notify_op_agent(self, target_func, *args): + l3_rpc_agent_api_str = ( + 'neutron.api.rpc.agentnotifiers.l3_rpc_agent_api.L3AgentNotifyAPI') + oldNotify = l3_rpc_agent_api.L3AgentNotify + try: + with mock.patch(l3_rpc_agent_api_str) as notifyApi: + l3_rpc_agent_api.L3AgentNotify = notifyApi + kargs = [item for item in args] + kargs.append(notifyApi) + target_func(*kargs) + except Exception: + l3_rpc_agent_api.L3AgentNotify = oldNotify + raise + else: + l3_rpc_agent_api.L3AgentNotify = oldNotify + + def _test_router_gateway_op_agent(self, notifyApi): with self.router() as r: with self.subnet() as s: - self._router_interface_action('add', - r['router']['id'], - s['subnet']['id'], - None) - # subnet cannot be delete as it's attached to a router - self._delete('subnets', s['subnet']['id'], - expected_code=exc.HTTPConflict.code) - # remove interface so test can exit without errors - self._router_interface_action('remove', - r['router']['id'], - s['subnet']['id'], - None) + self._set_net_external(s['subnet']['network_id']) + self._add_external_gateway_to_router( + r['router']['id'], + s['subnet']['network_id']) + self._remove_external_gateway_from_router( + r['router']['id'], + s['subnet']['network_id']) + self.assertEqual( + 2, notifyApi.routers_updated.call_count) + + def test_router_gateway_op_agent(self): + self._test_notify_op_agent(self._test_router_gateway_op_agent) + + def _test_interfaces_op_agent(self, r, notifyApi): + with self.port(no_delete=True) as p: + self._router_interface_action('add', + r['router']['id'], + None, + p['port']['id']) + # clean-up + self._router_interface_action('remove', + r['router']['id'], + None, + p['port']['id']) + self.assertEqual(2, notifyApi.routers_updated.call_count) + + def test_interfaces_op_agent(self): + with self.router() as r: + self._test_notify_op_agent( + self._test_interfaces_op_agent, r) + + def _test_floatingips_op_agent(self, notifyApi): + with self.floatingip_with_assoc(): + pass + # add gateway, add interface, associate, deletion of floatingip, + # delete gateway, delete interface + self.assertEqual(6, notifyApi.routers_updated.call_count) + + def test_floatingips_op_agent(self): + self._test_notify_op_agent(self._test_floatingips_op_agent) class L3NatDBTestCaseXML(L3NatDBTestCase): -- 2.45.2