Skip to content
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
20 changes: 12 additions & 8 deletions mfd_network_adapter/network_interface/feature/ip/linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,17 +189,21 @@ def release_ip(self, ip_version: IPVersion) -> None:

:param ip_version: IP version to use
"""
# release current lease
cmd = f"dhclient -r {self._interface().name}"
output = self._connection.execute_command(cmd, expected_return_codes={})
if output.return_code and "dhcpcd not running" not in output.stdout:
raise IPFeatureException(
f"Unknown error msg returned, while releasing IP on {self._interface().name} - {output.stderr}"
)
dhclient_output = self._connection.execute_command("command -v dhclient", expected_return_codes={0, 1})
if dhclient_output.return_code == 0:
# release current lease
cmd = f"dhclient -r {self._interface().name}"
Comment thread
adrianlasota marked this conversation as resolved.
Resolved
output = self._connection.execute_command(cmd, expected_return_codes={})
if output.return_code and "dhcpcd not running" not in output.stdout.casefold():
raise IPFeatureException(
f"Unknown error msg returned, while releasing IP on {self._interface().name} - {output.stderr}"
)
else:
logger.log(level=log_levels.MODULE_DEBUG, msg="dhclient not found, skipping DHCP release step.")
Comment thread
adrianlasota marked this conversation as resolved.
Resolved

# RHEL7 still keeps IP after releasing it, so we need to remove it
cmd = f"ip -{ip_version.value} addr flush dev {self._interface().name}"
self._connection.execute_command(cmd)
self._connection.execute_command(add_namespace_call_command(cmd, namespace=self._interface().namespace))

def renew_ip(self) -> None:
"""Refresh Ip address."""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,22 @@ def test_release_ip(self, interface):
]
)

def test_release_ip_dhclient_not_found(self, interface):
interface._connection.execute_command.side_effect = [
ConnectionCompletedProcess(return_code=1, args="", stdout="", stderr=""),
ConnectionCompletedProcess(return_code=0, args="", stdout="", stderr=""),
]
interface.ip.release_ip(IPVersion.V4)
interface._connection.execute_command.assert_called_with(f"ip -4 addr flush dev {interface.name}")

Comment thread
adrianlasota marked this conversation as resolved.
Resolved
def test_release_ip_raises_on_dhclient_error(self, interface):
interface._connection.execute_command.side_effect = [
ConnectionCompletedProcess(return_code=0, args="", stdout="", stderr=""),
ConnectionCompletedProcess(return_code=1, args="", stdout="some error", stderr="some error"),
]
with pytest.raises(IPFeatureException):
interface.ip.release_ip(IPVersion.V4)

def test_renew_ip(self, interface):
interface._connection.execute_command.return_value = ConnectionCompletedProcess(
return_code=0, args="", stdout="", stderr=""
Expand Down
Loading