From 80a8533f67a302e4f8a92043eb83e55e7a495acf Mon Sep 17 00:00:00 2001 From: erjxsrn Date: Tue, 1 Jul 2025 19:18:34 +0100 Subject: [PATCH 1/2] Unit Testcases --- .../tests/test_mtls_logging.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/eric-oss-hello-world-python-app/tests/test_mtls_logging.py b/eric-oss-hello-world-python-app/tests/test_mtls_logging.py index 42cffdc..33503f7 100644 --- a/eric-oss-hello-world-python-app/tests/test_mtls_logging.py +++ b/eric-oss-hello-world-python-app/tests/test_mtls_logging.py @@ -60,3 +60,21 @@ def send_log(message, logger_level, log_level): """Send a log through the MTLS logger""" logger = MtlsLogging(logger_level) logger.log(message, log_level) + +def test_log_handles_invalid_url(caplog): + """Ensure logger logs an error if requests.post raises InvalidURL""" + message = "Test message for InvalidURL" + with mock.patch.object(requests, "post", side_effect=requests.exceptions.InvalidURL("Bad URL")): + logger = MtlsLogging(Severity.DEBUG) + logger.log(message, Severity.CRITICAL) + assert "Request failed for mTLS logging: Bad URL" in caplog.text + + +def test_log_handles_missing_schema(caplog): + """Ensure logger logs an error if requests.post raises MissingSchema""" + message = "Test message for MissingSchema" + with mock.patch.object(requests, "post", side_effect=requests.exceptions.MissingSchema("Missing schema")): + logger = MtlsLogging(Severity.DEBUG) + logger.log(message, Severity.CRITICAL) + assert "Request failed for mTLS logging: Missing schema" in caplog.text + From 5d479bf9a26548293e4638b74210345bd64d17c3 Mon Sep 17 00:00:00 2001 From: erjxsrn Date: Tue, 1 Jul 2025 19:57:18 +0100 Subject: [PATCH 2/2] Testcase added --- .../tests/test_mtls_logging.py | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/eric-oss-hello-world-python-app/tests/test_mtls_logging.py b/eric-oss-hello-world-python-app/tests/test_mtls_logging.py index 33503f7..ef1a473 100644 --- a/eric-oss-hello-world-python-app/tests/test_mtls_logging.py +++ b/eric-oss-hello-world-python-app/tests/test_mtls_logging.py @@ -1,5 +1,7 @@ """Tests which cover the app's logging, both to STDOUT and to Log Aggregator""" +import builtins +import json from unittest import mock import requests from mtls_logging import MtlsLogging, Severity @@ -78,3 +80,30 @@ def test_log_handles_missing_schema(caplog): logger.log(message, Severity.CRITICAL) assert "Request failed for mTLS logging: Missing schema" in caplog.text + +def test_init_sets_log_level_from_log_ctrl_file(): + # Sample log control file contents with container mapping + log_ctrl_data = [ + {"container": "test-container", "severity": "critical"}, + {"container": "other-container", "severity": "warning"}, + ] + log_ctrl_json = json.dumps(log_ctrl_data) + + # Mocked config dict including the log_ctrl_file path + mock_config = { + "log_ctrl_file": "/dummy/path/logcontrol.json", + "ca_cert_file_name": "ca.pem", + "ca_cert_file_path": "certs", + "app_cert": "appcert.pem", + "app_key": "appkey.pem", + "app_cert_file_path": "certs", + "log_endpoint": "log.endpoint" + } + + # Patch config and environment variable + with mock.patch("mtls_logging.get_config", return_value=mock_config), \ + mock.patch("mtls_logging.get_os_env_string", return_value="test-container"), \ + mock.patch("builtins.open", mock.mock_open(read_data=log_ctrl_json)): + + logger = MtlsLogging(level=None) + assert logger.logger.level == Severity.CRITICAL