Skip to content

Commit

Permalink
add dhcp client config unittest
Browse files Browse the repository at this point in the history
  • Loading branch information
holmanb committed Jan 18, 2024
1 parent c32d93d commit 7fde445
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions tests/unittests/distros/test__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import pytest

from cloudinit import distros, util
from cloudinit.distros.ubuntu import Distro
from cloudinit.net.dhcp import Dhcpcd, IscDhclient, Udhcpc
from cloudinit.net.dhcp import subp as dhcp_subp
from tests.unittests import helpers

M_PATH = "cloudinit.distros."
Expand Down Expand Up @@ -501,3 +504,72 @@ def test_get_tmp_exec_path(
assert "/tmp" == tmp_path
else:
assert "/usr_lib_exec/cloud-init/clouddir" == tmp_path


@pytest.mark.parametrize(
"chosen_client, config, which_override",
[
pytest.param(
IscDhclient,
{"network": {"dhcp_client_priority": ["dhclient"]}},
None,
id="single_client_is_found_from_config_dhclient",
),
pytest.param(
Udhcpc,
{"network": {"dhcp_client_priority": ["udhcpc"]}},
None,
id="single_client_is_found_from_config_udhcpc",
),
pytest.param(
Dhcpcd,
{"network": {"dhcp_client_priority": ["dhcpcd"]}},
None,
id="single_client_is_found_from_config_dhcpcd",
),
pytest.param(
Dhcpcd,
{"network": {"dhcp_client_priority": ["dhcpcd", "dhclient"]}},
None,
id="first_client_is_found_from_config_dhcpcd",
),
pytest.param(
Udhcpc,
{
"network": {
"dhcp_client_priority": ["udhcpc", "dhcpcd", "dhclient"]
}
},
None,
id="first_client_is_found_from_config_udhcpc",
),
pytest.param(
IscDhclient,
{"network": {"dhcp_client_priority": []}},
None,
id="first_client_is_found_no_config_dhclient",
),
pytest.param(
Dhcpcd,
{
"network": {
"dhcp_client_priority": ["udhcpc", "dhcpcd", "dhclient"]
}
},
[False, False, True, True],
id="second_client_is_found_from_config_dhcpcd",
),
],
)
class TestDHCP:
@mock.patch("cloudinit.net.dhcp.subp.which")
def test_dhcp_configuration(
self, m_which, chosen_client, config, which_override
):
"""check that, when a user provides a configuration at
network.dhcp_client_priority, the correct client is chosen
"""
m_which.side_effect = which_override
distro = Distro("", {}, {})
distro._cfg = config
assert isinstance(distro.dhcp_client, chosen_client)

0 comments on commit 7fde445

Please sign in to comment.