|
| 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 main |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "testing" |
| 23 | + "time" |
| 24 | + |
| 25 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 26 | + v1 "sigs.k8s.io/gateway-api-inference-extension/api/v1" |
| 27 | +) |
| 28 | + |
| 29 | +func TestValidateInferencePool(t *testing.T) { |
| 30 | + ctx := context.Background() |
| 31 | + |
| 32 | + // baseInferencePool is a valid, InferencePool resource. |
| 33 | + baseInferencePool := v1.InferencePool{ |
| 34 | + ObjectMeta: metav1.ObjectMeta{ |
| 35 | + Name: "base-pool", |
| 36 | + Namespace: metav1.NamespaceDefault, |
| 37 | + }, |
| 38 | + Spec: v1.InferencePoolSpec{ |
| 39 | + TargetPorts: []v1.Port{ |
| 40 | + {Number: 8000}, |
| 41 | + }, |
| 42 | + Selector: v1.LabelSelector{ |
| 43 | + MatchLabels: map[v1.LabelKey]v1.LabelValue{ |
| 44 | + "app": "model-server", |
| 45 | + }, |
| 46 | + }, |
| 47 | + EndpointPickerRef: v1.EndpointPickerRef{ |
| 48 | + Name: "epp", |
| 49 | + Kind: "Service", |
| 50 | + Port: ptrTo(v1.Port{Number: 9002}), |
| 51 | + }, |
| 52 | + }, |
| 53 | + } |
| 54 | + |
| 55 | + testCases := []struct { |
| 56 | + desc string |
| 57 | + mutate func(ip *v1.InferencePool) |
| 58 | + wantErrors []string |
| 59 | + }{ |
| 60 | + { |
| 61 | + desc: "passes validation with a valid configuration", |
| 62 | + mutate: func(ip *v1.InferencePool) { |
| 63 | + }, |
| 64 | + wantErrors: nil, |
| 65 | + }, |
| 66 | + { |
| 67 | + desc: "fails validation when kind is unset (defaults to Service) and port is missing", |
| 68 | + mutate: func(ip *v1.InferencePool) { |
| 69 | + // By setting Kind to an empty string, we rely on the API server's default value of "Service". |
| 70 | + ip.Spec.EndpointPickerRef.Kind = "" |
| 71 | + ip.Spec.EndpointPickerRef.Port = nil |
| 72 | + }, |
| 73 | + wantErrors: []string{"port is required when kind is 'Service' or unspecified (defaults to 'Service')"}, |
| 74 | + }, |
| 75 | + { |
| 76 | + desc: "fails validation when kind is explicitly 'Service' and port is missing", |
| 77 | + mutate: func(ip *v1.InferencePool) { |
| 78 | + ip.Spec.EndpointPickerRef.Kind = "Service" |
| 79 | + ip.Spec.EndpointPickerRef.Port = nil |
| 80 | + }, |
| 81 | + wantErrors: []string{"port is required when kind is 'Service' or unspecified (defaults to 'Service')"}, |
| 82 | + }, |
| 83 | + } |
| 84 | + |
| 85 | + for _, tc := range testCases { |
| 86 | + t.Run(tc.desc, func(t *testing.T) { |
| 87 | + ip := baseInferencePool.DeepCopy() |
| 88 | + // Use a unique name for each test case to avoid conflicts. |
| 89 | + ip.Name = fmt.Sprintf("test-pool-%v", time.Now().UnixNano()) |
| 90 | + |
| 91 | + if tc.mutate != nil { |
| 92 | + tc.mutate(ip) |
| 93 | + } |
| 94 | + err := k8sClient.Create(ctx, ip) |
| 95 | + |
| 96 | + // This is a boolean XOR. It's true if one is true, but not both. |
| 97 | + // It ensures that an error is returned if and only if we expect one. |
| 98 | + if (len(tc.wantErrors) != 0) != (err != nil) { |
| 99 | + t.Fatalf("Unexpected response while creating InferencePool; got err=\n%v\n; want error=%v", err, tc.wantErrors != nil) |
| 100 | + } |
| 101 | + |
| 102 | + // If we got an error, check that it contains the expected substrings. |
| 103 | + var missingErrorStrings []string |
| 104 | + for _, wantError := range tc.wantErrors { |
| 105 | + if !celErrorStringMatches(err.Error(), wantError) { |
| 106 | + missingErrorStrings = append(missingErrorStrings, wantError) |
| 107 | + } |
| 108 | + } |
| 109 | + if len(missingErrorStrings) != 0 { |
| 110 | + t.Errorf("Unexpected response while creating InferencePool; got err=\n%v\n; missing strings within error=%q", err, missingErrorStrings) |
| 111 | + } |
| 112 | + }) |
| 113 | + } |
| 114 | +} |
0 commit comments