From d2eeed6522f3f01332f1ffda3e30e81630a6a034 Mon Sep 17 00:00:00 2001 From: i343759 Date: Tue, 11 Jun 2024 09:32:00 +0300 Subject: [PATCH] init --- tests/test_job_logging.py | 45 ++++++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/tests/test_job_logging.py b/tests/test_job_logging.py index 6e22fc4..1d16fa2 100644 --- a/tests/test_job_logging.py +++ b/tests/test_job_logging.py @@ -1,4 +1,4 @@ -""" Module to test the cf_logging library """ +"""Module to test the cf_logging library""" import uuid import logging import time @@ -12,12 +12,11 @@ # pylint: disable=protected-access - @pytest.fixture(autouse=True) def before_each(): - """ reset logging framework """ + """Reset logging framework and clear log handlers""" cf_logging._SETUP_DONE = False - + logging.getLogger().handlers = [] @pytest.mark.parametrize('log_callback', [ lambda logger, msg: logger.debug('message: %s', msg), @@ -27,7 +26,7 @@ def before_each(): lambda logger, msg: logger.critical('message: %s', msg) ]) def test_log_in_expected_format(log_callback): - """ Test the cf_logger as a standalone """ + """Test the cf_logger as a standalone""" cf_logging.init(level=logging.DEBUG) logger, stream = config_logger('cli.test') log_callback(logger, 'hi') @@ -35,10 +34,10 @@ def test_log_in_expected_format(log_callback): _, error = JsonValidator(JOB_LOG_SCHEMA).validate(log_json) assert error == {} - + assert not check_duplicates(stream.getvalue()) def test_set_correlation_id(): - """ Test setting correlation_id """ + """Test setting correlation_id""" correlation_id = '1234' cf_logging.init(level=logging.DEBUG) cf_logging.FRAMEWORK.context.set_correlation_id(correlation_id) @@ -52,10 +51,10 @@ def test_set_correlation_id(): assert error == {} assert log_json['correlation_id'] == correlation_id assert cf_logging.FRAMEWORK.context.get_correlation_id() == correlation_id - + assert not check_duplicates(stream.getvalue()) def test_exception_stacktrace(): - """ Test exception stacktrace is logged """ + """Test exception stacktrace is logged""" cf_logging.init(level=logging.DEBUG) logger, stream = config_logger('cli.test') @@ -69,10 +68,10 @@ def test_exception_stacktrace(): assert error == {} assert 'ZeroDivisionError' in str(log_json['stacktrace']) assert 'ZeroDivisionError' in log_json["msg"] - + assert not check_duplicates(stream.getvalue()) def test_exception_stacktrace_info_level(): - """ Test exception stacktrace is logged """ + """Test exception stacktrace is logged at info level""" cf_logging.init(level=logging.DEBUG) logger, stream = config_logger('cli.test') @@ -86,18 +85,21 @@ def test_exception_stacktrace_info_level(): assert error == {} assert 'ZeroDivisionError' in str(log_json['stacktrace']) assert 'ZeroDivisionError' in log_json["msg"] - + assert not check_duplicates(stream.getvalue()) def test_custom_fields_set(): - """ Test custom fields are set up """ + """Test custom fields are set up""" cf_logging.init(level=logging.DEBUG, custom_fields={'cf1': None}) assert 'cf1' in cf_logging.FRAMEWORK.custom_fields.keys() + logger, stream = config_logger('cli.test') + logger.info('hi') + assert not check_duplicates(stream.getvalue()) def test_thread_safety(): - """ test context keeps separate correlation ID per thread """ + """Test context keeps separate correlation ID per thread""" class _SampleThread(threading.Thread): def __init__(self): - super(_SampleThread, self).__init__() + super().__init__() self.correlation_id = str(uuid.uuid1()) self.read_correlation_id = '' @@ -119,3 +121,16 @@ def run(self): assert thread_one.correlation_id == thread_one.read_correlation_id assert thread_two.correlation_id == thread_two.read_correlation_id + logger, stream = config_logger('cli.test') + logger.info('hi') + assert not check_duplicates(stream.getvalue()) + +def check_duplicates(log_stream): + """Check for duplicate log records in the log stream""" + log_lines = log_stream.splitlines() + seen_logs = set() + for line in log_lines: + if line in seen_logs: + return True + seen_logs.add(line) + return False