Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
leoparente committed Jun 28, 2024
1 parent 2fbe998 commit 0244e69
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 3 deletions.
40 changes: 38 additions & 2 deletions diode-napalm-agent/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,42 @@ def test_run_driver_no_driver(
mock_client().ingest.assert_called_once()


def test_run_driver_with_not_intalled_driver(
mock_get_network_driver, mock_discover_device_driver
):
"""
Test run_driver function when driver is provided but not installed.
Args:
----
mock_get_network_driver: Mocked get_network_driver function.
mock_discover_device_driver: Mocked discover_device_driver function.
"""
info = Napalm(
driver="not_installed",
hostname="test_host",
username="user",
password="pass",
timeout=10,
optional_args={},
)
config = DiscoveryConfig(netbox={"site": "test_site"})

mock_np_driver = MagicMock()
mock_get_network_driver.return_value = mock_np_driver

with pytest.raises(Exception) as excinfo:
run_driver(info, config)

mock_discover_device_driver.assert_not_called()
mock_get_network_driver.assert_not_called()

assert str(excinfo.value).startswith(
f"Hostname {info.hostname}: specified driver '{info.driver}' was not found in the current supported drivers list:"
)


def test_run_driver_with_driver(
mock_client, mock_get_network_driver, mock_discover_device_driver
):
Expand All @@ -367,7 +403,7 @@ def test_run_driver_with_driver(
"""
info = Napalm(
driver="existing_driver",
driver="ios",
hostname="test_host",
username="user",
password="pass",
Expand All @@ -382,7 +418,7 @@ def test_run_driver_with_driver(
run_driver(info, config)

mock_discover_device_driver.assert_not_called()
mock_get_network_driver.assert_called_once_with("existing_driver")
mock_get_network_driver.assert_called_once_with("ios")
mock_np_driver.assert_called_once_with("test_host", "user", "pass", 10, {})
mock_client().ingest.assert_called_once()

Expand Down
56 changes: 55 additions & 1 deletion diode-napalm-agent/tests/test_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@
# Copyright 2024 NetBox Labs Inc
"""NetBox Labs - Discovery Unit Tests."""

import logging
from types import SimpleNamespace
from unittest.mock import MagicMock, patch

import pytest

from diode_napalm.discovery import discover_device_driver, supported_drivers
from diode_napalm.discovery import (
discover_device_driver,
napalm_driver_list,
set_napalm_logs_level,
supported_drivers,
)


@pytest.fixture
Expand All @@ -17,6 +23,20 @@ def mock_get_network_driver():
yield mock


@pytest.fixture
def mock_importlib_metadata_distributions():
"""Mock the importlib_metadata.distributions function."""
with patch("diode_napalm.discovery.importlib_metadata.distributions") as mock:
yield mock


@pytest.fixture
def mock_loggers():
"""Mock the logging.getLogger function for various loggers."""
with patch("diode_napalm.discovery.logging.getLogger") as mock:
yield mock


def test_discover_device_driver_success(mock_get_network_driver):
"""
Test successful discovery of a NAPALM driver.
Expand Down Expand Up @@ -126,3 +146,37 @@ def side_effect(driver_name):

driver = discover_device_driver(info)
assert driver == "nxos", "Expected the 'ios' driver to be found"


def test_napalm_driver_list(mock_importlib_metadata_distributions):
"""
Test the napalm_driver_list function to ensure it correctly lists available NAPALM drivers.
Args:
----
mock_importlib_metadata_distributions: Mocked importlib_metadata.distributions function.
"""
mock_distributions = [
MagicMock(metadata={"Name": "napalm-srl"}),
MagicMock(metadata={"Name": "napalm-fake-driver"}),
]
mock_importlib_metadata_distributions.return_value = mock_distributions
expected_drivers = ["ios", "eos", "junos", "nxos", "srl", "fake_driver"]
drivers = napalm_driver_list()
assert drivers == expected_drivers, f"Expected {expected_drivers}, got {drivers}"


def test_set_napalm_logs_level(mock_loggers):
"""
Test setting the logging level for NAPALM and related libraries.
Args:
----
mock_loggers: Mocked loggers for various libraries.
"""
set_napalm_logs_level(logging.DEBUG)

for logger in mock_loggers.values():
logger.setLevel.assert_called_once_with(logging.DEBUG)

0 comments on commit 0244e69

Please sign in to comment.