1
1
import boto3
2
2
import pytest
3
- from e2e import conftest
4
3
5
4
from tests .e2e .utils import data_fetcher
6
5
7
6
8
- @pytest .fixture (scope = "module" )
9
- def config () -> conftest .LambdaConfig :
10
- return {
11
- "parameters" : {},
12
- "environment_variables" : {
13
- "MESSAGE" : "logger message test" ,
14
- "LOG_LEVEL" : "INFO" ,
15
- "ADDITIONAL_KEY" : "extra_info" ,
16
- },
17
- }
7
+ @pytest .fixture
8
+ def basic_handler_fn (infrastructure : dict ) -> str :
9
+ return infrastructure .get ("BasicHandler" , "" )
18
10
19
11
20
- def test_basic_lambda_logs_visible (execute_lambda : conftest .InfrastructureOutput , config : conftest .LambdaConfig ):
21
- # GIVEN
22
- lambda_name = execute_lambda .get_lambda_function_name (cf_output_name = "basichandlerarn" )
23
- timestamp = execute_lambda .get_lambda_execution_time_timestamp ()
24
- cw_client = boto3 .client ("logs" )
25
-
26
- # WHEN
27
- filtered_logs = data_fetcher .get_logs (lambda_function_name = lambda_name , start_time = timestamp , log_client = cw_client )
12
+ @pytest .fixture
13
+ def basic_handler_fn_arn (infrastructure : dict ) -> str :
14
+ return infrastructure .get ("BasicHandlerArn" , "" )
28
15
29
- # THEN
30
- assert any (
31
- log .message == config ["environment_variables" ]["MESSAGE" ]
32
- and log .level == config ["environment_variables" ]["LOG_LEVEL" ]
33
- for log in filtered_logs
34
- )
35
16
36
-
37
- def test_basic_lambda_no_debug_logs_visible (
38
- execute_lambda : conftest .InfrastructureOutput , config : conftest .LambdaConfig
39
- ):
17
+ def test_basic_lambda_logs_visible (basic_handler_fn , basic_handler_fn_arn ):
40
18
# GIVEN
41
- lambda_name = execute_lambda .get_lambda_function_name (cf_output_name = "basichandlerarn" )
42
- timestamp = execute_lambda .get_lambda_execution_time_timestamp ()
43
19
cw_client = boto3 .client ("logs" )
44
-
45
- # WHEN
46
- filtered_logs = data_fetcher .get_logs (lambda_function_name = lambda_name , start_time = timestamp , log_client = cw_client )
47
-
48
- # THEN
49
- assert not any (
50
- log .message == config ["environment_variables" ]["MESSAGE" ] and log .level == "DEBUG" for log in filtered_logs
51
- )
52
-
53
-
54
- def test_basic_lambda_contextual_data_logged (execute_lambda : conftest .InfrastructureOutput ):
55
- # GIVEN
56
20
required_keys = (
57
21
"xray_trace_id" ,
58
22
"function_request_id" ,
@@ -62,82 +26,11 @@ def test_basic_lambda_contextual_data_logged(execute_lambda: conftest.Infrastruc
62
26
"cold_start" ,
63
27
)
64
28
65
- lambda_name = execute_lambda .get_lambda_function_name (cf_output_name = "basichandlerarn" )
66
- timestamp = execute_lambda .get_lambda_execution_time_timestamp ()
67
- cw_client = boto3 .client ("logs" )
68
-
69
29
# WHEN
70
- filtered_logs = data_fetcher .get_logs (lambda_function_name = lambda_name , start_time = timestamp , log_client = cw_client )
71
-
72
- # THEN
73
- assert all (keys in logs .dict (exclude_unset = True ) for logs in filtered_logs for keys in required_keys )
74
-
75
-
76
- def test_basic_lambda_additional_key_persistence_basic_lambda (
77
- execute_lambda : conftest .InfrastructureOutput , config : conftest .LambdaConfig
78
- ):
79
- # GIVEN
80
- lambda_name = execute_lambda .get_lambda_function_name (cf_output_name = "basichandlerarn" )
81
- timestamp = execute_lambda .get_lambda_execution_time_timestamp ()
82
- cw_client = boto3 .client ("logs" )
83
-
84
- # WHEN
85
- filtered_logs = data_fetcher .get_logs (lambda_function_name = lambda_name , start_time = timestamp , log_client = cw_client )
86
-
87
- # THEN
88
- assert any (
89
- log .extra_info
90
- and log .message == config ["environment_variables" ]["MESSAGE" ]
91
- and log .level == config ["environment_variables" ]["LOG_LEVEL" ]
92
- for log in filtered_logs
30
+ _ , execution_time = data_fetcher .get_lambda_response (lambda_arn = basic_handler_fn_arn )
31
+ filtered_logs = data_fetcher .get_logs (
32
+ lambda_function_name = basic_handler_fn , start_time = int (execution_time .timestamp ()), log_client = cw_client
93
33
)
94
34
95
-
96
- def test_basic_lambda_empty_event_logged (execute_lambda : conftest .InfrastructureOutput ):
97
-
98
- # GIVEN
99
- lambda_name = execute_lambda .get_lambda_function_name (cf_output_name = "basichandlerarn" )
100
- timestamp = execute_lambda .get_lambda_execution_time_timestamp ()
101
- cw_client = boto3 .client ("logs" )
102
-
103
- # WHEN
104
- filtered_logs = data_fetcher .get_logs (lambda_function_name = lambda_name , start_time = timestamp , log_client = cw_client )
105
-
106
- # THEN
107
- assert any (log .message == {} for log in filtered_logs )
108
-
109
-
110
- def test_no_context_lambda_contextual_data_not_logged (execute_lambda : conftest .InfrastructureOutput ):
111
-
112
- # GIVEN
113
- required_missing_keys = (
114
- "function_request_id" ,
115
- "function_arn" ,
116
- "function_memory_size" ,
117
- "function_name" ,
118
- "cold_start" ,
119
- )
120
-
121
- lambda_name = execute_lambda .get_lambda_function_name (cf_output_name = "nocontexthandlerarn" )
122
- timestamp = execute_lambda .get_lambda_execution_time_timestamp ()
123
- cw_client = boto3 .client ("logs" )
124
-
125
- # WHEN
126
- filtered_logs = data_fetcher .get_logs (lambda_function_name = lambda_name , start_time = timestamp , log_client = cw_client )
127
-
128
35
# THEN
129
- assert not any (keys in logs .dict (exclude_unset = True ) for logs in filtered_logs for keys in required_missing_keys )
130
-
131
-
132
- def test_no_context_lambda_event_not_logged (execute_lambda : conftest .InfrastructureOutput ):
133
-
134
- # GIVEN
135
- lambda_name = execute_lambda .get_lambda_function_name (cf_output_name = "nocontexthandlerarn" )
136
- timestamp = execute_lambda .get_lambda_execution_time_timestamp ()
137
- cw_client = boto3 .client ("logs" )
138
-
139
- # WHEN
140
- filtered_logs = data_fetcher .get_logs (lambda_function_name = lambda_name , start_time = timestamp , log_client = cw_client )
141
-
142
- # THEN
143
- assert not any (log .message == {} for log in filtered_logs )
36
+ assert all (keys in logs .dict (exclude_unset = True ) for logs in filtered_logs for keys in required_keys )
0 commit comments