From 0ace8e47075959fe439c5576b669b0235f93e4ce Mon Sep 17 00:00:00 2001 From: Erik Reid <erik.reid@geant.org> Date: Thu, 27 Jun 2019 17:57:00 +0200 Subject: [PATCH] fixed inconsistent snmp-interfaces cache def --- inventory_provider/snmp.py | 7 ++++--- inventory_provider/tasks/worker.py | 4 ++-- test/test_snmp_handling.py | 13 ++++++++----- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/inventory_provider/snmp.py b/inventory_provider/snmp.py index 06681911..6ca9e170 100644 --- a/inventory_provider/snmp.py +++ b/inventory_provider/snmp.py @@ -82,9 +82,10 @@ def walk(agent_hostname, community, base_oid): # pragma: no cover def get_router_snmp_indexes(hostname, community): - indexes = {} for ifc in walk(hostname, community, RFC1213_MIB_IFDESC): m = re.match(r'.*\.(\d+)$', ifc['oid']) assert m, 'sanity failure parsing oid: %r' % ifc['oid'] - indexes[ifc['value']] = int(m.group(1)) - return indexes + yield { + 'name': ifc['value'], + 'index': int(m.group(1)) + } diff --git a/inventory_provider/tasks/worker.py b/inventory_provider/tasks/worker.py index d1f85aba..8779a1d4 100644 --- a/inventory_provider/tasks/worker.py +++ b/inventory_provider/tasks/worker.py @@ -91,9 +91,9 @@ def snmp_refresh_interfaces(hostname, community): _save_value_json( 'snmp-interfaces:' + hostname, - snmp.get_router_snmp_indexes( + list(snmp.get_router_snmp_indexes( hostname, - community)) + community))) logger.debug( '<<< snmp_refresh_interfaces(%r, %r)' % (hostname, community)) diff --git a/test/test_snmp_handling.py b/test/test_snmp_handling.py index a0a07b4c..aef5b462 100644 --- a/test/test_snmp_handling.py +++ b/test/test_snmp_handling.py @@ -28,8 +28,13 @@ def test_snmp_interfaces(mocker, data_config, snmp_walk_responses): "$schema": "http://json-schema.org/draft-07/schema#", "type": "array", "items": { - "name": {"type": "string"}, - "index": {"type": "integer"} + "type": "object", + "properties": { + "name": {"type": "string"}, + "index": {"type": "integer"} + }, + "required": ["name", "index"], + "additionalProperties": False } } @@ -40,9 +45,7 @@ def test_snmp_interfaces(mocker, data_config, snmp_walk_responses): 'inventory_provider.snmp.walk', _mocked_walk) - interfaces = snmp.get_router_snmp_indexes( - 'ignored', 'ignored') - interfaces = list(interfaces) + interfaces = list(snmp.get_router_snmp_indexes('ignored', 'ignored')) jsonschema.validate(interfaces, expected_result_schema) assert interfaces, "interface list isn't empty" -- GitLab