From e073ff5adb963867fea3fca47ac645c6ea3621dd Mon Sep 17 00:00:00 2001 From: Christian Herdtweck Date: Mon, 15 Oct 2018 10:41:57 +0200 Subject: [PATCH] Rename log_helper base dir in tests --- tests/common/__init__.py | 0 tests/common/log_helper/__init__.py | 0 tests/common/log_helper/log_helper_test_imported.py | 23 +++++++++++++++++++++++ tests/common/log_helper/log_helper_test_main.py | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/common/log_helper/test_log_helper.py | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/util/__init__.py | 0 tests/util/log_helper/__init__.py | 0 tests/util/log_helper/log_helper_test_imported.py | 23 ----------------------- tests/util/log_helper/log_helper_test_main.py | 57 --------------------------------------------------------- tests/util/log_helper/test_log_helper.py | 112 ---------------------------------------------------------------------------------------------------------------- 10 files changed, 192 insertions(+), 192 deletions(-) create mode 100644 tests/common/__init__.py create mode 100644 tests/common/log_helper/__init__.py create mode 100644 tests/common/log_helper/log_helper_test_imported.py create mode 100644 tests/common/log_helper/log_helper_test_main.py create mode 100644 tests/common/log_helper/test_log_helper.py delete mode 100644 tests/util/__init__.py delete mode 100644 tests/util/log_helper/__init__.py delete mode 100644 tests/util/log_helper/log_helper_test_imported.py delete mode 100644 tests/util/log_helper/log_helper_test_main.py delete mode 100644 tests/util/log_helper/test_log_helper.py diff --git a/tests/common/__init__.py b/tests/common/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/tests/common/__init__.py diff --git a/tests/common/log_helper/__init__.py b/tests/common/log_helper/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/tests/common/log_helper/__init__.py diff --git a/tests/common/log_helper/log_helper_test_imported.py b/tests/common/log_helper/log_helper_test_imported.py new file mode 100644 index 0000000..04db006 --- /dev/null +++ b/tests/common/log_helper/log_helper_test_imported.py @@ -0,0 +1,23 @@ +""" +Dummy file that logs messages, meant to be imported +by the main test file +""" + +from oletools.util.log_helper import log_helper +import logging + +DEBUG_MESSAGE = 'imported: debug log' +INFO_MESSAGE = 'imported: info log' +WARNING_MESSAGE = 'imported: warning log' +ERROR_MESSAGE = 'imported: error log' +CRITICAL_MESSAGE = 'imported: critical log' + +logger = log_helper.get_or_create_silent_logger('test_imported', logging.ERROR) + + +def log(): + logger.debug(DEBUG_MESSAGE) + logger.info(INFO_MESSAGE) + logger.warning(WARNING_MESSAGE) + logger.error(ERROR_MESSAGE) + logger.critical(CRITICAL_MESSAGE) diff --git a/tests/common/log_helper/log_helper_test_main.py b/tests/common/log_helper/log_helper_test_main.py new file mode 100644 index 0000000..53bfabc --- /dev/null +++ b/tests/common/log_helper/log_helper_test_main.py @@ -0,0 +1,57 @@ +""" Test log_helpers """ + +import sys +from tests.util.log_helper import log_helper_test_imported +from oletools.util.log_helper import log_helper + +DEBUG_MESSAGE = 'main: debug log' +INFO_MESSAGE = 'main: info log' +WARNING_MESSAGE = 'main: warning log' +ERROR_MESSAGE = 'main: error log' +CRITICAL_MESSAGE = 'main: critical log' + +logger = log_helper.get_or_create_silent_logger('test_main') + + +def init_logging_and_log(args): + """ + Try to cover possible logging scenarios. For each scenario covered, here's the expected args and outcome: + - Log without enabling: [''] + * logging when being imported - should never print + - Log as JSON without enabling: ['as-json', ''] + * logging as JSON when being imported - should never print + - Enable and log: ['enable', ''] + * logging when being run as script - should log messages + - Enable and log as JSON: ['as-json', 'enable', ''] + * logging as JSON when being run as script - should log messages as JSON + - Enable, log as JSON and throw: ['enable', 'as-json', 'throw', ''] + * should produce JSON-compatible output, even after an unhandled exception + """ + + # the level should always be the last argument passed + level = args[-1] + use_json = 'as-json' in args + throw = 'throw' in args + + if 'enable' in args: + log_helper.enable_logging(use_json, level, stream=sys.stdout) + + _log() + + if throw: + raise Exception('An exception occurred before ending the logging') + + log_helper.end_logging() + + +def _log(): + logger.debug(DEBUG_MESSAGE) + logger.info(INFO_MESSAGE) + logger.warning(WARNING_MESSAGE) + logger.error(ERROR_MESSAGE) + logger.critical(CRITICAL_MESSAGE) + log_helper_test_imported.log() + + +if __name__ == '__main__': + init_logging_and_log(sys.argv[1:]) diff --git a/tests/common/log_helper/test_log_helper.py b/tests/common/log_helper/test_log_helper.py new file mode 100644 index 0000000..3c9f0af --- /dev/null +++ b/tests/common/log_helper/test_log_helper.py @@ -0,0 +1,112 @@ +""" Test the log helper + +This tests the generic log helper. +Check if it handles imported modules correctly +and that the default silent logger won't log when nothing is enabled +""" + +import unittest +import sys +import json +import subprocess +from tests.util.log_helper import log_helper_test_main +from tests.util.log_helper import log_helper_test_imported +from os.path import dirname, join, relpath, abspath + +# this is the common base of "tests" and "oletools" dirs +ROOT_DIRECTORY = abspath(join(__file__, '..', '..', '..', '..')) +TEST_FILE = relpath(join(dirname(__file__), 'log_helper_test_main.py'), ROOT_DIRECTORY) +PYTHON_EXECUTABLE = sys.executable + +MAIN_LOG_MESSAGES = [ + log_helper_test_main.DEBUG_MESSAGE, + log_helper_test_main.INFO_MESSAGE, + log_helper_test_main.WARNING_MESSAGE, + log_helper_test_main.ERROR_MESSAGE, + log_helper_test_main.CRITICAL_MESSAGE +] + + +class TestLogHelper(unittest.TestCase): + def test_it_doesnt_log_when_not_enabled(self): + output = self._run_test(['debug']) + self.assertTrue(len(output) == 0) + + def test_it_doesnt_log_json_when_not_enabled(self): + output = self._run_test(['as-json', 'debug']) + self.assertTrue(len(output) == 0) + + def test_logs_when_enabled(self): + output = self._run_test(['enable', 'warning']) + + expected_messages = [ + log_helper_test_main.WARNING_MESSAGE, + log_helper_test_main.ERROR_MESSAGE, + log_helper_test_main.CRITICAL_MESSAGE, + log_helper_test_imported.WARNING_MESSAGE, + log_helper_test_imported.ERROR_MESSAGE, + log_helper_test_imported.CRITICAL_MESSAGE + ] + + for msg in expected_messages: + self.assertIn(msg, output) + + def test_logs_json_when_enabled(self): + output = self._run_test(['enable', 'as-json', 'critical']) + + self._assert_json_messages(output, [ + log_helper_test_main.CRITICAL_MESSAGE, + log_helper_test_imported.CRITICAL_MESSAGE + ]) + + def test_json_correct_on_exceptions(self): + """ + Test that even on unhandled exceptions our JSON is always correct + """ + output = self._run_test(['enable', 'as-json', 'throw', 'critical'], False) + self._assert_json_messages(output, [ + log_helper_test_main.CRITICAL_MESSAGE, + log_helper_test_imported.CRITICAL_MESSAGE + ]) + + def _assert_json_messages(self, output, messages): + try: + json_data = json.loads(output) + self.assertEquals(len(json_data), len(messages)) + + for i in range(len(messages)): + self.assertEquals(messages[i], json_data[i]['msg']) + except ValueError: + self.fail('Invalid json:\n' + output) + + self.assertNotEqual(len(json_data), 0, msg='Output was empty') + + def _run_test(self, args, should_succeed=True): + """ + Use subprocess to better simulate the real scenario and avoid + logging conflicts when running multiple tests (since logging depends on singletons, + we might get errors or false positives between sequential tests runs) + """ + child = subprocess.Popen( + [PYTHON_EXECUTABLE, TEST_FILE] + args, + shell=False, + env={'PYTHONPATH': ROOT_DIRECTORY}, + universal_newlines=True, + cwd=ROOT_DIRECTORY, + stdin=None, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE + ) + (output, output_err) = child.communicate() + + if not isinstance(output, str): + output = output.decode('utf-8') + + self.assertEquals(child.returncode == 0, should_succeed) + + return output.strip() + + +# just in case somebody calls this file as a script +if __name__ == '__main__': + unittest.main() diff --git a/tests/util/__init__.py b/tests/util/__init__.py deleted file mode 100644 index e69de29..0000000 --- a/tests/util/__init__.py +++ /dev/null diff --git a/tests/util/log_helper/__init__.py b/tests/util/log_helper/__init__.py deleted file mode 100644 index e69de29..0000000 --- a/tests/util/log_helper/__init__.py +++ /dev/null diff --git a/tests/util/log_helper/log_helper_test_imported.py b/tests/util/log_helper/log_helper_test_imported.py deleted file mode 100644 index 04db006..0000000 --- a/tests/util/log_helper/log_helper_test_imported.py +++ /dev/null @@ -1,23 +0,0 @@ -""" -Dummy file that logs messages, meant to be imported -by the main test file -""" - -from oletools.util.log_helper import log_helper -import logging - -DEBUG_MESSAGE = 'imported: debug log' -INFO_MESSAGE = 'imported: info log' -WARNING_MESSAGE = 'imported: warning log' -ERROR_MESSAGE = 'imported: error log' -CRITICAL_MESSAGE = 'imported: critical log' - -logger = log_helper.get_or_create_silent_logger('test_imported', logging.ERROR) - - -def log(): - logger.debug(DEBUG_MESSAGE) - logger.info(INFO_MESSAGE) - logger.warning(WARNING_MESSAGE) - logger.error(ERROR_MESSAGE) - logger.critical(CRITICAL_MESSAGE) diff --git a/tests/util/log_helper/log_helper_test_main.py b/tests/util/log_helper/log_helper_test_main.py deleted file mode 100644 index 53bfabc..0000000 --- a/tests/util/log_helper/log_helper_test_main.py +++ /dev/null @@ -1,57 +0,0 @@ -""" Test log_helpers """ - -import sys -from tests.util.log_helper import log_helper_test_imported -from oletools.util.log_helper import log_helper - -DEBUG_MESSAGE = 'main: debug log' -INFO_MESSAGE = 'main: info log' -WARNING_MESSAGE = 'main: warning log' -ERROR_MESSAGE = 'main: error log' -CRITICAL_MESSAGE = 'main: critical log' - -logger = log_helper.get_or_create_silent_logger('test_main') - - -def init_logging_and_log(args): - """ - Try to cover possible logging scenarios. For each scenario covered, here's the expected args and outcome: - - Log without enabling: [''] - * logging when being imported - should never print - - Log as JSON without enabling: ['as-json', ''] - * logging as JSON when being imported - should never print - - Enable and log: ['enable', ''] - * logging when being run as script - should log messages - - Enable and log as JSON: ['as-json', 'enable', ''] - * logging as JSON when being run as script - should log messages as JSON - - Enable, log as JSON and throw: ['enable', 'as-json', 'throw', ''] - * should produce JSON-compatible output, even after an unhandled exception - """ - - # the level should always be the last argument passed - level = args[-1] - use_json = 'as-json' in args - throw = 'throw' in args - - if 'enable' in args: - log_helper.enable_logging(use_json, level, stream=sys.stdout) - - _log() - - if throw: - raise Exception('An exception occurred before ending the logging') - - log_helper.end_logging() - - -def _log(): - logger.debug(DEBUG_MESSAGE) - logger.info(INFO_MESSAGE) - logger.warning(WARNING_MESSAGE) - logger.error(ERROR_MESSAGE) - logger.critical(CRITICAL_MESSAGE) - log_helper_test_imported.log() - - -if __name__ == '__main__': - init_logging_and_log(sys.argv[1:]) diff --git a/tests/util/log_helper/test_log_helper.py b/tests/util/log_helper/test_log_helper.py deleted file mode 100644 index 3c9f0af..0000000 --- a/tests/util/log_helper/test_log_helper.py +++ /dev/null @@ -1,112 +0,0 @@ -""" Test the log helper - -This tests the generic log helper. -Check if it handles imported modules correctly -and that the default silent logger won't log when nothing is enabled -""" - -import unittest -import sys -import json -import subprocess -from tests.util.log_helper import log_helper_test_main -from tests.util.log_helper import log_helper_test_imported -from os.path import dirname, join, relpath, abspath - -# this is the common base of "tests" and "oletools" dirs -ROOT_DIRECTORY = abspath(join(__file__, '..', '..', '..', '..')) -TEST_FILE = relpath(join(dirname(__file__), 'log_helper_test_main.py'), ROOT_DIRECTORY) -PYTHON_EXECUTABLE = sys.executable - -MAIN_LOG_MESSAGES = [ - log_helper_test_main.DEBUG_MESSAGE, - log_helper_test_main.INFO_MESSAGE, - log_helper_test_main.WARNING_MESSAGE, - log_helper_test_main.ERROR_MESSAGE, - log_helper_test_main.CRITICAL_MESSAGE -] - - -class TestLogHelper(unittest.TestCase): - def test_it_doesnt_log_when_not_enabled(self): - output = self._run_test(['debug']) - self.assertTrue(len(output) == 0) - - def test_it_doesnt_log_json_when_not_enabled(self): - output = self._run_test(['as-json', 'debug']) - self.assertTrue(len(output) == 0) - - def test_logs_when_enabled(self): - output = self._run_test(['enable', 'warning']) - - expected_messages = [ - log_helper_test_main.WARNING_MESSAGE, - log_helper_test_main.ERROR_MESSAGE, - log_helper_test_main.CRITICAL_MESSAGE, - log_helper_test_imported.WARNING_MESSAGE, - log_helper_test_imported.ERROR_MESSAGE, - log_helper_test_imported.CRITICAL_MESSAGE - ] - - for msg in expected_messages: - self.assertIn(msg, output) - - def test_logs_json_when_enabled(self): - output = self._run_test(['enable', 'as-json', 'critical']) - - self._assert_json_messages(output, [ - log_helper_test_main.CRITICAL_MESSAGE, - log_helper_test_imported.CRITICAL_MESSAGE - ]) - - def test_json_correct_on_exceptions(self): - """ - Test that even on unhandled exceptions our JSON is always correct - """ - output = self._run_test(['enable', 'as-json', 'throw', 'critical'], False) - self._assert_json_messages(output, [ - log_helper_test_main.CRITICAL_MESSAGE, - log_helper_test_imported.CRITICAL_MESSAGE - ]) - - def _assert_json_messages(self, output, messages): - try: - json_data = json.loads(output) - self.assertEquals(len(json_data), len(messages)) - - for i in range(len(messages)): - self.assertEquals(messages[i], json_data[i]['msg']) - except ValueError: - self.fail('Invalid json:\n' + output) - - self.assertNotEqual(len(json_data), 0, msg='Output was empty') - - def _run_test(self, args, should_succeed=True): - """ - Use subprocess to better simulate the real scenario and avoid - logging conflicts when running multiple tests (since logging depends on singletons, - we might get errors or false positives between sequential tests runs) - """ - child = subprocess.Popen( - [PYTHON_EXECUTABLE, TEST_FILE] + args, - shell=False, - env={'PYTHONPATH': ROOT_DIRECTORY}, - universal_newlines=True, - cwd=ROOT_DIRECTORY, - stdin=None, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE - ) - (output, output_err) = child.communicate() - - if not isinstance(output, str): - output = output.decode('utf-8') - - self.assertEquals(child.returncode == 0, should_succeed) - - return output.strip() - - -# just in case somebody calls this file as a script -if __name__ == '__main__': - unittest.main() -- libgit2 0.21.4