-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathinvocationerror_test.go
More file actions
425 lines (331 loc) · 16.4 KB
/
invocationerror_test.go
File metadata and controls
425 lines (331 loc) · 16.4 KB
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package handler
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"go.amzn.com/lambda/appctx"
"go.amzn.com/lambda/fatalerror"
"go.amzn.com/lambda/interop"
"go.amzn.com/lambda/rapi/model"
"go.amzn.com/lambda/testdata"
"github.com/go-chi/chi/v5"
"github.com/stretchr/testify/assert"
)
// TestInvocationErrorHandler tests that API handler for
// invocation-time errors receives and passes information
// through to the Slicer unmodified.
func TestInvocationErrorHandler(t *testing.T) {
t.Run("GA", func(t *testing.T) { runTestInvocationErrorHandler(t) })
}
func addInvocationID(r *http.Request, invokeID string) *http.Request {
rctx := chi.NewRouteContext()
rctx.URLParams.Add("awsrequestid", invokeID)
return r.WithContext(context.WithValue(r.Context(), chi.RouteCtxKey, rctx))
}
func runTestInvocationErrorHandler(t *testing.T) {
flowTest := testdata.NewFlowTest()
flowTest.ConfigureForInit()
flowTest.Runtime.Ready()
handler := NewInvocationErrorHandler(flowTest.RegistrationService)
responseRecorder := httptest.NewRecorder()
appCtx := flowTest.AppCtx
// Invoke that we are sending response for must be placed into appCtx.
invoke := &interop.Invoke{
ID: "InvocationID1",
InvokedFunctionArn: "arn::dummy1",
CognitoIdentityID: "CognitoidentityID1",
CognitoIdentityPoolID: "CognitoidentityPollID1",
DeadlineNs: "deadlinens1",
ClientContext: "clientcontext1",
ContentType: "image/png",
Payload: strings.NewReader("Payload1"),
}
flowTest.ConfigureForInvoke(context.Background(), invoke)
// Error request, as submitted by custom runtime.
errorBody := []byte("My byte array is yours")
errorType := "ErrorType"
errorContentType := "application/MyBinaryType"
request := appctx.RequestWithAppCtx(httptest.NewRequest("POST", "/", bytes.NewReader(errorBody)), appCtx)
request = addInvocationID(request, invoke.ID)
request.Header.Set("Content-Type", errorContentType)
request.Header.Set("Lambda-runtime-functioN-erroR-typE", errorType) // Headers are case-insensitive anyway !
// Submit !
handler.ServeHTTP(responseRecorder, request)
// Assertions
// Validate response sent to the runtime.
assert.Equal(t, http.StatusAccepted, responseRecorder.Code, "Handler returned wrong status code: got %v expected %v",
responseRecorder.Code, http.StatusAccepted)
assert.JSONEq(t, fmt.Sprintf("{\"status\":\"%s\"}\n", "OK"), responseRecorder.Body.String())
assert.Equal(t, "application/json", responseRecorder.Header().Get("Content-Type"))
errorResponse := flowTest.InteropServer.ErrorResponse
assert.NotNil(t, errorResponse)
assert.Nil(t, flowTest.InteropServer.Response)
// Slicer falls back to using ErrorMessage when error
// payload is not provided. This fallback is not part
// of the RAPID API spec and is not available to
// customers.
assert.Equal(t, "", errorResponse.FunctionError.Message)
// Slicer falls back to using ErrorType when error
// payload is not provided. Customers can set error
// type header to use this fallback.
assert.Equal(t, fatalerror.RuntimeUnknown, errorResponse.FunctionError.Type)
// Payload is arbitrary data that customers submit - it's error response body.
assert.Equal(t, errorBody, errorResponse.Payload)
}
func TestInvocationErrorHandlerRemovesErrorCauseFromResponse(t *testing.T) {
flowTest := testdata.NewFlowTest()
flowTest.ConfigureForInit()
flowTest.Runtime.Ready()
handler := NewInvocationErrorHandler(flowTest.RegistrationService)
responseRecorder := httptest.NewRecorder()
appCtx := flowTest.AppCtx
invoke := &interop.Invoke{ID: "InvocationID1"}
flowTest.ConfigureForInvoke(context.Background(), invoke)
// Error request, as submitted by custom runtime.
errMsg, errType := "foo", "foo"
errorCause := json.RawMessage(`{"paths":[],"working_directory":[],"exceptions":[]}`)
errorWithCause := errorWithCauseRequest{
ErrorMessage: errMsg,
ErrorType: errType,
ErrorCause: errorCause,
}
requestBody, err := json.Marshal(errorWithCause)
assert.NoError(t, err, "error while creating test request")
errorContentType := errorWithCauseContentType
request := appctx.RequestWithAppCtx(httptest.NewRequest("POST", "/", bytes.NewReader(requestBody)), appCtx)
request = addInvocationID(request, invoke.ID)
request.Header.Set("Content-Type", errorContentType)
handler.ServeHTTP(responseRecorder, request)
expectedResponsePayload := []byte(fmt.Sprintf(`{"errorMessage":"%s","errorType":"%s"}`, errMsg, errType))
errorResponse := flowTest.InteropServer.ErrorResponse
assert.NotNil(t, errorResponse)
assert.Nil(t, flowTest.InteropServer.Response)
// Payload is arbitrary data that customers submit - it's error response body.
assert.JSONEq(t, string(expectedResponsePayload), string(errorResponse.Payload))
}
//////////////////////////////////////////////
///// Tests for error.cause Content-Type /////
//////////////////////////////////////////////
func TestInvocationErrorHandlerSendsErrorCauseToXRayForContentTypeErrorCause(t *testing.T) {
flowTest := testdata.NewFlowTest()
flowTest.ConfigureForInit()
flowTest.Runtime.Ready()
handler := NewInvocationErrorHandler(flowTest.RegistrationService)
responseRecorder := httptest.NewRecorder()
appCtx := flowTest.AppCtx
errorCause := json.RawMessage(`{"paths":[],"working_directory":"/foo/bar/baz","exceptions":[]}`)
errorWithCause := errorWithCauseRequest{
ErrorMessage: "foo",
ErrorType: "bar",
ErrorCause: errorCause,
}
requestBody, err := json.Marshal(errorWithCause)
assert.NoError(t, err, "error while creating test request")
invoke := &interop.Invoke{TraceID: "Root=TraceID;Parent=ParentID;Sampled=1", ID: "InvokeID"}
request := httptest.NewRequest("POST", "/", bytes.NewReader(requestBody))
request = addInvocationID(request, invoke.ID)
request.Header.Set("Content-Type", errorWithCauseContentType)
// Corresponding invoke must be placed into appCtx.
flowTest.ConfigureForInvoke(context.Background(), invoke)
// Run
handler.ServeHTTP(responseRecorder, appctx.RequestWithAppCtx(request, appCtx))
// Assert error response contains error cause
invokeErrorTraceData := appctx.LoadInvokeErrorTraceData(appCtx)
assert.NotNil(t, invokeErrorTraceData)
assert.Nil(t, flowTest.InteropServer.Response)
assert.JSONEq(t, string(errorCause), string(invokeErrorTraceData.ErrorCause))
}
func TestInvocationErrorHandlerSendsNullErrorCauseWhenErrorCauseFormatIsInvalidOrEmptyForContentTypeErrorCause(t *testing.T) {
causes := []json.RawMessage{
json.RawMessage(`{"foobar":"baz"}`),
json.RawMessage(`""`),
}
for _, cause := range causes {
flowTest := testdata.NewFlowTest()
flowTest.ConfigureForInit()
flowTest.Runtime.Ready()
appCtx := flowTest.AppCtx
errorWithCause := errorWithCauseRequest{
ErrorMessage: "foo",
ErrorType: "bar",
ErrorCause: json.RawMessage(cause),
}
requestBody, err := json.Marshal(errorWithCause)
assert.NoError(t, err, "error while creating test request")
invoke := &interop.Invoke{TraceID: "Root=TraceID;Parent=ParentID;Sampled=1", ID: "InvokeID"}
request := httptest.NewRequest("POST", "/", bytes.NewReader(requestBody))
request = addInvocationID(request, invoke.ID)
request.Header.Set("Content-Type", errorWithCauseContentType)
// Corresponding invoke must be placed into appCtx.
flowTest.ConfigureForInvoke(context.Background(), invoke)
// Run
NewInvocationErrorHandler(flowTest.RegistrationService).ServeHTTP(httptest.NewRecorder(), appctx.RequestWithAppCtx(request, appCtx))
invokeErrorTraceData := appctx.LoadInvokeErrorTraceData(appCtx)
assert.NotNil(t, invokeErrorTraceData)
assert.Nil(t, flowTest.InteropServer.Response)
assert.Equal(t, json.RawMessage(nil), invokeErrorTraceData.ErrorCause)
}
}
func TestInvocationErrorHandlerSendsCompactedErrorCauseWhenErrorCauseIsTooLargeForContentTypeErrorCause(t *testing.T) {
flowTest := testdata.NewFlowTest()
flowTest.ConfigureForInit()
flowTest.Runtime.Ready()
appCtx := flowTest.AppCtx
cause := json.RawMessage(`{"working_directory": "` + strings.Repeat(`a`, model.MaxErrorCauseSizeBytes+1) + `"}`)
errorWithCause := errorWithCauseRequest{
ErrorMessage: "foo",
ErrorType: "bar",
ErrorCause: json.RawMessage(cause),
}
requestBody, err := json.Marshal(errorWithCause)
assert.NoError(t, err, "error while creating test request")
invoke := &interop.Invoke{TraceID: "Root=TraceID;Parent=ParentID;Sampled=1", ID: "InvokeID"}
request := httptest.NewRequest("POST", "/", bytes.NewReader(requestBody))
request = addInvocationID(request, invoke.ID)
request.Header.Set("Content-Type", errorWithCauseContentType)
// Corresponding invoke must be placed into appCtx.
flowTest.ConfigureForInvoke(context.Background(), invoke)
// Run
NewInvocationErrorHandler(flowTest.RegistrationService).ServeHTTP(httptest.NewRecorder(), appctx.RequestWithAppCtx(request, appCtx))
invokeErrorTraceData := appctx.LoadInvokeErrorTraceData(appCtx)
assert.NotNil(t, invokeErrorTraceData)
assert.Nil(t, flowTest.InteropServer.Response)
errorCauseJSON, err := model.ValidatedErrorCauseJSON(invokeErrorTraceData.ErrorCause)
assert.NoError(t, err, "expected cause sent x-ray to be valid")
assert.True(t, len(errorCauseJSON) < model.MaxErrorCauseSizeBytes, "expected cause to be compacted to size")
}
func TestInvocationResponsePayloadIsDefaultErrorMessageWhenRequestParsingFailsForContentTypeErrorCause(t *testing.T) {
flowTest := testdata.NewFlowTest()
flowTest.ConfigureForInit()
flowTest.Runtime.Ready()
appCtx := flowTest.AppCtx
invalidRequestBody := json.RawMessage(`{"foo":bar}`)
invoke := &interop.Invoke{TraceID: "Root=TraceID;Parent=ParentID;Sampled=1", ID: "InvokeID"}
request := httptest.NewRequest("POST", "/", bytes.NewReader(invalidRequestBody))
request = addInvocationID(request, invoke.ID)
request.Header.Set(contentTypeHeader, errorWithCauseContentType)
request.Header.Set(functionResponseModeHeader, "function-response-mode")
// Corresponding invoke must be placed into appCtx.
flowTest.ConfigureForInvoke(context.Background(), invoke)
// Run
NewInvocationErrorHandler(flowTest.RegistrationService).ServeHTTP(httptest.NewRecorder(), appctx.RequestWithAppCtx(request, appCtx))
invokeErrorTraceData := appctx.LoadInvokeErrorTraceData(appCtx)
assert.NotNil(t, invokeErrorTraceData)
assert.Nil(t, flowTest.InteropServer.Response)
assert.Equal(t, "application/octet-stream", flowTest.InteropServer.ResponseContentType)
assert.Equal(t, "function-response-mode", flowTest.InteropServer.FunctionResponseMode)
errorResponse := flowTest.InteropServer.ErrorResponse
invokeResponsePayload := errorResponse.Payload
expectedResponse, _ := json.Marshal(invalidErrorBodyMessage)
assert.Equal(t, invokeResponsePayload, expectedResponse)
}
//////////////////////////////////////////////
///// Tests for X-Ray Error-Cause header /////
//////////////////////////////////////////////
func TestInvocationErrorHandlerSendsErrorCauseToXRayWhenXRayErrorCauseHeaderIsSet(t *testing.T) {
flowTest := testdata.NewFlowTest()
flowTest.ConfigureForInit()
flowTest.Runtime.Ready()
appCtx := flowTest.AppCtx
invoke := &interop.Invoke{TraceID: "Root=TraceID;Parent=ParentID;Sampled=1", ID: "InvokeID"}
request := httptest.NewRequest("POST", "/", bytes.NewReader([]byte(`foo doesn't matter`)))
request = addInvocationID(request, invoke.ID)
errorCause := json.RawMessage(`{"paths":[],"working_directory":"/foo/bar/baz","exceptions":[]}`)
request.Header.Set(xrayErrorCauseHeaderName, string(errorCause))
// Corresponding invoke must be placed into appCtx.
flowTest.ConfigureForInvoke(context.Background(), invoke)
// Run
NewInvocationErrorHandler(flowTest.RegistrationService).ServeHTTP(httptest.NewRecorder(), appctx.RequestWithAppCtx(request, appCtx))
invokeErrorTraceData := appctx.LoadInvokeErrorTraceData(appCtx)
assert.NotNil(t, invokeErrorTraceData)
assert.Nil(t, flowTest.InteropServer.Response)
assert.JSONEq(t, string(errorCause), string(invokeErrorTraceData.ErrorCause))
}
func TestInvocationErrorHandlerSendsNilCauseToXRayWhenXRayErrorCauseHeaderContainsInvalidCause(t *testing.T) {
invalidCauses := []json.RawMessage{
json.RawMessage(`{invalid:json}`),
json.RawMessage(``),
}
for _, errorCause := range invalidCauses {
flowTest := testdata.NewFlowTest()
flowTest.ConfigureForInit()
flowTest.Runtime.Ready()
appCtx := flowTest.AppCtx
invoke := &interop.Invoke{TraceID: "Root=TraceID;Parent=ParentID;Sampled=1", ID: "InvokeID"}
request := httptest.NewRequest("POST", "/", bytes.NewReader([]byte(`foo doesn't matter`)))
request = addInvocationID(request, invoke.ID)
request.Header.Set(xrayErrorCauseHeaderName, string(errorCause))
// Corresponding invoke must be placed into appCtx.
flowTest.ConfigureForInvoke(context.Background(), invoke)
// Run
NewInvocationErrorHandler(flowTest.RegistrationService).ServeHTTP(httptest.NewRecorder(), appctx.RequestWithAppCtx(request, appCtx))
invokeErrorTraceData := appctx.LoadInvokeErrorTraceData(appCtx)
assert.NotNil(t, invokeErrorTraceData)
assert.Nil(t, flowTest.InteropServer.Response)
assert.Equal(t, json.RawMessage(nil), invokeErrorTraceData.ErrorCause)
}
}
func TestInvocationErrorHandlerSendsCompactedErrorCauseToXRayWhenXRayErrorCauseInHeaderIsTooLarge(t *testing.T) {
flowTest := testdata.NewFlowTest()
flowTest.ConfigureForInit()
flowTest.Runtime.Ready()
appCtx := flowTest.AppCtx
invoke := &interop.Invoke{TraceID: "Root=TraceID;Parent=ParentID;Sampled=1", ID: "InvokeID"}
request := httptest.NewRequest("POST", "/", bytes.NewReader([]byte(`foo doesn't matter`)))
request = addInvocationID(request, invoke.ID)
errorCause := json.RawMessage(`{"working_directory": "` + strings.Repeat(`a`, model.MaxErrorCauseSizeBytes+1) + `"}`)
request.Header.Set(xrayErrorCauseHeaderName, string(errorCause))
// Corresponding invoke must be placed into appCtx.
flowTest.ConfigureForInvoke(context.Background(), invoke)
// Run
NewInvocationErrorHandler(flowTest.RegistrationService).ServeHTTP(httptest.NewRecorder(), appctx.RequestWithAppCtx(request, appCtx))
invokeErrorTraceData := appctx.LoadInvokeErrorTraceData(appCtx)
assert.NotNil(t, invokeErrorTraceData)
assert.Nil(t, flowTest.InteropServer.Response)
errorCauseJSON, err := model.ValidatedErrorCauseJSON(invokeErrorTraceData.ErrorCause)
assert.NoError(t, err, "expected cause sent x-ray to be valid")
assert.True(t, len(errorCauseJSON) < model.MaxErrorCauseSizeBytes, "expected cause to be compacted to size")
}
func TestInvocationErrorHandlerSendsNilToXRayWhenXRayErrorCauseHeaderIsNotSet(t *testing.T) {
flowTest := testdata.NewFlowTest()
flowTest.ConfigureForInit()
flowTest.Runtime.Ready()
appCtx := flowTest.AppCtx
invoke := &interop.Invoke{TraceID: "Root=TraceID;Parent=ParentID;Sampled=1", ID: "InvokeID"}
request := httptest.NewRequest("POST", "/", bytes.NewReader([]byte(`foo doesn't matter`)))
request = addInvocationID(request, invoke.ID)
// Corresponding invoke must be placed into appCtx.
flowTest.ConfigureForInvoke(context.Background(), invoke)
// Run
NewInvocationErrorHandler(flowTest.RegistrationService).ServeHTTP(httptest.NewRecorder(), appctx.RequestWithAppCtx(request, appCtx))
invokeErrorTraceData := appctx.LoadInvokeErrorTraceData(appCtx)
assert.NotNil(t, invokeErrorTraceData)
assert.Nil(t, flowTest.InteropServer.Response)
assert.Nil(t, invokeErrorTraceData.ErrorCause)
}
func TestInvocationErrorHandlerSendsErrorCauseToXRayWhenXRayErrorCauseContainsUTF8Characters(t *testing.T) {
flowTest := testdata.NewFlowTest()
flowTest.ConfigureForInit()
flowTest.Runtime.Ready()
appCtx := flowTest.AppCtx
invoke := &interop.Invoke{TraceID: "Root=TraceID;Parent=ParentID;Sampled=1", ID: "InvokeID"}
request := httptest.NewRequest("POST", "/", bytes.NewReader([]byte(`foo doesn't matter`)))
request = addInvocationID(request, invoke.ID)
errorCause := json.RawMessage(`{"exceptions":[],"working_directory":"κόσμε","paths":[]}`)
request.Header.Set(xrayErrorCauseHeaderName, string(errorCause))
// Corresponding invoke must be placed into appCtx.
flowTest.ConfigureForInvoke(context.Background(), invoke)
// Run
NewInvocationErrorHandler(flowTest.RegistrationService).ServeHTTP(httptest.NewRecorder(), appctx.RequestWithAppCtx(request, appCtx))
invokeErrorTraceData := appctx.LoadInvokeErrorTraceData(appCtx)
assert.NotNil(t, invokeErrorTraceData)
assert.Nil(t, flowTest.InteropServer.Response)
assert.JSONEq(t, string(errorCause), string(invokeErrorTraceData.ErrorCause))
}