From d4d36d42547fed687d16ccce35bbb54018df3f73 Mon Sep 17 00:00:00 2001 From: Erik Reid <erik.reid@geant.org> Date: Thu, 14 Jan 2021 17:26:46 +0100 Subject: [PATCH] added test of snmp.get_peer_state_info --- inventory_provider/snmp.py | 19 +++++++++++++++++-- test/test_snmp_handling.py | 39 +++++++++++++++++++++++++++++++++++--- 2 files changed, 53 insertions(+), 5 deletions(-) diff --git a/inventory_provider/snmp.py b/inventory_provider/snmp.py index 18b11300..17238cc9 100644 --- a/inventory_provider/snmp.py +++ b/inventory_provider/snmp.py @@ -46,6 +46,17 @@ def _v6address_oid2str(dotted_decimal): 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 """ https://stackoverflow.com/a/45001921 @@ -107,8 +118,10 @@ def walk(agent_hostname, community, base_oid): # pragma: no cover # .resolveWithMib(mibViewController) # for x in varBinds] for oid, val in varBinds: - result = {"oid": "." + str(oid), "value": _cast_snmp_value(val)} - # result = {"oid": "." + str(oid), "value": val.prettyPrint()} + result = { + "oid": _canonify_oid(oid), + "value": _cast_snmp_value(val) + } logger.debug(result) yield result @@ -136,8 +149,10 @@ def _v4str(l): def get_peer_state_info(hostname, community): oid_prefix = f'.{JNX_BGP_M2_PEER_STATE}.' for ifc in walk(hostname, community, JNX_BGP_M2_PEER_STATE): + assert ifc['oid'].startswith(oid_prefix), \ f'{ifc["oid"]}: {JNX_BGP_M2_PEER_STATE}' + rest = ifc['oid'][len(oid_prefix):] splits = rest.split('.') splits.pop(0) # no idea what this integer is ... diff --git a/test/test_snmp_handling.py b/test/test_snmp_handling.py index 6fe5c44e..ac65ec42 100644 --- a/test/test_snmp_handling.py +++ b/test/test_snmp_handling.py @@ -42,7 +42,7 @@ def _gen_mocked_nextCmd(mocked_walk_data): return _mocked_nextCmd -def test_snmp_interfaces(mocker, data_config): +def test_snmp_interfaces(mocker): expected_result_schema = { "$schema": "http://json-schema.org/draft-07/schema#", @@ -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.UdpTransportTarget', lambda x: None) interfaces = list(snmp.get_router_snmp_indexes('ignored', 'ignored')) 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) -- GitLab