1010from boto3 import client as boto3_client
1111from moto import mock_firehose , mock_s3
1212
13- from tests . utils_for_recordprocessor_tests .mock_environment_variables import (
13+ from utils_for_recordprocessor_tests .mock_environment_variables import (
1414 MOCK_ENVIRONMENT_DICT ,
1515 BucketNames ,
1616 Firehose ,
1717)
18- from tests . utils_for_recordprocessor_tests .values_for_recordprocessor_tests import (
18+ from utils_for_recordprocessor_tests .values_for_recordprocessor_tests import (
1919 MockFileDetails ,
2020 ValidMockFileContent ,
2121)
2424 from common .clients import REGION_NAME
2525 from errors import InvalidHeaders , NoOperationPermissions
2626 from file_level_validation import file_level_validation
27- from logging_decorator import generate_and_send_logs , send_log_to_firehose
2827
2928
30- from tests . utils_for_recordprocessor_tests .utils_for_recordprocessor_tests import (
29+ from utils_for_recordprocessor_tests .utils_for_recordprocessor_tests import (
3130 GenericSetUp ,
3231 GenericTearDown ,
3332)
@@ -76,67 +75,6 @@ def run(self, result=None):
7675 stack .enter_context (common_patch )
7776 super ().run (result )
7877
79- def test_send_log_to_firehose (self ):
80- """
81- Tests that the send_log_to_firehose function calls firehose_client.put_record with the correct arguments.
82- NOTE: mock_firehose does not persist the data, so at this level it is only possible to test what the call args
83- were, not that the data reached the destination.
84- """
85- log_data = {"test_key" : "test_value" }
86-
87- with patch ("logging_decorator.firehose_client" ) as mock_firehose_client :
88- send_log_to_firehose (log_data )
89-
90- expected_firehose_record = {"Data" : json .dumps ({"event" : log_data }).encode ("utf-8" )}
91- mock_firehose_client .put_record .assert_called_once_with (
92- DeliveryStreamName = Firehose .STREAM_NAME , Record = expected_firehose_record
93- )
94-
95- def test_generate_and_send_logs (self ):
96- """
97- Tests that the generate_and_send_logs function logs the correct data at the correct level for cloudwatch
98- and calls send_log_to_firehose with the correct log data
99- """
100- base_log_data = {"base_key" : "base_value" }
101- additional_log_data = {"additional_key" : "additional_value" }
102- start_time = 1672531200
103-
104- # CASE: Successful log - is_error_log arg set to False
105- with ( # noqa: E999
106- patch ("logging_decorator.logger" ) as mock_logger , # noqa: E999
107- patch ("logging_decorator.send_log_to_firehose" ) as mock_send_log_to_firehose , # noqa: E999
108- patch ("logging_decorator.time" ) as mock_time , # noqa: E999
109- ): # noqa: E999
110- mock_time .time .return_value = 1672531200.123456 # Mocks the end time to be 0.123456s after the start time
111- generate_and_send_logs (start_time , base_log_data , additional_log_data , is_error_log = False )
112-
113- expected_log_data = {
114- "base_key" : "base_value" ,
115- "time_taken" : "0.12346s" ,
116- "additional_key" : "additional_value" ,
117- }
118- log_data = json .loads (mock_logger .info .call_args [0 ][0 ])
119- self .assertEqual (log_data , expected_log_data )
120- mock_send_log_to_firehose .assert_called_once_with (expected_log_data )
121-
122- # CASE: Error log - is_error_log arg set to True
123- with ( # noqa: E999
124- patch ("logging_decorator.logger" ) as mock_logger , # noqa: E999
125- patch ("logging_decorator.send_log_to_firehose" ) as mock_send_log_to_firehose , # noqa: E999
126- patch ("logging_decorator.time" ) as mock_time , # noqa: E999
127- ): # noqa: E999
128- mock_time .time .return_value = 1672531200.123456 # Mocks the end time to be 0.123456s after the start time
129- generate_and_send_logs (start_time , base_log_data , additional_log_data , is_error_log = True )
130-
131- expected_log_data = {
132- "base_key" : "base_value" ,
133- "time_taken" : "0.12346s" ,
134- "additional_key" : "additional_value" ,
135- }
136- log_data = json .loads (mock_logger .error .call_args [0 ][0 ])
137- self .assertEqual (log_data , expected_log_data )
138- mock_send_log_to_firehose .assert_called_once_with (expected_log_data )
139-
14078 def test_splunk_logger_successful_validation (self ):
14179 """Tests the splunk logger is called when file-level validation is successful"""
14280
@@ -147,12 +85,14 @@ def test_splunk_logger_successful_validation(self):
14785 )
14886
14987 with ( # noqa: E999
88+ patch ("common.log_firehose.firehose_client" ) as mock_firehose_client , # noqa: E999
89+ patch ("common.log_decorator.logger" ) as mock_logger , # noqa: E999
15090 patch ("logging_decorator.datetime" ) as mock_datetime , # noqa: E999
151- patch ("logging_decorator.time" ) as mock_time , # noqa: E999
152- patch ("logging_decorator.logger" ) as mock_logger , # noqa: E999
153- patch ("logging_decorator.firehose_client" ) as mock_firehose_client , # noqa: E999
91+ patch ("logging_decorator.time" ) as mock_start_time , # noqa: E999
92+ patch ("common.log_decorator.time" ) as mock_end_time , # noqa: E999
15493 ): # noqa: E999
155- mock_time .time .side_effect = [1672531200 , 1672531200.123456 ]
94+ mock_start_time .time .return_value = 1672531200
95+ mock_end_time .time .return_value = 1672531200.123456
15696 mock_datetime .now .return_value = datetime (2024 , 1 , 1 , 12 , 0 , 0 )
15797 file_level_validation (deepcopy (MOCK_FILE_DETAILS .event_full_permissions_dict ))
15898
@@ -211,13 +151,15 @@ def test_splunk_logger_handled_failure(self):
211151 )
212152
213153 with ( # noqa: E999
154+ patch ("common.log_firehose.firehose_client" ) as mock_firehose_client , # noqa: E999
155+ patch ("common.log_decorator.logger" ) as mock_logger , # noqa: E999
214156 patch ("logging_decorator.datetime" ) as mock_datetime , # noqa: E999
215- patch ("logging_decorator.time" ) as mock_time , # noqa: E999
216- patch ("logging_decorator.logger" ) as mock_logger , # noqa: E999
217- patch ("logging_decorator.firehose_client" ) as mock_firehose_client , # noqa: E999
157+ patch ("logging_decorator.time" ) as mock_start_time , # noqa: E999
158+ patch ("common.log_decorator.time" ) as mock_end_time , # noqa: E999
218159 ): # noqa: E999
219160 mock_datetime .now .return_value = datetime (2024 , 1 , 1 , 12 , 0 , 0 )
220- mock_time .time .side_effect = [1672531200 , 1672531200.123456 ]
161+ mock_start_time .time .return_value = 1672531200
162+ mock_end_time .time .return_value = 1672531200.123456
221163 with self .assertRaises (expected_error_type ):
222164 file_level_validation (deepcopy (event_dict ))
223165
@@ -247,16 +189,18 @@ def test_splunk_logger_unhandled_failure(self):
247189 )
248190
249191 with ( # noqa: E999
192+ patch ("common.log_firehose.firehose_client" ) as mock_firehose_client , # noqa: E999
193+ patch ("common.log_decorator.logger" ) as mock_logger , # noqa: E999
250194 patch ("logging_decorator.datetime" ) as mock_datetime , # noqa: E999
251- patch ("logging_decorator.time" ) as mock_time , # noqa: E999
252- patch ("logging_decorator.logger" ) as mock_logger , # noqa: E999
253- patch ("logging_decorator.firehose_client" ) as mock_firehose_client , # noqa: E999
195+ patch ("logging_decorator.time" ) as mock_start_time , # noqa: E999
196+ patch ("common.log_decorator.time" ) as mock_end_time , # noqa: E999
254197 patch (
255198 "file_level_validation.validate_content_headers" ,
256199 side_effect = ValueError ("Test exception" ),
257200 ), # noqa: E999
258201 ): # noqa: E999
259- mock_time .time .side_effect = [1672531200 , 1672531200.123456 ]
202+ mock_start_time .time .return_value = 1672531200
203+ mock_end_time .time .return_value = 1672531200.123456
260204 mock_datetime .now .return_value = datetime (2024 , 1 , 1 , 12 , 0 , 0 )
261205 with self .assertRaises (ValueError ):
262206 file_level_validation (deepcopy (MOCK_FILE_DETAILS .event_full_permissions_dict ))
0 commit comments