1
- import datetime
2
- import os
3
- from functools import lru_cache
4
-
5
1
import boto3
6
2
import pytest
7
3
8
- from .. import utils
9
-
10
- dirname = os .path .dirname (__file__ )
4
+ from ..utils import helpers
11
5
12
6
13
7
@pytest .fixture (scope = "module" )
14
8
def config ():
15
- return {"MESSAGE" : "logger message test" , "LOG_LEVEL" : "INFO" , "ADDITIONAL_KEY" : "extra_info" }
9
+ return {
10
+ "parameters" : {},
11
+ "environment_variables" : {
12
+ "MESSAGE" : "logger message test" ,
13
+ "LOG_LEVEL" : "INFO" ,
14
+ "ADDITIONAL_KEY" : "extra_info" ,
15
+ },
16
+ }
16
17
17
18
18
- @pytest .fixture (scope = "module" )
19
- def deploy_lambdas (deploy , config ):
20
- handlers_dir = f"{ dirname } /handlers/"
21
-
22
- lambda_arns = deploy (
23
- handlers_name = utils .find_handlers (handlers_dir ),
24
- handlers_dir = handlers_dir ,
25
- environment_variables = config ,
19
+ @pytest .mark .e2e
20
+ def test_basic_lambda_logs_visible (execute_lambda , config ):
21
+ # GIVEN
22
+ lambda_arn = execute_lambda ["arns" ]["basichandlerarn" ]
23
+ timestamp = int (execute_lambda ["execution_time" ].timestamp () * 1000 )
24
+ cw_client = boto3 .client ("logs" )
25
+
26
+ # WHEN
27
+ filtered_logs = helpers .get_logs (
28
+ lambda_function_name = lambda_arn .split (":" )[- 1 ], start_time = timestamp , log_client = cw_client
26
29
)
27
30
28
- for name , arn in lambda_arns .items ():
29
- utils .trigger_lambda (lambda_arn = arn )
30
- print (f"lambda { name } triggered" )
31
- return lambda_arns
32
-
33
-
34
- @pytest .fixture (scope = "module" )
35
- def trigger_lambdas (deploy_lambdas ):
36
- for name , arn in deploy_lambdas .items ():
37
- utils .trigger_lambda (lambda_arn = arn )
38
- print (f"lambda { name } triggered" )
39
-
40
-
41
- @lru_cache (maxsize = 10 , typed = False )
42
- def fetch_logs (lambda_arn ):
43
- start_time = int (datetime .datetime .now ().timestamp () * 1000 )
44
- result = utils .trigger_lambda (lambda_arn = lambda_arn )
45
-
46
- filtered_logs = utils .get_logs (
47
- start_time = start_time ,
48
- lambda_function_name = lambda_arn .split (":" )[- 1 ],
49
- log_client = boto3 .client ("logs" ),
31
+ # THEN
32
+ assert any (
33
+ log .message == config ["environment_variables" ]["MESSAGE" ]
34
+ and log .level == config ["environment_variables" ]["LOG_LEVEL" ]
35
+ for log in filtered_logs
50
36
)
51
- return filtered_logs
52
37
53
38
54
39
@pytest .mark .e2e
55
- def test_basic_lambda_logs_visible (deploy_lambdas , config ):
56
-
57
- filtered_logs = fetch_logs (lambda_arn = deploy_lambdas ["basichandlerarn" ])
40
+ def test_basic_lambda_no_debug_logs_visible (execute_lambda , config ):
41
+ # GIVEN
42
+ lambda_arn = execute_lambda ["arns" ]["basichandlerarn" ]
43
+ timestamp = int (execute_lambda ["execution_time" ].timestamp () * 1000 )
44
+ cw_client = boto3 .client ("logs" )
45
+
46
+ # WHEN
47
+ filtered_logs = helpers .get_logs (
48
+ lambda_function_name = lambda_arn .split (":" )[- 1 ], start_time = timestamp , log_client = cw_client
49
+ )
58
50
59
- assert any (log .message == config ["MESSAGE" ] and log .level == config ["LOG_LEVEL" ] for log in filtered_logs )
51
+ # THEN
52
+ assert not any (
53
+ log .message == config ["environment_variables" ]["MESSAGE" ] and log .level == "DEBUG" for log in filtered_logs
54
+ )
60
55
61
56
62
57
@pytest .mark .e2e
63
- def test_basic_lambda_no_debug_logs_visible (deploy_lambdas , config ):
64
- filtered_logs = fetch_logs (lambda_arn = deploy_lambdas ["basichandlerarn" ])
65
-
66
- assert not any (log .message == config ["MESSAGE" ] and log .level == "DEBUG" for log in filtered_logs )
67
-
58
+ def test_basic_lambda_contextual_data_logged (execute_lambda ):
59
+ # GIVEN
60
+ lambda_arn = execute_lambda ["arns" ]["basichandlerarn" ]
61
+ timestamp = int (execute_lambda ["execution_time" ].timestamp () * 1000 )
62
+ cw_client = boto3 .client ("logs" )
63
+
64
+ # WHEN
65
+ filtered_logs = helpers .get_logs (
66
+ lambda_function_name = lambda_arn .split (":" )[- 1 ], start_time = timestamp , log_client = cw_client
67
+ )
68
68
69
- @pytest .mark .e2e
70
- def test_basic_lambda_contextual_data_logged (deploy_lambdas ):
71
- filtered_logs = fetch_logs (lambda_arn = deploy_lambdas ["basichandlerarn" ])
69
+ # THEN
72
70
for log in filtered_logs :
73
71
assert (
74
72
log .xray_trace_id
@@ -81,26 +79,58 @@ def test_basic_lambda_contextual_data_logged(deploy_lambdas):
81
79
82
80
83
81
@pytest .mark .e2e
84
- def test_basic_lambda_additional_key_persistence_basic_lambda (deploy_lambdas , config ):
85
- filtered_logs = fetch_logs ( lambda_arn = deploy_lambdas [ "basichandlerarn" ])
82
+ def test_basic_lambda_additional_key_persistence_basic_lambda (execute_lambda , config ):
83
+ # GIVEN
86
84
85
+ lambda_arn = execute_lambda ["arns" ]["basichandlerarn" ]
86
+ timestamp = int (execute_lambda ["execution_time" ].timestamp () * 1000 )
87
+ cw_client = boto3 .client ("logs" )
88
+
89
+ # WHEN
90
+ filtered_logs = helpers .get_logs (
91
+ lambda_function_name = lambda_arn .split (":" )[- 1 ], start_time = timestamp , log_client = cw_client
92
+ )
93
+
94
+ # THEN
87
95
assert any (
88
- log .extra_info and log .message == config ["MESSAGE" ] and log .level == config ["LOG_LEVEL" ]
96
+ log .extra_info
97
+ and log .message == config ["environment_variables" ]["MESSAGE" ]
98
+ and log .level == config ["environment_variables" ]["LOG_LEVEL" ]
89
99
for log in filtered_logs
90
100
)
91
101
92
102
93
103
@pytest .mark .e2e
94
- def test_basic_lambda_empty_event_logged (deploy_lambdas ):
95
- filtered_logs = fetch_logs (lambda_arn = deploy_lambdas ["basichandlerarn" ])
104
+ def test_basic_lambda_empty_event_logged (execute_lambda ):
105
+
106
+ # GIVEN
107
+ lambda_arn = execute_lambda ["arns" ]["basichandlerarn" ]
108
+ timestamp = int (execute_lambda ["execution_time" ].timestamp () * 1000 )
109
+ cw_client = boto3 .client ("logs" )
96
110
111
+ # WHEN
112
+ filtered_logs = helpers .get_logs (
113
+ lambda_function_name = lambda_arn .split (":" )[- 1 ], start_time = timestamp , log_client = cw_client
114
+ )
115
+
116
+ # THEN
97
117
assert any (log .message == {} for log in filtered_logs )
98
118
99
119
100
120
@pytest .mark .e2e
101
- def test_no_context_lambda_contextual_data_not_logged (deploy_lambdas ):
102
- filtered_logs = fetch_logs (lambda_arn = deploy_lambdas ["nocontexthandlerarn" ])
121
+ def test_no_context_lambda_contextual_data_not_logged (execute_lambda ):
122
+
123
+ # GIVEN
124
+ lambda_arn = execute_lambda ["arns" ]["nocontexthandlerarn" ]
125
+ timestamp = int (execute_lambda ["execution_time" ].timestamp () * 1000 )
126
+ cw_client = boto3 .client ("logs" )
127
+
128
+ # WHEN
129
+ filtered_logs = helpers .get_logs (
130
+ lambda_function_name = lambda_arn .split (":" )[- 1 ], start_time = timestamp , log_client = cw_client
131
+ )
103
132
133
+ # THEN
104
134
assert not any (
105
135
(
106
136
log .xray_trace_id
@@ -115,9 +145,19 @@ def test_no_context_lambda_contextual_data_not_logged(deploy_lambdas):
115
145
116
146
117
147
@pytest .mark .e2e
118
- def test_no_context_lambda_event_not_logged (deploy_lambdas ):
119
- filtered_logs = fetch_logs (lambda_arn = deploy_lambdas ["nocontexthandlerarn" ])
148
+ def test_no_context_lambda_event_not_logged (execute_lambda ):
149
+
150
+ # GIVEN
151
+ lambda_arn = execute_lambda ["arns" ]["nocontexthandlerarn" ]
152
+ timestamp = int (execute_lambda ["execution_time" ].timestamp () * 1000 )
153
+ cw_client = boto3 .client ("logs" )
154
+
155
+ # WHEN
156
+ filtered_logs = helpers .get_logs (
157
+ lambda_function_name = lambda_arn .split (":" )[- 1 ], start_time = timestamp , log_client = cw_client
158
+ )
120
159
160
+ # THEN
121
161
assert not any (log .message == {} for log in filtered_logs )
122
162
123
163
0 commit comments