Skip to content

Commit

Permalink
fix: proper check napalm community driver packages (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
leoparente authored Dec 5, 2024
1 parent 63bcb27 commit 7821ec5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
9 changes: 4 additions & 5 deletions diode-napalm-agent/diode_napalm/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,10 @@ def napalm_driver_list() -> list[str]:
"""
napalm_packages = ["ios", "eos", "junos", "nxos"]
prefix = "napalm-"
for dist in importlib_metadata.distributions():
if dist.metadata["Name"].startswith(prefix):
package = dist.metadata["Name"][len(prefix) :].replace("-", "_")
napalm_packages.append(package)
prefix = "napalm_"
for dist in importlib_metadata.packages_distributions():
if dist.startswith(prefix):
napalm_packages.append(dist[len(prefix) :])
return napalm_packages


Expand Down
18 changes: 10 additions & 8 deletions diode-napalm-agent/tests/test_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ def mock_get_network_driver():


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


Expand Down Expand Up @@ -148,20 +150,20 @@ def side_effect(driver_name):
assert driver == "nxos", "Expected the 'ios' driver to be found"


def test_napalm_driver_list(mock_importlib_metadata_distributions):
def test_napalm_driver_list(mock_importlib_metadata_packages_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_importlib_metadata_packages_distributions: Mocked importlib_metadata.packages_distributions function.
"""
mock_distributions = [
MagicMock(metadata={"Name": "napalm-srl"}),
MagicMock(metadata={"Name": "napalm-fake-driver"}),
"napalm_srl",
"napalm_fake_driver",
]
mock_importlib_metadata_distributions.return_value = mock_distributions
mock_importlib_metadata_packages_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}"
Expand Down

0 comments on commit 7821ec5

Please sign in to comment.