Skip to content

Commit

Permalink
net/dhcp: fix maybe_perform_dhcp_discovery check for interface=None
Browse files Browse the repository at this point in the history
If there are no NICs available when attempting to perform DHCP,
dhcp_discovery() will pass along interface=None.  Here is an example
failing backtrace:

```
  File "/usr/lib/python3/dist-packages/cloudinit/net/ephemeral.py", line 288, in obtain_lease
    self.lease = maybe_perform_dhcp_discovery(
  File "/usr/lib/python3/dist-packages/cloudinit/net/dhcp.py", line 98, in maybe_perform_dhcp_discovery
    return distro.dhcp_client.dhcp_discovery(interface, dhcp_log_func, distro)
  File "/usr/lib/python3/dist-packages/cloudinit/net/dhcp.py", line 324, in dhcp_discovery
    distro.net_ops.link_up(interface)
  File "/usr/lib/python3/dist-packages/cloudinit/net/netops/iproute2.py", line 10, in link_up
    subp.subp(
  File "/usr/lib/python3/dist-packages/cloudinit/subp.py", line 244, in subp
    bytes_args = [
  File "/usr/lib/python3/dist-packages/cloudinit/subp.py", line 245, in <listcomp>
    x if isinstance(x, bytes) else x.encode("utf-8") for x in args
AttributeError: 'NoneType' object has no attribute 'encode'
```

Restore the previous guard which raised NoDHCPLeaseInterfaceError and
add a unit test for coverage.

Note that the original checks included `if nic not in get_devicelist()`
which is not restored in this PR.

Signed-off-by: Chris Patterson <cpatterson@microsoft.com>
  • Loading branch information
cjp256 authored and holmanb committed Mar 6, 2024
1 parent 82d824f commit 63dffcb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
4 changes: 4 additions & 0 deletions cloudinit/net/dhcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ def maybe_perform_dhcp_discovery(distro, nic=None, dhcp_log_func=None):
returned.
"""
interface = nic or distro.fallback_interface
if interface is None:
LOG.debug("Skip dhcp_discovery: Unable to find fallback nic.")
raise NoDHCPLeaseInterfaceError()

return distro.dhcp_client.dhcp_discovery(interface, dhcp_log_func, distro)


Expand Down
7 changes: 7 additions & 0 deletions tests/unittests/net/test_dhcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -1318,3 +1318,10 @@ def test_dhcpcd_discovery_ib(
),
]
)


class TestMaybePerformDhcpDiscovery:
def test_none_and_missing_fallback(self):
with pytest.raises(NoDHCPLeaseInterfaceError):
distro = mock.Mock(fallback_interface=None)
maybe_perform_dhcp_discovery(distro, None)

0 comments on commit 63dffcb

Please sign in to comment.