From: Joshua Harlow Date: Tue, 10 Jun 2014 02:02:42 +0000 (-0700) Subject: Fix log capturing fixture X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=090d57e9ce4c74c1a106a0bc7b583e566624f85d;p=openstack-build%2Fcinder-build.git Fix log capturing fixture Instead of nuking the unit test logs capture them according to how tempest (and oslotest) capture them so that the tests when ran locally provide the full logging output instead of a miniaturized version. This adds in the following environment variables which can be used to control testr captured logging: - OS_LOG_CAPTURE (turns on log capturing so that the testr repository will have log output, at a INFO level) - OS_DEBUG (changes the captured logging level from INFO to DEBUG, only useful when OS_LOG_CAPTURE is enabled) It also creates a lambda that is used to test whether an environment is enabled (avoiding recreating the same set of checks in multiple places). Change-Id: I63d9a1b7fc6e7591fd6309cf68d9269117c023a6 --- diff --git a/cinder/test.py b/cinder/test.py index b394f4102..1523efc44 100644 --- a/cinder/test.py +++ b/cinder/test.py @@ -39,6 +39,7 @@ from cinder.common import config # noqa Need to register global_opts from cinder.db import migration from cinder.openstack.common.db.sqlalchemy import session from cinder.openstack.common import log as oslo_logging +from cinder.openstack.common import strutils from cinder.openstack.common import timeutils from cinder import rpc from cinder import service @@ -121,17 +122,23 @@ class TestCase(testtools.TestCase): self.useFixture(fixtures.NestedTempfile()) self.useFixture(fixtures.TempHomeDir()) - if (os.environ.get('OS_STDOUT_CAPTURE') == 'True' or - os.environ.get('OS_STDOUT_CAPTURE') == '1'): + environ_enabled = (lambda var_name: + strutils.bool_from_string(os.environ.get(var_name))) + if environ_enabled('OS_STDOUT_CAPTURE'): stdout = self.useFixture(fixtures.StringStream('stdout')).stream self.useFixture(fixtures.MonkeyPatch('sys.stdout', stdout)) - if (os.environ.get('OS_STDERR_CAPTURE') == 'True' or - os.environ.get('OS_STDERR_CAPTURE') == '1'): + if environ_enabled('OS_STDERR_CAPTURE'): stderr = self.useFixture(fixtures.StringStream('stderr')).stream self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr)) - - self.log_fixture = self.useFixture(fixtures.FakeLogger( - level=logging.DEBUG)) + if environ_enabled('OS_LOG_CAPTURE'): + log_format = '%(levelname)s [%(name)s] %(message)s' + if environ_enabled('OS_DEBUG'): + level = logging.DEBUG + else: + level = logging.INFO + self.useFixture(fixtures.LoggerFixture(nuke_handlers=False, + format=log_format, + level=level)) rpc.add_extra_exmods("cinder.tests") self.addCleanup(rpc.clear_extra_exmods) @@ -154,8 +161,6 @@ class TestCase(testtools.TestCase): CONF.set_default('connection', 'sqlite://', 'database') CONF.set_default('sqlite_synchronous', False) - self.log_fixture = self.useFixture(fixtures.FakeLogger()) - global _DB_CACHE if not _DB_CACHE: _DB_CACHE = Database(session, migration,