From: Matt Riedemann Date: Mon, 12 Oct 2015 13:44:14 +0000 (-0700) Subject: windows: don't use LOG.exception if not logging an exception X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=3522711b3e699e3dbc5c260b0f3f9ad6ea2d4ed7;p=openstack-build%2Fcinder-build.git windows: don't use LOG.exception if not logging an exception There is a bug in python 3.4 such that if you call LOG.exception and you're not in a logging handler context it fails. That bug is fixed inadvertantly in python 3.5 under: http://bugs.python.org/issue17911 This fixes the instance in the windows vhdutils module to use LOG.error rather than LOG.exception to avoid that logging error. Since LOG.exception was being used I'm assuming the original author wanted traceback given this is a utility method, so we use the exc_info kwarg which will log a traceback if there is one. Closes-Bug: #1505240 Change-Id: Ib3113f2c4752d37e890f97d259da5d51cbfcfb96 --- diff --git a/cinder/tests/unit/windows/test_vhdutils.py b/cinder/tests/unit/windows/test_vhdutils.py index 30129f4ac..3557b01f3 100644 --- a/cinder/tests/unit/windows/test_vhdutils.py +++ b/cinder/tests/unit/windows/test_vhdutils.py @@ -354,3 +354,29 @@ class VHDUtilsTestCase(test.TestCase): def test_reconnect_parent_failed(self): self._test_reconnect_parent(reconnect_failed=True) + + @mock.patch('sys.exc_info') + @mock.patch.object(vhdutils, 'LOG') + def test_run_and_check_output_fails_exc_info_set(self, mock_log, + mock_exc_info): + # we can't use assertRaises because we're mocking sys.exc_info and + # that messes up how assertRaises handles the exception + try: + self._vhdutils._run_and_check_output(lambda *args, **kwargs: 1) + self.fail('Expected _run_and_check_output to fail.') + except exception.VolumeBackendAPIException: + pass + mock_log.error.assert_called_once_with(mock.ANY, exc_info=True) + + @mock.patch('sys.exc_info', return_value=None) + @mock.patch.object(vhdutils, 'LOG') + def test_run_and_check_output_fails_exc_info_not_set(self, mock_log, + mock_exc_info): + # we can't use assertRaises because we're mocking sys.exc_info and + # that messes up how assertRaises handles the exception + try: + self._vhdutils._run_and_check_output(lambda *args, **kwargs: 1) + self.fail('Expected _run_and_check_output to fail.') + except exception.VolumeBackendAPIException: + pass + mock_log.error.assert_called_once_with(mock.ANY, exc_info=False) diff --git a/cinder/volume/drivers/windows/vhdutils.py b/cinder/volume/drivers/windows/vhdutils.py index 304480892..fafa4799f 100644 --- a/cinder/volume/drivers/windows/vhdutils.py +++ b/cinder/volume/drivers/windows/vhdutils.py @@ -28,6 +28,7 @@ http://msdn.microsoft.com/en-us/library/windows/desktop/dd323700.aspx """ import ctypes import os +import sys if os.name == 'nt': from ctypes import wintypes @@ -210,7 +211,7 @@ class VHDUtils(object): {'func_name': func_name, 'error_code': ret_val, 'error_message': error_message}) - LOG.exception(err) + LOG.error(err, exc_info=(sys.exc_info() is not None)) raise exception.VolumeBackendAPIException(err) @staticmethod