|
| 1 | +/* |
| 2 | +Copyright 2025. |
| 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 controller |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 23 | + "k8s.io/apimachinery/pkg/runtime" |
| 24 | + clientgoscheme "k8s.io/client-go/kubernetes/scheme" |
| 25 | + |
| 26 | + v1alpha1 "production-stack/api/v1alpha1" |
| 27 | +) |
| 28 | + |
| 29 | +func newTestScheme() *runtime.Scheme { |
| 30 | + s := runtime.NewScheme() |
| 31 | + _ = clientgoscheme.AddToScheme(s) |
| 32 | + _ = v1alpha1.AddToScheme(s) |
| 33 | + return s |
| 34 | +} |
| 35 | + |
| 36 | +// containsConsecutive reports whether args contains a and b as adjacent elements. |
| 37 | +func containsConsecutive(args []string, a, b string) bool { |
| 38 | + for i := 0; i+1 < len(args); i++ { |
| 39 | + if args[i] == a && args[i+1] == b { |
| 40 | + return true |
| 41 | + } |
| 42 | + } |
| 43 | + return false |
| 44 | +} |
| 45 | + |
| 46 | +// containsArg reports whether arg appears anywhere in args. |
| 47 | +func containsArg(args []string, arg string) bool { |
| 48 | + for _, a := range args { |
| 49 | + if a == arg { |
| 50 | + return true |
| 51 | + } |
| 52 | + } |
| 53 | + return false |
| 54 | +} |
| 55 | + |
| 56 | +// buildTestRouter constructs a minimal VLLMRouter with static service discovery. |
| 57 | +func buildTestRouter(routingLogic, sessionKey string, lmcachePort int32) *v1alpha1.VLLMRouter { |
| 58 | + return &v1alpha1.VLLMRouter{ |
| 59 | + ObjectMeta: metav1.ObjectMeta{ |
| 60 | + Name: "test-router", |
| 61 | + Namespace: "default", |
| 62 | + }, |
| 63 | + Spec: v1alpha1.VLLMRouterSpec{ |
| 64 | + Replicas: 1, |
| 65 | + ServiceDiscovery: "static", |
| 66 | + StaticBackends: "http://backend:8000", |
| 67 | + StaticModels: "test-model", |
| 68 | + Port: 80, |
| 69 | + RoutingLogic: routingLogic, |
| 70 | + SessionKey: sessionKey, |
| 71 | + LmcacheControllerPort: lmcachePort, |
| 72 | + Image: v1alpha1.ImageSpec{ |
| 73 | + Registry: "docker.io", |
| 74 | + Name: "vllm/router:latest", |
| 75 | + }, |
| 76 | + }, |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func TestDeploymentNeedsUpdate(t *testing.T) { |
| 81 | + r := &VLLMRouterReconciler{Scheme: newTestScheme()} |
| 82 | + |
| 83 | + base := buildTestRouter("roundrobin", "", 0) |
| 84 | + dep := r.deploymentForVLLMRouter(base) |
| 85 | + |
| 86 | + // identical spec → no update |
| 87 | + if r.deploymentNeedsUpdate(dep, base) { |
| 88 | + t.Error("expected no update for identical spec") |
| 89 | + } |
| 90 | + |
| 91 | + // routing logic changed → args differ → update required |
| 92 | + changed := buildTestRouter("prefixaware", "", 0) |
| 93 | + if !r.deploymentNeedsUpdate(dep, changed) { |
| 94 | + t.Error("expected update when routingLogic changes") |
| 95 | + } |
| 96 | + |
| 97 | + // kvaware with port added → args differ → update required |
| 98 | + withPort := buildTestRouter("kvaware", "", 9000) |
| 99 | + if !r.deploymentNeedsUpdate(dep, withPort) { |
| 100 | + t.Error("expected update when lmcacheControllerPort added") |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +func TestDeploymentArgsRouting(t *testing.T) { |
| 105 | + r := &VLLMRouterReconciler{Scheme: newTestScheme()} |
| 106 | + |
| 107 | + tests := []struct { |
| 108 | + name string |
| 109 | + routingLogic string |
| 110 | + sessionKey string |
| 111 | + lmcachePort int32 |
| 112 | + wantPresent [][2]string // pairs that must appear consecutively |
| 113 | + wantAbsent []string // flags that must not appear |
| 114 | + }{ |
| 115 | + { |
| 116 | + name: "roundrobin", |
| 117 | + routingLogic: "roundrobin", |
| 118 | + wantPresent: [][2]string{{"--routing-logic", "roundrobin"}}, |
| 119 | + wantAbsent: []string{"--lmcache-controller-port"}, |
| 120 | + }, |
| 121 | + { |
| 122 | + name: "session with key", |
| 123 | + routingLogic: "session", |
| 124 | + sessionKey: "mykey", |
| 125 | + wantPresent: [][2]string{ |
| 126 | + {"--routing-logic", "session"}, |
| 127 | + {"--session-key", "mykey"}, |
| 128 | + }, |
| 129 | + wantAbsent: []string{"--lmcache-controller-port"}, |
| 130 | + }, |
| 131 | + { |
| 132 | + name: "prefixaware", |
| 133 | + routingLogic: "prefixaware", |
| 134 | + wantPresent: [][2]string{{"--routing-logic", "prefixaware"}}, |
| 135 | + wantAbsent: []string{"--lmcache-controller-port"}, |
| 136 | + }, |
| 137 | + { |
| 138 | + name: "kvaware default port", |
| 139 | + routingLogic: "kvaware", |
| 140 | + lmcachePort: 9000, |
| 141 | + wantPresent: [][2]string{ |
| 142 | + {"--routing-logic", "kvaware"}, |
| 143 | + {"--lmcache-controller-port", "9000"}, |
| 144 | + }, |
| 145 | + }, |
| 146 | + { |
| 147 | + name: "kvaware custom port", |
| 148 | + routingLogic: "kvaware", |
| 149 | + lmcachePort: 8888, |
| 150 | + wantPresent: [][2]string{ |
| 151 | + {"--routing-logic", "kvaware"}, |
| 152 | + {"--lmcache-controller-port", "8888"}, |
| 153 | + }, |
| 154 | + }, |
| 155 | + { |
| 156 | + name: "kvaware zero port", |
| 157 | + routingLogic: "kvaware", |
| 158 | + lmcachePort: 0, |
| 159 | + wantPresent: [][2]string{{"--routing-logic", "kvaware"}}, |
| 160 | + wantAbsent: []string{"--lmcache-controller-port"}, |
| 161 | + }, |
| 162 | + } |
| 163 | + |
| 164 | + for _, tc := range tests { |
| 165 | + t.Run(tc.name, func(t *testing.T) { |
| 166 | + router := buildTestRouter(tc.routingLogic, tc.sessionKey, tc.lmcachePort) |
| 167 | + dep := r.deploymentForVLLMRouter(router) |
| 168 | + |
| 169 | + if dep == nil { |
| 170 | + t.Fatal("deploymentForVLLMRouter returned nil") |
| 171 | + } |
| 172 | + if len(dep.Spec.Template.Spec.Containers) == 0 { |
| 173 | + t.Fatal("deployment has no containers") |
| 174 | + } |
| 175 | + |
| 176 | + args := dep.Spec.Template.Spec.Containers[0].Args |
| 177 | + |
| 178 | + for _, pair := range tc.wantPresent { |
| 179 | + if !containsConsecutive(args, pair[0], pair[1]) { |
| 180 | + t.Errorf("expected args to contain %q %q, got: %v", pair[0], pair[1], args) |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + for _, flag := range tc.wantAbsent { |
| 185 | + if containsArg(args, flag) { |
| 186 | + t.Errorf("expected args NOT to contain %q, got: %v", flag, args) |
| 187 | + } |
| 188 | + } |
| 189 | + }) |
| 190 | + } |
| 191 | +} |
0 commit comments