From 6521c80a0016c19ca0f024d101e0bfe50c4004bc Mon Sep 17 00:00:00 2001 From: Lex Li Date: Sat, 13 Jan 2024 01:45:46 -0500 Subject: [PATCH] Added more test cases and cleaned up. --- .vscode/settings.json | 6 ++- Development.md | 1 + ...rt-between-pdu-varbinds-and-mib-objects.py | 11 +++++ tests/smi/manager/test_mib-tree-inspection.py | 42 +++++++++++++++++++ 4 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 tests/smi/manager/test_convert-between-pdu-varbinds-and-mib-objects.py create mode 100644 tests/smi/manager/test_mib-tree-inspection.py diff --git a/.vscode/settings.json b/.vscode/settings.json index e5a288a9b..d7338ad7e 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,7 @@ { - "restructuredtext.syntaxHighlighting.disabled": true + "python.testing.pytestArgs": [ + "tests" + ], + "python.testing.unittestEnabled": false, + "python.testing.pytestEnabled": true, } \ No newline at end of file diff --git a/Development.md b/Development.md index 9c14547b6..4ab376041 100644 --- a/Development.md +++ b/Development.md @@ -4,6 +4,7 @@ ```bash poetry env use 3.12 +poetry env info --path ``` ## Bump Version Number diff --git a/tests/smi/manager/test_convert-between-pdu-varbinds-and-mib-objects.py b/tests/smi/manager/test_convert-between-pdu-varbinds-and-mib-objects.py new file mode 100644 index 000000000..d296c12f7 --- /dev/null +++ b/tests/smi/manager/test_convert-between-pdu-varbinds-and-mib-objects.py @@ -0,0 +1,11 @@ +import pytest +from unittest.mock import patch +from pysnmp.smi import builder, view, rfc1902, error + +def test_addAsn1MibSource(): + mibBuilder = builder.MibBuilder() + mibView = view.MibViewController(mibBuilder) + mibVar = rfc1902.ObjectIdentity("IF-MIB", "ifInOctets", 1) + mibVar.addAsn1MibSource('https://mibs.pysnmp.com/asn1/@mib@') + mibVar.resolveWithMib(mibView) + assert mibVar.prettyPrint() == 'IF-MIB::ifInOctets.1' \ No newline at end of file diff --git a/tests/smi/manager/test_mib-tree-inspection.py b/tests/smi/manager/test_mib-tree-inspection.py new file mode 100644 index 000000000..0f47b10bb --- /dev/null +++ b/tests/smi/manager/test_mib-tree-inspection.py @@ -0,0 +1,42 @@ +import pytest +from pysnmp.smi import builder, view + +mibBuilder = builder.MibBuilder() +mibBuilder.addMibSources(builder.DirMibSource("/opt/pysnmp_mibs")) +mibBuilder.loadModules( + "SNMPv2-MIB", "SNMP-FRAMEWORK-MIB", "SNMP-COMMUNITY-MIB", "IP-MIB" +) +mibView = view.MibViewController(mibBuilder) + +def test_getNodeName_by_OID(): + oid, label, suffix = mibView.getNodeName((1, 3, 6, 1, 2, 1, 1, 1)) + assert oid == (1, 3, 6, 1, 2, 1, 1, 1) + assert label == ('iso', 'org', 'dod', 'internet', 'mgmt', 'mib-2', 'system', 'sysDescr') + assert suffix == () + +def test_getNodeName_by_label(): + oid, label, suffix = mibView.getNodeName((1, 3, 6, 1, 2, "mib-2", 1, "sysDescr")) + assert oid == (1, 3, 6, 1, 2, 1, 1, 1) + assert label == ('iso', 'org', 'dod', 'internet', 'mgmt', 'mib-2', 'system', 'sysDescr') + assert suffix == () + +def test_getNodeName_by_symbol_description(): + oid, label, suffix = mibView.getNodeName(("sysDescr",)) + assert oid == (1, 3, 6, 1, 2, 1, 1, 1) + assert label == ('iso', 'org', 'dod', 'internet', 'mgmt', 'mib-2', 'system', 'sysDescr') + assert suffix == () + +def test_getNodeName_by_symbol_description_with_module_name(): + oid, label, suffix = mibView.getNodeName(("snmpEngineID",), "SNMP-FRAMEWORK-MIB") + assert oid == (1, 3, 6, 1, 6, 3, 10, 2, 1, 1) + assert label == ('iso', 'org', 'dod', 'internet', 'snmpV2', 'snmpModules', 'snmpFrameworkMIB', 'snmpFrameworkMIBObjects', 'snmpEngine', 'snmpEngineID') + assert suffix == () + + (mibNode,) = mibBuilder.importSymbols("SNMP-FRAMEWORK-MIB", "snmpEngineID") + assert mibNode.syntax.prettyPrint() != '' + +def test_getNodeName_by_symbol_location_lookup_by_name(): + modName, symName, suffix = mibView.getNodeLocation(("snmpCommunityEntry",)) + assert modName == "SNMP-COMMUNITY-MIB" + assert symName == "snmpCommunityEntry" + assert suffix == () \ No newline at end of file