Skip to content

Commit 85e86ba

Browse files
committed
logging INFO issues
1 parent 40b6e05 commit 85e86ba

File tree

2 files changed

+37
-49
lines changed

2 files changed

+37
-49
lines changed

lambdas/shared/src/common/clients.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,9 @@
22
import logging
33
from boto3 import client as boto3_client, resource as boto3_resource
44

5-
# Configure root logger with a numeric level and ensure any handlers accept INFO.
65
logging.basicConfig(level=logging.INFO)
7-
root_logger = logging.getLogger()
8-
root_logger.setLevel(logging.INFO)
9-
10-
for handler in root_logger.handlers:
11-
try:
12-
handler.setLevel(logging.INFO)
13-
except Exception:
14-
pass
15-
16-
# Export the configured root logger for modules to use
17-
logger = root_logger
6+
logger = logging.getLogger()
7+
logger.setLevel(logging.INFO)
188

199
STREAM_NAME = os.getenv("SPLUNK_FIREHOSE_NAME", "firehose-name-not-defined")
2010
CONFIG_BUCKET_NAME = os.getenv("CONFIG_BUCKET_NAME", "variconfig-bucketable-not-defined")
Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,61 @@
11
import unittest
2-
from unittest.mock import patch
3-
import common.clients as clients
2+
from unittest.mock import patch, MagicMock
3+
import logging
44
import importlib
5+
import common.clients as clients
56

67

78
class TestClients(unittest.TestCase):
8-
99
BUCKET_NAME = "default-bucket"
1010
AWS_REGION = "eu-west-2"
1111

1212
def setUp(self):
13-
self.boto3_client_patch = patch("boto3.client")
13+
# Patch boto3.client
14+
self.boto3_client_patch = patch("boto3.client", autospec=True)
1415
self.mock_boto3_client = self.boto3_client_patch.start()
15-
self.logging_patch = patch("logging.getLogger")
16-
self.mock_logging = self.logging_patch.start()
17-
self.logger_info_patcher = patch("logging.Logger.info")
18-
self.mock_logger_info = self.logger_info_patcher.start()
19-
self.getenv_patch = patch("os.getenv")
16+
self.addCleanup(self.boto3_client_patch.stop)
17+
18+
# Patch logging.getLogger
19+
self.logging_patch = patch("logging.getLogger", autospec=True)
20+
self.mock_getLogger = self.logging_patch.start()
21+
self.addCleanup(self.logging_patch.stop)
22+
23+
# Patch os.getenv
24+
self.getenv_patch = patch("os.getenv", autospec=True)
2025
self.mock_getenv = self.getenv_patch.start()
26+
self.addCleanup(self.getenv_patch.stop)
27+
28+
# Set environment variable mock return values
2129
self.mock_getenv.side_effect = lambda key, default=None: {
2230
"CONFIG_BUCKET_NAME": self.BUCKET_NAME,
2331
"AWS_REGION": self.AWS_REGION,
2432
}.get(key, default)
2533

26-
self.mock_boto3_client.return_value = self.mock_boto3_client
27-
self.mock_boto3_client.return_value.send_message = {}
28-
29-
def tearDown(self):
30-
patch.stopall()
34+
# Simulate logger instance and patch setLevel
35+
self.mock_logger_instance = MagicMock()
36+
self.mock_getLogger.return_value = self.mock_logger_instance
3137

32-
def test_os_environ(self):
33-
# Test if environment variables are set correctly
38+
# Reload the module under test to apply patches
3439
importlib.reload(clients)
40+
41+
def test_env_variables_loaded(self):
42+
"""Test that environment variables are loaded correctly"""
3543
self.assertEqual(clients.CONFIG_BUCKET_NAME, self.BUCKET_NAME)
3644
self.assertEqual(clients.REGION_NAME, self.AWS_REGION)
3745

38-
def test_boto3_client(self):
39-
''' Test boto3 client is created with correct parameters '''
40-
importlib.reload(clients)
46+
def test_boto3_client_created_for_s3(self):
47+
"""Test that S3 boto3 client is created with correct region"""
4148
self.mock_boto3_client.assert_any_call("s3", region_name=self.AWS_REGION)
4249

43-
def test_firehose_client(self):
44-
''' Test firehose client is created with correct parameters '''
45-
importlib.reload(clients)
50+
def test_boto3_client_created_for_firehose(self):
51+
"""Test that Firehose boto3 client is created with correct region"""
4652
self.mock_boto3_client.assert_any_call("firehose", region_name=self.AWS_REGION)
4753

48-
def test_logging_setup(self):
49-
''' Test logging is set up correctly '''
50-
importlib.reload(clients)
51-
self.assertTrue(hasattr(clients, 'logger'))
54+
def test_logger_is_initialized(self):
55+
"""Test that a logger instance is initialized"""
56+
self.mock_getLogger.assert_called_once_with()
57+
self.assertTrue(hasattr(clients, "logger"))
5258

53-
def test_logging_configuration(self):
54-
''' Test logging configuration '''
55-
importlib.reload(clients)
56-
clients.logger.setLevel.assert_called_once_with("INFO")
57-
58-
def test_logging_initialization(self):
59-
''' Test logging initialization '''
60-
importlib.reload(clients)
61-
self.mock_logging.assert_called_once_with()
62-
self.assertTrue(hasattr(clients, 'logger'))
63-
clients.logger.setLevel.assert_any_call("INFO")
59+
def test_logger_set_level(self):
60+
"""Test that logger level is set to INFO"""
61+
self.mock_logger_instance.setLevel.assert_called_once_with(logging.INFO)

0 commit comments

Comments
 (0)