Skip to content
Snippets Groups Projects
test_snmp_handling.py 1.12 KiB
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#",
        "type": "array",
        "items": {
            "name": {"type": "string"},
            "index": {"type": "integer"}
        }
    }

    def _mocked_walk(agent_hostname, community, ignored_oid):
        return snmp_walk_responses

    mocker.patch(
        'inventory_provider.snmp.walk',
        _mocked_walk)

    interfaces = snmp.get_router_snmp_indexes(
        'ignored', 'ignored')
    interfaces = list(interfaces)

    jsonschema.validate(interfaces, expected_result_schema)
    assert interfaces, "interface list isn't empty"