forked from localstack/lambda-runtime-init
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandlers_test.go
More file actions
341 lines (268 loc) · 8.99 KB
/
handlers_test.go
File metadata and controls
341 lines (268 loc) · 8.99 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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package rapid
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"net/http/httptest"
"regexp"
"strconv"
"strings"
"sync"
"testing"
"time"
"go.amzn.com/lambda/appctx"
"go.amzn.com/lambda/core"
"go.amzn.com/lambda/fatalerror"
"go.amzn.com/lambda/interop"
"go.amzn.com/lambda/rapi"
"go.amzn.com/lambda/rapi/handler"
"go.amzn.com/lambda/rapi/rendering"
"go.amzn.com/lambda/rapidcore/env"
"go.amzn.com/lambda/supervisor/model"
"go.amzn.com/lambda/telemetry"
"go.amzn.com/lambda/testdata"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
func BenchmarkChannelsSelect10(b *testing.B) {
c1 := make(chan int)
c2 := make(chan int)
c3 := make(chan int)
c4 := make(chan int)
c5 := make(chan int)
c6 := make(chan int)
c7 := make(chan int)
c8 := make(chan int)
c9 := make(chan int)
c10 := make(chan int)
for n := 0; n < b.N; n++ {
select {
case <-c1:
case <-c2:
case <-c3:
case <-c4:
case <-c5:
case <-c6:
case <-c7:
case <-c8:
case <-c9:
case <-c10:
default:
}
}
}
func BenchmarkChannelsSelect2(b *testing.B) {
c1 := make(chan int)
c2 := make(chan int)
for n := 0; n < b.N; n++ {
select {
case <-c1:
case <-c2:
default:
}
}
}
func TestGetExtensionNamesWithNoExtensions(t *testing.T) {
rs := core.NewRegistrationService(nil, nil)
c := &rapidContext{
registrationService: rs,
}
assert.Equal(t, "", c.GetExtensionNames())
}
func TestGetExtensionNamesWithMultipleExtensions(t *testing.T) {
rs := core.NewRegistrationService(nil, nil)
_, _ = rs.CreateExternalAgent("Example1")
_, _ = rs.CreateInternalAgent("Example2")
_, _ = rs.CreateExternalAgent("Example3")
_, _ = rs.CreateInternalAgent("Example4")
c := &rapidContext{
registrationService: rs,
}
r := regexp.MustCompile(`^(Example\d;){3}(Example\d)$`)
assert.True(t, r.MatchString(c.GetExtensionNames()))
}
func TestGetExtensionNamesWithTooManyExtensions(t *testing.T) {
rs := core.NewRegistrationService(nil, nil)
for i := 10; i < 60; i++ {
_, _ = rs.CreateExternalAgent("E" + strconv.Itoa(i))
}
c := &rapidContext{
registrationService: rs,
}
output := c.GetExtensionNames()
r := regexp.MustCompile(`^(E\d\d;){30}(E\d\d)$`)
assert.LessOrEqual(t, len(output), maxExtensionNamesLength)
assert.True(t, r.MatchString(output))
}
func TestGetExtensionNamesWithTooLongExtensionName(t *testing.T) {
rs := core.NewRegistrationService(nil, nil)
for i := 10; i < 60; i++ {
_, _ = rs.CreateExternalAgent(strings.Repeat("E", 130))
}
c := &rapidContext{
registrationService: rs,
}
assert.Equal(t, "", c.GetExtensionNames())
}
// This test confirms our assumption that http client can establish a tcp connection
// to a listening server.
func TestListen(t *testing.T) {
flowTest := testdata.NewFlowTest()
flowTest.ConfigureForInit()
flowTest.ConfigureForInvoke(context.Background(), &interop.Invoke{ID: "ID", DeadlineNs: "1", Payload: strings.NewReader("MyTest")})
ctx := context.Background()
telemetryAPIEnabled := true
server := rapi.NewServer("127.0.0.1", 0, flowTest.AppCtx, flowTest.RegistrationService, flowTest.RenderingService, telemetryAPIEnabled, flowTest.TelemetrySubscription, flowTest.TelemetrySubscription, flowTest.CredentialsService)
err := server.Listen()
assert.NoError(t, err)
defer server.Close()
go func() {
time.Sleep(time.Second)
fmt.Println("Serving...")
server.Serve(ctx)
}()
done := make(chan struct{})
go func() {
fmt.Println("Connecting...")
resp, err1 := http.Get(fmt.Sprintf("http://%s:%d/2018-06-01/runtime/invocation/next", server.Host(), server.Port()))
assert.Nil(t, err1)
body, err2 := io.ReadAll(resp.Body)
assert.Nil(t, err2)
assert.Equal(t, "MyTest", string(body))
done <- struct{}{}
}()
<-done
}
func makeRapidContext(appCtx appctx.ApplicationContext, initFlow core.InitFlowSynchronization, invokeFlow core.InvokeFlowSynchronization, registrationService core.RegistrationService, supervisor *processSupervisor) *rapidContext {
appctx.StoreInitType(appCtx, true)
appctx.StoreInteropServer(appCtx, MockInteropServer{})
renderingService := rendering.NewRenderingService()
credentialsService := core.NewCredentialsService()
credentialsService.SetCredentials("token", "key", "secret", "session", time.Now())
// Runtime state machine
runtime := core.NewRuntime(initFlow, invokeFlow)
registrationService.PreregisterRuntime(runtime)
runtime.SetState(runtime.RuntimeRestoreReadyState)
rapidCtx := &rapidContext{
// Internally initialized configurations
appCtx: appCtx,
initDone: true,
initFlow: initFlow,
invokeFlow: invokeFlow,
registrationService: registrationService,
renderingService: renderingService,
credentialsService: credentialsService,
handlerExecutionMutex: sync.Mutex{},
shutdownContext: newShutdownContext(),
eventsAPI: &telemetry.NoOpEventsAPI{},
}
if supervisor != nil {
rapidCtx.supervisor = *supervisor
}
return rapidCtx
}
const hookErrorType = "Runtime.RestoreHookUserErrorType"
func makeRequest(appCtx appctx.ApplicationContext) *http.Request {
errorBody := []byte("My byte array is yours")
request := appctx.RequestWithAppCtx(httptest.NewRequest("POST", "/", bytes.NewReader(errorBody)), appCtx)
request.Header.Set("Content-Type", "application/MyBinaryType")
request.Header.Set("Lambda-Runtime-Function-Error-Type", hookErrorType)
return request
}
type MockInteropServer struct{}
func (server MockInteropServer) GetCurrentInvokeID() string {
return ""
}
func (server MockInteropServer) SendRuntimeReady() error {
return nil
}
func (server MockInteropServer) SendInitErrorResponse(response *interop.ErrorInvokeResponse) error {
return nil
}
func TestRestoreErrorAndAwaitRestoreCompletionRaceCondition(t *testing.T) {
appCtx := appctx.NewApplicationContext()
initFlow := core.NewInitFlowSynchronization()
invokeFlow := core.NewInvokeFlowSynchronization()
registrationService := core.NewRegistrationService(initFlow, invokeFlow)
rapidCtx := makeRapidContext(appCtx, initFlow, invokeFlow, registrationService, nil /* don't set process supervisor */)
// Runtime state machine
runtime := core.NewRuntime(initFlow, invokeFlow)
registrationService.PreregisterRuntime(runtime)
runtime.SetState(runtime.RuntimeRestoreReadyState)
restore := &interop.Restore{
AwsKey: "key",
AwsSecret: "secret",
AwsSession: "session",
CredentialsExpiry: time.Now(),
RestoreHookTimeoutMs: 10 * 1000,
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
_, err := rapidCtx.HandleRestore(restore)
assert.Equal(t, err.Error(), "errRestoreHookUserError")
v, ok := err.(interop.ErrRestoreHookUserError)
assert.True(t, ok)
assert.Equal(t, v.UserError.Type, fatalerror.ErrorType(hookErrorType))
}()
responseRecorder := httptest.NewRecorder()
handler := handler.NewRestoreErrorHandler(registrationService)
request := makeRequest(appCtx)
wg.Add(1)
time.Sleep(1 * time.Second)
runtime.SetState(runtime.RuntimeRestoringState)
go func() {
defer wg.Done()
handler.ServeHTTP(responseRecorder, request)
}()
wg.Wait()
}
type MockedProcessSupervisor struct {
mock.Mock
}
func (supv *MockedProcessSupervisor) Exec(ctx context.Context, req *model.ExecRequest) error {
args := supv.Called(req)
return args.Error(0)
}
func (supv *MockedProcessSupervisor) Events(ctx context.Context, req *model.EventsRequest) (<-chan model.Event, error) {
args := supv.Called(req)
err := args.Error(1)
if err != nil {
return nil, err
}
return args.Get(0).(<-chan model.Event), nil
}
func (supv *MockedProcessSupervisor) Terminate(ctx context.Context, req *model.TerminateRequest) error {
args := supv.Called(req)
return args.Error(0)
}
func (supv *MockedProcessSupervisor) Kill(ctx context.Context, req *model.KillRequest) error {
args := supv.Called(req)
return args.Error(0)
}
var _ model.ProcessSupervisor = (*MockedProcessSupervisor)(nil)
func TestSetupEventWatcherErrorHandling(t *testing.T) {
appCtx := appctx.NewApplicationContext()
initFlow := core.NewInitFlowSynchronization()
invokeFlow := core.NewInvokeFlowSynchronization()
registrationService := core.NewRegistrationService(initFlow, invokeFlow)
mockedProcessSupervisor := &MockedProcessSupervisor{}
mockedProcessSupervisor.On("Events", mock.Anything).Return(nil, fmt.Errorf("events call failed"))
procSupv := &processSupervisor{ProcessSupervisor: mockedProcessSupervisor}
rapidCtx := makeRapidContext(appCtx, initFlow, invokeFlow, registrationService, procSupv)
initSuccessResponseChan := make(chan interop.InitSuccess)
initFailureResponseChan := make(chan interop.InitFailure)
init := &interop.Init{EnvironmentVariables: env.NewEnvironment()}
go assert.NotPanics(t, func() {
rapidCtx.HandleInit(init, initSuccessResponseChan, initFailureResponseChan)
})
failure := <-initFailureResponseChan
failure.Ack <- struct{}{}
errorType := interop.InitFailure(failure).ErrorType
assert.Equal(t, fatalerror.SandboxFailure, errorType)
}