Skip to content
Snippets Groups Projects
Commit d4d36d42 authored by Erik Reid's avatar Erik Reid
Browse files

added test of snmp.get_peer_state_info

parent e8d478df
No related branches found
No related tags found
No related merge requests found
...@@ -46,6 +46,17 @@ def _v6address_oid2str(dotted_decimal): ...@@ -46,6 +46,17 @@ def _v6address_oid2str(dotted_decimal):
return ":".join(hex_params) return ":".join(hex_params)
def _canonify_oid(oid):
"""
Our output oid's always begin with '.'.
:param oid: a thing that stringifies to a dotted oid
:return: a string like '.#.#.#...#'
"""
oid = str(oid)
return oid if oid.startswith('.') else f'.{oid}'
def walk(agent_hostname, community, base_oid): # pragma: no cover def walk(agent_hostname, community, base_oid): # pragma: no cover
""" """
https://stackoverflow.com/a/45001921 https://stackoverflow.com/a/45001921
...@@ -107,8 +118,10 @@ def walk(agent_hostname, community, base_oid): # pragma: no cover ...@@ -107,8 +118,10 @@ def walk(agent_hostname, community, base_oid): # pragma: no cover
# .resolveWithMib(mibViewController) # .resolveWithMib(mibViewController)
# for x in varBinds] # for x in varBinds]
for oid, val in varBinds: for oid, val in varBinds:
result = {"oid": "." + str(oid), "value": _cast_snmp_value(val)} result = {
# result = {"oid": "." + str(oid), "value": val.prettyPrint()} "oid": _canonify_oid(oid),
"value": _cast_snmp_value(val)
}
logger.debug(result) logger.debug(result)
yield result yield result
...@@ -136,8 +149,10 @@ def _v4str(l): ...@@ -136,8 +149,10 @@ def _v4str(l):
def get_peer_state_info(hostname, community): def get_peer_state_info(hostname, community):
oid_prefix = f'.{JNX_BGP_M2_PEER_STATE}.' oid_prefix = f'.{JNX_BGP_M2_PEER_STATE}.'
for ifc in walk(hostname, community, JNX_BGP_M2_PEER_STATE): for ifc in walk(hostname, community, JNX_BGP_M2_PEER_STATE):
assert ifc['oid'].startswith(oid_prefix), \ assert ifc['oid'].startswith(oid_prefix), \
f'{ifc["oid"]}: {JNX_BGP_M2_PEER_STATE}' f'{ifc["oid"]}: {JNX_BGP_M2_PEER_STATE}'
rest = ifc['oid'][len(oid_prefix):] rest = ifc['oid'][len(oid_prefix):]
splits = rest.split('.') splits = rest.split('.')
splits.pop(0) # no idea what this integer is ... splits.pop(0) # no idea what this integer is ...
......
...@@ -42,7 +42,7 @@ def _gen_mocked_nextCmd(mocked_walk_data): ...@@ -42,7 +42,7 @@ def _gen_mocked_nextCmd(mocked_walk_data):
return _mocked_nextCmd return _mocked_nextCmd
def test_snmp_interfaces(mocker, data_config): def test_snmp_interfaces(mocker):
expected_result_schema = { expected_result_schema = {
"$schema": "http://json-schema.org/draft-07/schema#", "$schema": "http://json-schema.org/draft-07/schema#",
...@@ -58,11 +58,44 @@ def test_snmp_interfaces(mocker, data_config): ...@@ -58,11 +58,44 @@ def test_snmp_interfaces(mocker, data_config):
} }
} }
_mocked_nextCmd = _gen_mocked_nextCmd(interfaces_walk_responses()) test_data = list(interfaces_walk_responses())
assert test_data # sanity
_mocked_nextCmd = _gen_mocked_nextCmd(test_data)
mocker.patch('inventory_provider.snmp.nextCmd', _mocked_nextCmd) mocker.patch('inventory_provider.snmp.nextCmd', _mocked_nextCmd)
mocker.patch('inventory_provider.snmp.UdpTransportTarget', lambda x: None) mocker.patch('inventory_provider.snmp.UdpTransportTarget', lambda x: None)
interfaces = list(snmp.get_router_snmp_indexes('ignored', 'ignored')) interfaces = list(snmp.get_router_snmp_indexes('ignored', 'ignored'))
jsonschema.validate(interfaces, expected_result_schema) jsonschema.validate(interfaces, expected_result_schema)
assert interfaces, "interface list isn't empty" assert len(interfaces) == len(test_data)
def test_peer_info(mocker):
expected_result_schema = {
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "array",
"items": {
"type": "object",
"properties": {
"local": {"type": "string"},
"remote": {"type": "string"},
"oid": {"type": "string"}
},
"required": ["local", "remote", "oid"],
"additionalProperties": False
}
}
test_data = list(peering_walk_responses())
assert test_data # sanity
_mocked_nextCmd = _gen_mocked_nextCmd(test_data)
mocker.patch('inventory_provider.snmp.nextCmd', _mocked_nextCmd)
mocker.patch('inventory_provider.snmp.UdpTransportTarget', lambda x: None)
peerings = list(snmp.get_peer_state_info('ignored', 'ignored'))
jsonschema.validate(peerings, expected_result_schema)
assert len(peerings) == len(test_data)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment