File: conftest.py

package info (click to toggle)
python-easysnmp 0.2.6-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 692 kB
  • sloc: ansic: 3,735; python: 1,410; makefile: 161
file content (130 lines) | stat: -rw-r--r-- 3,615 bytes parent folder | download | duplicates (2)
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
from __future__ import unicode_literals

import logging
import pytest
from sys import version_info

if version_info[0] >= 3:
    from subprocess import Popen, DEVNULL
else:
    from subprocess import Popen
    from os import devnull

import easysnmp


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

    pass


if version_info[0] >= 3:  # Required to avoid UnboundLocalError in Python 3

    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)
            )

else:

    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)
        """
        DEVNULL = open(devnull, "w")
        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)
            )
        DEVNULL.close()


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


SESS_V1_ARGS = {
    "version": 1,
    "hostname": "localhost",
    "remote_port": 11161,
    "community": "public",
}

SESS_V2_ARGS = {
    "version": 2,
    "hostname": "localhost",
    "remote_port": 11161,
    "community": "public",
}

SESS_V3_ARGS = {
    "version": 3,
    "hostname": "localhost",
    "remote_port": 11161,
    "security_level": "authPriv",
    "security_username": "initial",
    "privacy_password": "priv_pass",
    "auth_password": "auth_pass",
}


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


@pytest.fixture
def sess(sess_args):
    return easysnmp.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():
    return SESS_V3_ARGS