forked from kubernetes-sigs/gateway-api-inference-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest_test.go
231 lines (218 loc) · 6.29 KB
/
request_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
221
222
223
224
225
226
227
228
229
230
231
/*
Copyright 2025 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package handlers
import (
"context"
"encoding/json"
"strings"
"testing"
basepb "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
extProcPb "github.com/envoyproxy/go-control-plane/envoy/service/ext_proc/v3"
"github.com/google/go-cmp/cmp"
"google.golang.org/protobuf/testing/protocmp"
"k8s.io/component-base/metrics/legacyregistry"
metricsutils "k8s.io/component-base/metrics/testutil"
"sigs.k8s.io/gateway-api-inference-extension/pkg/body-based-routing/metrics"
logutil "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/util/logging"
)
func TestHandleRequestBody(t *testing.T) {
metrics.Register()
ctx := logutil.NewTestLoggerIntoContext(context.Background())
tests := []struct {
name string
body map[string]any
streaming bool
want []*extProcPb.ProcessingResponse
wantErr bool
}{
{
name: "model not found",
body: map[string]any{
"prompt": "Tell me a joke",
},
want: []*extProcPb.ProcessingResponse{
{
Response: &extProcPb.ProcessingResponse_RequestBody{
RequestBody: &extProcPb.BodyResponse{},
},
},
},
},
{
name: "model not found with streaming",
body: map[string]any{
"prompt": "Tell me a joke",
},
streaming: true,
want: []*extProcPb.ProcessingResponse{
{
Response: &extProcPb.ProcessingResponse_RequestHeaders{
RequestHeaders: &extProcPb.HeadersResponse{},
},
},
{
Response: &extProcPb.ProcessingResponse_RequestBody{
RequestBody: &extProcPb.BodyResponse{
Response: &extProcPb.CommonResponse{
BodyMutation: &extProcPb.BodyMutation{
Mutation: &extProcPb.BodyMutation_StreamedResponse{
StreamedResponse: &extProcPb.StreamedBodyResponse{
Body: mapToBytes(t, map[string]any{
"prompt": "Tell me a joke",
}),
EndOfStream: true,
},
},
},
},
},
},
},
},
},
{
name: "model is not string",
body: map[string]any{
"model": 1,
"prompt": "Tell me a joke",
},
wantErr: true,
},
{
name: "success",
body: map[string]any{
"model": "foo",
"prompt": "Tell me a joke",
},
want: []*extProcPb.ProcessingResponse{
{
Response: &extProcPb.ProcessingResponse_RequestBody{
RequestBody: &extProcPb.BodyResponse{
Response: &extProcPb.CommonResponse{
// Necessary so that the new headers are used in the routing decision.
ClearRouteCache: true,
HeaderMutation: &extProcPb.HeaderMutation{
SetHeaders: []*basepb.HeaderValueOption{
{
Header: &basepb.HeaderValue{
Key: "X-Gateway-Model-Name",
RawValue: []byte("foo"),
},
},
{
Header: &basepb.HeaderValue{
Key: "X-Gateway-BBR",
RawValue: []byte("true"),
},
},
},
},
},
},
},
},
},
},
{
name: "success-with-streaming",
body: map[string]any{
"model": "foo",
"prompt": "Tell me a joke",
},
streaming: true,
want: []*extProcPb.ProcessingResponse{
{
Response: &extProcPb.ProcessingResponse_RequestHeaders{
RequestHeaders: &extProcPb.HeadersResponse{
Response: &extProcPb.CommonResponse{
ClearRouteCache: true,
HeaderMutation: &extProcPb.HeaderMutation{
SetHeaders: []*basepb.HeaderValueOption{
{
Header: &basepb.HeaderValue{
Key: "X-Gateway-Model-Name",
RawValue: []byte("foo"),
},
},
{
Header: &basepb.HeaderValue{
Key: "X-Gateway-BBR",
RawValue: []byte("true"),
},
},
},
},
},
},
},
},
{
Response: &extProcPb.ProcessingResponse_RequestBody{
RequestBody: &extProcPb.BodyResponse{
Response: &extProcPb.CommonResponse{
BodyMutation: &extProcPb.BodyMutation{
Mutation: &extProcPb.BodyMutation_StreamedResponse{
StreamedResponse: &extProcPb.StreamedBodyResponse{
Body: mapToBytes(t, map[string]any{
"model": "foo",
"prompt": "Tell me a joke",
}),
EndOfStream: true,
},
},
},
},
},
},
},
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
server := &Server{streaming: test.streaming}
resp, err := server.HandleRequestBody(ctx, test.body)
if err != nil {
if !test.wantErr {
t.Fatalf("HandleRequestBody returned unexpected error: %v, want %v", err, test.wantErr)
}
return
}
if diff := cmp.Diff(test.want, resp, protocmp.Transform()); diff != "" {
t.Errorf("HandleRequestBody returned unexpected response, diff(-want, +got): %v", diff)
}
})
}
wantMetrics := `
# HELP bbr_model_not_in_body_total [ALPHA] Count of times the model was not present in the request body.
# TYPE bbr_model_not_in_body_total counter
bbr_model_not_in_body_total{} 1
# HELP bbr_model_not_parsed_total [ALPHA] Count of times the model was in the request body but we could not parse it.
# TYPE bbr_model_not_parsed_total counter
bbr_model_not_parsed_total{} 1
# HELP bbr_success_total [ALPHA] Count of successes pulling model name from body and injecting it in the request headers.
# TYPE bbr_success_total counter
bbr_success_total{} 1
`
if err := metricsutils.GatherAndCompare(legacyregistry.DefaultGatherer, strings.NewReader(wantMetrics), "inference_model_request_total"); err != nil {
t.Error(err)
}
}
func mapToBytes(t *testing.T, m map[string]any) []byte {
// Convert map to JSON byte array
bytes, err := json.Marshal(m)
if err != nil {
t.Fatalf("Marshal(): %v", err)
}
return bytes
}