Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions eric-oss-hello-world-python-app/tests/test_mtls_logging.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -60,3 +62,48 @@ 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


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