From 99e997e90c540a460ff71e274db51358bc6959e9 Mon Sep 17 00:00:00 2001 From: Hongbin Lu Date: Tue, 21 Mar 2017 19:55:48 -0500 Subject: [PATCH] Add test to verify log record can be formatted This is a tech-debt for I6948e766d08c3e781a699cca8212b4303aae3273 Change-Id: I4fac5004ec778c8cc8212ef4b109995429841b75 Related-Bug: #1674185 --- oslo_privsep/tests/test_daemon.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/oslo_privsep/tests/test_daemon.py b/oslo_privsep/tests/test_daemon.py index 76599bd..3a73a15 100644 --- a/oslo_privsep/tests/test_daemon.py +++ b/oslo_privsep/tests/test_daemon.py @@ -20,6 +20,7 @@ import mock import platform import time +from oslo_log import formatters from oslo_log import log as logging from oslotest import base import testtools @@ -110,6 +111,28 @@ class LogTest(testctx.TestContextTestCase): self.assertEqual(logging.WARN, record.levelno) self.assertEqual('logme', record.funcName) + def test_format_record(self): + logs = [] + + self.useFixture(fixtures.FakeLogger( + level=logging.INFO, format='dummy', + # fixtures.FakeLogger accepts only a formatter + # class/function, not an instance :( + formatter=functools.partial(LogRecorder, logs))) + + logme(logging.WARN, u'test with exc', exc_info=True) + + time.sleep(0.1) # Hack to give logging thread a chance to run + + self.assertEqual(1, len(logs)) + + record = logs[0] + # Verify the log record can be formatted by ContextFormatter + fake_config = mock.Mock( + logging_default_format_string="NOCTXT: %(message)s") + formatter = formatters.ContextFormatter(config=fake_config) + formatter.format(record) + @testtools.skipIf(platform.system() != 'Linux', 'works only on Linux platform.')