|
| 1 | +/* |
| 2 | +Copyright 2025 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package bbr |
| 18 | + |
| 19 | +import ( |
| 20 | + "encoding/json" |
| 21 | + |
| 22 | + envoyCorev3 "github.com/envoyproxy/go-control-plane/envoy/config/core/v3" |
| 23 | + extProcPb "github.com/envoyproxy/go-control-plane/envoy/service/ext_proc/v3" |
| 24 | +) |
| 25 | + |
| 26 | +// --- Response Expectations (Streaming) --- |
| 27 | + |
| 28 | +// ExpectBBRHeader asserts that BBR set the specific model header and cleared the route cache. |
| 29 | +func ExpectBBRHeader(modelName string) *extProcPb.ProcessingResponse { |
| 30 | + return &extProcPb.ProcessingResponse{ |
| 31 | + Response: &extProcPb.ProcessingResponse_RequestHeaders{ |
| 32 | + RequestHeaders: &extProcPb.HeadersResponse{ |
| 33 | + Response: &extProcPb.CommonResponse{ |
| 34 | + ClearRouteCache: true, |
| 35 | + HeaderMutation: &extProcPb.HeaderMutation{ |
| 36 | + SetHeaders: []*envoyCorev3.HeaderValueOption{ |
| 37 | + { |
| 38 | + Header: &envoyCorev3.HeaderValue{ |
| 39 | + Key: "X-Gateway-Model-Name", |
| 40 | + RawValue: []byte(modelName), |
| 41 | + }, |
| 42 | + }, |
| 43 | + }, |
| 44 | + }, |
| 45 | + }, |
| 46 | + }, |
| 47 | + }, |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +// ExpectBBRBodyPassThrough asserts that BBR reconstructs and passes the body through. |
| 52 | +// BBR buffers the body to inspect it, then sends it downstream as a single chunk (usually). |
| 53 | +func ExpectBBRBodyPassThrough(prompt, model string) *extProcPb.ProcessingResponse { |
| 54 | + j := map[string]any{ |
| 55 | + "max_tokens": 100, "prompt": prompt, "temperature": 0, |
| 56 | + } |
| 57 | + if model != "" { |
| 58 | + j["model"] = model |
| 59 | + } |
| 60 | + b, _ := json.Marshal(j) |
| 61 | + |
| 62 | + return &extProcPb.ProcessingResponse{ |
| 63 | + Response: &extProcPb.ProcessingResponse_RequestBody{ |
| 64 | + RequestBody: &extProcPb.BodyResponse{ |
| 65 | + Response: &extProcPb.CommonResponse{ |
| 66 | + BodyMutation: &extProcPb.BodyMutation{ |
| 67 | + Mutation: &extProcPb.BodyMutation_StreamedResponse{ |
| 68 | + StreamedResponse: &extProcPb.StreamedBodyResponse{ |
| 69 | + Body: b, |
| 70 | + EndOfStream: true, |
| 71 | + }, |
| 72 | + }, |
| 73 | + }, |
| 74 | + }, |
| 75 | + }, |
| 76 | + }, |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +// ExpectBBRNoOpHeader asserts that BBR did nothing to the headers (e.g., when no model is found). |
| 81 | +func ExpectBBRNoOpHeader() *extProcPb.ProcessingResponse { |
| 82 | + return &extProcPb.ProcessingResponse{ |
| 83 | + Response: &extProcPb.ProcessingResponse_RequestHeaders{ |
| 84 | + RequestHeaders: &extProcPb.HeadersResponse{}, |
| 85 | + }, |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +// --- Response Expectations (Unary) --- |
| 90 | + |
| 91 | +// ExpectBBRUnaryResponse creates expected response for unary tests where the body is mutated directly. |
| 92 | +func ExpectBBRUnaryResponse(modelName string) *extProcPb.ProcessingResponse { |
| 93 | + resp := &extProcPb.ProcessingResponse{} |
| 94 | + |
| 95 | + // If modelName is present, we expect header mutations. |
| 96 | + if modelName != "" { |
| 97 | + resp.Response = &extProcPb.ProcessingResponse_RequestBody{ |
| 98 | + RequestBody: &extProcPb.BodyResponse{ |
| 99 | + Response: &extProcPb.CommonResponse{ |
| 100 | + ClearRouteCache: true, |
| 101 | + HeaderMutation: &extProcPb.HeaderMutation{ |
| 102 | + SetHeaders: []*envoyCorev3.HeaderValueOption{ |
| 103 | + { |
| 104 | + Header: &envoyCorev3.HeaderValue{ |
| 105 | + Key: "X-Gateway-Model-Name", |
| 106 | + RawValue: []byte(modelName), |
| 107 | + }, |
| 108 | + }, |
| 109 | + }, |
| 110 | + }, |
| 111 | + }, |
| 112 | + }, |
| 113 | + } |
| 114 | + } else { |
| 115 | + // Otherwise, expect a No-Op on the body. |
| 116 | + resp.Response = &extProcPb.ProcessingResponse_RequestBody{ |
| 117 | + RequestBody: &extProcPb.BodyResponse{}, |
| 118 | + } |
| 119 | + } |
| 120 | + return resp |
| 121 | +} |
0 commit comments