From 32d0fc0ca05a274148a716bed88fe68238aa3745 Mon Sep 17 00:00:00 2001 From: shixuantong Date: Tue, 5 Nov 2024 17:06:55 +0800 Subject: [PATCH] test: fix mocking leaks --- backport-test-fix-mocking-leaks-4815.patch | 110 +++++++++++++++++++++ cloud-init.spec | 9 +- 2 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 backport-test-fix-mocking-leaks-4815.patch diff --git a/backport-test-fix-mocking-leaks-4815.patch b/backport-test-fix-mocking-leaks-4815.patch new file mode 100644 index 0000000..1db43d3 --- /dev/null +++ b/backport-test-fix-mocking-leaks-4815.patch @@ -0,0 +1,110 @@ +From ef2e48e9344e4849c10475fa9a823d50fb241512 Mon Sep 17 00:00:00 2001 +From: Alberto Contreras +Date: Mon, 29 Jan 2024 18:30:56 +0100 +Subject: [PATCH] test: fix mocking leaks (#4815) + +In pytest==8.0.0, the collection algorithm changed which results in +unittests being executed in a different order. This is shows some +mocking leaks that are fixed in this commit, as: + +- super().tearDown not called +- super().setUp doubly called +- Distro.default_updates_event not mocked +--- + tests/unittests/config/test_cc_growpart.py | 1 + + tests/unittests/config/test_cc_seed_random.py | 1 + + tests/unittests/sources/test_altcloud.py | 4 ++++ + tests/unittests/sources/test_azure.py | 1 - + tests/unittests/test_stages.py | 4 ++++ + 5 files changed, 10 insertions(+), 1 deletion(-) + +diff --git a/tests/unittests/config/test_cc_growpart.py b/tests/unittests/config/test_cc_growpart.py +index 85a4759..79c46e2 100644 +--- a/tests/unittests/config/test_cc_growpart.py ++++ b/tests/unittests/config/test_cc_growpart.py +@@ -135,6 +135,7 @@ class TestConfig(TestCase): + def tearDown(self): + self.tmpfile.close() + os.remove(self.tmppath) ++ super().tearDown() + + @mock.patch.object(os.path, "isfile", return_value=False) + @mock.patch.dict("os.environ", clear=True) +diff --git a/tests/unittests/config/test_cc_seed_random.py b/tests/unittests/config/test_cc_seed_random.py +index 3ba2a96..b9b1250 100644 +--- a/tests/unittests/config/test_cc_seed_random.py ++++ b/tests/unittests/config/test_cc_seed_random.py +@@ -42,6 +42,7 @@ class TestRandomSeed(TestCase): + def tearDown(self): + apply_patches([i for i in reversed(self.unapply)]) + util.del_file(self._seed_file) ++ super().tearDown() + + def apply_patches(self, patches): + ret = apply_patches(patches) +diff --git a/tests/unittests/sources/test_altcloud.py b/tests/unittests/sources/test_altcloud.py +index b4bc44b..dba0f6a 100644 +--- a/tests/unittests/sources/test_altcloud.py ++++ b/tests/unittests/sources/test_altcloud.py +@@ -92,6 +92,7 @@ class TestGetCloudType(CiTestCase): + # Reset + dmi.read_dmi_data = self.dmi_data + force_arch() ++ super().tearDown() + + def test_cloud_info_file_ioerror(self): + """Return UNKNOWN when /etc/sysconfig/cloud-info exists but errors.""" +@@ -230,6 +231,7 @@ class TestGetDataNoCloudInfoFile(CiTestCase): + dmi.read_dmi_data = self.dmi_data + # Return back to original arch + force_arch() ++ super().tearDown() + + def test_rhev_no_cloud_file(self): + """Test No cloud info file module get_data() forcing RHEV.""" +@@ -346,6 +348,7 @@ class TestUserDataVsphere(CiTestCase): + pass + + dsac.CLOUD_INFO_FILE = "/etc/sysconfig/cloud-info" ++ super().tearDown() + + @mock.patch("cloudinit.sources.DataSourceAltCloud.util.find_devs_with") + @mock.patch("cloudinit.sources.DataSourceAltCloud.util.mount_cb") +@@ -409,6 +412,7 @@ class TestReadUserDataCallback(CiTestCase): + shutil.rmtree(self.mount_dir) + except OSError: + pass ++ super().tearDown() + + def test_callback_both(self): + """Test read_user_data_callback() with both files.""" +diff --git a/tests/unittests/sources/test_azure.py b/tests/unittests/sources/test_azure.py +index 3a36418..c4be540 100644 +--- a/tests/unittests/sources/test_azure.py ++++ b/tests/unittests/sources/test_azure.py +@@ -1064,7 +1064,6 @@ class TestAzureDataSource(CiTestCase): + mock.MagicMock(), + ) + ) +- super(TestAzureDataSource, self).setUp() + + def apply_patches(self, patches): + for module, name, new in patches: +diff --git a/tests/unittests/test_stages.py b/tests/unittests/test_stages.py +index 7730608..4c54257 100644 +--- a/tests/unittests/test_stages.py ++++ b/tests/unittests/test_stages.py +@@ -441,6 +441,10 @@ class TestInit: + assert not self.tmpdir.join(path).exists() + + @mock.patch("cloudinit.distros.ubuntu.Distro") ++ @mock.patch.dict( ++ sources.DataSource.default_update_events, ++ {EventScope.NETWORK: {EventType.BOOT_NEW_INSTANCE}}, ++ ) + def test_apply_network_on_same_instance_id(self, m_ubuntu, caplog): + """Only call distro.networking.apply_network_config_names on same + instance id.""" +-- +2.27.0 + diff --git a/cloud-init.spec b/cloud-init.spec index 1a5f509..4cd23ea 100644 --- a/cloud-init.spec +++ b/cloud-init.spec @@ -1,6 +1,6 @@ Name: cloud-init Version: 23.4.1 -Release: 8 +Release: 9 Summary: the defacto multi-distribution package that handles early initialization of a cloud instance. License: ASL 2.0 or GPLv3 URL: http://launchpad.net/cloud-init @@ -29,6 +29,7 @@ Patch6011: backport-fix-netplan-Fix-predictable-interface-rename-issue-5.patch Patch6012: backport-fix-Fall-back-to-cached-local-ds-if-no-valid-ds-foun.patch Patch6013: backport-fix-openstack-Fix-bond-mac_address-5369.patch Patch6014: backport-fix-net-klibc-ipconfig-PROTO-compatibility-5437.patch +Patch6015: backport-test-fix-mocking-leaks-4815.patch Patch9000: do-not-generate-dsa.patch @@ -161,6 +162,12 @@ fi %exclude /usr/share/doc/* %changelog +* Wed Nov 06 2024 shixuantong - 23.4.1-9 +- Type:bugfix +- CVE:NA +- SUG:NA +- DESC:test: fix mocking leaks + * Thu Sep 5 2024 dongyuzhen - 23.4.1-8 - Type:bugfix - CVE:NA -- Gitee