From 06f891048189305a91d2bfd01768346973255083 Mon Sep 17 00:00:00 2001 From: Morten Brekkevold Date: Wed, 20 Nov 2024 12:52:06 +0000 Subject: [PATCH 1/3] Regression test for #3225 --- tests/unittests/netmap/stubs_test.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 tests/unittests/netmap/stubs_test.py diff --git a/tests/unittests/netmap/stubs_test.py b/tests/unittests/netmap/stubs_test.py new file mode 100644 index 0000000000..0b5307fbaa --- /dev/null +++ b/tests/unittests/netmap/stubs_test.py @@ -0,0 +1,18 @@ +from nav.netmap.stubs import Netbox +from nav.models import manage + + +class TestNetboxEqWithNonStubValues: + def test_when_value_is_orm_model_with_same_sysname_it_should_return_true(self): + localhost = manage.Netbox(sysname='localhost') + stub = Netbox() + stub.sysname = localhost.sysname + assert stub == localhost + + def test_when_value_is_orm_model_with_different_sysname_it_should_return_false( + self, + ): + localhost = manage.Netbox(sysname='localhost') + stub = Netbox() + stub.sysname = "stubhost" + assert stub != localhost From 0ffd527c4c544a5020bd6536484077a80c7d8378 Mon Sep 17 00:00:00 2001 From: Morten Brekkevold Date: Wed, 20 Nov 2024 12:54:39 +0000 Subject: [PATCH 2/3] Allow comparison to other types of Netbox objects This stub class cannot reasonably expect to always be compared to other values of the same class. In fact, most of the time, it will be compared to instances of the real class that it is stubbing. Since the comparison is mostly about verifying that the `sysname` attribute has the same value, this changes the implementation to be more lenient to avoid stupid crash bugs (and this fixes the broken regression tests introduced by the parent commit) --- python/nav/netmap/stubs/__init__.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/python/nav/netmap/stubs/__init__.py b/python/nav/netmap/stubs/__init__.py index 9e8da80aca..77daf391f4 100644 --- a/python/nav/netmap/stubs/__init__.py +++ b/python/nav/netmap/stubs/__init__.py @@ -36,10 +36,8 @@ def __repr__(self): def __key(self): return self.sysname - # Yes we know we access private variable - # pylint: disable=W0212 - def __eq__(self, i): - return self.__key() == i.__key() + def __eq__(self, value): + return self.sysname == getattr(value, "sysname", None) def __hash__(self): return hash(self.__key()) From a92583a4dfaa6d73ebe90443db35a0879704b076 Mon Sep 17 00:00:00 2001 From: Morten Brekkevold Date: Wed, 20 Nov 2024 12:57:05 +0000 Subject: [PATCH 3/3] Add news fragment --- changelog.d/3225.fixed.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/3225.fixed.md diff --git a/changelog.d/3225.fixed.md b/changelog.d/3225.fixed.md new file mode 100644 index 0000000000..8c01d58522 --- /dev/null +++ b/changelog.d/3225.fixed.md @@ -0,0 +1 @@ +Fix spurious crashing when loading some Netmap layer 3 views