Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using shutil which to find path for ethtool #33

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 typing import cast

from ops.charm import CharmBase, RelationChangedEvent, RelationJoinedEvent
Expand Down Expand Up @@ -141,12 +142,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
Loading