From dc0eafbc7d88be99e11301081adb41ad7b50338e Mon Sep 17 00:00:00 2001
From: James Falcon <james.falcon@canonical.com>
Date: Mon, 11 Mar 2024 19:37:48 -0500
Subject: [PATCH] test: fix `disable_sysfs_net` mock (#5065)

The fixture parametrization ability added in 9baf31c doesn't work
as expected. When you have a session-wide fixture, the setup is run
once, then further invocations of the fixture (including autouse) uses a
cached version of the fixture.  Teardown for the session fixture happens
at the end of all test runs. This also applies to mock patching. Since
the mock patching happens only once, parametrizing the fixture to yield
without patching doesn't undo the initial mock setup; the
parametrization of `disable_sys_net` effectively does nothing.

The good news is that patches stack, so current tests that patch
`get_sys_class_path` differently will still work fine. If we need to
disable the patching entirely, that is also possible by saving the
original `get_sys_class_path` before applying the global disable mock,
then having a separate mock that has a side effect of calling
the original function.

Reference:https://github.com/canonical/cloud-init/commit/dc0eafbc7d88be99e11301081adb41ad7b50338e
Conflict:NA
---
 tests/unittests/conftest.py      | 14 ++------------
 tests/unittests/net/test_init.py |  5 +----
 2 files changed, 3 insertions(+), 16 deletions(-)

diff --git a/tests/unittests/conftest.py b/tests/unittests/conftest.py
index 22bc189..bdd21c3 100644
--- a/tests/unittests/conftest.py
+++ b/tests/unittests/conftest.py
@@ -62,18 +62,8 @@ def fake_filesystem(mocker, tmpdir):
 
 
 @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
+def disable_sysfs_net(tmpdir_factory):
+    """Avoid tests which read the underlying host's /syc/class/net."""
     mock_sysfs = f"{tmpdir_factory.mktemp('sysfs')}/"
     with mock.patch(
         "cloudinit.net.get_sys_class_path", return_value=mock_sysfs
diff --git a/tests/unittests/net/test_init.py b/tests/unittests/net/test_init.py
index a7b75ab..51e54d0 100644
--- a/tests/unittests/net/test_init.py
+++ b/tests/unittests/net/test_init.py
@@ -42,10 +42,7 @@ class TestSysDevPath:
 
 class TestReadSysNet:
     @pytest.fixture(autouse=True)
-    @pytest.mark.parametrize(
-        "disable_sysfs_net", [False], indirect=["disable_sysfs_net"]
-    )
-    def setup(self, disable_sysfs_net, tmpdir_factory):
+    def setup(self, tmpdir_factory):
         # We mock invididual numbered tmpdirs here because these tests write
         # to the sysfs directory and stale test artifacts break later tests.
         mock_sysfs = f"{tmpdir_factory.mktemp('sysfs', numbered=True)}/"
-- 
2.43.0

