From 80d3f46d4e2f978d056803bfe95ac714131cdd2e Mon Sep 17 00:00:00 2001 From: Olivier Gayot Date: Wed, 28 Jan 2026 18:43:19 +0100 Subject: [PATCH 1/2] rtnetlink: make MyMock* classes compatible with pyroute2 0.7.11 (noble) Signed-off-by: Olivier Gayot --- probert/tests/rtnetlink/test_addr.py | 4 ++++ probert/tests/rtnetlink/test_link.py | 4 ++++ probert/tests/rtnetlink/test_route.py | 7 +++++-- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/probert/tests/rtnetlink/test_addr.py b/probert/tests/rtnetlink/test_addr.py index 183f4cb..22b557d 100644 --- a/probert/tests/rtnetlink/test_addr.py +++ b/probert/tests/rtnetlink/test_addr.py @@ -23,6 +23,10 @@ class MyMockAddr(WithGetAttrMixin, MockAddress): """Subclass of pyroute2's MockAddr that makes it behave like a nlmsg""" + # MockAddress in pyroute2 0.9 has default values for index and address but + # the version in pyroute2 0.7.11 (noble) does not. + def __init__(self, index=0, address=None, *args, **kwargs) -> None: + super().__init__(index, address, *args, **kwargs) class TestAddrBuildEventData(unittest.TestCase): diff --git a/probert/tests/rtnetlink/test_link.py b/probert/tests/rtnetlink/test_link.py index 653eaee..8b6880d 100644 --- a/probert/tests/rtnetlink/test_link.py +++ b/probert/tests/rtnetlink/test_link.py @@ -23,6 +23,10 @@ class MyMockLink(WithGetAttrMixin, MockLink): """Subclass of pyroute2's MockLink that makes it behave like a nlmsg""" + # MockLink in pyroute2 0.9 has default values for index and ifname but + # the version in pyroute2 0.7.11 (noble) does not. + def __init__(self, index=0, ifname="", *args, **kwargs) -> None: + super().__init__(index, ifname, *args, **kwargs) class TestLinkBuildEventData(unittest.TestCase): diff --git a/probert/tests/rtnetlink/test_route.py b/probert/tests/rtnetlink/test_route.py index fc43053..2658582 100644 --- a/probert/tests/rtnetlink/test_route.py +++ b/probert/tests/rtnetlink/test_route.py @@ -25,7 +25,9 @@ class MyMockRoute(WithGetAttrMixin, MockRoute): """Subclass of pyroute2's MockRoute that makes it behave like a nlmsg... and works around a bug""" - def __init__(self, *args, **kwargs): + # MockRoute in pyroute2 0.9 has default values for dst and oif but the + # version in pyroute2 0.7.11 (noble) does not. + def __init__(self, dst=None, oif=0, *args, **kwargs) -> None: # Workaround ambiguity with route type. # See https://github.com/svinota/pyroute2/pull/1409 if "type" in kwargs: @@ -34,7 +36,8 @@ def __init__(self, *args, **kwargs): kwargs["type"] = kwargs["route_type"] else: kwargs["type"] = rtypes["RTN_UNICAST"] - super().__init__(*args, **kwargs) + + super().__init__(dst, oif, *args, **kwargs) class TestGetIfindex(unittest.TestCase): From b080bcf81a2a11fc0427efef3abcd65b8bd15716 Mon Sep 17 00:00:00 2001 From: Olivier Gayot Date: Thu, 29 Jan 2026 11:24:33 +0100 Subject: [PATCH 2/2] rtnetlink: properly mock ipr.fileno accross versions of pyroute2 Signed-off-by: Olivier Gayot --- probert/tests/rtnetlink/test_listener.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/probert/tests/rtnetlink/test_listener.py b/probert/tests/rtnetlink/test_listener.py index bf5969a..a75c6ad 100644 --- a/probert/tests/rtnetlink/test_listener.py +++ b/probert/tests/rtnetlink/test_listener.py @@ -12,6 +12,7 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +import importlib.metadata import socket import unittest from unittest import mock @@ -201,8 +202,16 @@ def test_data_ready(self): m_handle.mock_calls) def test_fileno(self): - # We need to patch the class, not the instance for some reason. - with mock.patch.object(IPRoute, "fileno", return_value=42): + version_str = importlib.metadata.version("pyroute2") + pyroute2_version = tuple(int(x) for x in version_str.split(".")) + + if pyroute2_version < (0, 9): + target = self.listener.ipr + else: + # We need to patch the class, not the instance for some reason. + target = IPRoute + + with mock.patch.object(target, "fileno", return_value=42): self.assertEqual(42, self.listener.fileno()) def test_set_link_flags(self):