From 896dc8dc76093301f412269d12152e4b9ad74696 Mon Sep 17 00:00:00 2001 From: Simon Oliver Tveit Date: Tue, 31 Oct 2023 14:10:45 +0100 Subject: [PATCH] Add tests for cisco poe --- .../portadmin/portadmin_poe_cisco_test.py | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 tests/unittests/portadmin/portadmin_poe_cisco_test.py diff --git a/tests/unittests/portadmin/portadmin_poe_cisco_test.py b/tests/unittests/portadmin/portadmin_poe_cisco_test.py new file mode 100644 index 0000000000..5351528ef4 --- /dev/null +++ b/tests/unittests/portadmin/portadmin_poe_cisco_test.py @@ -0,0 +1,51 @@ +from mock import Mock + +import pytest + +from nav.portadmin.snmp.cisco import Cisco +from nav.portadmin.handlers import ( + POEIndexNotFoundError, + POEStateNotSupportedError, +) + + +def test_returns_correct_poe_state_options_cisco(handler_cisco): + state_options = handler_cisco.get_poe_state_options() + assert Cisco.POE_AUTO in state_options + assert Cisco.POE_STATIC in state_options + assert Cisco.POE_LIMIT in state_options + assert Cisco.POE_DISABLE in state_options + + +class TestGetPoeState: + def test_should_raise_exception_if_unknown_poe_state_cisco(self, handler_cisco): + handler_cisco._get_poe_indexes_for_interface = Mock(return_value=(1, 1)) + handler_cisco._query_netbox = Mock(return_value=76) + interface = Mock() + with pytest.raises(POEStateNotSupportedError): + handler_cisco.get_poe_states([interface]) + + def test_should_raise_exception_if_no_poe_indexes_cisco(self, handler_cisco): + handler_cisco._get_poe_indexes_for_interface = Mock( + side_effect=POEIndexNotFoundError("Fail") + ) + interface = Mock() + with pytest.raises(POEIndexNotFoundError): + handler_cisco.get_poe_states([interface]) + + def test_dict_should_give_none_if_interface_does_not_support_poe( + self, handler_cisco + ): + handler_cisco._get_poe_indexes_for_interface = Mock(return_value=(1, 1)) + handler_cisco._query_netbox = Mock(return_value=None) + interface = Mock(ifindex=1) + states = handler_cisco.get_poe_states([interface]) + assert states[interface.ifindex] is None + + def test_returns_correct_poe_state_cisco(self, handler_cisco): + expected_state = Cisco.POE_AUTO + handler_cisco._get_poe_indexes_for_interface = Mock(return_value=(1, 1)) + handler_cisco._query_netbox = Mock(return_value=expected_state.state) + interface = Mock(ifindex=1) + state = handler_cisco.get_poe_states([interface]) + assert state[interface.ifindex] == expected_state