Skip to content

Commit

Permalink
bug(tests): mock reads of host's /sys/class/net via get_sys_class_path
Browse files Browse the repository at this point in the history
Avoid leaking reads to the underlying host's /sys/class/net files.
Some test environments contain virtual network hardware and
configuration such as Calico network devices which provide
duplicated MAC addresses for each device in /sys/class/net. This
results in errors from unittests calling cloudinit.net.get_interfaces.

Provide a disable_sysfs_net` fixture and use it in net-related
modules or functions to avoid unrelated test failures due to
environmental differences.

Provide the ability to turn off this fixture for tests which
need to write to the mocked sysfs tmp directory so test artifacts
do not pollute other tests.

This fixture can be disabled by passing False to the disable_sysfs_net
via request.param. We want to avoid polluting tox.ini with pytest.marks
for infrequently used cases like this.

Also fix whitespace in tox.ini
  • Loading branch information
blackboxsw authored and major committed Mar 26, 2024
1 parent 1c64ee1 commit d9570d8
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions tests/unittests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,26 @@ def disable_sysfs_net(tmpdir_factory):
yield mock_sysfs


@pytest.fixture(scope="session", autouse=True)
def disable_sysfs_net(request, tmpdir_factory):
"""Avoid tests which read the undertying host's /syc/class/net.
To allow unobscured reads of /sys/class/net on the host we can
parametrize the fixture with:
@pytest.mark.parametrize("disable_sysfs_net", [False], indirect=True)
"""
if hasattr(request, "param") and getattr(request, "param") is False:
# Test disabled this fixture, perform no mocks.
yield
return
mock_sysfs = f"{tmpdir_factory.mktemp('sysfs')}/"
with mock.patch(
"cloudinit.net.get_sys_class_path", return_value=mock_sysfs
):
yield mock_sysfs


@pytest.fixture(autouse=True)
def disable_dns_lookup(request):
if "allow_dns_lookup" in request.keywords:
Expand Down

0 comments on commit d9570d8

Please sign in to comment.