From 072a164ec11a593e0204e0e13bf665a2f5410dbd Mon Sep 17 00:00:00 2001 From: Simon Oliver Tveit Date: Tue, 5 Dec 2023 12:12:42 +0100 Subject: [PATCH 1/4] Fix handling of interfaces without PoE support POEIndexNotFoundError will be raised if POEPorts do not exist for an interface, and this is likely to be the case for an interface that does not support PoE. get_poe_states should set state to None for these interfaces, like it does if PoeNotSupportedError is raised. --- python/nav/portadmin/snmp/cisco.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/nav/portadmin/snmp/cisco.py b/python/nav/portadmin/snmp/cisco.py index bbe1a9d18e..7a3c255886 100644 --- a/python/nav/portadmin/snmp/cisco.py +++ b/python/nav/portadmin/snmp/cisco.py @@ -351,7 +351,7 @@ def _get_poe_indexes_for_interface( try: poeport = manage.POEPort.objects.get(interface=interface) except manage.POEPort.DoesNotExist: - raise POEIndexNotFoundError( + raise POENotSupportedError( "This interface does not have PoE indexes defined" ) unit_number = poeport.poegroup.index From e4985c1b7d249256d714c080ed372a4f280fa791 Mon Sep 17 00:00:00 2001 From: Simon Oliver Tveit Date: Tue, 5 Dec 2023 12:25:34 +0100 Subject: [PATCH 2/4] Fix logic in tests interface not having poeport should be considered as the interface not supporting poe --- tests/unittests/portadmin/portadmin_poe_cisco_test.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/unittests/portadmin/portadmin_poe_cisco_test.py b/tests/unittests/portadmin/portadmin_poe_cisco_test.py index 2c48c638d0..2a279bb00e 100644 --- a/tests/unittests/portadmin/portadmin_poe_cisco_test.py +++ b/tests/unittests/portadmin/portadmin_poe_cisco_test.py @@ -28,12 +28,12 @@ def test_should_raise_exception_if_unknown_poe_state(self, handler_cisco): handler_cisco.get_poe_states([interface]) @pytest.mark.usefixtures('poeport_get_mock_error') - def test_should_raise_exception_if_interface_is_missing_poeport( + def test_dict_should_give_none_if_interface_does_not_have_poeport( self, handler_cisco ): interface = Mock(interface="interface") - with pytest.raises(POEIndexNotFoundError): - handler_cisco.get_poe_states([interface]) + states = handler_cisco.get_poe_states([interface]) + assert states[interface.ifname] is None @pytest.mark.usefixtures('poeport_get_mock') def test_dict_should_give_none_if_interface_does_not_support_poe( From 878c20160121148ddd4affaf9e6d244d958a1851 Mon Sep 17 00:00:00 2001 From: Simon Oliver Tveit Date: Tue, 5 Dec 2023 12:26:51 +0100 Subject: [PATCH 3/4] Fix mock object properties --- tests/unittests/portadmin/portadmin_poe_cisco_test.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/unittests/portadmin/portadmin_poe_cisco_test.py b/tests/unittests/portadmin/portadmin_poe_cisco_test.py index 2a279bb00e..c2be889fc6 100644 --- a/tests/unittests/portadmin/portadmin_poe_cisco_test.py +++ b/tests/unittests/portadmin/portadmin_poe_cisco_test.py @@ -23,7 +23,7 @@ class TestGetPoeState: @pytest.mark.usefixtures('poeport_get_mock') def test_should_raise_exception_if_unknown_poe_state(self, handler_cisco): handler_cisco._query_netbox = Mock(return_value=76) - interface = Mock(interface="interface") + interface = Mock(ifname="interface") with pytest.raises(POEStateNotSupportedError): handler_cisco.get_poe_states([interface]) @@ -31,7 +31,7 @@ def test_should_raise_exception_if_unknown_poe_state(self, handler_cisco): def test_dict_should_give_none_if_interface_does_not_have_poeport( self, handler_cisco ): - interface = Mock(interface="interface") + interface = Mock(ifname="interface") states = handler_cisco.get_poe_states([interface]) assert states[interface.ifname] is None @@ -40,7 +40,7 @@ def test_dict_should_give_none_if_interface_does_not_support_poe( self, handler_cisco ): handler_cisco._query_netbox = Mock(return_value=None) - interface = Mock(interface="interface") + interface = Mock(ifname="interface") states = handler_cisco.get_poe_states([interface]) assert states[interface.ifname] is None From 33381e1a4b82f4e708326e62d9eefc4e9bd7f279 Mon Sep 17 00:00:00 2001 From: Simon Oliver Tveit Date: Tue, 5 Dec 2023 13:13:35 +0100 Subject: [PATCH 4/4] Remove unused exception --- python/nav/portadmin/handlers.py | 4 ---- python/nav/portadmin/snmp/cisco.py | 1 - python/nav/web/portadmin/views.py | 2 -- tests/unittests/portadmin/portadmin_poe_cisco_test.py | 5 +---- 4 files changed, 1 insertion(+), 11 deletions(-) diff --git a/python/nav/portadmin/handlers.py b/python/nav/portadmin/handlers.py index 5a2e8816a1..7c74e559df 100644 --- a/python/nav/portadmin/handlers.py +++ b/python/nav/portadmin/handlers.py @@ -352,7 +352,3 @@ class POEStateNotSupportedError(ManagementError): class XMLParseError(ManagementError): """Raised when failing to parse XML""" - - -class POEIndexNotFoundError(ManagementError): - """Raised when a PoE index could not be found for an interface""" diff --git a/python/nav/portadmin/snmp/cisco.py b/python/nav/portadmin/snmp/cisco.py index 7a3c255886..d2f4fa5b0c 100644 --- a/python/nav/portadmin/snmp/cisco.py +++ b/python/nav/portadmin/snmp/cisco.py @@ -28,7 +28,6 @@ PoeState, POEStateNotSupportedError, POENotSupportedError, - POEIndexNotFoundError, ) from nav.models import manage diff --git a/python/nav/web/portadmin/views.py b/python/nav/web/portadmin/views.py index 211910965a..e021807750 100644 --- a/python/nav/web/portadmin/views.py +++ b/python/nav/web/portadmin/views.py @@ -56,7 +56,6 @@ NoResponseError, ProtocolError, ManagementError, - POEIndexNotFoundError, XMLParseError, POEStateNotSupportedError, ) @@ -248,7 +247,6 @@ def populate_infodict(request, netbox, interfaces): messages.error(request, str(error)) except ( - POEIndexNotFoundError, XMLParseError, POEStateNotSupportedError, ) as error: diff --git a/tests/unittests/portadmin/portadmin_poe_cisco_test.py b/tests/unittests/portadmin/portadmin_poe_cisco_test.py index c2be889fc6..717d0252d2 100644 --- a/tests/unittests/portadmin/portadmin_poe_cisco_test.py +++ b/tests/unittests/portadmin/portadmin_poe_cisco_test.py @@ -3,10 +3,7 @@ import pytest from nav.portadmin.snmp.cisco import Cisco -from nav.portadmin.handlers import ( - POEIndexNotFoundError, - POEStateNotSupportedError, -) +from nav.portadmin.handlers import POEStateNotSupportedError from nav.models import manage