File: test_session.py

package info (click to toggle)
python-ezsnmp 1.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,256 kB
  • sloc: cpp: 3,746; python: 1,987; javascript: 1,110; makefile: 17; sh: 12
file content (486 lines) | stat: -rw-r--r-- 14,292 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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
from __future__ import unicode_literals

import platform
import random
import re
import pytest
from time import sleep

from ezsnmp.exceptions import (
    EzSNMPError,
    EzSNMPConnectionError,
    EzSNMPTimeoutError,
    EzSNMPNoSuchObjectError,
    EzSNMPNoSuchInstanceError,
    EzSNMPNoSuchNameError,
)

from ezsnmp.session import Session


def test_session_invalid_snmp_version():
    with pytest.raises(ValueError):
        Session(version=4)


@pytest.mark.parametrize("version", [1, 2, 3])
def test_session_invalid_hostname(version):
    with pytest.raises(EzSNMPConnectionError):
        session = Session(hostname="invalid", version=version)
        session.get("sysContact.0")


@pytest.mark.parametrize("version", [1, 2, 3])
def test_session_invalid_hostname_and_remote_port(version):
    with pytest.raises(ValueError):
        Session(hostname="localhost:162", remote_port=163, version=version)


@pytest.mark.parametrize("version", [1, 2, 3])
def test_session_hostname_and_remote_port_split(version):
    session = Session(hostname="localhost:162", version=version)
    assert session.hostname == "localhost"
    assert session.remote_port == 162


@pytest.mark.parametrize("version", [1, 2, 3])
def test_session_invalid_port(version):
    with pytest.raises(EzSNMPTimeoutError):
        session = Session(remote_port=1234, version=version, timeout=0.2, retries=1)
        session.get("sysContact.0")


@pytest.mark.parametrize("version", [1, 2, 3])
def test_session_ipv6_address(version):
    session = Session(hostname="2001:db8::", version=version)
    assert session.hostname == "2001:db8::"
    assert session.connect_hostname == "2001:db8::"


@pytest.mark.parametrize("version", [1, 2, 3])
def test_session_ipv6_address_and_remote_port(version):
    session = Session(
        hostname="fd5d:12c9:2201:1:bc9c:f8ff:fe5c:57fa",
        remote_port=162,
        version=version,
    )
    assert session.hostname == "fd5d:12c9:2201:1:bc9c:f8ff:fe5c:57fa"
    assert session.remote_port == 162
    assert session.connect_hostname == "[fd5d:12c9:2201:1:bc9c:f8ff:fe5c:57fa]:162"
    del session


@pytest.mark.parametrize("version", [1, 2, 3])
def test_session_ipv6_address_and_remote_port_split(version):
    session = Session(hostname="[2001:db8::]:161", version=version)
    assert session.hostname == "[2001:db8::]"
    assert session.remote_port == 161
    assert session.connect_hostname == "[2001:db8::]:161"
    del session


@pytest.mark.parametrize("version", [1, 2, 3])
def test_session_ipv6_address_with_protocol_and_remote_port_split(version):
    session = Session(hostname="udp6:[2001:db8::]:162", version=version)
    assert session.hostname == "udp6:[2001:db8::]"
    assert session.remote_port == 162
    assert session.connect_hostname == "udp6:[2001:db8::]:162"
    del session


@pytest.mark.parametrize("version", [1, 2, 3])
def test_session_ipv6_address_with_protocol(version):
    session = Session(hostname="udp6:[2001:db8::]", version=version)
    assert session.hostname == "udp6:[2001:db8::]"
    assert session.connect_hostname == "udp6:[2001:db8::]"
    del session


@pytest.mark.parametrize("version", [1, 2, 3])
def test_session_ipv6_is_not_ipv6(version):
    with pytest.raises(ValueError):
        Session(hostname="[foo::bar]:161", version=version)


@pytest.mark.parametrize("version", [1, 2, 3])
def test_session_ipv6_invalid_hostname_and_remote_port(version):
    with pytest.raises(ValueError):
        Session(
            hostname="[fd5d:12c9:2201:1:bc9c:f8ff:fe5c:57fa]:161",
            remote_port=162,
            version=version,
        )


