From bd2b62a2e4884855dae0c3910ef7200e0cc05e5f Mon Sep 17 00:00:00 2001 From: Takashi Kajinami Date: Sat, 5 Jul 2025 17:44:43 +0900 Subject: [PATCH] Migrate ExceptionHelper to test utilities This helper is used only in unit tests, so should be maintained within the tests module. Change-Id: I1ce3dc4d7d27e3a8b3f1c525f721e40d79098af1 Signed-off-by: Takashi Kajinami --- nova/tests/unit/compute/test_compute.py | 28 ++++++++++----------- nova/tests/unit/conductor/test_conductor.py | 4 +-- nova/tests/unit/utils.py | 23 +++++++++++++++++ nova/utils.py | 21 ---------------- 4 files changed, 39 insertions(+), 37 deletions(-) diff --git a/nova/tests/unit/compute/test_compute.py b/nova/tests/unit/compute/test_compute.py index d9ed6ae846..26e0ecec55 100644 --- a/nova/tests/unit/compute/test_compute.py +++ b/nova/tests/unit/compute/test_compute.py @@ -1344,7 +1344,7 @@ class ComputeVolumeTestCase(BaseTestCase): self.compute.volume_snapshot_create, self.context, self.instance_object, 'fake_id', {}) - self.compute = utils.ExceptionHelper(self.compute) + self.compute = test_utils.ExceptionHelper(self.compute) self.assertRaises(NotImplementedError, self.compute.volume_snapshot_create, self.context, @@ -1366,7 +1366,7 @@ class ComputeVolumeTestCase(BaseTestCase): self.compute.volume_snapshot_delete, self.context, self.instance_object, uuids.volume, uuids.snapshot, {}) - self.compute = utils.ExceptionHelper(self.compute) + self.compute = test_utils.ExceptionHelper(self.compute) self.assertRaises(NotImplementedError, self.compute.volume_snapshot_delete, self.context, @@ -4396,7 +4396,7 @@ class ComputeTestCase(BaseTestCase, self.compute.get_console_output, self.context, instance, 0) - self.compute = utils.ExceptionHelper(self.compute) + self.compute = test_utils.ExceptionHelper(self.compute) self.assertRaises(NotImplementedError, self.compute.get_console_output, self.context, @@ -4419,7 +4419,7 @@ class ComputeTestCase(BaseTestCase, self.compute.get_console_output, self.context, instance, 0) - self.compute = utils.ExceptionHelper(self.compute) + self.compute = test_utils.ExceptionHelper(self.compute) self.assertRaises(exception.InstanceNotFound, self.compute.get_console_output, self.context, @@ -4532,7 +4532,7 @@ class ComputeTestCase(BaseTestCase, self.compute.get_vnc_console, self.context, 'invalid', instance=instance) - self.compute = utils.ExceptionHelper(self.compute) + self.compute = test_utils.ExceptionHelper(self.compute) self.assertRaises(exception.ConsoleTypeInvalid, self.compute.get_vnc_console, @@ -4553,7 +4553,7 @@ class ComputeTestCase(BaseTestCase, self.compute.get_vnc_console, self.context, None, instance=instance) - self.compute = utils.ExceptionHelper(self.compute) + self.compute = test_utils.ExceptionHelper(self.compute) self.assertRaises(exception.ConsoleTypeInvalid, self.compute.get_vnc_console, @@ -4573,7 +4573,7 @@ class ComputeTestCase(BaseTestCase, self.compute.get_vnc_console, self.context, 'novnc', instance=instance) - self.compute = utils.ExceptionHelper(self.compute) + self.compute = test_utils.ExceptionHelper(self.compute) self.assertRaises(NotImplementedError, self.compute.get_vnc_console, @@ -4616,7 +4616,7 @@ class ComputeTestCase(BaseTestCase, self.compute.get_spice_console, self.context, 'invalid', instance=instance) - self.compute = utils.ExceptionHelper(self.compute) + self.compute = test_utils.ExceptionHelper(self.compute) self.assertRaises(exception.ConsoleTypeInvalid, self.compute.get_spice_console, @@ -4638,7 +4638,7 @@ class ComputeTestCase(BaseTestCase, self.compute.get_spice_console, self.context, 'spice-html5', instance=instance) - self.compute = utils.ExceptionHelper(self.compute) + self.compute = test_utils.ExceptionHelper(self.compute) self.assertRaises(NotImplementedError, self.compute.get_spice_console, @@ -4658,7 +4658,7 @@ class ComputeTestCase(BaseTestCase, self.compute.get_spice_console, self.context, None, instance=instance) - self.compute = utils.ExceptionHelper(self.compute) + self.compute = test_utils.ExceptionHelper(self.compute) self.assertRaises(exception.ConsoleTypeInvalid, self.compute.get_spice_console, @@ -4678,7 +4678,7 @@ class ComputeTestCase(BaseTestCase, self.stub_out("nova.virt.fake.FakeDriver.get_vnc_console", fake_driver_get_console) - self.compute = utils.ExceptionHelper(self.compute) + self.compute = test_utils.ExceptionHelper(self.compute) self.assertRaises(exception.InstanceNotReady, self.compute.get_vnc_console, self.context, 'novnc', @@ -4696,7 +4696,7 @@ class ComputeTestCase(BaseTestCase, self.stub_out("nova.virt.fake.FakeDriver.get_spice_console", fake_driver_get_console) - self.compute = utils.ExceptionHelper(self.compute) + self.compute = test_utils.ExceptionHelper(self.compute) self.assertRaises(exception.InstanceNotReady, self.compute.get_spice_console, self.context, 'spice-html5', @@ -4707,7 +4707,7 @@ class ComputeTestCase(BaseTestCase, instance = self._create_fake_instance_obj( params={'vm_state': vm_states.BUILDING}) - self.compute = utils.ExceptionHelper(self.compute) + self.compute = test_utils.ExceptionHelper(self.compute) self.assertRaises(exception.ConsoleTypeUnavailable, self.compute.get_vnc_console, self.context, 'novnc', @@ -4718,7 +4718,7 @@ class ComputeTestCase(BaseTestCase, instance = self._create_fake_instance_obj( params={'vm_state': vm_states.BUILDING}) - self.compute = utils.ExceptionHelper(self.compute) + self.compute = test_utils.ExceptionHelper(self.compute) self.assertRaises(exception.ConsoleTypeUnavailable, self.compute.get_spice_console, self.context, 'spice-html5', diff --git a/nova/tests/unit/conductor/test_conductor.py b/nova/tests/unit/conductor/test_conductor.py index d9cd719e74..bb5d3e36ab 100644 --- a/nova/tests/unit/conductor/test_conductor.py +++ b/nova/tests/unit/conductor/test_conductor.py @@ -3650,7 +3650,7 @@ class ConductorTaskTestCase(_BaseTaskTestCase, test_compute.BaseTestCase): inst_obj = objects.Instance._from_db_object( self.context, objects.Instance(), instance, []) mock_execute.side_effect = ex - self.conductor = utils.ExceptionHelper(self.conductor) + self.conductor = test_utils.ExceptionHelper(self.conductor) self.assertRaises(type(ex), self.conductor.migrate_server, self.context, inst_obj, @@ -3679,7 +3679,7 @@ class ConductorTaskTestCase(_BaseTaskTestCase, test_compute.BaseTestCase): ex = exc.InvalidCPUInfo(reason="invalid cpu info.") mock_execute.side_effect = ex - self.conductor = utils.ExceptionHelper(self.conductor) + self.conductor = test_utils.ExceptionHelper(self.conductor) self.assertRaises(exc.InvalidCPUInfo, self.conductor.migrate_server, self.context, inst_obj, diff --git a/nova/tests/unit/utils.py b/nova/tests/unit/utils.py index 51edc45686..c92ed54650 100644 --- a/nova/tests/unit/utils.py +++ b/nova/tests/unit/utils.py @@ -14,11 +14,14 @@ import collections import errno +import functools import platform import socket import sys from unittest import mock +import oslo_messaging as messaging + from nova.compute import flavors import nova.conf import nova.context @@ -368,3 +371,23 @@ def assert_legacy_instance_delete_notification_by_uuid( assert call[0][1].db_connection is not None else: assert call[0][1].db_connection is None + + +class ExceptionHelper(object): + """Class to wrap another and translate the ClientExceptions raised by its + function calls to the actual ones. + """ + + def __init__(self, target): + self._target = target + + def __getattr__(self, name): + func = getattr(self._target, name) + + @functools.wraps(func) + def wrapper(*args, **kwargs): + try: + return func(*args, **kwargs) + except messaging.ExpectedException as e: + raise e.exc_info[1] + return wrapper diff --git a/nova/utils.py b/nova/utils.py index 4a80ab0e48..cf2e0b8c53 100644 --- a/nova/utils.py +++ b/nova/utils.py @@ -41,7 +41,6 @@ from oslo_concurrency import lockutils from oslo_concurrency import processutils from oslo_context import context as common_context from oslo_log import log as logging -import oslo_messaging as messaging from oslo_utils import encodeutils from oslo_utils import importutils from oslo_utils import strutils @@ -508,26 +507,6 @@ def expects_func_args(*args): return _decorator_checker -class ExceptionHelper(object): - """Class to wrap another and translate the ClientExceptions raised by its - function calls to the actual ones. - """ - - def __init__(self, target): - self._target = target - - def __getattr__(self, name): - func = getattr(self._target, name) - - @functools.wraps(func) - def wrapper(*args, **kwargs): - try: - return func(*args, **kwargs) - except messaging.ExpectedException as e: - raise e.exc_info[1] - return wrapper - - def check_string_length(value, name=None, min_length=0, max_length=None): """Check the length of specified string :param value: the value of the string