1 Star 0 Fork 27

桐小哥/cloud-init

forked from src-openEuler/cloud-init 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
backport-test-fix-disable_sysfs_net-mock.patch 3.05 KB
一键复制 编辑 原始数据 按行查看 历史
桐小哥 提交于 2024-02-27 09:56 . fix test failed
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
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/tong_1001/cloud-init.git
git@gitee.com:tong_1001/cloud-init.git
tong_1001
cloud-init
cloud-init
master

搜索帮助