Skip to content

Commit

Permalink
Merge pull request #32 from xtrusia/main
Browse files Browse the repository at this point in the history
Using shutil which to find path for ethtool
  • Loading branch information
wolsen authored Sep 30, 2024
2 parents 2e86396 + a8959b3 commit 22118a2
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import logging
import os
import subprocess
import shutil

from ops.charm import CharmBase
from ops.framework import StoredState
Expand Down Expand Up @@ -133,12 +134,17 @@ def i40e_filter(path: Path) -> bool:
)
return

ethtool_cmd = shutil.which("ethtool")
if ethtool_cmd is None:
logger.info("ethtool not found in PATH")
return

for nic in nics:
logger.info("Using ethtool(8) to disable FW lldp for %s" % nic)
subprocess.run(
[
"sudo",
"/usr/sbin/ethtool",
ethtool_cmd,
"--set-priv-flags",
nic,
"disable-fw-lldp",
Expand Down
33 changes: 32 additions & 1 deletion tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ def test_disable_i40e_lldp_without_i40e_nics(
mock_subprocess.assert_not_called()

@patch("charm.Path")
@patch("charm.shutil.which")
@patch("charm.subprocess.run")
@patch("charm.logger")
def test_disable_i40e_lldp_with_i40e_nics(
self, mock_logger, mock_subprocess, mock_path
self, mock_logger, mock_subprocess, mock_which, mock_path
):
mock_which.return_value = "/usr/sbin/ethtool"

nic_list = ["eth0", "eth1"]
mock_nics = []
Expand Down Expand Up @@ -77,6 +79,35 @@ def test_disable_i40e_lldp_with_i40e_nics(
f"Using ethtool(8) to disable FW lldp for {nic_name}"
)

@patch("charm.Path")
@patch("charm.shutil.which")
@patch("charm.subprocess.run")
@patch("charm.logger")
def test_disable_i40e_lldp_no_ethtool(
self, mock_logger, mock_subprocess, mock_which, mock_path
):
mock_which.return_value = None

nic_list = ["eth0", "eth1"]
mock_nics = []

for nic_name in nic_list:
mock_nic = MagicMock()
mock_nic.name = nic_name
mock_driver = mock_nic / "device/driver"
mock_driver.resolve.return_value = Path(
"/sys/class/net/{nic_name}/device/driver/i40e"
)
mock_nics.append(mock_nic)

mock_path("/sys/class/net").iterdir.return_value = mock_nics

self.harness.charm.disable_i40e_lldp()

mock_logger.info.assert_any_call("ethtool not found in PATH")

mock_subprocess.assert_not_called()

@patch("charm.apt")
def test_install(self, _apt):
self.harness.charm.on.install.emit()
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ deps =
codespell
commands =
codespell --skip .git --skip .tox --skip build --skip lib --skip venv --skip .mypy_cache {toxinidir}
ruff {[vars]all_path}
ruff check {[vars]all_path}
black --check --diff {[vars]all_path}

[testenv:static-{charm,lib}]
Expand Down

0 comments on commit 22118a2

Please sign in to comment.