Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse SNMP profile version numbers using existing ManagementProfile.snmp_version property #2768

Merged
merged 3 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions python/nav/ipdevpoll/snmp/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
lunkwill42 marked this conversation as resolved.
Show resolved Hide resolved
else:
_logger.debug("%r has no snmp profile", netbox)
return None
Expand Down
28 changes: 28 additions & 0 deletions tests/unittests/ipdevpoll/snmp/common_test.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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(
Expand All @@ -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
Loading