diff --git a/docker/test/Dockerfile.native b/docker/test/Dockerfile.native index 47648bc2..afe75a5d 100644 --- a/docker/test/Dockerfile.native +++ b/docker/test/Dockerfile.native @@ -150,6 +150,8 @@ RUN echo "agentaddress 127.0.0.1" >> /etc/snmp/snmpd.conf RUN echo "rwcommunity public localhost" >> /etc/snmp/snmpd.conf RUN echo "agentxsocket unix:/var/run/snmp.sock" >> /etc/snmp/snmpd.conf RUN echo "agentxperms 777 777" >> /etc/snmp/snmpd.conf +RUN echo "trap2sink localhost public 162" >> /etc/snmp/snmpd.conf +RUN echo "disableAuthorization yes" >> /etc/snmp/snmptrapd.conf # Dont need to expose restconf ports for internal tests #EXPOSE 80/tcp diff --git a/test/test_snmp_notifications.sh b/test/test_snmp_notifications.sh new file mode 100755 index 00000000..0157e370 --- /dev/null +++ b/test/test_snmp_notifications.sh @@ -0,0 +1,158 @@ +#!/usr/bin/env bash +# SNMP notifications (SNMP v2 traps) MIB test + +# Magic line must be first in script (see README.md) +s="$_" ; . ./lib.sh || if [ "$s" = $0 ]; then exit 0; else return 0; fi + +# Re-use main example backend state callbacks +APPNAME=example + +if [ ${ENABLE_NETSNMP} != "yes" ]; then + echo "Skipping test, Net-SNMP support not enabled." + rm -rf $dir + if [ "$s" = $0 ]; then exit 0; else return 0; fi +fi + +: ${PERIOD:=2} + +cfg=$dir/conf.xml +fyang=$dir/stream.yang + +# AgentX unix socket +SOCK=/var/run/snmp.sock + +cat < $cfg + + $cfg + ${YANG_INSTALLDIR} + ${YANG_STANDARD_DIR} + ${MIB_GENERATED_YANG_DIR} + $fyang + $dir/$APPNAME.sock + /usr/local/lib/$APPNAME/backend + /usr/local/var/run/$APPNAME.pidfile + $dir + unix:$SOCK + example + true + true + true + streams + 60 + +EOF + +cat < $fyang +module example { + namespace "urn:example:clixon"; + prefix ex; + import ietf-yang-smiv2 { + prefix "smiv2"; + } + organization "Example, Inc."; + contact "support at example.com"; + description "Example Notification Data Model Module."; + revision "2016-07-07" { + description "Initial version."; + reference "example.com document 2-9976."; + } + notification event { + smiv2:oid "1.3.6.1.4.1.8072.200.1"; + description "Example notification event."; + leaf event-class { + smiv2:oid "1.3.6.1.4.1.8072.200.1.1"; + type string; + description "Event class identifier."; + } + container reportingEntity { + smiv2:oid "1.3.6.1.4.1.8072.200.1.2"; + description "Event specific information."; + leaf card { + smiv2:oid "1.3.6.1.4.1.8072.200.1.2.1"; + type string; + description "Line card identifier."; + } + } + leaf severity { + smiv2:oid "1.3.6.1.4.1.8072.200.1.3"; + type string; + description "Event severity description."; + } + } + container state { + config false; + description "state data for the example application (must be here for example get operation)"; + leaf-list op { + type string; + } + } +} +EOF + +function testinit(){ + new "test params: -s init -f $cfg -- -n ${PERIOD}" + if [ $BE -ne 0 ]; then + # Kill old backend and start a new one + new "kill old backend" + sudo clixon_backend -zf $cfg + if [ $? -ne 0 ]; then + err "Failed to start backend" + fi + + sudo pkill -f clixon_backend + + new "Starting backend" + start_backend -s init -f $cfg -- -n ${PERIOD} + fi + + new "wait backend" + wait_backend + + if [ $SN -ne 0 ]; then + # Kill old clixon_snmp, if any + new "Terminating any old clixon_snmp processes" + sudo killall -q clixon_snmp + + new "Starting clixon_snmp" + # XXX augmented objects seem to be registered twice: error: duplicate registration: MIB modules snmpSetSerialNo and AgentX subagent 52, session 0x562087a70e20, subsession 0x562087a820c0 (oid .1.3.6.1.6.3.1.1.6.1). + + start_snmp $cfg + fi + + new "wait snmp" + wait_snmp +} + +function testexit(){ + stop_snmp + if [ $BE -ne 0 ]; then + new "Kill backend" + # Check if premature kill + pid=$(pgrep -u root -f clixon_backend) + if [ -z "$pid" ]; then + err "backend already dead" + fi + # kill backend + stop_backend -f $cfg + fi +} + +new "SNMP tests" +testinit + + +new "Start snmptrapd and listen for traps - expect 2-3 notifications" +ret=$(timeout 6s snmptrapd -f -Ot -Lo -F\"%#v\\n\") +expect="DISMAN-EVENT-MIB::sysUpTimeInstance = [0-9]*, SNMPv2-MIB::snmpTrapOID.0 = OID: NET-SNMP-MIB::netSnmp.200.1, NET-SNMP-MIB::netSnmp.200.1.1 = STRING: \"fault\", NET-SNMP-MIB::netSnmp.200.1.2.1 = STRING: \"Ethernet0\", NET-SNMP-MIB::netSnmp.200.1.3 = STRING: \"major\"" +match=$(echo "$ret" | grep -Eo "$expect") +if [ -z "$match" ]; then + err "$expect" "$ret" +fi + +new "Cleaning up" +testexit + +rm -rf $dir + +new "endtest" +endtest