From: Remi Gacogne Date: Wed, 6 Jun 2018 15:15:51 +0000 (+0200) Subject: rec: Add regression tests for SNMP X-Git-Tag: dnsdist-1.3.1~11^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=aa7a54c910f03af7c71a9bec0e4f8afda320aeb3;p=pdns rec: Add regression tests for SNMP --- diff --git a/build-scripts/travis.sh b/build-scripts/travis.sh index 99efc4e0e..ef4a0a6ce 100755 --- a/build-scripts/travis.sh +++ b/build-scripts/travis.sh @@ -355,6 +355,12 @@ install_recursor() { run "sudo touch /etc/authbind/byport/53" run "sudo chmod 755 /etc/authbind/byport/53" run "cd ${TRAVIS_BUILD_DIR}" + # install SNMP + run "sudo sed -i \"s/agentxperms 0700 0755 recursor/agentxperms 0700 0755 ${USER}/g\" regression-tests.recursor-dnssec/snmpd.conf" + run "sudo cp -f regression-tests.recursor-dnssec/snmpd.conf /etc/snmp/snmpd.conf" + run "sudo service snmpd restart" + ## fun story, the directory perms are only applied if it doesn't exist yet, and it is created by the init script, so.. + run "sudo chmod 0755 /var/agentx" } install_dnsdist() { diff --git a/regression-tests.recursor-dnssec/requirements.txt b/regression-tests.recursor-dnssec/requirements.txt index 383124778..18aa37e59 100644 --- a/regression-tests.recursor-dnssec/requirements.txt +++ b/regression-tests.recursor-dnssec/requirements.txt @@ -1,5 +1,5 @@ dnspython>=1.11 nose>=1.3.7 +pysnmp>=4.3.4 requests>=2.1.0 Twisted>0.15.0 - diff --git a/regression-tests.recursor-dnssec/snmpd.conf b/regression-tests.recursor-dnssec/snmpd.conf new file mode 100644 index 000000000..b881dd7ec --- /dev/null +++ b/regression-tests.recursor-dnssec/snmpd.conf @@ -0,0 +1,13 @@ + +# act as an Agent X master so that the recursor can export SNMP statistics +master agentx + +# allow the recursor to connect to the Agent X master socket +agentxperms 0700 0755 recursor + +# SNMPv2c community +rocommunity secretcommunity + +# SNMPv3 user +createUser secretuser SHA "mysecretauthkey" AES "mysecretenckey" +rouser secretuser diff --git a/regression-tests.recursor-dnssec/test_SNMP.py b/regression-tests.recursor-dnssec/test_SNMP.py new file mode 100644 index 000000000..df688828f --- /dev/null +++ b/regression-tests.recursor-dnssec/test_SNMP.py @@ -0,0 +1,78 @@ +import time + +from pysnmp.hlapi import * + +from recursortests import RecursorTest + +class TestSNMP(RecursorTest): + + _snmpTimeout = 2.0 + _snmpServer = '127.0.0.1' + _snmpPort = 161 + _snmpV2Community = 'secretcommunity' + _snmpV3User = 'secretuser' + _snmpV3AuthKey = 'mysecretauthkey' + _snmpV3EncKey = 'mysecretenckey' + _snmpOID = '1.3.6.1.4.1.43315.2' + _queriesSent = 0 + _confdir = 'SNMP' + _config_template = """ + snmp-agent=yes + """ + + def _checkStatsValues(self, results): + for i in list(range(1, 93)): + oid = self._snmpOID + '.1.' + str(i) + '.0' + self.assertTrue(oid in results) + self.assertTrue(isinstance(results[oid], Counter64)) + + # check uptime > 0 + self.assertGreater(results['1.3.6.1.4.1.43315.2.1.75.0'], 0) + # check memory usage > 0 + self.assertGreater(results['1.3.6.1.4.1.43315.2.1.76.0'], 0) + + def _getSNMPStats(self, auth): + results = {} + for (errorIndication, errorStatus, errorIndex, varBinds) in nextCmd(SnmpEngine(), + auth, + UdpTransportTarget((self._snmpServer, self._snmpPort), timeout=self._snmpTimeout), + ContextData(), + ObjectType(ObjectIdentity(self._snmpOID)), + lookupMib=False): + self.assertFalse(errorIndication) + self.assertFalse(errorStatus) + self.assertTrue(varBinds) + for key, value in varBinds: + keystr = key.prettyPrint() + if not keystr.startswith(self._snmpOID): + continue + results[keystr] = value + + return results + + def _checkStats(self, auth): + # wait 1s so that the uptime is > 0 + time.sleep(1) + + results = self._getSNMPStats(auth) + self._checkStatsValues(results) + + def testSNMPv2Stats(self): + """ + SNMP: Retrieve statistics via SNMPv2c + """ + + auth = CommunityData(self._snmpV2Community, mpModel=1) + self._checkStats(auth) + + def testSNMPv3Stats(self): + """ + SNMP: Retrieve statistics via SNMPv3 + """ + + auth = UsmUserData(self._snmpV3User, + authKey=self._snmpV3AuthKey, + privKey=self._snmpV3EncKey, + authProtocol=usmHMACSHAAuthProtocol, + privProtocol=usmAesCfb128Protocol) + self._checkStats(auth)