]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Unify exception thrown in l3-agent-scheduler fails
authorJaume Devesa <devvesa@gmail.com>
Wed, 24 Jul 2013 14:54:09 +0000 (16:54 +0200)
committerJaume Devesa <devvesa@gmail.com>
Thu, 15 Aug 2013 08:50:34 +0000 (10:50 +0200)
Since you can only attach a single l3 agent to a router, when you try
to add another l3 agent to a router that already have one, the l3
agent scheduler raises an exception.

This fix removes the discrimination by id: either it is the same agent
or another one, the router can not be hosted and the same exception is
raised.

Change-Id: If832bbd4bf17e4e0c4720172aded4c9fffedc6fc
Fixes: bug #1154622
neutron/db/agentschedulers_db.py
neutron/tests/unit/openvswitch/test_agent_scheduler.py

index d44b4b509f20e377d81e781a7538520b2858308a..94866c8b305d8d14fdb3e3c0e191147b35444737 100644 (file)
@@ -132,12 +132,11 @@ class L3AgentSchedulerDbMixin(l3agentscheduler.L3AgentSchedulerPluginBase,
                 raise l3agentscheduler.InvalidL3Agent(id=id)
             query = context.session.query(RouterL3AgentBinding)
             try:
-                binding = query.filter(
-                    RouterL3AgentBinding.l3_agent_id == agent_db.id,
-                    RouterL3AgentBinding.router_id == router_id).one()
-                if binding:
-                    raise l3agentscheduler.RouterHostedByL3Agent(
-                        router_id=router_id, agent_id=id)
+                binding = query.filter_by(router_id=router_id).one()
+
+                raise l3agentscheduler.RouterHostedByL3Agent(
+                    router_id=router_id,
+                    agent_id=binding.l3_agent_id)
             except exc.NoResultFound:
                 pass
 
@@ -229,7 +228,7 @@ class L3AgentSchedulerDbMixin(l3agentscheduler.L3AgentSchedulerPluginBase,
             l3_agents = [l3_agent for l3_agent in
                          l3_agents if not
                          agents_db.AgentDbMixin.is_agent_down(
-                         l3_agent['heartbeat_timestamp'])]
+                             l3_agent['heartbeat_timestamp'])]
         return l3_agents
 
     def _get_l3_bindings_hosting_routers(self, context, router_ids):
index bc206662f11e180405a98f64bbdc79516f8a6d6a..c843a2b10888d0a57f003ea26674ae9cf0c5996a 100644 (file)
@@ -825,10 +825,11 @@ class OvsAgentSchedulerTestCase(test_l3_plugin.L3NatTestCaseMixin,
         self.assertEqual(0, len(l3agents))
 
     def test_router_sync_data(self):
-        with contextlib.nested(self.subnet(),
-                               self.subnet(cidr='10.0.2.0/24'),
-                               self.subnet(cidr='10.0.3.0/24')) as (
-                                   s1, s2, s3):
+        with contextlib.nested(
+            self.subnet(),
+            self.subnet(cidr='10.0.2.0/24'),
+            self.subnet(cidr='10.0.3.0/24')
+        ) as (s1, s2, s3):
             self._register_agent_states()
             self._set_net_external(s1['subnet']['network_id'])
             data = {'router': {'tenant_id': uuidutils.generate_uuid()}}
@@ -908,6 +909,30 @@ class OvsAgentSchedulerTestCase(test_l3_plugin.L3NatTestCaseMixin,
         self.assertEqual(0, num_before_add)
         self.assertEqual(1, num_after_add)
 
+    def test_router_add_to_l3_agent_two_times(self):
+        with self.router() as router1:
+            self._register_agent_states()
+            hosta_id = self._get_agent_id(constants.AGENT_TYPE_L3,
+                                          L3_HOSTA)
+            self._add_router_to_l3_agent(hosta_id,
+                                         router1['router']['id'])
+            self._add_router_to_l3_agent(hosta_id,
+                                         router1['router']['id'],
+                                         expected_code=exc.HTTPConflict.code)
+
+    def test_router_add_to_two_l3_agents(self):
+        with self.router() as router1:
+            self._register_agent_states()
+            hosta_id = self._get_agent_id(constants.AGENT_TYPE_L3,
+                                          L3_HOSTA)
+            hostb_id = self._get_agent_id(constants.AGENT_TYPE_L3,
+                                          L3_HOSTB)
+            self._add_router_to_l3_agent(hosta_id,
+                                         router1['router']['id'])
+            self._add_router_to_l3_agent(hostb_id,
+                                         router1['router']['id'],
+                                         expected_code=exc.HTTPConflict.code)
+
     def test_router_policy(self):
         with self.router() as router1:
             self._register_agent_states()