Unify exception thrown in l3-agent-scheduler fails

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
This commit is contained in:
Jaume Devesa 2013-07-24 16:54:09 +02:00
parent ea16d6583a
commit 5db010a9a7
2 changed files with 35 additions and 11 deletions

View 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):

View 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()