]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Separate l3 db tests for l3 agent into their own test case
authorSalvatore Orlando <salv.orlando@gmail.com>
Fri, 23 Aug 2013 10:37:57 +0000 (03:37 -0700)
committerSalvatore Orlando <salv.orlando@gmail.com>
Fri, 23 Aug 2013 10:45:52 +0000 (03:45 -0700)
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

index 3ce117cd1a315dfc8216c1fb88c7bec668bb8e94..14c9b221244ac4b72e4d107f3c3911621dbe2d28 100644 (file)
@@ -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):