Skip to content

Commit

Permalink
iproute: fix route filter
Browse files Browse the repository at this point in the history
* make show command eq dump
* add tests

Bug-Url: #1225
  • Loading branch information
svinota committed Jan 8, 2025
1 parent 44aa658 commit 05ada93
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 4 deletions.
8 changes: 4 additions & 4 deletions pyroute2/iproute/linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def get_default_request_filters(mode, command):
'addr': [AddressFieldFilter(), AddressIPRouteFilter(command)],
'neigh': [NeighbourFieldFilter(), NeighbourIPRouteFilter(command)],
'route': [
RouteFieldFilter(add_defaults=(command != 'dump')),
RouteFieldFilter(add_defaults=(command not in ('dump', 'show'))),
RouteIPRouteFilter(command),
],
'rule': [RuleFieldFilter(), RuleIPRouteFilter(command)],
Expand All @@ -118,7 +118,7 @@ def get_default_request_filters(mode, command):
def get_dump_filter(mode, command, query):
if 'dump_filter' in query:
return query.pop('dump_filter'), query
if command != 'dump':
if command not in ('dump', 'show'):
return RequestProcessor(), query
new_query = {}
if 'family' in query:
Expand Down Expand Up @@ -2233,7 +2233,7 @@ async def route(self, command, **kwarg):
self, msg, command, command_map, dump_filter, arguments
)
await request.send()
if command == 'dump':
if command in ('dump', 'show'):
return request.response()
return [x async for x in request.response()]

Expand Down Expand Up @@ -2480,7 +2480,7 @@ async def collect_op():
if (
len(argv) > 0
and isinstance(argv[0], str)
and argv[0].startswith('dump')
and (argv[0].startswith('dump') or argv[0].startswith('show'))
):
task = collect_dump
else:
Expand Down
40 changes: 40 additions & 0 deletions tests/test_core/test_ipr/test_route_async.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import pytest

from pyroute2 import AsyncIPRoute


@pytest.mark.parametrize(
"command,kwarg",
[
("dump", {"table": 255}),
("show", {"table": 255}),
("dump", {"match": {"table": 255}}),
("show", {"match": {"table": 255}}),
],
)
@pytest.mark.asyncio
async def test_route_filter(async_ipr, command, kwarg):
assert set(
[
route.get('table')
async for route in await async_ipr.route(command, **kwarg)
]
) == set([255])


@pytest.mark.parametrize(
"command,kwarg",
[
("dump", {"table": 255, "family": 1}),
("show", {"table": 255, "family": 1}),
],
)
@pytest.mark.asyncio
async def test_route_filter_strict(command, kwarg):
async with AsyncIPRoute(strict_check=True) as ipr:
assert set(
[
route.get('table')
async for route in await ipr.route(command, **kwarg)
]
) == set([255])
32 changes: 32 additions & 0 deletions tests/test_core/test_ipr/test_route_sync.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import pytest

from pyroute2 import IPRoute


@pytest.mark.parametrize(
"command,kwarg",
[
("dump", {"table": 255}),
("show", {"table": 255}),
("dump", {"match": {"table": 255}}),
("show", {"match": {"table": 255}}),
],
)
def test_route_filter(sync_ipr, command, kwarg):
assert set(
[route.get('table') for route in sync_ipr.route(command, **kwarg)]
) == set([255])


@pytest.mark.parametrize(
"command,kwarg",
[
("dump", {"table": 255, "family": 1}),
("show", {"table": 255, "family": 1}),
],
)
def test_route_filter_strict(command, kwarg):
with IPRoute(strict_check=True) as ipr:
assert set(
[route.get('table') for route in ipr.route(command, **kwarg)]
) == set([255])

0 comments on commit 05ada93

Please sign in to comment.