]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Fix log capturing fixture
authorJoshua Harlow <harlowja@yahoo-inc.com>
Tue, 10 Jun 2014 02:02:42 +0000 (19:02 -0700)
committerJoshua Harlow <harlowja@yahoo-inc.com>
Tue, 10 Jun 2014 22:22:26 +0000 (15:22 -0700)
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

cinder/test.py

index b394f4102ffb8d4163607295887666ea1827c33c..1523efc448c79a512b38d3757331515b3b910ba6 100644 (file)
@@ -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,