-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2751 from lunkwill42/feature/fix-preferred-snmp-p…
…rofile-logic Allow write-enabled SNMP profiles to be used for reading when no read-only profiles are available
- Loading branch information
Showing
7 changed files
with
105 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
from unittest.mock import patch | ||
|
||
from nav.models.manage import Netbox, ManagementProfile | ||
|
||
import pytest | ||
|
||
|
||
class TestGetPreferredSnmpProfiles: | ||
def test_when_write_required_is_false_then_it_should_prefer_a_read_profile( | ||
self, mocked_netbox | ||
): | ||
profile = mocked_netbox.get_preferred_snmp_management_profile() | ||
assert profile.name == "v2 read" | ||
|
||
def test_when_write_required_is_true_then_it_should_return_a_write_profile( | ||
self, mocked_netbox | ||
): | ||
profile = mocked_netbox.get_preferred_snmp_management_profile( | ||
require_write=True | ||
) | ||
assert profile.name.startswith("v2 write") | ||
|
||
def test_when_write_required_is_false_it_should_return_a_writable_profile_of_a_higher_snmp_version( | ||
self, mocked_netbox | ||
): | ||
profiles = mocked_netbox.profiles.filter() | ||
profiles.append( | ||
ManagementProfile( | ||
name="v3 write", | ||
protocol=ManagementProfile.PROTOCOL_SNMPV3, | ||
configuration={"write": True}, | ||
) | ||
) | ||
profile = mocked_netbox.get_preferred_snmp_management_profile() | ||
assert profile.name == "v3 write" | ||
|
||
|
||
@pytest.fixture | ||
def mocked_netbox(mock_profiles): | ||
with patch.object(Netbox, "profiles") as profiles: | ||
netbox = Netbox(sysname="test", ip="127.0.0.1") | ||
profiles.filter.return_value = mock_profiles | ||
yield netbox | ||
|
||
|
||
@pytest.fixture | ||
def mock_profiles(): | ||
profiles = [ | ||
ManagementProfile( | ||
name="v2 write first", | ||
protocol=ManagementProfile.PROTOCOL_SNMP, | ||
configuration={"version": 2, "write": True}, | ||
), | ||
ManagementProfile( | ||
name="v2 write second", | ||
protocol=ManagementProfile.PROTOCOL_SNMP, | ||
configuration={"version": 2, "write": True}, | ||
), | ||
ManagementProfile( | ||
name="v2 read", | ||
protocol=ManagementProfile.PROTOCOL_SNMP, | ||
configuration={"version": 2, "write": False}, | ||
), | ||
] | ||
yield profiles |