diff --git a/python/nav/ipdevpoll/snmp/common.py b/python/nav/ipdevpoll/snmp/common.py index 795d399519..b17447e1cb 100644 --- a/python/nav/ipdevpoll/snmp/common.py +++ b/python/nav/ipdevpoll/snmp/common.py @@ -204,13 +204,11 @@ def factory( if netbox: profile = netbox.get_preferred_snmp_management_profile() if profile: - if profile.protocol == profile.PROTOCOL_SNMPV3: - kwargs["version"] = 3 kwargs_out.update( {k: v for k, v in profile.configuration.items() if hasattr(cls, k)} ) - # Sometimes profiles store the version number as a string - kwargs_out["version"] = int(kwargs_out["version"]) + # Let profile object parse its own version to an int + kwargs_out["version"] = profile.snmp_version else: _logger.debug("%r has no snmp profile", netbox) return None diff --git a/tests/unittests/ipdevpoll/snmp/common_test.py b/tests/unittests/ipdevpoll/snmp/common_test.py index f2fae2adbf..6b34caffd7 100644 --- a/tests/unittests/ipdevpoll/snmp/common_test.py +++ b/tests/unittests/ipdevpoll/snmp/common_test.py @@ -1,7 +1,10 @@ +from unittest.mock import Mock + import pytest from nav.ipdevpoll.snmp.common import SNMPParameters from nav.Snmp.defines import AuthenticationProtocol, PrivacyProtocol, SecurityLevel +from nav.models.manage import ManagementProfile class TestSNMPParameters: @@ -41,6 +44,18 @@ def test_should_contain_sec_name_cmdline_argument(self, snmpv3_params): assert "-u foobar" in args +class TestSNMPParametersFactory: + @pytest.mark.parametrize("version_value", (2, "2", "2c")) + def test_snmp_v2_profile_should_be_parsed_without_error( + self, snmpv2c_profile, version_value + ): + mock_netbox = Mock() + mock_netbox.get_preferred_snmp_management_profile.return_value = snmpv2c_profile + snmpv2c_profile.configuration["version"] = version_value + params = SNMPParameters.factory(mock_netbox) + assert params.version == 2 + + @pytest.fixture def snmpv3_params(): param = SNMPParameters( @@ -53,3 +68,16 @@ def snmpv3_params(): priv_password="secret2", ) yield param + + +@pytest.fixture +def snmpv2c_profile(): + profile = ManagementProfile( + protocol=ManagementProfile.PROTOCOL_SNMP, + configuration={ + "version": "2c", + "community": "private", + "write": True, + }, + ) + yield profile