summaryrefslogtreecommitdiff
path: root/nova/test.py
diff options
context:
space:
mode:
authorBalazs Gibizer <gibi@redhat.com>2022-07-28 19:50:29 +0200
committerSean Mooney <work@seanmooney.info>2022-12-01 18:14:01 +0000
commit54c7c97cb83609cbcba44856d412dae11fe8643b (patch)
tree835571796cb5bd1607f05ee9bb0eb8a9f9101e70 /nova/test.py
parenta340630c5c78e5f5856004d0a551dfa93df7f28d (diff)
downloadnova-54c7c97cb83609cbcba44856d412dae11fe8643b.tar.gz
Remove double mocking
In py310 unittest.mock does not allow to mock the same function twice as the second mocking will fail to autospec the Mock object created by the first mocking. This patch manually fixes the double mocking. Fixed cases: 1) one of the mock was totally unnecessary so it was removed 2) the second mock specialized the behavior of the first generic mock. In this case the second mock is replaced with the configuration of the first mock 3) a test case with two test steps mocked the same function for each step with overlapping mocks. Here the overlap was removed to have the two mock exists independently The get_connection injection in the libvirt functional test needed a further tweak (yeah I know it has many already) to act like a single mock (basically case #2) instead of a temporary re-mocking. Still the globalness of the get_connection mocking warrant the special set / reset logic there. Conflicts: nova/tests/unit/api/openstack/compute/test_limits.py nova/tests/unit/virt/libvirt/volume/test_lightos.py nova/tests/unit/virt/test_block_device.py Change-Id: I3998d0d49583806ac1c3ae64f1b1fe343cefd20d (cherry picked from commit f8cf050a1380ae844e0184ed45f4a04fde3b07a9) (cherry picked from commit b40bd1bf52c87c31e18caf85d79dd03da6c7cffc)
Diffstat (limited to 'nova/test.py')
-rw-r--r--nova/test.py15
1 files changed, 10 insertions, 5 deletions
diff --git a/nova/test.py b/nova/test.py
index 2adac89532..a7ed312c12 100644
--- a/nova/test.py
+++ b/nova/test.py
@@ -355,7 +355,7 @@ class TestCase(base.BaseTestCase):
self.useFixture(fixtures.MonkeyPatch(old, new))
@staticmethod
- def patch_exists(patched_path, result):
+ def patch_exists(patched_path, result, other=None):
"""Provide a static method version of patch_exists(), which if you
haven't already imported nova.test can be slightly easier to
use as a context manager within a test method via:
@@ -364,7 +364,7 @@ class TestCase(base.BaseTestCase):
with self.patch_exists(path, True):
...
"""
- return patch_exists(patched_path, result)
+ return patch_exists(patched_path, result, other)
@staticmethod
def patch_open(patched_path, read_data):
@@ -845,10 +845,12 @@ class ContainKeyValue(object):
@contextlib.contextmanager
-def patch_exists(patched_path, result):
+def patch_exists(patched_path, result, other=None):
"""Selectively patch os.path.exists() so that if it's called with
patched_path, return result. Calls with any other path are passed
- through to the real os.path.exists() function.
+ through to the real os.path.exists() function if other is not provided.
+ If other is provided then that will be the result of the call on paths
+ other than patched_path.
Either import and use as a decorator / context manager, or use the
nova.TestCase.patch_exists() static method as a context manager.
@@ -882,7 +884,10 @@ def patch_exists(patched_path, result):
def fake_exists(path):
if path == patched_path:
return result
- return real_exists(path)
+ elif other is not None:
+ return other
+ else:
+ return real_exists(path)
with mock.patch.object(os.path, "exists") as mock_exists:
mock_exists.side_effect = fake_exists