Skip to content

Commit

Permalink
e2e: ensures the route recalculated
Browse files Browse the repository at this point in the history
Signed-off-by: Takeshi Yoneda <t.y.mathetake@gmail.com>
  • Loading branch information
mathetake committed Jan 20, 2025
1 parent dd256b0 commit 7cff0b9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
29 changes: 16 additions & 13 deletions tests/e2e/translation_testupstream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,25 @@ func TestTranslationWithTestUpstream(t *testing.T) {

t.Run("/chat/completions", func(t *testing.T) {
for _, tc := range []struct {
name string
modelName string
expPath string
fakeResponseBody string
name string
modelName string
expTestUpstreamID string
expPath string
fakeResponseBody string
}{
{
name: "openai",
modelName: "some-cool-model",
expPath: "/v1/chat/completions",
fakeResponseBody: `{"choices":[{"message":{"content":"This is a test."}}]}`,
name: "openai",
modelName: "some-cool-model",
expTestUpstreamID: "primary",
expPath: "/v1/chat/completions",
fakeResponseBody: `{"choices":[{"message":{"content":"This is a test."}}]}`,
},
{
name: "aws-bedrock",
modelName: "another-cool-model",
expPath: "/model/another-cool-model/converse",
fakeResponseBody: `{"output":{"message":{"content":[{"text":"response"},{"text":"from"},{"text":"assistant"}],"role":"assistant"}},"stopReason":null,"usage":{"inputTokens":10,"outputTokens":20,"totalTokens":30}}`,
name: "aws-bedrock",
modelName: "another-cool-model",
expTestUpstreamID: "canary",
expPath: "/model/another-cool-model/converse",
fakeResponseBody: `{"output":{"message":{"content":[{"text":"response"},{"text":"from"},{"text":"assistant"}],"role":"assistant"}},"stopReason":null,"usage":{"inputTokens":10,"outputTokens":20,"totalTokens":30}}`,
},
} {
t.Run(tc.name, func(t *testing.T) {
Expand All @@ -58,7 +61,7 @@ func TestTranslationWithTestUpstream(t *testing.T) {
t.Logf("modelName: %s", tc.modelName)
client := openai.NewClient(option.WithBaseURL(fwd.address()+"/v1/"),
option.WithHeader(
"x-test-case-name", tc.name),
"x-expected-testupstream-id", tc.expTestUpstreamID),
option.WithHeader(
"x-expected-path", base64.StdEncoding.EncodeToString([]byte(tc.expPath))),
option.WithHeader("x-response-body",
Expand Down
15 changes: 15 additions & 0 deletions tests/testupstream/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ const (
// nonExpectedHeadersKey is the key for the non-expected request headers.
// The value is a base64 encoded string of comma separated header keys expected to be absent.
nonExpectedRequestHeadersKey = "x-non-expected-request-headers"

expectedTestUpstreamIDKey = "x-expected-testupstream-id"
)

// main starts a server that listens on port 1063 and responds with the expected response body and headers
Expand Down Expand Up @@ -170,6 +172,19 @@ func handler(w http.ResponseWriter, r *http.Request) {
fmt.Println("no non-expected headers in the request")
}

if v := r.Header.Get(expectedTestUpstreamIDKey); v != "" {
if os.Getenv("TESTUPSTREAM_ID") != v {
msg := fmt.Sprintf("unexpected testupstream-id: received by '%s' but expected '%s'\n", os.Getenv("TESTUPSTREAM_ID"), v)
fmt.Println(msg)
http.Error(w, msg, http.StatusBadRequest)
return
} else {
fmt.Println("testupstream-id matched:", v)
}
} else {
fmt.Println("no expected testupstream-id")
}

expectedPath, err := base64.StdEncoding.DecodeString(r.Header.Get(expectedPathHeaderKey))
if err != nil {
fmt.Println("failed to decode the expected path")
Expand Down

0 comments on commit 7cff0b9

Please sign in to comment.