Commit 7d89934cb57cda96653d6b0ec0285e88d47263c6
1 parent
8d49c606
tests: Test new "type" field of json logging output
Every logging call can have an optional arg type='...' which is ignored when logging to text but shows in json output. The "type" of regular logging output in json is type='msg'. Added 2 messages with different type, adjusted existing test, created 2 new ones.
Showing
3 changed files
with
57 additions
and
0 deletions
tests/common/log_helper/log_helper_test_imported.py
| ... | ... | @@ -11,6 +11,8 @@ INFO_MESSAGE = 'imported: info log' |
| 11 | 11 | WARNING_MESSAGE = 'imported: warning log' |
| 12 | 12 | ERROR_MESSAGE = 'imported: error log' |
| 13 | 13 | CRITICAL_MESSAGE = 'imported: critical log' |
| 14 | +RESULT_MESSAGE = 'imported: result log' | |
| 15 | +RESULT_TYPE = 'imported: result' | |
| 14 | 16 | |
| 15 | 17 | logger = log_helper.get_or_create_silent_logger('test_imported', logging.ERROR) |
| 16 | 18 | |
| ... | ... | @@ -21,3 +23,4 @@ def log(): |
| 21 | 23 | logger.warning(WARNING_MESSAGE) |
| 22 | 24 | logger.error(ERROR_MESSAGE) |
| 23 | 25 | logger.critical(CRITICAL_MESSAGE) |
| 26 | + logger.info(RESULT_MESSAGE, type=RESULT_TYPE) | ... | ... |
tests/common/log_helper/log_helper_test_main.py
| ... | ... | @@ -9,6 +9,8 @@ INFO_MESSAGE = 'main: info log' |
| 9 | 9 | WARNING_MESSAGE = 'main: warning log' |
| 10 | 10 | ERROR_MESSAGE = 'main: error log' |
| 11 | 11 | CRITICAL_MESSAGE = 'main: critical log' |
| 12 | +RESULT_MESSAGE = 'main: result log' | |
| 13 | +RESULT_TYPE = 'main: result' | |
| 12 | 14 | |
| 13 | 15 | logger = log_helper.get_or_create_silent_logger('test_main') |
| 14 | 16 | |
| ... | ... | @@ -50,6 +52,7 @@ def _log(): |
| 50 | 52 | logger.warning(WARNING_MESSAGE) |
| 51 | 53 | logger.error(ERROR_MESSAGE) |
| 52 | 54 | logger.critical(CRITICAL_MESSAGE) |
| 55 | + logger.info(RESULT_MESSAGE, type=RESULT_TYPE) | |
| 53 | 56 | log_helper_test_imported.log() |
| 54 | 57 | |
| 55 | 58 | ... | ... |
tests/common/log_helper/test_log_helper.py
| ... | ... | @@ -61,6 +61,57 @@ class TestLogHelper(unittest.TestCase): |
| 61 | 61 | log_helper_test_imported.CRITICAL_MESSAGE |
| 62 | 62 | ]) |
| 63 | 63 | |
| 64 | + def test_logs_type_ignored(self): | |
| 65 | + """Run test script with logging enabled at info level. Want no type.""" | |
| 66 | + output = self._run_test(['enable', 'info']) | |
| 67 | + | |
| 68 | + expect = '\n'.join([ | |
| 69 | + 'INFO ' + log_helper_test_main.INFO_MESSAGE, | |
| 70 | + 'WARNING ' + log_helper_test_main.WARNING_MESSAGE, | |
| 71 | + 'ERROR ' + log_helper_test_main.ERROR_MESSAGE, | |
| 72 | + 'CRITICAL ' + log_helper_test_main.CRITICAL_MESSAGE, | |
| 73 | + 'INFO ' + log_helper_test_main.RESULT_MESSAGE, | |
| 74 | + 'INFO ' + log_helper_test_imported.INFO_MESSAGE, | |
| 75 | + 'WARNING ' + log_helper_test_imported.WARNING_MESSAGE, | |
| 76 | + 'ERROR ' + log_helper_test_imported.ERROR_MESSAGE, | |
| 77 | + 'CRITICAL ' + log_helper_test_imported.CRITICAL_MESSAGE, | |
| 78 | + 'INFO ' + log_helper_test_imported.RESULT_MESSAGE, | |
| 79 | + ]) | |
| 80 | + self.assertEqual(output, expect) | |
| 81 | + | |
| 82 | + def test_logs_type_in_json(self): | |
| 83 | + """Check type field is contained in json log.""" | |
| 84 | + output = self._run_test(['enable', 'as-json', 'info']) | |
| 85 | + | |
| 86 | + # convert to json preserving order of output | |
| 87 | + jout = json.loads(output) | |
| 88 | + | |
| 89 | + jexpect = [ | |
| 90 | + dict(type='msg', level='INFO', | |
| 91 | + msg=log_helper_test_main.INFO_MESSAGE), | |
| 92 | + dict(type='msg', level='WARNING', | |
| 93 | + msg=log_helper_test_main.WARNING_MESSAGE), | |
| 94 | + dict(type='msg', level='ERROR', | |
| 95 | + msg=log_helper_test_main.ERROR_MESSAGE), | |
| 96 | + dict(type='msg', level='CRITICAL', | |
| 97 | + msg=log_helper_test_main.CRITICAL_MESSAGE), | |
| 98 | + # this is the important entry (has a different "type" field): | |
| 99 | + dict(type=log_helper_test_main.RESULT_TYPE, level='INFO', | |
| 100 | + msg=log_helper_test_main.RESULT_MESSAGE), | |
| 101 | + dict(type='msg', level='INFO', | |
| 102 | + msg=log_helper_test_imported.INFO_MESSAGE), | |
| 103 | + dict(type='msg', level='WARNING', | |
| 104 | + msg=log_helper_test_imported.WARNING_MESSAGE), | |
| 105 | + dict(type='msg', level='ERROR', | |
| 106 | + msg=log_helper_test_imported.ERROR_MESSAGE), | |
| 107 | + dict(type='msg', level='CRITICAL', | |
| 108 | + msg=log_helper_test_imported.CRITICAL_MESSAGE), | |
| 109 | + # ... and this: | |
| 110 | + dict(type=log_helper_test_imported.RESULT_TYPE, level='INFO', | |
| 111 | + msg=log_helper_test_imported.RESULT_MESSAGE), | |
| 112 | + ] | |
| 113 | + self.assertEqual(jout, jexpect) | |
| 114 | + | |
| 64 | 115 | def test_json_correct_on_exceptions(self): |
| 65 | 116 | """ |
| 66 | 117 | Test that even on unhandled exceptions our JSON is always correct | ... | ... |