-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_test.go
More file actions
210 lines (182 loc) · 6.58 KB
/
lambda_test.go
File metadata and controls
210 lines (182 loc) · 6.58 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
package dispatchlambda_test
import (
"context"
"encoding/base64"
"errors"
"testing"
sdkv1 "buf.build/gen/go/stealthrocket/dispatch-proto/protocolbuffers/go/dispatch/sdk/v1"
"github.com/aws/aws-lambda-go/lambda/messages"
"github.com/aws/aws-lambda-go/lambdacontext"
"github.com/dispatchrun/dispatch-go"
"github.com/dispatchrun/dispatch-go/dispatchlambda"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"
"google.golang.org/protobuf/types/known/wrapperspb"
)
func TestHandlerEmptyPayload(t *testing.T) {
fn := dispatch.Func("handler", func(ctx context.Context, input string) (string, error) {
return "", nil
})
h := dispatchlambda.Handler(fn)
_, err := h.Invoke(context.Background(), nil)
assertInvokeError(t, err, "Bad Request", "empty payload")
}
func TestHandlerShortPayload(t *testing.T) {
fn := dispatch.Func("handler", func(ctx context.Context, input string) (string, error) {
return "", nil
})
h := dispatchlambda.Handler(fn)
_, err := h.Invoke(context.Background(), []byte(`@`))
assertInvokeError(t, err, "Bad Request", "payload is too short")
}
func TestHandlerNonBase64Payload(t *testing.T) {
fn := dispatch.Func("handler", func(ctx context.Context, input string) (string, error) {
return "", nil
})
h := dispatchlambda.Handler(fn)
_, err := h.Invoke(context.Background(), []byte(`"not base64"`))
assertInvokeError(t, err, "Bad Request", "payload is not base64 encoded")
}
func TestHandlerInvokePayloadNotProtobufMessage(t *testing.T) {
fn := dispatch.Func("handler", func(ctx context.Context, input string) (string, error) {
return "", nil
})
h := dispatchlambda.Handler(fn)
ctx := lambdacontext.NewContext(context.Background(), &lambdacontext.LambdaContext{
InvokedFunctionArn: "arn:aws:lambda:us-east-1:123456789012:function:my-function:1",
})
_, err := h.Invoke(ctx, []byte(`"aW52b2tlZDovL2Z1bmN0aW9uOg=="`))
assertInvokeError(t, err, "Bad Request", "raw payload did not contain a protobuf encoded execution request")
}
func TestHandlerInvokeError(t *testing.T) {
fn := dispatch.Func("handler", func(ctx context.Context, input string) (string, error) {
return "", errors.New("invoke error")
})
h := dispatchlambda.Handler(fn)
ctx := lambdacontext.NewContext(context.Background(), &lambdacontext.LambdaContext{
InvokedFunctionArn: "arn:aws:lambda:us-east-1:123456789012:function:my-function:1",
})
input, err := anypb.New(&wrapperspb.StringValue{Value: "input"})
if err != nil {
t.Fatalf("unexpected error creating input: %v", err)
}
req := &sdkv1.RunRequest{
Function: "handler",
Directive: &sdkv1.RunRequest_Input{
Input: input,
},
}
b, err := proto.Marshal(req)
if err != nil {
t.Fatalf("unexpected error marshaling request: %v", err)
}
payload := make([]byte, 2+base64.StdEncoding.EncodedLen(len(b)))
payload[0] = '"'
payload[len(payload)-1] = '"'
base64.StdEncoding.Encode(payload[1:len(payload)-1], b)
b, err = h.Invoke(ctx, payload)
if err != nil {
t.Fatalf("unexpected error invoking function: %v", err)
}
payload = make([]byte, base64.StdEncoding.DecodedLen(len(b)-2))
n, err := base64.StdEncoding.Decode(payload, b[1:len(b)-1])
if err != nil {
t.Fatalf("unexpected error decoding payload: %v", err)
}
res := new(sdkv1.RunResponse)
if err := proto.Unmarshal(payload[:n], res); err != nil {
t.Fatalf("unexpected error unmarshaling result: %v", err)
}
switch coro := res.Directive.(type) {
case *sdkv1.RunResponse_Exit:
err := coro.Exit.GetResult().GetError()
if err.Type != "errorString" {
t.Errorf("expected coroutine to return an invoke error, got %q", err.Type)
}
if err.Message != "invoke error" {
t.Errorf("expected coroutine to return an invoke error with message %q, got %q", "invoke error", err.Message)
}
default:
t.Errorf("expected coroutine to return an error, got %T", coro)
}
}
func TestHandlerInvokeFunction(t *testing.T) {
fn := dispatch.Func("handler", func(ctx context.Context, input string) (string, error) {
return input + "output", nil
})
h := dispatchlambda.Handler(fn)
const (
functionVersion = "1"
unqualifiedFunctionARN = "arn:aws:lambda:us-east-1:123456789012:function:my-function"
qualifiedFunctionARN = unqualifiedFunctionARN + ":" + functionVersion
)
ctx := lambdacontext.NewContext(context.Background(), &lambdacontext.LambdaContext{
InvokedFunctionArn: qualifiedFunctionARN,
})
input, err := anypb.New(&wrapperspb.StringValue{Value: "input"})
if err != nil {
t.Fatalf("unexpected error creating input: %v", err)
}
req := &sdkv1.RunRequest{
Function: "handler",
Directive: &sdkv1.RunRequest_Input{
Input: input,
},
}
b, err := proto.Marshal(req)
if err != nil {
t.Fatalf("unexpected error marshaling request: %v", err)
}
payload := make([]byte, 2+base64.StdEncoding.EncodedLen(len(b)))
payload[0] = '"'
payload[len(payload)-1] = '"'
base64.StdEncoding.Encode(payload[1:len(payload)-1], b)
b, err = h.Invoke(ctx, payload)
if err != nil {
t.Fatalf("unexpected error invoking function: %v", err)
}
payload = make([]byte, base64.StdEncoding.DecodedLen(len(b)-2))
n, err := base64.StdEncoding.Decode(payload, b[1:len(b)-1])
if err != nil {
t.Fatalf("unexpected error decoding payload: %v", err)
}
res := new(sdkv1.RunResponse)
if err := proto.Unmarshal(payload[:n], res); err != nil {
t.Fatalf("unexpected error unmarshaling result: %v", err)
}
if res.Status != sdkv1.Status_STATUS_OK {
t.Errorf("expected coroutine to return status %q, got %q", sdkv1.Status_STATUS_OK, res.Status)
}
switch coro := res.Directive.(type) {
case *sdkv1.RunResponse_Exit:
out := coro.Exit.GetResult().GetOutput()
if out.GetTypeUrl() != "type.googleapis.com/google.protobuf.StringValue" {
t.Errorf("expected coroutine to return an output of type %q, got %q", "type.googleapis.com/google.protobuf.StringValue", out.GetTypeUrl())
}
var output wrapperspb.StringValue
if err := out.UnmarshalTo(&output); err != nil {
t.Fatalf("unexpected error unmarshaling output: %v", err)
}
if output.Value != "inputoutput" {
t.Errorf("expected coroutine to return an output with value %q, got %q", "inputoutput", output.Value)
}
default:
t.Errorf("expected coroutine to return an error, got %T", coro)
}
}
func assertInvokeError(t *testing.T, err error, typ, msg string) {
t.Helper()
var invokeErr messages.InvokeResponse_Error
if !errors.As(err, &invokeErr) {
t.Errorf("expected InvokeResponse_Error, got %T", err)
return
}
if invokeErr.Type != typ {
t.Errorf("expected error type %q, got %q", typ, invokeErr.Type)
return
}
if invokeErr.Message != msg {
t.Errorf("expected error message %q, got %q", msg, invokeErr.Message)
return
}
}