File: test_v1arch_v1_get.py

package info (click to toggle)
python-pysnmp4 7.1.21-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,564 kB
  • sloc: python: 33,654; makefile: 166; javascript: 4
file content (307 lines) | stat: -rw-r--r-- 12,381 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import asyncio
from datetime import datetime
import pytest
from pysnmp.hlapi.v1arch.asyncio import *
from pysnmp.proto.errind import RequestTimedOut
from pysnmp.proto.rfc1905 import errorStatus as pysnmp_errorStatus
from pysnmp.smi import builder, compiler, view

from tests.agent_context import AGENT_PORT, AgentContextManager


@pytest.mark.asyncio
async def test_v1_get():
    async with AgentContextManager():
        with SnmpDispatcher() as snmpDispatcher:
            iterator = await get_cmd(
                snmpDispatcher,
                CommunityData("public", mpModel=0),
                await UdpTransportTarget.create(("localhost", AGENT_PORT)),
                ("1.3.6.1.2.1.1.1.0", None),
            )

            errorIndication, errorStatus, errorIndex, varBinds = iterator

            assert errorIndication is None
            assert errorStatus == 0
            assert errorIndex == 0
            assert len(varBinds) == 1
            assert varBinds[0][0].prettyPrint() == "1.3.6.1.2.1.1.1.0"
            assert varBinds[0][1].prettyPrint().startswith("PySNMP engine version")

            name = pysnmp_errorStatus.namedValues.getName(errorStatus)
            assert name == "noError"


@pytest.mark.asyncio
async def test_v1_get_ipv6():
    async with AgentContextManager(enable_ipv6=True):
        with SnmpDispatcher() as snmpDispatcher:
            iterator = await get_cmd(
                snmpDispatcher,
                CommunityData("public", mpModel=0),
                await Udp6TransportTarget.create(("localhost", AGENT_PORT)),
                ("1.3.6.1.2.1.1.1.0", None),
            )

            errorIndication, errorStatus, errorIndex, varBinds = iterator

            assert errorIndication is None
            assert errorStatus == 0
            assert errorIndex == 0
            assert len(varBinds) == 1
            assert varBinds[0][0].prettyPrint() == "1.3.6.1.2.1.1.1.0"
            assert varBinds[0][1].prettyPrint().startswith("PySNMP engine version")

            name = pysnmp_errorStatus.namedValues.getName(errorStatus)
            assert name == "noError"


@pytest.mark.asyncio
async def test_v1_get_timeout_invalid_target():
    with SnmpDispatcher() as snmpDispatcher:

        async def run_get():
            errorIndication, errorStatus, errorIndex, varBinds = await get_cmd(
                snmpDispatcher,
                CommunityData("community_string"),
                await UdpTransportTarget.create(("1.2.3.4", 161), timeout=1, retries=0),
                ObjectType(ObjectIdentity("1.3.6.1.4.1.60069.9.1.0")),
            )
            assert isinstance(errorIndication, RequestTimedOut)
            assert errorStatus == 0
            assert errorIndex == 0
            assert len(varBinds) == 0

        start = datetime.now()
        try:
            await asyncio.wait_for(run_get(), timeout=3)
            end = datetime.now()
            elapsed_time = (end - start).total_seconds()
            assert elapsed_time >= 1 and elapsed_time <= 3
        except asyncio.TimeoutError:
            assert False, "Test case timed out"


@pytest.mark.asyncio
async def test_v1_get_timeout_slow_object():
    async with AgentContextManager(enable_custom_objects=True):
        with SnmpDispatcher() as snmpDispatcher:

            async def run_get():
                errorIndication, errorStatus, errorIndex, varBinds = await get_cmd(
                    snmpDispatcher,
                    CommunityData("public", mpModel=0),
                    await UdpTransportTarget.create(
                        ("localhost", AGENT_PORT), timeout=1, retries=0
                    ),
                    ObjectType(ObjectIdentity("1.3.6.1.4.1.60069.9.1.0")),
                )
                for varBind in varBinds:
                    print([str(varBind[0]), varBind[1]])

            start = datetime.now()
            try:
                await asyncio.wait_for(run_get(), timeout=3)
                end = datetime.now()
                elapsed_time = (end - start).total_seconds()
                assert elapsed_time >= 1 and elapsed_time <= 3
            except asyncio.TimeoutError:
                assert False, "Test case timed out"


@pytest.mark.asyncio
async def test_v1_get_no_access_object():
    async with AgentContextManager(enable_custom_objects=True):
        with SnmpDispatcher() as snmpDispatcher:
            errorIndication, errorStatus, errorIndex, varBinds = await get_cmd(
                snmpDispatcher,
                CommunityData("public", mpModel=0),
                await UdpTransportTarget.create(
                    ("localhost", AGENT_PORT), timeout=1, retries=0
                ),
                ObjectType(ObjectIdentity("1.3.6.1.4.1.60069.9.3")),
            )
            assert errorIndication is None
            assert (
                errorStatus.prettyPrint() == "noSuchName"
            )  # v1 does not have noAccess


@pytest.mark.asyncio
async def test_v1_get_dateandtime_object():
    async with AgentContextManager(enable_custom_objects=True):
        with SnmpDispatcher() as 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", "testDateAndTime", 0)
                ),  # "1.3.6.1.4.1.60069.9.5.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::testDateAndTime.0"
            )
            assert (
                varBinds[0][1].prettyPrint() == "2024-11-3,19:52:40.0,+0:0"
            )  # IMPORTANT: test DateAndTime display hint.


@pytest.mark.asyncio
async def test_v1_get_physaddress_object():
    async with AgentContextManager(enable_custom_objects=True):
        with SnmpDispatcher() as 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", "testPhysAddress", 0)
                ),  # "1.3.6.1.4.1.60069.9.6.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::testPhysAddress.0"
            )
            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):
        with SnmpDispatcher() as 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):
        with SnmpDispatcher() as 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() == "0.5"  # GitHub issue #139
            )  # IMPORTANT: test display hint "d-1".


@pytest.mark.asyncio
async def test_v1_get_multiple_objects():
    async with AgentContextManager(enable_custom_objects=True):
        with SnmpDispatcher() as 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

            netiftable = [
                ObjectIdentity("IF-MIB", "ifDescr", 1),
                ObjectIdentity("IF-MIB", "ifDescr", 2),
            ]

            errorIndication, errorStatus, errorIndex, varBinds = await get_cmd(
                snmpDispatcher,
                CommunityData("public", mpModel=0),
                await UdpTransportTarget.create(
                    ("demo.pysnmp.com", 161), timeout=1, retries=0
                ),
                *[ObjectType(x) for x in netiftable],
            )
            assert errorIndication is None
            assert errorIndication is None
            assert errorStatus == 0
            assert errorIndex == 0
            assert len(varBinds) == 2
            assert varBinds[0][1].prettyPrint() != varBinds[1][1].prettyPrint()