Skip to content

Commit

Permalink
Added a few test cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
lextm committed Sep 17, 2023
1 parent bf0167d commit 0cfe715
Show file tree
Hide file tree
Showing 13 changed files with 225 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
| $ snmptrap -v2c -c public demo.pysnmp.com 12345 1.3.6.1.6.3.1.1.5.2
| $ snmpinform -v2c -c public demo.pysnmp.com 12345 1.3.6.1.6.3.1.1.5.2
| $ snmptrap -v2c -c public demo.pysnmp.com 12345 1.3.6.1.6.3.1.1.5.2
"""#
import asyncio
Expand Down
1 change: 1 addition & 0 deletions tests/hlapi/asyncio/agent/ntforg/test_default-v1-trap.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ async def test_send_notification():
assert errorStatus == 0
assert errorIndex == 0
assert varBinds == []
snmpEngine.transportDispatcher.closeDispatcher()
6 changes: 4 additions & 2 deletions tests/hlapi/asyncio/agent/ntforg/test_v3-inform.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

@pytest.mark.asyncio
async def test_send_v3_inform_notification():
snmpEngine = SnmpEngine()
errorIndication, errorStatus, errorIndex, varBinds = await sendNotification(
SnmpEngine(),
snmpEngine,
UsmUserData('usr-md5-des', 'authkey1', 'privkey1'),
UdpTransportTarget(('demo.pysnmp.com', 162)),
ContextData(),
Expand All @@ -22,4 +23,5 @@ async def test_send_v3_inform_notification():
assert isinstance(errorIndication, RequestTimedOut)
assert errorStatus == 0
assert errorIndex == 0
assert varBinds == []
assert varBinds == []
snmpEngine.transportDispatcher.closeDispatcher()
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import pytest
from pysnmp.hlapi.asyncio import *

@pytest.mark.asyncio
async def test_custom_asn1_mib_search_path():
snmpEngine = SnmpEngine()
errorIndication, errorStatus, errorIndex, varBinds = await getCmd(
snmpEngine,
CommunityData('public'),
UdpTransportTarget(('demo.pysnmp.com', 161)),
ContextData(),
ObjectType(ObjectIdentity('IF-MIB', 'ifInOctets', 1).addAsn1MibSource('file:///usr/share/snmp',
'https://mibs.pysnmp.com/asn1/@mib@'))
)

assert errorIndication is None
assert errorStatus == 0
assert len(varBinds) == 1
assert varBinds[0][0].prettyPrint() == 'IF-MIB::ifInOctets.1'

snmpEngine.transportDispatcher.closeDispatcher()
24 changes: 24 additions & 0 deletions tests/hlapi/asyncio/manager/cmdgen/test_send_trap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import pytest
from pysnmp.hlapi.asyncio import *

@pytest.mark.asyncio
async def test_send_trap():
snmpEngine = SnmpEngine()
mibBuilder = snmpEngine.msgAndPduDsp.mibInstrumController.mibBuilder
sysUpTime, = mibBuilder.importSymbols('__SNMPv2-MIB', 'sysUpTime')
sysUpTime.syntax = TimeTicks(12345)

errorIndication, errorStatus, errorIndex, varBinds = await sendNotification(
snmpEngine,
CommunityData('public', mpModel=0),
UdpTransportTarget(('demo.pysnmp.com', 162)),
ContextData(),
"trap",
NotificationType(ObjectIdentity('NET-SNMP-EXAMPLES-MIB', 'netSnmpExampleNotification')).addVarBinds(ObjectType(ObjectIdentity('NET-SNMP-EXAMPLES-MIB','netSnmpExampleHeartbeatRate'), 1))
)

assert errorIndication is None
assert errorStatus == 0
assert len(varBinds) == 0

snmpEngine.transportDispatcher.closeDispatcher()
29 changes: 29 additions & 0 deletions tests/hlapi/asyncio/manager/cmdgen/test_usm_sha_aes128.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import pytest
from pysnmp.hlapi.asyncio import *


@pytest.mark.asyncio
async def test_usm_sha_aes128():
snmpEngine = SnmpEngine()
authData = UsmUserData(
"usr-sha-aes",
"authkey1",
"privkey1",
authProtocol=usmHMACSHAAuthProtocol,
privProtocol=usmAesCfb128Protocol,
)
errorIndication, errorStatus, errorIndex, varBinds = await getCmd(
snmpEngine,
authData,
UdpTransportTarget(("demo.pysnmp.com", 161)),
ContextData(),
ObjectType(ObjectIdentity("SNMPv2-MIB", "sysDescr", 0)),
)

assert errorIndication is None
assert errorStatus == 0
assert len(varBinds) == 1
assert varBinds[0][0].prettyPrint() == "SNMPv2-MIB::sysDescr.0"
isinstance(varBinds[0][1], OctetString)

snmpEngine.transportDispatcher.closeDispatcher()
21 changes: 21 additions & 0 deletions tests/hlapi/asyncio/manager/cmdgen/test_v1_get.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import pytest
from pysnmp.hlapi.asyncio import *

@pytest.mark.asyncio
async def test_v1_get():
snmpEngine = SnmpEngine()
errorIndication, errorStatus, errorIndex, varBinds = await getCmd(
snmpEngine,
CommunityData('public', mpModel=0),
UdpTransportTarget(('demo.pysnmp.com', 161)),
ContextData(),
ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0))
)

assert errorIndication is None
assert errorStatus == 0
assert len(varBinds) == 1
assert varBinds[0][0].prettyPrint() == 'SNMPv2-MIB::sysDescr.0'
assert isinstance(varBinds[0][1], OctetString)

snmpEngine.transportDispatcher.closeDispatcher()
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,6 @@ async def test_v1_next():
assert errorIndication is None
assert errorStatus == 0
assert errorIndex == 0
assert len(varBinds) > 0
assert len(varBinds) > 0

slim.close()
21 changes: 21 additions & 0 deletions tests/hlapi/asyncio/manager/cmdgen/test_v1_set.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import pytest
from pysnmp.hlapi.asyncio.slim import Slim
from pysnmp.smi.rfc1902 import ObjectIdentity, ObjectType

@pytest.mark.asyncio
async def test_v1_set():
slim = Slim(1)
errorIndication, errorStatus, errorIndex, varBinds = await slim.set(
'public',
'demo.pysnmp.com',
161,
ObjectType(ObjectIdentity("SNMPv2-MIB", "sysLocation", 0), "Shanghai")
)

assert errorIndication is None
assert errorStatus == 0
assert len(varBinds) == 1
assert varBinds[0][0].prettyPrint() == 'SNMPv2-MIB::sysLocation.0'
assert varBinds[0][1].prettyPrint() == 'Shanghai'

slim.close()
21 changes: 21 additions & 0 deletions tests/hlapi/asyncio/manager/cmdgen/test_v2c_bulk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import pytest
from pysnmp.hlapi.asyncio.slim import Slim
from pysnmp.smi.rfc1902 import ObjectIdentity, ObjectType

@pytest.mark.asyncio
async def test_v2c_bulk():
slim = Slim()
errorIndication, errorStatus, errorIndex, varBinds = await slim.bulk(
'public',
'demo.pysnmp.com',
161,
0,
50,
ObjectType(ObjectIdentity("SNMPv2-MIB", "sysDescr", 0)),
)

assert errorIndication is None
assert errorStatus == 0
assert len(varBinds) > 0

slim.close()
21 changes: 21 additions & 0 deletions tests/hlapi/asyncio/manager/cmdgen/test_v2c_get.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import pytest
from pysnmp.hlapi.asyncio.slim import Slim
from pysnmp.hlapi.asyncio import *

@pytest.mark.asyncio
async def test_v1_get():
slim = Slim()
errorIndication, errorStatus, errorIndex, varBinds = await slim.get(
'public',
'demo.pysnmp.com',
161,
ObjectType(ObjectIdentity("SNMPv2-MIB", "sysDescr", 0)),
)

assert errorIndication is None
assert errorStatus == 0
assert len(varBinds) == 1
assert varBinds[0][0].prettyPrint() == 'SNMPv2-MIB::sysDescr.0'
assert isinstance(varBinds[0][1], OctetString)

slim.close()
38 changes: 38 additions & 0 deletions tests/hlapi/asyncio/manager/cmdgen/test_v2c_next.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""
SNMPv1
++++++
Send SNMP GET request using the following options:
* with SNMPv1, community 'public'
* over IPv4/UDP
* to an Agent at demo.pysnmp.com:161
* for an instance of SNMPv2-MIB::sysDescr.0 MIB object
* Based on asyncio I/O framework
Functionally similar to:
| $ snmpgetnext -v1 -c public demo.pysnmp.com SNMPv2-MIB::sysDescr.0
"""#
import asyncio
import pytest
from pysnmp.hlapi.asyncio.slim import Slim
from pysnmp.smi.rfc1902 import ObjectIdentity, ObjectType