def test_session_set_multiple_next(sess, reset_values):
    # Destroy succeeds even if no row exists
    sess.set(".1.3.6.1.6.3.12.1.2.1.9.116.101.115.116", 6)
    success = sess.set_multiple(
        [
            (".1.3.6.1.6.3.12.1.2.1.2.116.101.115.116", ".1.3.6.1.6.1.1"),
            (".1.3.6.1.6.3.12.1.2.1.3.116.101.115.116", "1234"),
            (".1.3.6.1.6.3.12.1.2.1.9.116.101.115.116", 4),
        ]
    )
    assert success

    res = sess.get_next(
        ["snmpTargetAddrTDomain", "snmpTargetAddrTAddress", "snmpTargetAddrRowStatus"]
    )

    assert len(res) == 3

    assert res[0].oid == "snmpTargetAddrTDomain"
    assert res[0].oid_index == "116.101.115.116"
    assert res[0].value == ".1.3.6.1.6.1.1"
    assert res[0].snmp_type == "OBJECTID"

    assert res[1].oid == "snmpTargetAddrTAddress"
    assert res[1].oid_index == "116.101.115.116"
    assert res[1].value == "1234"
    assert res[1].snmp_type == "OCTETSTR"

    assert res[2].oid == "snmpTargetAddrRowStatus"
    assert res[2].oid_index == "116.101.115.116"
    assert res[2].value == "3"
    assert res[2].snmp_type == "INTEGER"

    del sess


def test_session_set_clear(sess):
    res = sess.set(".1.3.6.1.6.3.12.1.2.1.9.116.101.115.116", 6)
    assert res == 1

    res = sess.get_next(
        ["snmpTargetAddrTDomain", "snmpTargetAddrTAddress", "snmpTargetAddrRowStatus"]
    )

    assert len(res) == 3

    assert res[0].oid == "snmpUnavailableContexts"
    assert res[0].oid_index == "0"
    assert res[0].value == "0"
    assert res[0].snmp_type == "COUNTER"

    assert res[1].oid == "snmpUnavailableContexts"
    assert res[1].oid_index == "0"
    assert res[1].value == "0"
    assert res[1].snmp_type == "COUNTER"

    assert res[2].oid == "snmpUnavailableContexts"
    assert res[2].oid_index == "0"
    assert res[2].value == "0"
    assert res[2].snmp_type == "COUNTER"

    del sess


def test_session_get(sess):
    res = sess.get([("sysUpTime", "0"), ("sysContact", "0"), ("sysLocation", "0")])

    assert len(res) == 3

    assert res[0].oid == "sysUpTimeInstance"
    assert res[0].oid_index == ""
    assert int(res[0].value) > 0
    assert res[0].snmp_type == "TICKS"

    assert res[1].oid == "sysContact"
    assert res[1].oid_index == "0"
    assert res[1].value == "G. S. Marzot <gmarzot@marzot.net>"
    assert res[1].snmp_type == "OCTETSTR"

    assert res[2].oid == "sysLocation"
    assert res[2].oid_index == "0"
    assert res[2].value == "my original location"
    assert res[2].snmp_type == "OCTETSTR"

    del sess


def test_session_get_use_numeric(sess):
    sess.use_numeric = True
    res = sess.get("sysContact.0")

    assert res.oid == ".1.3.6.1.2.1.1.4"
    assert res.oid_index == "0"
    assert res.value == "G. S. Marzot <gmarzot@marzot.net>"
    assert res.snmp_type == "OCTETSTR"

    del sess


def test_session_get_use_sprint_value(sess):
    sess.use_sprint_value = True
    res = sess.get("sysUpTimeInstance")

    assert res.oid == "sysUpTimeInstance"
    assert res.oid_index == ""
    assert re.match(r"^\d+:\d+:\d+:\d+\.\d+$", res.value)
    assert res.snmp_type == "TICKS"

    del sess


