File: conftest.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 (106 lines) | stat: -rw-r--r-- 2,463 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
from __future__ import unicode_literals

import logging
import pytest
from sys import version_info
from subprocess import Popen, DEVNULL
import ezsnmp
from .session_parameters import (
    SESS_V1_ARGS,
    SESS_V2_ARGS,
    SESS_V3_MD5_DES_ARGS,
    SESS_V3_MD5_AES_ARGS,
    SESS_V3_SHA_AES_ARGS,
    SESS_V3_SHA_NO_PRIV_ARGS,
    SESS_V3_MD5_NO_PRIV_ARGS,
)

assert version_info[0] == 3 and version_info[1] >= 8


class SNMPSetCLIError(Exception):
    """An exception raised when an SNMP SET fails via the CLI."""

    pass


def snmp_set_via_cli(oid, value, type):
    """
    Sets an SNMP variable using the snmpset command.

    :param oid: the OID to update
    :param value: the new value to set the OID to
    :param type: a single character type as required by the snmpset command
                    (i: INTEGER, u: unsigned INTEGER, t: TIMETICKS,
                    a: IPADDRESS o: OBJID, s: STRING, x: HEX STRING,
                    d: DECIMAL STRING, b: BITS U: unsigned int64,
                    I: signed int64, F: float, D: double)
    """
    process = Popen(
        "snmpset -v2c -c public localhost:11161 {} {} {}".format(
            oid, type, '"{}"'.format(value) if type == "s" else value
        ),
        stdout=DEVNULL,
        stderr=DEVNULL,
        shell=True,
    )
    process.communicate()
    if process.returncode != 0:
        raise SNMPSetCLIError(
            "failed to set {0} to {1} (type {2})".format(oid, value, type)
        )


# Disable logging for the C interface
snmp_logger = logging.getLogger("ezsnmp.interface")
snmp_logger.disabled = True


@pytest.fixture(
    params=[
        SESS_V1_ARGS,
        SESS_V2_ARGS,
        SESS_V3_MD5_DES_ARGS,
        SESS_V3_MD5_AES_ARGS,
        SESS_V3_SHA_AES_ARGS,
    ]
)
def sess_args(request):
    return request.param


@pytest.fixture
def sess(sess_args):
    return ezsnmp.Session(**sess_args)


@pytest.fixture
def reset_values():
    yield None
    snmp_set_via_cli("sysLocation.0", "my original location", "s")
    snmp_set_via_cli("nsCacheTimeout.1.3.6.1.2.1.2.2", "0", "i")


@pytest.fixture
def sess_v3_md5_des():
    return SESS_V3_MD5_DES_ARGS


@pytest.fixture
def sess_v3_md5_aes():
    return SESS_V3_MD5_AES_ARGS


@pytest.fixture
def sess_v3_sha_aes():
    return SESS_V3_SHA_AES_ARGS


@pytest.fixture
def sess_v3_sha_no_priv():
    return SESS_V3_SHA_NO_PRIV_ARGS


@pytest.fixture
def sess_v3_md5_no_priv():
    return SESS_V3_MD5_NO_PRIV_ARGS