#!/usr/bin/env bash # snmpset. This requires deviation of MIB-YANG to make write operations # Get default value, set new value via SNMP and check it, set new value via NETCONF and check # Selected types from CLIXON/IF-MIB/ENTITY mib # Also an incomplete commit failed test # Magic line must be first in script (see README.md) s="$_" ; . ./lib.sh || if [ "$s" = $0 ]; then exit 0; else return 0; fi 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 snmpd=$(type -p snmpd) snmpget="$(type -p snmpget) -On -c public -v2c localhost " snmpset="$(type -p snmpset) -On -c public -v2c localhost " cfg=$dir/conf.xml fyang=$dir/clixon-example.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 /var/tmp/$APPNAME.pidfile /usr/local/lib/$APPNAME/backend $dir unix:$SOCK CLIXON-TYPES-MIB IF-MIB ENTITY-MIB true ietf-netconf:startup $APPNAME /usr/local/lib/$APPNAME/cli /usr/local/lib/$APPNAME/clispec EOF cat < $fyang module clixon-example{ yang-version 1.1; namespace "urn:example:clixon"; prefix ex; import CLIXON-TYPES-MIB { prefix "clixon-types"; } import IF-MIB { prefix "if-mib"; } import ENTITY-MIB { prefix "entity-mib"; } deviation "/clixon-types:CLIXON-TYPES-MIB" { deviate replace { config true; } } deviation "/if-mib:IF-MIB" { deviate replace { config true; } } deviation "/entity-mib:ENTITY-MIB" { deviate replace { config true; } } } EOF if true; then # Dont start with a state (default) cat < $dir/startup_db <${DATASTORE_TOP}> EOF else # Start with a state (debug) cat < $dir/startup_db <${DATASTORE_TOP}> 42 4.3.2.1 9 9 1 aa:bb:cc:dd:ee:ff EOF fi function testinit(){ new "test params: -f $cfg" 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 startup -f $cfg 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" start_snmp $cfg & fi new "wait snmp" wait_snmp } # Set value via SNMP, read value via SNMP and CLI # Args: # 1: name # 2: type # 3: value SNMP value # 4: value2 SNMP value2 (as shown "after" snmpset) # 5: xvalue XML/Clixon value # 6: OID function testrun() { name=$1 type=$2 value=$3 value2=$4 xvalue=$5 oid=$6 # Type from man snmpset case $type in "INTEGER") set_type="i" ;; "STRING") set_type="s" ;; "HEX STRING") set_type="x" ;; "TIMETICKS") set_type="t" ;; "IPADDRESS") set_type="a" ;; "OBJID") set_type="o" ;; "BITS") set_type="b" ;; *) set_type="s" ;; esac new "Set $name via SNMP" if [ $type == "STRING" ]; then echo "$snmpset $oid $set_type $value" expectpart "$($snmpset $oid $set_type $value)" 0 "$type:" "$value" else echo "$snmpset $oid $set_type $value2" expectpart "$($snmpset $oid $set_type $value)" 0 "$type: $value2" fi new "Check $name via SNMP" if [ "$type" == "STRING" ]; then expectpart "$($snmpget $oid)" 0 "$type:" "$value" else expectpart "$($snmpget $oid)" 0 "$type: $value2" fi new "Check $name via CLI" expectpart "$($clixon_cli -1 -f $cfg show config xml)" 0 "<$name>$xvalue" } 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 MIB=".1.3.6.1.4.1.8072.200" IFMIB=".1.3.6.1.2.1" ENTMIB=".1.3.6.1.2.1.47.1.1.1" testrun clixonExampleInteger INTEGER 1234 1234 1234 ${MIB}.1.1 testrun clixonExampleSleeper INTEGER -1 -1 -1 ${MIB}.1.2 testrun clixonExampleString STRING foobar foobar foobar ${MIB}.1.3 testrun ifPromiscuousMode INTEGER 1 1 true ${MIB}.1.10 # boolean testrun ifIpAddr IPADDRESS 1.2.3.4 1.2.3.4 1.2.3.4 ${MIB}.1.13 # InetAddress # XXX It was supposed to test writing hardware address type, but it is also read-only #testrun ifPhysAddress STRING ff:ee:dd:cc:bb:aa ff:ee:dd:cc:bb:aa ff:ee:dd:cc:bb:aa ${IFMIB}.2.2.1.6.1 # Inline testrun for rowstatus complicated logic name=ifStackStatus type=INTEGER oid=${IFMIB}.31.1.2.1.3.5.9 new "Set $name via SNMP" expectpart "$($snmpset $oid i 4)" 0 "$type: createAndGo(4)" new "Check $name via SNMP" expectpart "$($snmpget $oid)" 0 "$type: active(1)" new "Check $name via CLI" expectpart "$($clixon_cli -1 -f $cfg show config xml)" 0 "<$name>active" # restart backend with synthetic validation failure # Incomplete commit failed test: the commit fails by logging but this is not actually checked 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 startup -f $cfg -- -V CLIXON-TYPES-MIB/clixonExampleScalars/clixonExampleInteger fi new "wait backend" wait_backend new "set value with error" expectpart "$($snmpset ${MIB}.1.1 i 4321 2>&1)" 2 "commitFailed" new "Cleaning up" testexit rm -rf $dir new "endtest" endtest