From 815393dff452d948ee27fe6696b3956f86deb9f5 Mon Sep 17 00:00:00 2001 From: Clay Gerrard Date: Mon, 15 Sep 2025 14:07:36 -0500 Subject: [PATCH] test: fix module state pollution The disable_fallocate function provided in common.utils doesn't really have a way to undo it - it's tested independently in test_utils. It shouldn't be used on test_diskfile or else test_utils fallocate tests will fail afterwards. Change-Id: I6ffa97b39111ba25f85ba7cfde21440d975dc760 Signed-off-by: Clay Gerrard --- test/unit/obj/test_diskfile.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/test/unit/obj/test_diskfile.py b/test/unit/obj/test_diskfile.py index d04e431739..0126419bbb 100644 --- a/test/unit/obj/test_diskfile.py +++ b/test/unit/obj/test_diskfile.py @@ -5029,19 +5029,18 @@ class DiskFileMixin(BaseDiskFileTestMixin): self.assertEqual(raised.exception.errno, errno.EACCES) def test_create_close_oserror(self): - # This is a horrible hack so you can run this test in isolation. - # Some of the ctypes machinery calls os.close(), and that runs afoul - # of our mock. - with mock.patch.object(utils, '_sys_fallocate', None): - utils.disable_fallocate() - + err = OSError(errno.EACCES, os.strerror(errno.EACCES)) + # Disable fallocate for this test. Otherwise, the ctypes machinery may + # also call os.close (for example, when this test is run in isolation), + # but we're only interested in what diskfile is doing. + with mock.patch.object(utils, '_fallocate_enabled', False): df = self.df_mgr.get_diskfile(self.existing_device, '0', 'abc', '123', 'xyz', policy=POLICIES.legacy) - with mock.patch("swift.obj.diskfile.os.close", - mock.MagicMock(side_effect=OSError( - errno.EACCES, os.strerror(errno.EACCES)))): - with df.create(size=200): - pass + with mock.patch("swift.obj.diskfile.os.close") as mock_close: + mock_close.side_effect = err + with df.create(size=200) as dfw: + fd = dfw._fd + self.assertEqual([mock.call(fd)], mock_close.call_args_list) def test_write_metadata(self): df, df_data = self._create_test_file(b'1234567890')