Skip to content
Snippets Groups Projects
test_snmp_handling.py 1.27 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-interfaces.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": {
            "type": "object",
            "properties": {
                "name": {"type": "string"},
                "index": {"type": "integer"}
            },
            "required": ["name", "index"],
            "additionalProperties": False
        }
    }

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

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

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

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