Commit c2fb1ab6 authored by Peter V. Saveliev's avatar Peter V. Saveliev

ipdb: fix prioritized routes replacement

Bug-Url: https://github.com/svinota/pyroute2/issues/310
parent ce1fa119
......@@ -393,6 +393,9 @@ class BaseRoute(Transactional):
old_key = self.make_key(self)
new_key = self.make_key(transaction)
if old_key != new_key:
# delete old record
if devop == 'set':
self.nl.route('del', **dict(old_key._asdict()))
# assume we can not move routes between tables (yet ;)
if self['family'] == AF_MPLS:
route_index = self.ipdb.routes.tables['mpls'].idx
......@@ -401,15 +404,16 @@ class BaseRoute(Transactional):
.routes
.tables[self['table'] or 254]
.idx)
if new_key not in route_index:
# re-link the route record
if new_key in route_index:
raise CommitException('route idx conflict')
else:
route_index[new_key] = {'key': new_key,
'route': self}
else:
raise CommitException('route idx conflict')
self.nl.route(devop, **transaction)
del route_index[old_key]
else:
self.nl.route(devop, **transaction)
# wipe the old key, if needed
if old_key in route_index:
del route_index[old_key]
self.nl.route(devop, **transaction)
transaction.wait_all_targets()
for key in ('metrics', 'via'):
if transaction[key] and transaction[key]._targets:
......
......@@ -997,6 +997,26 @@ class TestExplicit(BasicSetup):
with self.ip.routes['172.16.0.0/24'] as r:
r.remove()
def test_routes_set_priority(self):
require_user('root')
assert '172.16.0.0/24' not in self.ip.routes
with self.ip.routes.add({'dst': '172.16.0.0/24',
'gateway': '127.0.0.2',
'priority': 100}) as r:
pass
assert '172.16.0.0/24' in self.ip.routes.keys()
assert grep('ip ro', pattern='172.16.0.0/24.*127.0.0.2.*100')
# change the priority
with self.ip.routes['172.16.0.0/24'] as r:
r.priority = 287
# check the changes
assert len(grep('ip ro', pattern='172.16.0.0/24.*127.0.0.2')) == 1
assert grep('ip ro', pattern='172.16.0.0/24.*127.0.0.2.*287')
assert self.ip.routes['172.16.0.0/24'].priority == 287
# delete the route
with self.ip.routes['172.16.0.0/24'] as r:
r.remove()
def test_routes(self):
require_user('root')
assert '172.16.0.0/24' not in self.ip.routes
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment