|
| 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 scheduling |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/google/go-cmp/cmp" |
| 24 | + "github.com/google/uuid" |
| 25 | + "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/backend" |
| 26 | + backendmetrics "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/backend/metrics" // Import config for thresholds |
| 27 | + "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/types" |
| 28 | +) |
| 29 | + |
| 30 | +// Tests the scheduler for conformance tests. |
| 31 | +func TestSchedule(t *testing.T) { |
| 32 | + tests := []struct { |
| 33 | + name string |
| 34 | + input []*backendmetrics.FakePodMetrics |
| 35 | + req *types.LLMRequest |
| 36 | + wantRes map[string]*types.Result |
| 37 | + err bool |
| 38 | + }{ |
| 39 | + { |
| 40 | + name: "no pods in datastore and req header is set", |
| 41 | + req: &types.LLMRequest{ |
| 42 | + Headers: map[string]string{"test-epp-endpoint-selection": "random-endpoint"}, |
| 43 | + RequestId: uuid.NewString(), |
| 44 | + }, |
| 45 | + wantRes: nil, |
| 46 | + err: true, |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "req header not set", |
| 50 | + input: []*backendmetrics.FakePodMetrics{ |
| 51 | + {Pod: &backend.Pod{Address: "random-endpoint"}}, |
| 52 | + }, |
| 53 | + req: &types.LLMRequest{ |
| 54 | + Headers: map[string]string{}, // Deliberately set an empty header. |
| 55 | + RequestId: uuid.NewString(), |
| 56 | + }, |
| 57 | + wantRes: nil, |
| 58 | + err: true, |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "no pods address in datastore matches req header address", |
| 62 | + input: []*backendmetrics.FakePodMetrics{ |
| 63 | + {Pod: &backend.Pod{Address: "nonmatched-endpoint"}}, |
| 64 | + }, |
| 65 | + req: &types.LLMRequest{ |
| 66 | + Headers: map[string]string{"test-epp-endpoint-selection": "matched-endpoint"}, |
| 67 | + RequestId: uuid.NewString(), |
| 68 | + }, |
| 69 | + wantRes: nil, |
| 70 | + err: true, |
| 71 | + }, |
| 72 | + { |
| 73 | + name: "one pod address in datastore matches req header address", |
| 74 | + input: []*backendmetrics.FakePodMetrics{ |
| 75 | + {Pod: &backend.Pod{Address: "nonmatched-endpoint"}}, |
| 76 | + {Pod: &backend.Pod{Address: "matched-endpoint"}}, |
| 77 | + }, |
| 78 | + req: &types.LLMRequest{ |
| 79 | + Headers: map[string]string{"test-epp-endpoint-selection": "matched-endpoint"}, |
| 80 | + RequestId: uuid.NewString(), |
| 81 | + }, |
| 82 | + wantRes: map[string]*types.Result{ |
| 83 | + "req-header-based-profile": { |
| 84 | + TargetPod: &types.ScoredPod{ |
| 85 | + Pod: &types.PodMetrics{ |
| 86 | + Pod: &backend.Pod{ |
| 87 | + Address: "matched-endpoint", |
| 88 | + Labels: map[string]string{}, |
| 89 | + }, |
| 90 | + }, |
| 91 | + }, |
| 92 | + }, |
| 93 | + }, |
| 94 | + }, |
| 95 | + } |
| 96 | + |
| 97 | + for _, test := range tests { |
| 98 | + t.Run(test.name, func(t *testing.T) { |
| 99 | + scheduler := NewReqHeaderBasedScheduler(&fakeDataStore{pods: test.input}) |
| 100 | + got, err := scheduler.Schedule(context.Background(), test.req) |
| 101 | + if test.err != (err != nil) { |
| 102 | + t.Errorf("Unexpected error, got %v, want %v", err, test.err) |
| 103 | + } |
| 104 | + |
| 105 | + if diff := cmp.Diff(test.wantRes, got); diff != "" { |
| 106 | + t.Errorf("Unexpected output (-want +got): %v", diff) |
| 107 | + } |
| 108 | + }) |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +type fakeDataStore struct { |
| 113 | + pods []*backendmetrics.FakePodMetrics |
| 114 | +} |
| 115 | + |
| 116 | +func (fds *fakeDataStore) PodGetAll() []backendmetrics.PodMetrics { |
| 117 | + pm := make([]backendmetrics.PodMetrics, 0, len(fds.pods)) |
| 118 | + for _, pod := range fds.pods { |
| 119 | + pm = append(pm, pod) |
| 120 | + } |
| 121 | + return pm |
| 122 | +} |
0 commit comments