/
test_lambda_handler.py
297 lines (256 loc) · 8 KB
/
test_lambda_handler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# Copyright 2010 New Relic, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import functools
from copy import deepcopy
import pytest
from testing_support.fixtures import override_application_settings
from testing_support.validators.validate_transaction_event_attributes import (
validate_transaction_event_attributes,
)
from testing_support.validators.validate_transaction_trace_attributes import (
validate_transaction_trace_attributes,
)
from newrelic.api import lambda_handler
# NOTE: this fixture will force all tests in this file to assume that a cold
# start has occurred, *except* when a test has a parameter named
# "is_cold" and its value is True
@pytest.fixture(autouse=True)
def force_cold_start_status(request):
try:
is_cold_start = request.getfixturevalue("is_cold")
lambda_handler.COLD_START_RECORDED = not is_cold_start
except Exception:
lambda_handler.COLD_START_RECORDED = True
@lambda_handler.lambda_handler()
def handler(event, context):
return {
"statusCode": "200",
"body": "{}",
"headers": {
"Content-Type": "application/json",
"Content-Length": 2,
},
}
_override_settings = {
"attributes.include": ["request.parameters.*", "request.headers.*"],
}
_expected_attributes = {
"agent": [
"aws.requestId",
"aws.lambda.arn",
"request.method",
"request.uri",
"response.status",
"response.headers.contentType",
"response.headers.contentLength",
],
"user": [],
"intrinsic": [],
}
_exact_attrs = {
"agent": {
"request.parameters.foo": "bar",
"request.headers.host": "myhost",
},
"user": {},
"intrinsic": {},
}
empty_event = {}
firehose_event = {
"records": [
{
"recordId": "495469866831355442",
"data": "SGVsbG8sIHRoaXMgaXMgYSB0ZXN0IDEyMy4=",
"approximateArrivalTimestamp": 1495072949453,
}
],
"region": "us-west-2",
"deliveryStreamArn": "arn:aws:kinesis:EXAMPLE",
"invocationId": "invocationIdExample",
}
class Context(object):
aws_request_id = "cookies"
invoked_function_arn = "arn"
function_name = "cats"
function_version = "$LATEST"
memory_limit_in_mb = 128
@pytest.mark.parametrize("is_cold", (False, True))
def test_lambda_transaction_attributes(is_cold, monkeypatch):
# setup copies of the attribute lists for this test only
_forgone_params = {}
_exact = deepcopy(_exact_attrs)
_expected = deepcopy(_expected_attributes)
# if we have a cold start, then we should see aws.lambda.coldStart=True
if is_cold:
_exact["agent"]["aws.lambda.coldStart"] = True
_expected["agent"].append("aws.lambda.coldStart")
# otherwise, then we need to make sure that we don't see it at all
else:
_forgone_params = {"agent": ["aws.lambda.coldStart"], "user": [], "intrinsic": []}
@validate_transaction_trace_attributes(required_params=_expected, forgone_params=_forgone_params)
@validate_transaction_event_attributes(
required_params=_expected, forgone_params=_forgone_params, exact_attrs=_exact
)
@override_application_settings(_override_settings)
def _test():
monkeypatch.setenv("AWS_REGION", "earth")
handler(
{
"httpMethod": "GET",
"path": "/",
"headers": {
"HOST": "myhost",
},
"queryStringParameters": {"foo": "bar"},
"multiValueQueryStringParameters": {"foo": ["bar"]},
},
Context,
)
_test()
@validate_transaction_trace_attributes(_expected_attributes)
@validate_transaction_event_attributes(_expected_attributes)
@override_application_settings(_override_settings)
def test_lambda_malformed_api_gateway_payload(monkeypatch):
monkeypatch.setenv("AWS_REGION", "earth")
handler(
{
"httpMethod": "GET",
"path": "/",
"headers": {},
"queryStringParameters": 42,
"multiValueQueryStringParameters": 42,
},
Context,
)
_malformed_request_attributes = {
"agent": [
"aws.requestId",
"aws.lambda.arn",
],
"user": [],
"intrinsic": [],
}
@validate_transaction_trace_attributes(_malformed_request_attributes)
@validate_transaction_event_attributes(_malformed_request_attributes)
@override_application_settings(_override_settings)
def test_lambda_malformed_request_headers():
handler(
{
"httpMethod": "GET",
"path": "/",
"headers": None,
},
Context,
)
_malformed_response_attributes = {
"agent": [
"aws.requestId",
"aws.lambda.arn",
"request.method",
"request.uri",
"response.status",
],
"user": [],
"intrinsic": [],
}
@validate_transaction_trace_attributes(_malformed_response_attributes)
@validate_transaction_event_attributes(_malformed_response_attributes)
@override_application_settings(_override_settings)
def test_lambda_malformed_response_headers():
@lambda_handler.lambda_handler()
def handler(event, context):
return {
"statusCode": 200,
"body": "{}",
"headers": None,
}
handler(
{
"httpMethod": "GET",
"path": "/",
"headers": {},
},
Context,
)
_no_status_code_response = {
"agent": [
"aws.requestId",
"aws.lambda.arn",
"request.method",
"request.uri",
"response.headers.contentType",
"response.headers.contentLength",
],
"user": [],
"intrinsic": [],
}
@validate_transaction_trace_attributes(_no_status_code_response)
@validate_transaction_event_attributes(_no_status_code_response)
@override_application_settings(_override_settings)
def test_lambda_no_status_code_response():
@lambda_handler.lambda_handler()
def handler(event, context):
return {
"body": "{}",
"headers": {
"Content-Type": "application/json",
"Content-Length": 2,
},
}
handler(
{
"httpMethod": "GET",
"path": "/",
"headers": {},
},
Context,
)
@pytest.mark.parametrize("event,arn", ((empty_event, None), (firehose_event, "arn:aws:kinesis:EXAMPLE")))
def test_lambda_event_source_arn_attribute(event, arn):
if arn is None:
_exact = None
_expected = None
_forgone = {
"user": [],
"intrinsic": [],
"agent": ["aws.lambda.eventSource.arn"],
}
else:
_exact = {
"user": {},
"intrinsic": {},
"agent": {"aws.lambda.eventSource.arn": arn},
}
_expected = {
"user": [],
"intrinsic": [],
"agent": ["aws.lambda.eventSource.arn"],
}
_forgone = None
@validate_transaction_trace_attributes(required_params=_expected, forgone_params=_forgone)
@validate_transaction_event_attributes(required_params=_expected, forgone_params=_forgone, exact_attrs=_exact)
@override_application_settings(_override_settings)
def _test():
handler(event, Context)
_test()
@pytest.mark.parametrize(
"api",
(
lambda_handler.lambda_handler,
functools.partial(lambda_handler.LambdaHandlerWrapper, handler),
),
)
def test_deprecation_warnings(api):
with pytest.deprecated_call():
api()