|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""SNMP basic |
| 3 | +
|
| 4 | +Enable the SNMP agent with a read-only community and query it over |
| 5 | +SNMPv2c from the host. Verify sysObjectID, that IF-MIB ifName at a given index |
| 6 | +matches the interface of that if-index in the operational datastore, |
| 7 | +that ifHCInOctets is consistent with the interface statistics, that an |
| 8 | +unknown community is refused, and that disabling the agent stops it |
| 9 | +answering. |
| 10 | +
|
| 11 | +Queries use numeric OIDs, so the test does not depend on MIB text files |
| 12 | +being installed in the container. |
| 13 | +
|
| 14 | +""" |
| 15 | + |
| 16 | +import time |
| 17 | + |
| 18 | +import infamy |
| 19 | +import infamy.iface as iface |
| 20 | + |
| 21 | +# SNMPv2-MIB::sysObjectID.0, and the arc confd hands out for Infix |
| 22 | +SYSOBJECTID = ".1.3.6.1.2.1.1.2.0" |
| 23 | +INFIX_OID = ".1.3.6.1.4.1.61046.1.1.1" |
| 24 | + |
| 25 | +# IF-MIB::ifName (ifXTable) and IF-MIB::ifHCInOctets |
| 26 | +IFNAME = ".1.3.6.1.2.1.31.1.1.1.1" |
| 27 | +IFHCINOCTETS = ".1.3.6.1.2.1.31.1.1.1.6" |
| 28 | + |
| 29 | +COMMUNITY = "infix-test" |
| 30 | +TARGET_IP = "10.0.0.10" |
| 31 | + |
| 32 | + |
| 33 | +def retry(fn, attempts=10, interval=1): |
| 34 | + """Poll fn until it returns something truthy, None on timeout. |
| 35 | +
|
| 36 | + infamy.until() raises instead, which turns a quiet agent into a |
| 37 | + traceback rather than a failed step saying what was expected. |
| 38 | + """ |
| 39 | + for _ in range(attempts): |
| 40 | + val = fn() |
| 41 | + if val: |
| 42 | + return val |
| 43 | + time.sleep(interval) |
| 44 | + |
| 45 | + return None |
| 46 | + |
| 47 | + |
| 48 | +def snmpget(netns, oid, community=COMMUNITY): |
| 49 | + """Return the value half of a single-varbind response, or None.""" |
| 50 | + rc = netns.runsh(f"snmpget -v2c -c {community} -r 1 -t 2 -On -Oqv " |
| 51 | + f"{TARGET_IP} {oid}") |
| 52 | + if rc.returncode != 0: |
| 53 | + return None |
| 54 | + |
| 55 | + out = rc.stdout.strip() |
| 56 | + return out if out and "No Such" not in out and "Timeout" not in out else None |
| 57 | + |
| 58 | + |
| 59 | +with infamy.Test() as test: |
| 60 | + with test.step("Set up topology and attach to target DUT"): |
| 61 | + env = infamy.Env() |
| 62 | + target = env.attach("target", "mgmt") |
| 63 | + _, hport = env.ltop.xlate("host", "data") |
| 64 | + |
| 65 | + if not target.has_model("ietf-snmp"): |
| 66 | + test.skip() |
| 67 | + |
| 68 | + with test.step("Set IPv4 address 10.0.0.10/24 on target:data"): |
| 69 | + target.put_config_dicts({ |
| 70 | + "ietf-interfaces": { |
| 71 | + "interfaces": { |
| 72 | + "interface": [{ |
| 73 | + "name": target["data"], |
| 74 | + "enabled": True, |
| 75 | + "ipv4": { |
| 76 | + "address": [{ |
| 77 | + "ip": TARGET_IP, |
| 78 | + "prefix-length": 24 |
| 79 | + }] |
| 80 | + } |
| 81 | + }] |
| 82 | + } |
| 83 | + } |
| 84 | + }) |
| 85 | + |
| 86 | + with test.step("Enable SNMP agent with a read-only community"): |
| 87 | + # No version and no listen: the agent should then serve every |
| 88 | + # version it supports, on every address, port 161. |
| 89 | + target.put_config_dicts({ |
| 90 | + "ietf-snmp": { |
| 91 | + "snmp": { |
| 92 | + "engine": {"enabled": True}, |
| 93 | + "community": [{ |
| 94 | + "index": "monitor", |
| 95 | + "text-name": COMMUNITY, |
| 96 | + "security-name": "monitor" |
| 97 | + }] |
| 98 | + } |
| 99 | + } |
| 100 | + }) |
| 101 | + |
| 102 | + with test.step("Read if-index and in-octets for target:data from operational"): |
| 103 | + ifindex = iface.get_param(target, target["data"], "if-index") |
| 104 | + stats = iface.get_param(target, target["data"], "statistics") or {} |
| 105 | + octets = int(stats.get("in-octets", 0)) |
| 106 | + print(f"target: {target['data']} is if-index {ifindex}, in-octets {octets}") |
| 107 | + if ifindex is None: |
| 108 | + print(f"target: no if-index for {target['data']}") |
| 109 | + test.fail() |
| 110 | + |
| 111 | + with infamy.IsolatedMacVlan(hport) as netns: |
| 112 | + netns.addip("10.0.0.1") |
| 113 | + |
| 114 | + with test.step("Verify the host has the net-snmp client tools"): |
| 115 | + # Without this the queries below all come back empty and |
| 116 | + # every failure looks like the agent not answering. |
| 117 | + if netns.runsh("command -v snmpget").returncode != 0: |
| 118 | + print("host: snmpget not found, the test container image " |
| 119 | + "needs the net-snmp-tools package") |
| 120 | + test.fail() |
| 121 | + |
| 122 | + with test.step("Verify agent reports the Infix sysObjectID"): |
| 123 | + # The agent has just been started, give it a moment to bind. |
| 124 | + val = retry(lambda: snmpget(netns, SYSOBJECTID)) |
| 125 | + print(f"host: sysObjectID.0 = {val}") |
| 126 | + if val != INFIX_OID: |
| 127 | + test.fail() |
| 128 | + |
| 129 | + with test.step("Verify IF-MIB ifName at if-index matches the interface name"): |
| 130 | + val = snmpget(netns, f"{IFNAME}.{ifindex}") |
| 131 | + print(f"host: ifName.{ifindex} = {val}") |
| 132 | + if val != target["data"]: |
| 133 | + test.fail() |
| 134 | + |
| 135 | + with test.step("Verify IF-MIB ifHCInOctets is consistent with operational data"): |
| 136 | + val = snmpget(netns, f"{IFHCINOCTETS}.{ifindex}") |
| 137 | + print(f"host: ifHCInOctets.{ifindex} = {val}") |
| 138 | + if val is None or not val.isdigit() or int(val) < octets: |
| 139 | + test.fail() |
| 140 | + |
| 141 | + with test.step("Verify an unknown community is refused"): |
| 142 | + if snmpget(netns, SYSOBJECTID, "wrong-community") is not None: |
| 143 | + test.fail() |
| 144 | + |
| 145 | + with test.step("Disable SNMP agent"): |
| 146 | + target.put_config_dicts({ |
| 147 | + "ietf-snmp": {"snmp": {"engine": {"enabled": False}}} |
| 148 | + }) |
| 149 | + |
| 150 | + with test.step("Verify agent no longer responds"): |
| 151 | + if not retry(lambda: snmpget(netns, SYSOBJECTID) is None): |
| 152 | + print("host: agent still answering after being disabled") |
| 153 | + test.fail() |
| 154 | + |
| 155 | + test.succeed() |
0 commit comments