Skip to content

Commit

Permalink
requests: Merge pull request #1252 from svinota/1246-strict-rule
Browse files Browse the repository at this point in the history
requests: fix rule (src|dst)(_len)

Bug-Url: #1252
Bug-Url: #1246
  • Loading branch information
svinota authored Jan 21, 2025
2 parents 250824b + cb7034d commit 671dc99
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
5 changes: 4 additions & 1 deletion pyroute2/requests/rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ def finalize(self, context):
if 'table' in context and 'action' not in context:
context['action'] = 'to_tbl'
for key in ('src_len', 'dst_len'):
if context.get(key, None) is None and key[:3] in context:
if (
context.get(key, None) is None
and context.get(key[:3], None) is not None
):
context[key] = {socket.AF_INET6: 128, socket.AF_INET: 32}[
context['family']
]
8 changes: 8 additions & 0 deletions tests/test_core/pr2test/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ def route_exists(dst, table='main', netns=None, timeout=1, retry=0.2):
)


def rule_exists(priority, netns=None, timeout=1, retry=0.2):
ns = [] if netns is None else ['ip', 'netns', 'exec', netns]
filters = [ip_object_filter(query='.priority', value=priority)]
return wait_for_ip_object(
ns + ['ip', '-json', 'rule', 'show'], filters, timeout, retry
)


def class_exists(
ifname,
handle,
Expand Down
24 changes: 24 additions & 0 deletions tests/test_core/test_ipr/test_rule_async.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import pytest
from pr2test.tools import rule_exists


@pytest.mark.parametrize(
'priority,spec',
[
(30313, {'table': 10}),
(30314, {'table': 10, 'src': None}),
(30315, {'table': 10, 'dst': None}),
(30316, {'table': 10, 'dst': '127.0.0.0/24'}),
(30317, {'table': 10, 'src': '127.0.0.0/24'}),
],
)
@pytest.mark.parametrize(
'async_ipr',
[{'netns': True, 'ext_ack': True, 'strict_check': True}],
indirect=True,
)
@pytest.mark.asyncio
async def test_rule_strict_src(async_ipr, priority, spec):
netns = async_ipr.status['netns']
await async_ipr.rule('add', priority=priority, **spec)
assert rule_exists(priority=priority, netns=netns)
23 changes: 23 additions & 0 deletions tests/test_core/test_ipr/test_rule_sync.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import pytest
from pr2test.tools import rule_exists


@pytest.mark.parametrize(
'priority,spec',
[
(30313, {'table': 10}),
(30314, {'table': 10, 'src': None}),
(30315, {'table': 10, 'dst': None}),
(30316, {'table': 10, 'dst': '127.0.0.0/24'}),
(30317, {'table': 10, 'src': '127.0.0.0/24'}),
],
)
@pytest.mark.parametrize(
'sync_ipr',
[{'netns': True, 'ext_ack': True, 'strict_check': True}],
indirect=True,
)
def test_rule_strict_src(sync_ipr, priority, spec):
netns = sync_ipr.status['netns']
sync_ipr.rule('add', priority=priority, **spec)
assert rule_exists(priority=priority, netns=netns)

0 comments on commit 671dc99

Please sign in to comment.