summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDoug Hellmann <doug@doughellmann.com>2018-08-12 15:59:11 -0400
committerDoug Hellmann <doug@doughellmann.com>2018-08-12 18:56:14 -0400
commitdbab9ab8c2a0ee08d8259e4934a2799dc07fdbd8 (patch)
tree353817b0f7de8ee31a7a15861bc19a7766718ea7
parent184ea300d05630d94a9c221340a626bd3460ae75 (diff)
downloadoslo-log-dbab9ab8c2a0ee08d8259e4934a2799dc07fdbd8.tar.gz
rewrite tests to not rely on implementation details of logging module
The tests for ensuring the results of the adapter are passed to the logger correctly rely more on the implementation details of the logging library than on the adapter supposedly being tested. As the underlying implementation changes in various ways, this causes the tests to fail (most recently when we try to run the tests under python 3.6. Rather than adding yet another case to the conditional for this specific version of python, rewrite the tests so they do not rely on the underlying implementation details. Change-Id: Id4d416852553b8d11ad27e25edcf9cceab3eb1b6 Signed-off-by: Doug Hellmann <doug@doughellmann.com> fix Change-Id: I1f5140b0a9499c94e13fa3cd0f7151dc6c952621 Signed-off-by: Doug Hellmann <doug@doughellmann.com>
-rw-r--r--oslo_log/tests/unit/test_log.py78
1 files changed, 42 insertions, 36 deletions
diff --git a/oslo_log/tests/unit/test_log.py b/oslo_log/tests/unit/test_log.py
index 7878525..e5cfc1f 100644
--- a/oslo_log/tests/unit/test_log.py
+++ b/oslo_log/tests/unit/test_log.py
@@ -1702,6 +1702,20 @@ class LogConfigTestCase(BaseTestCase):
disable_existing_loggers=False)
+class SavingAdapter(log.KeywordArgumentAdapter):
+
+ def __init__(self, *args, **kwds):
+ super(log.KeywordArgumentAdapter, self).__init__(*args, **kwds)
+ self.results = []
+
+ def process(self, msg, kwargs):
+ # Run the real adapter and save the inputs and outputs
+ # before returning them so the test can examine both.
+ results = super(SavingAdapter, self).process(msg, kwargs)
+ self.results.append((msg, kwargs, results))
+ return results
+
+
class KeywordArgumentAdapterTestCase(BaseTestCase):
def setUp(self):
@@ -1754,52 +1768,44 @@ class KeywordArgumentAdapterTestCase(BaseTestCase):
kwargs)
def test_pass_args_to_log(self):
- a = log.KeywordArgumentAdapter(self.mock_log, {})
+ a = SavingAdapter(self.mock_log, {})
+
message = 'message'
exc_message = 'exception'
- key = 'name'
val = 'value'
a.log(logging.DEBUG, message, name=val, exc_info=exc_message)
- if six.PY3:
- self.mock_log._log.assert_called_once_with(
- logging.DEBUG,
- message,
- (),
- extra={key: val, 'extra_keys': [key]},
- exc_info=exc_message
- )
- else:
- self.mock_log.log.assert_called_once_with(
- logging.DEBUG,
- message,
- extra={key: val, 'extra_keys': [key]},
- exc_info=exc_message
- )
+
+ expected = {
+ 'exc_info': exc_message,
+ 'extra': {'name': val, 'extra_keys': ['name']},
+ }
+
+ actual = a.results[0]
+ self.assertEqual(message, actual[0])
+ self.assertEqual(expected, actual[1])
+ results = actual[2]
+ self.assertEqual(message, results[0])
+ self.assertEqual(expected, results[1])
def test_pass_args_via_debug(self):
- a = log.KeywordArgumentAdapter(self.mock_log, {})
+
+ a = SavingAdapter(self.mock_log, {})
message = 'message'
exc_message = 'exception'
- key = 'name'
val = 'value'
a.debug(message, name=val, exc_info=exc_message)
- # The adapter implementation for debug() is different for
- # python 3, so we expect a different method to be called
- # internally.
- if six.PY3:
- self.mock_log._log.assert_called_once_with(
- logging.DEBUG,
- message,
- (),
- extra={key: val, 'extra_keys': [key]},
- exc_info=exc_message
- )
- else:
- self.mock_log.debug.assert_called_once_with(
- message,
- extra={key: val, 'extra_keys': [key]},
- exc_info=exc_message
- )
+
+ expected = {
+ 'exc_info': exc_message,
+ 'extra': {'name': val, 'extra_keys': ['name']},
+ }
+
+ actual = a.results[0]
+ self.assertEqual(message, actual[0])
+ self.assertEqual(expected, actual[1])
+ results = actual[2]
+ self.assertEqual(message, results[0])
+ self.assertEqual(expected, results[1])
class UnicodeConversionTestCase(BaseTestCase):