import json import os import jsonschema import pytest from inventory_provider import snmp import inventory_provider TEST_DATA_DIRNAME = os.path.realpath(os.path.join( inventory_provider.__path__[0], "..", "test", "data")) @pytest.fixture def snmp_walk_responses(): test_data_filename = os.path.join(TEST_DATA_DIRNAME, "snmp-walk.json") with open(test_data_filename) as f: return json.loads(f.read()) def test_snmp_interfaces(mocker, data_config, snmp_walk_responses): expected_result_schema = { "$schema": "http://json-schema.org/draft-07/schema#", "definitions": { "v4ifc": { "type": "object", "properties": { "v4Address": { "type": "string", "pattern": r'^(\d+\.){3}\d+$' }, "v4Mask": { "type": "string", "pattern": r'^(\d+\.){3}\d+$' }, "v4InterfaceName": {"type": "string"}, "index": { "type": "string", "pattern": r'^\d+$' } }, "required": [ "v4Address", "v4Mask", "v4InterfaceName", "index"], "additionalProperties": False }, "v6ifc": { "type": "object", "properties": { "v6Address": { "type": "string", "pattern": r'^[a-f\d:]+$' }, "v6Mask": { "type": "string", "pattern": r'^\d+$' }, "v6InterfaceName": {"type": "string"}, "index": { "type": "string", "pattern": r'^\d+$' } }, "required": [ "v6Address", "v6Mask", "v6InterfaceName", "index"], "additionalProperties": False } }, "type": "array", "items": { "anyOf": [ {"$ref": "#/definitions/v4ifc"}, {"$ref": "#/definitions/v6ifc"} ] } } def _mocked_walk(agent_hostname, community, base_oid): return [e for e in snmp_walk_responses if e['oid'].startswith(base_oid)] mocker.patch( 'inventory_provider.snmp.walk', _mocked_walk) interfaces = snmp.get_router_interfaces( 'ignored', 'ignored', {'oids': data_config['oids']}) interfaces = list(interfaces) jsonschema.validate(interfaces, expected_result_schema) assert interfaces, "interface list isn't empty"