Make NovaException format errors fatal for tests

Recently, as part of the centralize-config-options work, functionality
that detects errors in NovaException formats was removed [1] because
the config option controlling it was solely being used for testing.

We have had to do a bug fixes like [2] where a testing exception format
error in a mock decorator caused gorpy output to the console during
unit test runs.

To help prevent future bugs, this restores the error detecting
functionality by patching the exception handling to reraise format
error exceptions, only for tests. The _log_exception() function has to
be patched at import time in order to catch exception KeyErrors that
are in mock decorators because those evaluate at test listing time,
before tests actually run.

[1] https://review.openstack.org/#/c/397823
[2] https://review.openstack.org/#/c/411502

Change-Id: I680fd46d029ff58bd3b72ef7c7903c2271b26549
This commit is contained in:
melanie witt 2016-12-16 03:46:13 +00:00
parent fa987fe309
commit 73b78e397f

View File

@ -30,6 +30,7 @@ import datetime
import inspect
import os
import pprint
import sys
import fixtures
import mock
@ -48,6 +49,7 @@ import testtools
from nova import context
from nova import db
from nova import exception
from nova.network import manager as network_manager
from nova.network.security_group import openstack_driver
from nova import objects
@ -168,6 +170,26 @@ def _patch_mock_to_raise_for_invalid_assert_calls():
_patch_mock_to_raise_for_invalid_assert_calls()
class NovaExceptionReraiseFormatError(object):
real_log_exception = exception.NovaException._log_exception
@classmethod
def patch(cls):
exception.NovaException._log_exception = cls._wrap_log_exception
@staticmethod
def _wrap_log_exception(self):
exc_info = sys.exc_info()
NovaExceptionReraiseFormatError.real_log_exception(self)
six.reraise(*exc_info)
# NOTE(melwitt) This needs to be done at import time in order to also catch
# NovaException format errors that are in mock decorators. In these cases, the
# errors will be raised during test listing, before tests actually run.
NovaExceptionReraiseFormatError.patch()
class TestCase(testtools.TestCase):
"""Test case base class for all unit tests.