def test_session_get_use_enums(sess):
    sess.use_enums = True
    res = sess.get("ifAdminStatus.1")

    assert res.oid == "ifAdminStatus"
    assert res.oid_index == "1"
    assert res.value == "up"
    assert res.snmp_type == "INTEGER"

    del sess


def test_session_get_next(sess):
    res = sess.get_next([("sysUpTime", "0"), ("sysContact", "0"), ("sysLocation", "0")])

    assert len(res) == 3

    assert res[0].oid == "sysContact"
    assert res[0].oid_index == "0"
    assert res[0].value == "G. S. Marzot <gmarzot@marzot.net>"
    assert res[0].snmp_type == "OCTETSTR"

    assert res[1].oid == "sysName"
    assert res[1].oid_index == "0"
    assert res[1].value == platform.node()
    assert res[1].snmp_type == "OCTETSTR"

    assert res[2].oid == "sysORLastChange"
    assert res[2].oid_index == "0"
    assert int(res[2].value) >= 0
    assert res[2].snmp_type == "TICKS"

    del sess


def test_session_set(sess, reset_values):
    res = sess.get(("sysLocation", "0"))
    assert res.value != "my newer location"

    success = sess.set(("sysLocation", "0"), "my newer location")
    assert success

    res = sess.get(("sysLocation", "0"))
    assert res.value == "my newer location"

    del sess


def test_session_set_multiple(sess, reset_values):
    res = sess.get(["sysLocation.0", "nsCacheTimeout.1.3.6.1.2.1.2.2"])
    assert res[0].value != "my newer location"
    assert res[1].value != "160"

    success = sess.set_multiple(
        [
            ("sysLocation.0", "my newer location"),
            (("nsCacheTimeout", ".1.3.6.1.2.1.2.2"), 160),
        ]
    )
    assert success

    res = sess.get(["sysLocation.0", "nsCacheTimeout.1.3.6.1.2.1.2.2"])
    assert res[0].value == "my newer location"
    assert res[1].value == "160"

    del sess


def test_session_get_bulk(sess):  # noqa
    if sess.version == 1:
        with pytest.raises(EzSNMPError):
            sess.get_bulk(
                [
                    "sysUpTime",
                    "sysORLastChange",
                    "sysORID",
                    "sysORDescr",
                    "sysORUpTime",
                ],
                2,
                8,
            )
    else:
        res = sess.get_bulk(
            ["sysUpTime", "sysORLastChange", "sysORID", "sysORDescr", "sysORUpTime"],
            2,
            8,
        )

        assert len(res) == 26

        assert res[0].oid == "sysUpTimeInstance"
        assert res[0].oid_index == ""
        assert int(res[0].value) > 0
        assert res[0].snmp_type == "TICKS"

        assert res[4].oid == "sysORUpTime"
        assert res[4].oid_index == "1"
        assert int(res[4].value) >= 0
        assert res[4].snmp_type == "TICKS"

        del sess


def test_session_get_invalid_instance(sess):
    # Sadly, SNMP v1 doesn't distuingish between an invalid instance and an
    # invalid object ID, instead it excepts with noSuchName
    if sess.version == 1:
        with pytest.raises(EzSNMPNoSuchNameError):
            sess.get("sysDescr.100")
    else:
        res = sess.get("sysDescr.100")
        assert res.snmp_type == "NOSUCHINSTANCE"


def test_session_get_invalid_instance_with_abort_enabled(sess):
    # Sadly, SNMP v1 doesn't distuingish between an invalid instance and an
    # invalid object ID, instead it excepts with noSuchName
    sess.abort_on_nonexistent = True
    if sess.version == 1:
        with pytest.raises(EzSNMPNoSuchNameError):
            sess.get("sysDescr.100")
    else:
        with pytest.raises(EzSNMPNoSuchInstanceError):
            sess.get("sysDescr.100")


def test_session_get_invalid_object(sess):
    if sess.version == 1:
        with pytest.raises(EzSNMPNoSuchNameError):
            sess.get("iso")
    else:
        res = sess.get("iso")
        assert res.snmp_type == "NOSUCHOBJECT"


