Merge "Wrap "WMI not found" exception"
This commit is contained in:
commit
94222ff769
|
@ -227,17 +227,18 @@ def _is_not_found_exc(exc):
|
|||
return hresult == _WBEM_E_NOT_FOUND
|
||||
|
||||
|
||||
def not_found_decorator(func):
|
||||
def not_found_decorator(translated_exc=exceptions.NotFound):
|
||||
"""Wraps x_wmi: Not Found exceptions as os_win.exceptions.NotFound."""
|
||||
|
||||
def inner(*args, **kwargs):
|
||||
try:
|
||||
return func(*args, **kwargs)
|
||||
except exceptions.x_wmi as ex:
|
||||
if _is_not_found_exc(ex):
|
||||
LOG.debug('x_wmi: Not Found exception raised while '
|
||||
'running %s', func.__name__)
|
||||
raise exceptions.NotFound(six.text_type(ex))
|
||||
raise
|
||||
|
||||
return inner
|
||||
def wrapper(func):
|
||||
def inner(*args, **kwargs):
|
||||
try:
|
||||
return func(*args, **kwargs)
|
||||
except exceptions.x_wmi as ex:
|
||||
if _is_not_found_exc(ex):
|
||||
LOG.debug('x_wmi: Not Found exception raised while '
|
||||
'running %s', func.__name__)
|
||||
raise translated_exc(message=six.text_type(ex))
|
||||
raise
|
||||
return inner
|
||||
return wrapper
|
||||
|
|
|
@ -256,15 +256,17 @@ class UtilsTestCase(base.BaseTestCase):
|
|||
@mock.patch.object(_utils, 'get_com_error_hresult')
|
||||
def test_not_found_decorator(self, mock_get_com_error_hresult):
|
||||
mock_get_com_error_hresult.side_effect = lambda x: x
|
||||
translated_exc = exceptions.HyperVVMNotFoundException
|
||||
|
||||
@_utils.not_found_decorator
|
||||
@_utils.not_found_decorator(
|
||||
translated_exc=translated_exc)
|
||||
def f(to_call):
|
||||
to_call()
|
||||
|
||||
to_call = mock.Mock()
|
||||
to_call.side_effect = exceptions.x_wmi(
|
||||
'expected error', com_error=_utils._WBEM_E_NOT_FOUND)
|
||||
self.assertRaises(exceptions.NotFound, f, to_call)
|
||||
self.assertRaises(translated_exc, f, to_call)
|
||||
|
||||
to_call.side_effect = exceptions.x_wmi()
|
||||
self.assertRaises(exceptions.x_wmi, f, to_call)
|
||||
|
|
|
@ -158,6 +158,8 @@ class VMUtils(baseutils.BaseUtilsVirt):
|
|||
['ElementName'],
|
||||
VirtualSystemType=self._VIRTUAL_SYSTEM_TYPE_REALIZED)]
|
||||
|
||||
@_utils.not_found_decorator(
|
||||
translated_exc=exceptions.HyperVVMNotFoundException)
|
||||
def get_vm_summary_info(self, vm_name):
|
||||
vmsettings = self._lookup_vm_check(vm_name)
|
||||
|
||||
|
|
|
@ -185,7 +185,7 @@ class JobUtils(baseutils.BaseUtilsVirt):
|
|||
|
||||
_stop_jobs_with_timeout()
|
||||
|
||||
@_utils.not_found_decorator
|
||||
@_utils.not_found_decorator()
|
||||
@_utils.retry_decorator(exceptions=exceptions.HyperVException)
|
||||
def add_virt_resource(self, virt_resource, parent):
|
||||
(job_path, new_resources,
|
||||
|
@ -196,7 +196,7 @@ class JobUtils(baseutils.BaseUtilsVirt):
|
|||
|
||||
# modify_virt_resource can fail, especially while setting up the VM's
|
||||
# serial port connection. Retrying the operation will yield success.
|
||||
@_utils.not_found_decorator
|
||||
@_utils.not_found_decorator()
|
||||
@_utils.retry_decorator(exceptions=exceptions.HyperVException)
|
||||
def modify_virt_resource(self, virt_resource):
|
||||
(job_path, out_set_data,
|
||||
|
@ -207,7 +207,7 @@ class JobUtils(baseutils.BaseUtilsVirt):
|
|||
def remove_virt_resource(self, virt_resource):
|
||||
self.remove_multiple_virt_resources([virt_resource])
|
||||
|
||||
@_utils.not_found_decorator
|
||||
@_utils.not_found_decorator()
|
||||
@_utils.retry_decorator(exceptions=exceptions.HyperVException)
|
||||
def remove_multiple_virt_resources(self, virt_resources):
|
||||
(job, ret_val) = self._vs_man_svc.RemoveResourceSettings(
|
||||
|
@ -217,7 +217,7 @@ class JobUtils(baseutils.BaseUtilsVirt):
|
|||
def add_virt_feature(self, virt_feature, parent):
|
||||
self.add_multiple_virt_features([virt_feature], parent)
|
||||
|
||||
@_utils.not_found_decorator
|
||||
@_utils.not_found_decorator()
|
||||
@_utils.retry_decorator(exceptions=exceptions.HyperVException)
|
||||
def add_multiple_virt_features(self, virt_features, parent):
|
||||
(job_path, out_set_data,
|
||||
|
@ -228,7 +228,7 @@ class JobUtils(baseutils.BaseUtilsVirt):
|
|||
def remove_virt_feature(self, virt_feature):
|
||||
self.remove_multiple_virt_features([virt_feature])
|
||||
|
||||
@_utils.not_found_decorator
|
||||
@_utils.not_found_decorator()
|
||||
def remove_multiple_virt_features(self, virt_features):
|
||||
(job_path, ret_val) = self._vs_man_svc.RemoveFeatureSettings(
|
||||
FeatureSettings=[f.path_() for f in virt_features])
|
||||
|
|
Loading…
Reference in New Issue