diff --git a/tests/unittests/conftest.py b/tests/unittests/conftest.py index 20dc5cc594ed..f291aee03b98 100644 --- a/tests/unittests/conftest.py +++ b/tests/unittests/conftest.py @@ -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: