-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathevent_test.go
220 lines (201 loc) · 5.79 KB
/
event_test.go
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
package main
import (
"bytes"
"context"
"errors"
"fmt"
"log/slog"
"strings"
"testing"
"github.com/aws/aws-lambda-go/lambda/messages"
"github.com/lithammer/dedent"
"github.com/lmittmann/tint"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
// MockLambdaCaller is a mock implementation of the lambdaCaller interface
type MockLambdaCaller struct {
mock.Mock
}
func (m *MockLambdaCaller) Invoke(data []byte) (messages.InvokeResponse, error) {
args := m.Called(data)
return args.Get(0).(messages.InvokeResponse), args.Error(1) //nolint:wrapcheck,forcetypeassert
}
func TestRunLambdaEvent(t *testing.T) { //nolint:funlen
tests := map[string]struct {
event string
parseJSON bool
invokeResp messages.InvokeResponse
invokeErr error
expectedOutput string
expectedErr error
}{
"successful invocation without JSON parsing": {
event: `{"key": "value"}`,
parseJSON: false,
invokeResp: messages.InvokeResponse{
Payload: []byte(`{"response": "success"}`),
},
invokeErr: nil,
expectedOutput: `
---------------------------------------------------------------------------
0 INF Starting local Lambda invocation with Event
0 DBG Invoking lambda event
0 DBG Handling lambda event response
0 INF Lambda returned JSON payload:
{
"response": "success"
}
0 INF Lambda invocation complete, Exiting...
---------------------------------------------------------------------------
`,
expectedErr: nil,
},
"successful invocation with JSON parsing": {
event: `{"key": "value"}`,
parseJSON: true,
invokeResp: messages.InvokeResponse{
Payload: []byte(`{"response": "{\"innerKey\": \"innerValue\"}"}`),
},
invokeErr: nil,
expectedOutput: `
---------------------------------------------------------------------------
0 INF Starting local Lambda invocation with Event
0 DBG Invoking lambda event
0 DBG Handling lambda event response
0 INF Lambda returned JSON payload:
{
"response": {
"innerKey": "innerValue"
}
}
0 INF Lambda invocation complete, Exiting...
---------------------------------------------------------------------------
`,
expectedErr: nil,
},
"invoke error": {
event: `{"key": "value"}`,
parseJSON: false,
invokeResp: messages.InvokeResponse{},
invokeErr: errors.New("invoke error"),
expectedOutput: `
---------------------------------------------------------------------------
0 INF Starting local Lambda invocation with Event
0 DBG Invoking lambda event
`,
expectedErr: fmt.Errorf("[in lambdalocal.RunLambdaEvent] invoke failed: %w", errors.New("invoke error")),
},
"lambda returned error - valid JSON body": {
event: `{"key": "value"}`,
parseJSON: false,
invokeResp: messages.InvokeResponse{
Payload: []byte(`{"error": "test error"}`),
Error: &messages.InvokeResponse_Error{
Message: "test error",
Type: "",
StackTrace: []*messages.InvokeResponse_Error_StackFrame{
{
Path: "path1",
Line: 1,
Label: "part_1",
},
{
Path: "path2",
Line: 2,
Label: "part_2",
},
},
ShouldExit: false,
},
},
invokeErr: nil,
expectedOutput: `
---------------------------------------------------------------------------
0 INF Starting local Lambda invocation with Event
0 DBG Invoking lambda event
0 DBG Handling lambda event response
0 ERR Lambda returned error:
Returned error: test error
Stack trace:
path1:1 - part_1
path2:2 - part_2
0 INF Lambda returned JSON payload:
{
"error": "test error"
}
0 INF Lambda invocation complete, Exiting...
---------------------------------------------------------------------------
`,
expectedErr: nil,
},
"lambda returned error - invalid JSON body": {
event: `{"key": "value"}`,
parseJSON: false,
invokeResp: messages.InvokeResponse{
Payload: []byte(`invalid JSON`),
Error: &messages.InvokeResponse_Error{
Message: "test error",
Type: "",
StackTrace: []*messages.InvokeResponse_Error_StackFrame{
{
Path: "path1",
Line: 1,
Label: "part_1",
},
{
Path: "path2",
Line: 2,
Label: "part_2",
},
},
ShouldExit: false,
},
},
invokeErr: nil,
expectedOutput: `
---------------------------------------------------------------------------
0 INF Starting local Lambda invocation with Event
0 DBG Invoking lambda event
0 DBG Handling lambda event response
0 ERR Lambda returned error:
Returned error: test error
Stack trace:
path1:1 - part_1
path2:2 - part_2
0 INF Lambda returned non-JSON payload:
invalid JSON
0 INF Lambda invocation complete, Exiting...
---------------------------------------------------------------------------
`,
expectedErr: nil,
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
mockLambdaRPC := new(MockLambdaCaller)
var buf bytes.Buffer
logger := slog.New(
tint.NewHandler(
&buf, &tint.Options{
Level: slog.LevelDebug,
TimeFormat: "0",
NoColor: true,
},
),
)
mockLambdaRPC.On("Invoke", []byte(tc.event)).Return(tc.invokeResp, tc.invokeErr)
err := RunLambdaEvent(context.Background(), &buf, mockLambdaRPC, tc.event, tc.parseJSON, logger)
if tc.expectedErr == nil {
require.NoError(t, err)
} else {
assert.Equal(t, tc.expectedErr, err)
}
output := strings.TrimSpace(buf.String())
// fmt.Println(output)
assert.Equal(t, strings.TrimSpace(dedent.Dedent(tc.expectedOutput)), output)
mockLambdaRPC.AssertExpectations(t)
})
}
}