|
| 1 | +/* |
| 2 | +Copyright 2025 The Aibrix Team. |
| 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 routingalgorithms |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "encoding/base64" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/stretchr/testify/assert" |
| 25 | + "github.com/vllm-project/aibrix/pkg/types" |
| 26 | + |
| 27 | + v1 "k8s.io/api/core/v1" |
| 28 | +) |
| 29 | + |
| 30 | +func TestSessionAffinityRouter(t *testing.T) { |
| 31 | + tests := []struct { |
| 32 | + name string |
| 33 | + reqHeaders map[string]string |
| 34 | + readyPods []*v1.Pod |
| 35 | + expectErr bool |
| 36 | + expectPossibleAddrs []string // all valid target addresses (IP:port) that may be selected |
| 37 | + }{ |
| 38 | + { |
| 39 | + name: "valid session ID matches ready pod", |
| 40 | + reqHeaders: map[string]string{ |
| 41 | + sessionIDHeader: base64.StdEncoding.EncodeToString([]byte("10.0.0.2:8000")), |
| 42 | + }, |
| 43 | + readyPods: []*v1.Pod{ |
| 44 | + newPod("pod1", "10.0.0.1", true, map[string]string{"model.aibrix.ai/port": "8000"}), |
| 45 | + newPod("pod2", "10.0.0.2", true, map[string]string{"model.aibrix.ai/port": "8000"}), |
| 46 | + newPod("pod3", "10.0.0.3", true, map[string]string{"model.aibrix.ai/port": "8000"}), |
| 47 | + }, |
| 48 | + expectErr: false, |
| 49 | + expectPossibleAddrs: []string{"10.0.0.2:8000"}, |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "no session ID → fallback to any ready pod", |
| 53 | + reqHeaders: nil, |
| 54 | + readyPods: []*v1.Pod{ |
| 55 | + newPod("pod1", "10.0.0.1", true, map[string]string{"model.aibrix.ai/port": "8000"}), |
| 56 | + newPod("pod2", "10.0.0.2", true, map[string]string{"model.aibrix.ai/port": "8000"}), |
| 57 | + }, |
| 58 | + expectErr: false, |
| 59 | + expectPossibleAddrs: []string{"10.0.0.1:8000", "10.0.0.2:8000"}, |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "invalid base64 session ID → fallback", |
| 63 | + reqHeaders: map[string]string{ |
| 64 | + sessionIDHeader: "%%%INVALID_BASE64%%%", |
| 65 | + }, |
| 66 | + readyPods: []*v1.Pod{ |
| 67 | + newPod("a", "192.168.1.10", true, map[string]string{"model.aibrix.ai/port": "8000"}), |
| 68 | + newPod("b", "192.168.1.11", true, map[string]string{"model.aibrix.ai/port": "8000"}), |
| 69 | + }, |
| 70 | + expectErr: false, |
| 71 | + expectPossibleAddrs: []string{"192.168.1.10:8000", "192.168.1.11:8000"}, |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "session ID points to non-existent address → fallback", |
| 75 | + reqHeaders: map[string]string{ |
| 76 | + sessionIDHeader: base64.StdEncoding.EncodeToString([]byte("10.99.99.99:8000")), // 不存在的 IP |
| 77 | + }, |
| 78 | + readyPods: []*v1.Pod{ |
| 79 | + newPod("x", "10.1.1.1", true, map[string]string{"model.aibrix.ai/port": "8000"}), |
| 80 | + newPod("y", "10.1.1.2", true, map[string]string{"model.aibrix.ai/port": "8000"}), |
| 81 | + }, |
| 82 | + expectErr: false, |
| 83 | + expectPossibleAddrs: []string{"10.1.1.1:8000", "10.1.1.2:8000"}, |
| 84 | + }, |
| 85 | + } |
| 86 | + |
| 87 | + for _, tt := range tests { |
| 88 | + t.Run(tt.name, func(t *testing.T) { |
| 89 | + router := &sessionAffinityRouter{} |
| 90 | + |
| 91 | + ctx := types.NewRoutingContext(context.Background(), "test", "model1", "", "", "") |
| 92 | + ctx.ReqHeaders = tt.reqHeaders |
| 93 | + |
| 94 | + podList := newMockPodList(tt.readyPods, nil) |
| 95 | + |
| 96 | + addr, err := router.Route(ctx, podList) |
| 97 | + |
| 98 | + if tt.expectErr { |
| 99 | + assert.Error(t, err) |
| 100 | + return |
| 101 | + } |
| 102 | + |
| 103 | + assert.NoError(t, err) |
| 104 | + assert.NotNil(t, ctx.RespHeaders, "RespHeaders should not be nil") |
| 105 | + assert.Contains(t, ctx.RespHeaders, sessionIDHeader, "Response must include session ID header") |
| 106 | + |
| 107 | + // verify the returned address is one of the expected ready pod addresses |
| 108 | + assert.Contains(t, tt.expectPossibleAddrs, addr, "selected address must be one of the ready pods' IP:port") |
| 109 | + |
| 110 | + // verify that the session ID in the response decodes to the same address |
| 111 | + sessionB64 := ctx.RespHeaders[sessionIDHeader] |
| 112 | + sessionBytes, decodeErr := base64.StdEncoding.DecodeString(sessionB64) |
| 113 | + assert.NoError(t, decodeErr, "session ID must be valid base64") |
| 114 | + actualSessionAddr := string(sessionBytes) |
| 115 | + |
| 116 | + assert.Equal(t, addr, actualSessionAddr, "session ID must encode the same address as returned by Route()") |
| 117 | + }) |
| 118 | + } |
| 119 | +} |
0 commit comments