Skip to content

Commit

Permalink
snmp: don't let CommandGenerator be a singleton
Browse files Browse the repository at this point in the history
With SNMPv3, it seems to keep some state. Add some test to highlight the bug.

Closes #35.
  • Loading branch information
vincentbernat committed Jun 26, 2015
1 parent f0558ce commit 4b2db7f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
3 changes: 1 addition & 2 deletions snimpy/snmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ class Session(object):
session. From such an instance, one can get information from the
associated agent."""

_cmdgen = cmdgen.CommandGenerator()

def __init__(self, host,
community="public", version=2,
secname=None,
Expand Down Expand Up @@ -116,6 +114,7 @@ def __init__(self, host,
"""
self._host = host
self._version = version
self._cmdgen = cmdgen.CommandGenerator()

# Put authentication stuff in self._auth
if version in [1, 2]:
Expand Down
12 changes: 7 additions & 5 deletions tests/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ class TestAgent(object):

"""Agent for testing purpose"""

def __init__(self, ipv6=False):
def __init__(self, ipv6=False, community='public', authpass='authpass', privpass='privpass'):
q = Queue()
self.ipv6 = ipv6
self.community = community
self.authpass = authpass
self.privpass = privpass
self._process = Process(target=self._setup, args=(q,))
self._process.start()
self.port = q.get()
Expand All @@ -40,16 +43,15 @@ def _setup(self, q):
udp.domainName,
udp.UdpTransport().openServerMode(('127.0.0.1', port)))
# Community is public and MIB is writable
config.addV1System(snmpEngine, 'read-write', 'public')
config.addV1System(snmpEngine, 'read-write', self.community)
config.addVacmUser(snmpEngine, 1, 'read-write', 'noAuthNoPriv',
(1, 3, 6), (1, 3, 6))
config.addVacmUser(snmpEngine, 2, 'read-write', 'noAuthNoPriv',
(1, 3, 6), (1, 3, 6))
config.addV3User(
snmpEngine, 'read-write',
config.usmHMACMD5AuthProtocol, 'authpass',
config.usmAesCfb128Protocol, 'privpass'
)
config.usmHMACMD5AuthProtocol, self.authpass,
config.usmAesCfb128Protocol, self.privpass)
config.addVacmUser(snmpEngine, 3, 'read-write', 'authPriv',
(1, 3, 6), (1, 3, 6))

Expand Down
20 changes: 20 additions & 0 deletions tests/test_snmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,26 @@ def setUp(self):
authprotocol="MD5", authpassword="authpass",
privprotocol="AES", privpassword="privpass")

def testSeveralSessions(self):
agent2 = agent.TestAgent(authpass='authpass2', privpass='privpass2')
try:
session1 = self.session
session2 = snmp.Session(
host="127.0.0.1:{0}".format(agent2.port),
version=3,
secname="read-write",
authprotocol="MD5", authpassword="authpass2",
privprotocol="AES", privpassword="privpass2")
ooid = mib.get('SNMPv2-MIB', 'sysDescr').oid + (0,)
oid1, a1 = session1.get(ooid)[0]
oid2, a2 = session2.get(ooid)[0]
self.assertEqual(oid1, ooid)
self.assertEqual(oid2, ooid)
self.assertEqual(a1, b"Snimpy Test Agent")
self.assertEqual(a2, b"Snimpy Test Agent")
finally:
agent2.terminate()


class TestSnmpTransports(unittest.TestCase):

Expand Down

0 comments on commit 4b2db7f

Please sign in to comment.