|
6 | 6 | import time |
7 | 7 | import yaml |
8 | 8 |
|
| 9 | +from unittest import mock |
| 10 | + |
9 | 11 | from aws_cdk import App |
10 | 12 |
|
11 | 13 | from .utils import DummyLambdaStack, SentryTestServer, SAM_PORT |
12 | 14 |
|
| 15 | + |
13 | 16 | SAM_TEMPLATE_FILE = "sam.template.yaml" |
14 | 17 |
|
15 | 18 |
|
@@ -44,7 +47,8 @@ def test_environment(): |
44 | 47 | "--debug", |
45 | 48 | "--template", |
46 | 49 | SAM_TEMPLATE_FILE, |
47 | | - "--warm-containers", "EAGER", |
| 50 | + "--warm-containers", |
| 51 | + "EAGER", |
48 | 52 | ], |
49 | 53 | stdout=debug_log, |
50 | 54 | stderr=debug_log, |
@@ -93,79 +97,80 @@ def lambda_client(): |
93 | 97 | ) |
94 | 98 |
|
95 | 99 |
|
96 | | -# def test_basic_ok(lambda_client, test_environment): |
97 | | -# response = lambda_client.invoke( |
98 | | -# FunctionName="BasicOk", |
99 | | -# Payload=json.dumps({"name": "Ivana"}), |
100 | | -# ) |
101 | | -# result = json.loads(response["Payload"].read().decode()) |
102 | | -# assert result == {"event": {"name": "Ivana"}} |
103 | | - |
104 | | -# envelopes = test_environment["server"].envelopes |
105 | | -# assert len(envelopes) == 1 |
106 | | - |
107 | | -# transaction = envelopes[0] |
108 | | -# assert transaction["type"] == "transaction" |
109 | | - |
110 | | - |
111 | | -def test_xxx(lambda_client, test_environment): |
112 | | - for x in range(20): |
113 | | - test_environment["server"].clear_envelopes() |
114 | | - print(f"*** BasicException {x} ***") |
115 | | - response = lambda_client.invoke( |
116 | | - FunctionName="BasicException", |
117 | | - Payload=json.dumps({}), |
118 | | - ) |
119 | | - print("- RESPONSE") |
120 | | - print(response) |
121 | | - print("- PAYLOAD") |
122 | | - print(response["Payload"].read().decode()) |
123 | | - num_envelopes = len(test_environment["server"].envelopes) |
124 | | - print(f'- ENVELOPES {num_envelopes}') |
125 | | - assert num_envelopes == 2 |
126 | | - |
127 | | - |
128 | | -# def test_basic(lambda_client, test_environment): |
129 | | -# response = lambda_client.invoke( |
130 | | -# FunctionName="BasicException", |
131 | | -# Payload=json.dumps({"name": "Neel"}), |
132 | | -# ) |
133 | | -# print("RESPONSE") |
134 | | -# print(response) |
135 | | -# print("PAYLOAD") |
136 | | -# print(response["Payload"].read().decode()) |
137 | | -# result = json.loads(response["Payload"].read().decode()) |
138 | | -# print("RESULT") |
139 | | -# print(result) |
140 | | - |
141 | | -# envelopes = test_environment["server"].envelopes |
142 | | -# (error,) = envelopes |
143 | | - |
144 | | -# assert error["level"] == "error" |
145 | | -# (exception,) = error["exception"]["values"] |
146 | | -# assert exception["type"] == "Exception" |
147 | | -# assert exception["value"] == "Oh!" |
148 | | - |
149 | | -# (frame1,) = exception["stacktrace"]["frames"] |
150 | | -# assert frame1["filename"] == "test_lambda.py" |
151 | | -# assert frame1["abs_path"] == "/var/task/test_lambda.py" |
152 | | -# assert frame1["function"] == "test_handler" |
153 | | - |
154 | | -# assert frame1["in_app"] is True |
155 | | - |
156 | | -# assert exception["mechanism"]["type"] == "aws_lambda" |
157 | | -# assert not exception["mechanism"]["handled"] |
158 | | - |
159 | | -# assert error["extra"]["lambda"]["function_name"].startswith("test_") |
160 | | - |
161 | | -# logs_url = error["extra"]["cloudwatch logs"]["url"] |
162 | | -# assert logs_url.startswith("https://console.aws.amazon.com/cloudwatch/home?region") |
163 | | -# assert not re.search("(=;|=$)", logs_url) |
164 | | -# assert error["extra"]["cloudwatch logs"]["log_group"].startswith( |
165 | | -# "/aws/lambda/test_" |
166 | | -# ) |
167 | | - |
168 | | -# log_stream_re = "^[0-9]{4}/[0-9]{2}/[0-9]{2}/\\[[^\\]]+][a-f0-9]+$" |
169 | | -# log_stream = error["extra"]["cloudwatch logs"]["log_stream"] |
170 | | - |
171 | | -# assert re.match(log_stream_re, log_stream) |
| 100 | +def test_basic_no_exception(lambda_client, test_environment): |
| 101 | + lambda_client.invoke( |
| 102 | + FunctionName="BasicOk", |
| 103 | + Payload=json.dumps({}), |
| 104 | + ) |
| 105 | + envelopes = test_environment["server"].envelopes |
| 106 | + |
| 107 | + (transaction_event,) = envelopes |
| 108 | + |
| 109 | + assert transaction_event["type"] == "transaction" |
| 110 | + assert transaction_event["transaction"] == "BasicOk" |
| 111 | + assert transaction_event["sdk"]["name"] == "sentry.python.aws_lambda" |
| 112 | + assert transaction_event["tags"] == {"aws_region": "us-east-1"} |
| 113 | + |
| 114 | + assert transaction_event["extra"]["cloudwatch logs"] == { |
| 115 | + "log_group": mock.ANY, |
| 116 | + "log_stream": mock.ANY, |
| 117 | + "url": mock.ANY, |
| 118 | + } |
| 119 | + assert transaction_event["extra"]["lambda"] == { |
| 120 | + "aws_request_id": mock.ANY, |
| 121 | + "execution_duration_in_millis": mock.ANY, |
| 122 | + "function_name": "BasicOk", |
| 123 | + "function_version": "$LATEST", |
| 124 | + "invoked_function_arn": "arn:aws:lambda:us-east-1:012345678912:function:BasicOk", |
| 125 | + "remaining_time_in_millis": mock.ANY, |
| 126 | + } |
| 127 | + assert transaction_event["contexts"]["trace"] == { |
| 128 | + "op": "function.aws", |
| 129 | + "description": mock.ANY, |
| 130 | + "span_id": mock.ANY, |
| 131 | + "parent_span_id": mock.ANY, |
| 132 | + "trace_id": mock.ANY, |
| 133 | + "origin": "auto.function.aws_lambda", |
| 134 | + "data": mock.ANY, |
| 135 | + } |
| 136 | + |
| 137 | + |
| 138 | +def test_basic_exception(lambda_client, test_environment): |
| 139 | + lambda_client.invoke( |
| 140 | + FunctionName="BasicException", |
| 141 | + Payload=json.dumps({}), |
| 142 | + ) |
| 143 | + envelopes = test_environment["server"].envelopes |
| 144 | + |
| 145 | + # The second envelope we ignore. |
| 146 | + # It is the transaction that we test in test_basic_no_exception. |
| 147 | + (error_event, _) = envelopes |
| 148 | + |
| 149 | + assert error_event["level"] == "error" |
| 150 | + assert error_event["exception"]["values"][0]["type"] == "RuntimeError" |
| 151 | + assert error_event["exception"]["values"][0]["value"] == "Oh!" |
| 152 | + assert error_event["sdk"]["name"] == "sentry.python.aws_lambda" |
| 153 | + |
| 154 | + assert error_event["tags"] == {"aws_region": "us-east-1"} |
| 155 | + assert error_event["extra"]["cloudwatch logs"] == { |
| 156 | + "log_group": mock.ANY, |
| 157 | + "log_stream": mock.ANY, |
| 158 | + "url": mock.ANY, |
| 159 | + } |
| 160 | + assert error_event["extra"]["lambda"] == { |
| 161 | + "aws_request_id": mock.ANY, |
| 162 | + "execution_duration_in_millis": mock.ANY, |
| 163 | + "function_name": "BasicException", |
| 164 | + "function_version": "$LATEST", |
| 165 | + "invoked_function_arn": "arn:aws:lambda:us-east-1:012345678912:function:BasicException", |
| 166 | + "remaining_time_in_millis": mock.ANY, |
| 167 | + } |
| 168 | + assert error_event["contexts"]["trace"] == { |
| 169 | + "op": "function.aws", |
| 170 | + "description": mock.ANY, |
| 171 | + "span_id": mock.ANY, |
| 172 | + "parent_span_id": mock.ANY, |
| 173 | + "trace_id": mock.ANY, |
| 174 | + "origin": "auto.function.aws_lambda", |
| 175 | + "data": mock.ANY, |
| 176 | + } |
0 commit comments