Repository navigation
Expand file tree
/
Copy pathinvocation_test.go
More file actions
201 lines (191 loc) · 6.58 KB
/
Copy pathinvocation_test.go
File metadata and controls
201 lines (191 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
package edge
import (
"context"
"errors"
"io"
"net/http"
"strings"
"testing"
"time"
"github.com/asteroid-computing/go-lambda-edge/identity"
)
func TestInvocationRejectsInheritedCaller(t *testing.T) {
caller, err := identity.NewIAM("arn:aws:iam::123456789012:user/Alice", identity.SourceGatewayAssertion)
if err != nil {
t.Fatal(err)
}
parent, err := identity.NewContext(t.Context(), caller)
if err != nil {
t.Fatal(err)
}
ran := false
err = withInvocation(parent, func(context.Context, *invocation) error {
ran = true
return nil
})
if ran || !errors.Is(err, ErrIdentity) || !errors.Is(err, identity.ErrConflict) {
t.Fatalf("inherited caller: ran=%t, err=%v; want identity conflict before work", ran, err)
}
if detail, ok := errors.AsType[*InvocationError](err); !ok || detail.Operation() != "identity" {
t.Errorf("inherited caller error = %v, want identity operation", err)
}
if strings.Contains(err.Error(), "Alice") {
t.Error("isolation error leaked caller identity")
}
if err := withInvocation(t.Context(), func(context.Context, *invocation) error { return nil }); err != nil {
t.Errorf("independent next invocation rejected: %v", err)
}
}
func TestInvocationRejectsInvalidContextBeforeRunning(t *testing.T) {
canceled, cancel := context.WithCancel(t.Context())
cancel()
expired, stop := context.WithDeadline(t.Context(), time.Unix(0, 0))
defer stop()
for _, tt := range []struct {
name string
ctx context.Context
want error
}{
{name: "nil"},
{name: "canceled", ctx: canceled, want: context.Canceled},
{name: "expired", ctx: expired, want: context.DeadlineExceeded},
} {
t.Run(tt.name, func(t *testing.T) {
ran := false
err := withInvocation(tt.ctx, func(context.Context, *invocation) error {
ran = true
return nil
})
if ran || err == nil || tt.want != nil && !errors.Is(err, tt.want) {
t.Errorf("withInvocation(%s) ran=%v err=%v, want no run and %v", tt.name, ran, err, tt.want)
}
})
}
}
func TestInvocationScopeIncludesAllStages(t *testing.T) {
type contextKey struct{}
parent := context.WithValue(t.Context(), contextKey{}, "invocation value")
var scope context.Context
var original *trackedBody
var replacement *trackedBody
err := withInvocation(parent, func(ctx context.Context, inv *invocation) error {
scope = ctx
// Conversion, identity, handler, and finalization all execute under ctx.
r, err := requestV2(ctx, v2RequestEvent())
if err != nil {
return err
}
original = &trackedBody{Reader: r.Body}
r.Body = original
inv.ownRequest(r)
if r.Context() != ctx || ctx.Err() != nil || ctx.Value(contextKey{}) != "invocation value" {
t.Error("converted request lost invocation scope/value")
}
replacement = &trackedBody{Reader: strings.NewReader("application-owned")}
r.Body = replacement
if original.closed || ctx.Err() != nil {
t.Error("scope/body closed before finalization")
}
_, err = marshalResponse(struct{ StatusCode int }{StatusCode: http.StatusOK})
return err
})
if err != nil {
t.Fatal(err)
}
if scope.Err() != context.Canceled || parent.Err() != nil || !original.closed || replacement.closed {
t.Errorf("completed scope: child=%v parent=%v original closed=%v replacement closed=%v", scope.Err(), parent.Err(), original.closed, replacement.closed)
}
}
type failingCloseBody struct {
io.Reader
closed bool
}
func (b *failingCloseBody) Close() error {
b.closed = true
return errors.New("private body credential or path")
}
func TestInvocationErrorPrecedence(t *testing.T) {
operationErr := errors.New("operation failed")
for _, tt := range []struct {
name string
operation error
cancel bool
failCleanup bool
}{
{name: "success"},
{name: "late_cancel", cancel: true},
{name: "operation_failure", operation: operationErr},
{name: "operation_and_cancel", operation: operationErr, cancel: true},
{name: "cleanup_failure", failCleanup: true},
{name: "cleanup_and_cancel", failCleanup: true, cancel: true},
{name: "operation_and_cleanup", operation: operationErr, failCleanup: true},
{name: "all_failures", operation: operationErr, failCleanup: true, cancel: true},
{name: "standard_http_error", operation: http.ErrContentLength},
} {
t.Run(tt.name, func(t *testing.T) {
parent, cancel := context.WithCancel(t.Context())
defer cancel()
var scope context.Context
body := &failingCloseBody{Reader: http.NoBody}
err := withInvocation(parent, func(ctx context.Context, inv *invocation) error {
scope = ctx
if tt.failCleanup {
inv.ownRequest(&http.Request{Body: body})
}
if tt.cancel {
cancel()
}
return tt.operation
})
wantError := tt.operation != nil || tt.failCleanup || tt.cancel
if (err != nil) != wantError {
t.Fatalf("withInvocation() = %v, want error %v", err, wantError)
}
if tt.operation != nil && !errors.Is(err, tt.operation) {
t.Errorf("error %v does not preserve operation %v", err, tt.operation)
}
wantCanceled := tt.cancel && tt.operation == nil && !tt.failCleanup
if errors.Is(err, context.Canceled) != wantCanceled {
t.Errorf("error %v matches cancellation=%v, want %v", err, errors.Is(err, context.Canceled), wantCanceled)
}
if tt.failCleanup && (!body.closed || strings.Contains(err.Error(), "private")) {
t.Errorf("cleanup closed=%v err=%v, want sanitized failure", body.closed, err)
}
if scope.Err() != context.Canceled {
t.Errorf("child after return = %v, want canceled", scope.Err())
}
})
}
}
func TestInvocationCleansUpBeforeHandler(t *testing.T) {
identityErr := errors.New("identity failed")
body := &trackedBody{Reader: http.NoBody}
var scope context.Context
err := withInvocation(t.Context(), func(ctx context.Context, inv *invocation) error {
scope = ctx
inv.ownRequest(&http.Request{Body: body})
return identityErr
})
if !errors.Is(err, identityErr) || !body.closed || scope.Err() != context.Canceled {
t.Errorf("pre-handler failure: err=%v closed=%v context=%v", err, body.closed, scope.Err())
}
}
func TestInvocationPreservesPanic(t *testing.T) {
panicValue := new("application panic")
body := &failingCloseBody{Reader: http.NoBody}
var scope context.Context
var recovered any
func() {
defer func() { recovered = recover() }()
if err := withInvocation(t.Context(), func(ctx context.Context, inv *invocation) error {
scope = ctx
inv.ownRequest(&http.Request{Body: body})
panic(panicValue)
}); err != nil {
t.Errorf("panicking invocation returned %v", err)
}
}()
if recovered != panicValue || !body.closed || scope.Err() != context.Canceled {
t.Errorf("panic result: value=%v closed=%v context=%v", recovered, body.closed, scope.Err())
}
}