Skip to content
Snippets Groups Projects
conftest.py 2.48 KiB
import json
import pathlib
from unittest.mock import patch
from brian_polling_manager.interface_stats import cli
from brian_polling_manager.interface_stats.vendors import juniper, nokia
import pytest


DATA_DIR = pathlib.Path(__file__).parent / "data"
JUNIPER_DATA_FILENAME_EXTENSION = "-interface-info.xml"

JUNIPER_ROUTERS = [
    path.name[: -len(JUNIPER_DATA_FILENAME_EXTENSION)]
    for path in DATA_DIR.iterdir()
    if path.name.endswith(JUNIPER_DATA_FILENAME_EXTENSION)
]

NOKIA_ROUTERS = list(
    {
        path.name[: -len(suffix)]
        for suffix in {"-ports.xml", "-lags.xml", "-router-interfaces.xml"}
        for path in DATA_DIR.iterdir()
        if path.name.endswith(suffix)
    }
)


@pytest.fixture
def data_dir():
    return DATA_DIR


@pytest.fixture
def mocked_get_netconf(data_dir):
    def get_netconf(vendor, router_name, **_):
        if vendor == cli.Vendor.JUNIPER:
            return juniper.get_netconf_interface_info_from_source_dir(
                router_name, source_dir=data_dir
            )

        else:
            return nokia.get_netconf_interface_info_from_source_dir(
                router_name, source_dir=data_dir
            )

    with patch.object(
        cli.Vendor, "get_netconf", autospec=True, side_effect=get_netconf
    ) as mock:
        yield mock


def poller_interfaces():
    file = DATA_DIR / "poller-interfaces.json"
    return json.loads(file.read_text())


@pytest.fixture(scope="session")
def juniper_inventory():
    def _excluded(ifc):
        return ifc["name"].startswith("dsc")

    polled = {}
    for ifc in poller_interfaces():
        if _excluded(ifc):
            continue
        polled.setdefault(ifc["router"], set()).add(ifc["name"])
    return polled


@pytest.fixture
def all_juniper_routers():
    return JUNIPER_ROUTERS


@pytest.fixture
def all_nokia_routers():
    return NOKIA_ROUTERS


@pytest.fixture(params=JUNIPER_ROUTERS)
def juniper_router_fqdn(request):
    # This fixture passes all juniper router fqdn's as a parametrized fixture. We could
    # do a pytest.mark.parametrize instead but we'd have to redefine JUNIPER_ROUTERS in
    # the test module or import from this conftest directly
    return request.param


@pytest.fixture(params=NOKIA_ROUTERS)
def nokia_router_fqdn(request):
    # This fixture passes all nokia router fqdn's as a parametrized fixture. We could
    # do a pytest.mark.parametrize instead but we'd have to redefine NOKIA_ROUTERS in
    # the test module or import from this conftest directly
    return request.param