@pytest.mark.asyncio
async def test_v1_next():
slim = Slim()
errorIndication, errorStatus, errorIndex, varBinds = await slim.next(
'public',
'demo.pysnmp.com',
161,
ObjectType(ObjectIdentity("SNMPv2-MIB", "sysDescr", 0)),
)

assert errorIndication is None
assert errorStatus == 0
assert errorIndex == 0
assert len(varBinds) > 0

slim.close()
21 changes: 21 additions & 0 deletions tests/hlapi/asyncio/manager/cmdgen/test_v2c_set.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import pytest
from pysnmp.hlapi.asyncio.slim import Slim
from pysnmp.smi.rfc1902 import ObjectIdentity, ObjectType

@pytest.mark.asyncio
async def test_v1_set():
slim = Slim()
errorIndication, errorStatus, errorIndex, varBinds = await slim.set(
'public',
'demo.pysnmp.com',
161,
ObjectType(ObjectIdentity("SNMPv2-MIB", "sysLocation", 0), "Shanghai")
)

assert errorIndication is None
assert errorStatus == 0
assert len(varBinds) == 1
assert varBinds[0][0].prettyPrint() == 'SNMPv2-MIB::sysLocation.0'
assert varBinds[0][1].prettyPrint() == 'Shanghai'

slim.close()

0 comments on commit 0cfe715

Please sign in to comment.