aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichal Nasiadka <mnasiadka@gmail.com>2025-05-20 12:17:25 +0200
committerMichal Arbet <michal.arbet@ultimum.io>2025-05-21 14:58:57 +0200
commite13801b4a1e5f587ee0581168eea0d42fea6cb66 (patch)
tree4cd0ae40367f0c9405e229152d67d51cbd9d2468
parent0006320d98fa7e594fbd17ade36d2572ca3231ed (diff)
downloadkolla-e13801b4a1e5f587ee0581168eea0d42fea6cb66.tar.gz
kolla-e13801b4a1e5f587ee0581168eea0d42fea6cb66.zip
Fix mock_open() expectations for Python 3.13+
Python 3.13 introduces a behavior change in unittest.mock.mock_open(), which now calls close() when exiting the context manager as described in bpo-44185 [1]. This causes additional mock calls to appear, breaking strict mock_calls assertions. This patch fix tests and add compatibility for Python 3.13. From mock documentation [2]: "bpo-44185: unittest.mock.mock_open() will call the close() method of the file handle mock when it is exiting from the context manager. Patch by Samet Yaslan." [1] https://bugs.python.org/issue44185 [2] https://mock.readthedocs.io/en/latest/changelog.html Change-Id: Ib8e2a8a20a828c4c69deedd307c9b726f51489f8
-rw-r--r--tests/test_set_config.py35
1 files changed, 26 insertions, 9 deletions
diff --git a/tests/test_set_config.py b/tests/test_set_config.py
index 3b528e445..3d4fc5a45 100644
--- a/tests/test_set_config.py
+++ b/tests/test_set_config.py
@@ -48,15 +48,32 @@ class LoadFromFile(base.BaseTestCase):
with mock.patch.object(set_configs, 'open', mo):
config = set_configs.load_config()
set_configs.copy_config(config)
- self.assertEqual([
- mock.call('/var/lib/kolla/config_files/config.json'),
- mock.call().__enter__(),
- mock.call().read(),
- mock.call().__exit__(None, None, None),
- mock.call('/run_command', 'w+'),
- mock.call().__enter__(),
- mock.call().write('/bin/true'),
- mock.call().__exit__(None, None, None)], mo.mock_calls)
+ if sys.version_info >= (3, 13):
+ calls = [
+ mock.call('/var/lib/kolla/config_files/config.json'),
+ mock.call().__enter__(),
+ mock.call().read(),
+ mock.call().__exit__(None, None, None),
+ mock.call().close(),
+ mock.call('/run_command', 'w+'),
+ mock.call().__enter__(),
+ mock.call().write('/bin/true'),
+ mock.call().__exit__(None, None, None),
+ mock.call().close()
+ ]
+ else:
+ calls = [
+ mock.call('/var/lib/kolla/config_files/config.json'),
+ mock.call().__enter__(),
+ mock.call().read(),
+ mock.call().__exit__(None, None, None),
+ mock.call('/run_command', 'w+'),
+ mock.call().__enter__(),
+ mock.call().write('/bin/true'),
+ mock.call().__exit__(None, None, None)
+ ]
+
+ self.assertEqual(calls, mo.mock_calls)
FAKE_CONFIG_FILES = [