summaryrefslogtreecommitdiffstats
path: root/tests/unit/test_log.py
diff options
context:
space:
mode:
authorJason Kölker <jason@koelker.net>2013-02-13 16:57:56 -0600
committerJason Kölker <jason@koelker.net>2013-02-13 17:00:38 -0600
commit449ddfc682ff75964f659da97c3eb3de7596a439 (patch)
tree5d90b77831495d1c268abe6ef75b7d446d459220 /tests/unit/test_log.py
parenteaab5fae2502198e9fa57d0d90a7204a2bd83b16 (diff)
downloadoslo-449ddfc682ff75964f659da97c3eb3de7596a439.tar.gz
oslo-449ddfc682ff75964f659da97c3eb3de7596a439.tar.xz
oslo-449ddfc682ff75964f659da97c3eb3de7596a439.zip
Don't use subprocess for testing excepthook
* The previous test tested the funtionality of the excepthook using subprocess.popen. We don't need to test the interpreter, just test that our excepthook is installed and test that it functions as expected. * Fix Bug 1124617 Change-Id: Ifd8d35b842b913003100097d917d30bf66e5cb7a
Diffstat (limited to 'tests/unit/test_log.py')
-rw-r--r--tests/unit/test_log.py36
1 files changed, 18 insertions, 18 deletions
diff --git a/tests/unit/test_log.py b/tests/unit/test_log.py
index 3ea5943..a0788f1 100644
--- a/tests/unit/test_log.py
+++ b/tests/unit/test_log.py
@@ -1,10 +1,7 @@
import cStringIO
-import exceptions
import logging
import StringIO
-import subprocess
import sys
-import textwrap
from openstack.common import cfg
from openstack.common import context
@@ -225,25 +222,28 @@ class ExceptionLoggingTestCase(test_utils.BaseTestCase):
"""Test that Exceptions are logged"""
def test_excepthook_logs_exception(self):
- code = textwrap.dedent("""
- import sys
- from openstack.common import log as logging
+ product_name = 'somename'
+ exc_log = log.getLogger(product_name)
- logging.setup('somename')
- raise Exception('Some error happened')
- """)
+ stream = cStringIO.StringIO()
+ handler = logging.StreamHandler(stream)
+ handler.setFormatter(log.LegacyFormatter())
+ exc_log.logger.addHandler(handler)
+ self.addCleanup(exc_log.logger.removeHandler, handler)
+ excepthook = log._create_logging_excepthook(product_name)
- child = subprocess.Popen([
- sys.executable, "-"],
- stdin=subprocess.PIPE,
- stdout=subprocess.PIPE,
- stderr=subprocess.PIPE)
+ try:
+ raise Exception('Some error happened')
+ except Exception:
+ excepthook(*sys.exc_info())
- (out, err) = child.communicate(input=code)
+ expected_string = "CRITICAL somename [-] Some error happened"
+ self.assertTrue(expected_string in stream.getvalue(),
+ msg="Exception is not logged")
- self.assertTrue(
- "CRITICAL somename [-] Some error happened" in err,
- msg="Exception is not logged")
+ def test_excepthook_installed(self):
+ log.setup("test_excepthook_installed")
+ self.assertTrue(sys.excepthook != sys.__excepthook__)
class FancyRecordTestCase(test_utils.BaseTestCase):