Skip to content

Commit

Permalink
Added two display hint test cases. Enabled breakpoints in MIBs.
Browse files Browse the repository at this point in the history
  • Loading branch information
lextm committed Nov 3, 2024
1 parent 8739a06 commit 0fa232c
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 6 deletions.
15 changes: 9 additions & 6 deletions pysnmp/smi/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
from pysnmp import debug, version as pysnmp_version
from pysnmp.smi import error

if __debug__:
import runpy


PY_SUFFIXES = SOURCE_SUFFIXES + BYTECODE_SUFFIXES

Expand Down Expand Up @@ -354,7 +357,12 @@ def load_module(self, modName, **userCtx):
g = {"mibBuilder": self, "userCtx": userCtx}

try:
exec(codeObj, g)
if __debug__:
runpy.run_path(
modPath, g
) # IMPORTANT: enable break points in loaded MIBs
else:
exec(codeObj, g)

except Exception:
self.__modPathsSeen.remove(modPath)
Expand Down Expand Up @@ -417,11 +425,6 @@ def load_modules(self, *modNames, **userCtx):
f"{modName} compilation error(s): {errs}"
)

if errs:
raise error.MibNotFoundError(
f"{modName} compilation error(s): {errs}"
)

# compilation succeeded, MIB might load now
self.load_module(modName, **userCtx)

Expand Down
11 changes: 11 additions & 0 deletions tests/agent_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import asyncio
import time


# Set the port to 1611 instead of 161, because 161 is a
# privileged port and requires root access
AGENT_PORT = 1611
Expand Down Expand Up @@ -152,6 +153,16 @@ def get_fixed_date_and_time():
MibScalarInstance(
(1, 3, 6, 1, 4, 1, 60069, 9, 6), (0,), PhysAddress(initial_phys_address)
),
MibScalar((1, 3, 6, 1, 4, 1, 60069, 9, 7), v2c.Integer32()).setMaxAccess(
"read-write"
),
MibScalarInstance((1, 3, 6, 1, 4, 1, 60069, 9, 7), (0,), v2c.Integer32(50)),
MibScalar((1, 3, 6, 1, 4, 1, 60069, 9, 8), v2c.Unsigned32()).setMaxAccess(
"read-write"
),
MibScalarInstance(
(1, 3, 6, 1, 4, 1, 60069, 9, 8), (0,), v2c.Unsigned32(50)
),
)

# --- end of Managed Object Instance initialization ----
Expand Down
68 changes: 68 additions & 0 deletions tests/hlapi/v1arch/asyncio/manager/cmdgen/test_v1arch_v1_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,71 @@ async def test_v1_get_physaddress_object():
assert (
varBinds[0][1].prettyPrint() == "00:11:22:33:44:55"
) # IMPORTANT: test PhysAddress display hint.


@pytest.mark.asyncio
async def test_v1_get_scaled_integer_object():
async with AgentContextManager(enable_custom_objects=True):
snmpDispatcher = SnmpDispatcher()
# Step 1: Set up MIB builder and add custom MIB directory
mibBuilder = builder.MibBuilder()
compiler.addMibCompiler(mibBuilder)
mibViewController = view.MibViewController(mibBuilder)

# Load the custom MIB
mibBuilder.loadModules("LEXTUDIO-TEST-MIB")
snmpDispatcher.cache["mibViewController"] = mibViewController

errorIndication, errorStatus, errorIndex, varBinds = await get_cmd(
snmpDispatcher,
CommunityData("public", mpModel=0),
await UdpTransportTarget.create(
("localhost", AGENT_PORT), timeout=1, retries=0
),
ObjectType(
ObjectIdentity("LEXTUDIO-TEST-MIB", "testScaledInteger", 0)
), # "1.3.6.1.4.1.60069.9.7.0"
)
assert errorIndication is None
assert errorIndication is None
assert errorStatus == 0
assert errorIndex == 0
assert len(varBinds) == 1
assert varBinds[0][0].prettyPrint() == "LEXTUDIO-TEST-MIB::testScaledInteger.0"
assert (
varBinds[0][1].prettyPrint() == "5.0"
) # IMPORTANT: test display hint "d-1".


@pytest.mark.asyncio
async def test_v1_get_scaled_unsigned_object():
async with AgentContextManager(enable_custom_objects=True):
snmpDispatcher = SnmpDispatcher()
# Step 1: Set up MIB builder and add custom MIB directory
mibBuilder = builder.MibBuilder()
compiler.addMibCompiler(mibBuilder)
mibViewController = view.MibViewController(mibBuilder)

# Load the custom MIB
mibBuilder.loadModules("LEXTUDIO-TEST-MIB")
snmpDispatcher.cache["mibViewController"] = mibViewController

errorIndication, errorStatus, errorIndex, varBinds = await get_cmd(
snmpDispatcher,
CommunityData("public", mpModel=0),
await UdpTransportTarget.create(
("localhost", AGENT_PORT), timeout=1, retries=0
),
ObjectType(
ObjectIdentity("LEXTUDIO-TEST-MIB", "testScaledUnsigned", 0)
), # "1.3.6.1.4.1.60069.9.8.0"
)
assert errorIndication is None
assert errorIndication is None
assert errorStatus == 0
assert errorIndex == 0
assert len(varBinds) == 1
assert varBinds[0][0].prettyPrint() == "LEXTUDIO-TEST-MIB::testScaledUnsigned.0"
assert (
varBinds[0][1].prettyPrint() == "50" # GitHub issue #139
) # IMPORTANT: test display hint "d-1".

0 comments on commit 0fa232c

Please sign in to comment.