def test_session_get_invalid_object_with_abort_enabled(sess):
    sess.abort_on_nonexistent = True
    if sess.version == 1:
        with pytest.raises(EzSNMPNoSuchNameError):
            sess.get("iso")
    else:
        with pytest.raises(EzSNMPNoSuchObjectError):
            sess.get("iso")


def test_session_walk(sess):
    res = sess.walk("system")

    assert len(res) >= 7

    assert res[0].oid == "sysDescr"
    assert res[0].oid_index == "0"
    assert platform.version() in res[0].value
    assert res[0].snmp_type == "OCTETSTR"

    assert res[3].oid == "sysContact"
    assert res[3].oid_index == "0"
    assert res[3].value == "G. S. Marzot <gmarzot@marzot.net>"
    assert res[3].snmp_type == "OCTETSTR"

    assert res[4].oid == "sysName"
    assert res[4].oid_index == "0"
    assert res[4].value == platform.node()
    assert res[4].snmp_type == "OCTETSTR"

    assert res[5].oid == "sysLocation"
    assert res[5].oid_index == "0"
    assert res[5].value == "my original location"
    assert res[5].snmp_type == "OCTETSTR"

    del sess


def test_session_bulkwalk(sess):
    if sess.version == 1:
        with pytest.raises(EzSNMPError):
            sess.bulkwalk("system")
    else:
        res = sess.walk("system")

        assert len(res) >= 7

        assert res[0].oid == "sysDescr"
        assert res[0].oid_index == "0"
        assert platform.version() in res[0].value
        assert res[0].snmp_type == "OCTETSTR"

        assert res[3].oid == "sysContact"
        assert res[3].oid_index == "0"
        assert res[3].value == "G. S. Marzot <gmarzot@marzot.net>"
        assert res[3].snmp_type == "OCTETSTR"

        assert res[4].oid == "sysName"
        assert res[4].oid_index == "0"
        assert res[4].value == platform.node()
        assert res[4].snmp_type == "OCTETSTR"

        assert res[5].oid == "sysLocation"
        assert res[5].oid_index == "0"
        assert res[5].value == "my original location"
        assert res[5].snmp_type == "OCTETSTR"

        del sess


def test_session_walk_all(sess):
    # Introduce some radom sleep to prevent us DDoSing our own snmp test server.
    sleep(random.uniform(0.1, 0.500))

    # OID 1.3.6.1.6.3.16.1.5.2.1.6.6.95.110.111.110.101.95.1.2
    # or SNMP-VIEW-BASED-ACM-MIB::vacmViewTreeFamilyStatus."_none_".1.2
    # appears to return a noSuchName error when using v1, but not with v2c.
    # This may be a Net-SNMP snmpd bug.
    if sess.version == 1:
        with pytest.raises(EzSNMPNoSuchNameError):
            sess.walk(".")
    else:
        res = sess.walk(".")

        assert len(res) > 0

        assert res[0].oid == "sysDescr"
        assert res[0].oid_index == "0"
        assert platform.version() in res[0].value
        assert res[0].snmp_type == "OCTETSTR"

        assert res[3].oid == "sysContact"
        assert res[3].oid_index == "0"
        assert res[3].value == "G. S. Marzot <gmarzot@marzot.net>"
        assert res[3].snmp_type == "OCTETSTR"

        assert res[4].oid == "sysName"
        assert res[4].oid_index == "0"
        assert res[4].value == platform.node()
        assert res[4].snmp_type == "OCTETSTR"

        assert res[5].oid == "sysLocation"
        assert res[5].oid_index == "0"
        assert res[5].value == "my original location"
        assert res[5].snmp_type == "OCTETSTR"

        del sess


def test_session_update():
    s = Session(version=3)
    ptr = s.sess_ptr
    s.version = 1
    s.update_session()
    assert ptr != s.sess_ptr
    s.tunneled = True
    ptr = s.sess_ptr
    with pytest.raises(ValueError):
        s.update_session()
    assert ptr == s.sess_ptr
    s.update_session(tunneled=False, version=2)
    assert s.version == 2
    assert s.tunneled is False

    del s