|
| 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 v1alpha2 |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/stretchr/testify/require" |
| 24 | + corev1 "k8s.io/api/core/v1" |
| 25 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 26 | + "k8s.io/apimachinery/pkg/runtime" |
| 27 | + ctrl_client "sigs.k8s.io/controller-runtime/pkg/client" |
| 28 | + "sigs.k8s.io/controller-runtime/pkg/envtest" |
| 29 | +) |
| 30 | + |
| 31 | +// TestOpenAIConfigCELValidation pins the OpenAIConfig admission rules against a |
| 32 | +// real kube-apiserver loaded with the shipped CRDs: |
| 33 | +// - maxTokens and maxCompletionTokens are mutually exclusive (type-level |
| 34 | +// XValidation), because native OpenAI reasoning models reject max_tokens |
| 35 | +// while some OpenAI-compatible endpoints reject max_completion_tokens, so |
| 36 | +// sending both risks hard 400s. |
| 37 | +// - both fields carry Minimum=1, so a non-positive value is rejected at |
| 38 | +// admission rather than silently ignored by the translator. |
| 39 | +func TestOpenAIConfigCELValidation(t *testing.T) { |
| 40 | + testEnv := &envtest.Environment{ |
| 41 | + BinaryAssetsDirectory: envtestAssetsDir(t), |
| 42 | + CRDDirectoryPaths: []string{crdBasesDir(t)}, |
| 43 | + ErrorIfCRDPathMissing: true, |
| 44 | + } |
| 45 | + cfg, err := testEnv.Start() |
| 46 | + require.NoError(t, err) |
| 47 | + t.Cleanup(func() { _ = testEnv.Stop() }) |
| 48 | + |
| 49 | + scheme := runtime.NewScheme() |
| 50 | + require.NoError(t, corev1.AddToScheme(scheme)) |
| 51 | + require.NoError(t, AddToScheme(scheme)) |
| 52 | + cl, err := ctrl_client.New(cfg, ctrl_client.Options{Scheme: scheme}) |
| 53 | + require.NoError(t, err) |
| 54 | + |
| 55 | + ctx := context.Background() |
| 56 | + const ns = "openai-cel" |
| 57 | + require.NoError(t, cl.Create(ctx, &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: ns}})) |
| 58 | + |
| 59 | + cases := []struct { |
| 60 | + name string |
| 61 | + build func() ctrl_client.Object |
| 62 | + wantReject string // substring in admission error; empty means accept |
| 63 | + }{ |
| 64 | + { |
| 65 | + name: "both maxTokens and maxCompletionTokens rejected", |
| 66 | + build: func() ctrl_client.Object { |
| 67 | + return &ModelConfig{ |
| 68 | + ObjectMeta: metav1.ObjectMeta{Name: "mc-both-token-caps", Namespace: ns}, |
| 69 | + Spec: ModelConfigSpec{ |
| 70 | + Model: "gpt-4", |
| 71 | + Provider: ModelProviderOpenAI, |
| 72 | + OpenAI: &OpenAIConfig{MaxTokens: 1000, MaxCompletionTokens: 1000}, |
| 73 | + }, |
| 74 | + } |
| 75 | + }, |
| 76 | + wantReject: "maxTokens and maxCompletionTokens are mutually exclusive", |
| 77 | + }, |
| 78 | + { |
| 79 | + name: "maxTokens below minimum rejected", |
| 80 | + build: func() ctrl_client.Object { |
| 81 | + return &ModelConfig{ |
| 82 | + ObjectMeta: metav1.ObjectMeta{Name: "mc-maxtokens-neg", Namespace: ns}, |
| 83 | + Spec: ModelConfigSpec{ |
| 84 | + Model: "gpt-4", |
| 85 | + Provider: ModelProviderOpenAI, |
| 86 | + OpenAI: &OpenAIConfig{MaxTokens: -1}, |
| 87 | + }, |
| 88 | + } |
| 89 | + }, |
| 90 | + wantReject: "maxTokens", |
| 91 | + }, |
| 92 | + { |
| 93 | + name: "maxCompletionTokens below minimum rejected", |
| 94 | + build: func() ctrl_client.Object { |
| 95 | + return &ModelConfig{ |
| 96 | + ObjectMeta: metav1.ObjectMeta{Name: "mc-maxcompletion-neg", Namespace: ns}, |
| 97 | + Spec: ModelConfigSpec{ |
| 98 | + Model: "gpt-4", |
| 99 | + Provider: ModelProviderOpenAI, |
| 100 | + OpenAI: &OpenAIConfig{MaxCompletionTokens: -1}, |
| 101 | + }, |
| 102 | + } |
| 103 | + }, |
| 104 | + wantReject: "maxCompletionTokens", |
| 105 | + }, |
| 106 | + { |
| 107 | + name: "only maxTokens accepted", |
| 108 | + build: func() ctrl_client.Object { |
| 109 | + return &ModelConfig{ |
| 110 | + ObjectMeta: metav1.ObjectMeta{Name: "mc-maxtokens-only", Namespace: ns}, |
| 111 | + Spec: ModelConfigSpec{ |
| 112 | + Model: "gpt-4", |
| 113 | + Provider: ModelProviderOpenAI, |
| 114 | + OpenAI: &OpenAIConfig{MaxTokens: 1000}, |
| 115 | + }, |
| 116 | + } |
| 117 | + }, |
| 118 | + }, |
| 119 | + { |
| 120 | + name: "only maxCompletionTokens accepted", |
| 121 | + build: func() ctrl_client.Object { |
| 122 | + return &ModelConfig{ |
| 123 | + ObjectMeta: metav1.ObjectMeta{Name: "mc-maxcompletion-only", Namespace: ns}, |
| 124 | + Spec: ModelConfigSpec{ |
| 125 | + Model: "gpt-5", |
| 126 | + Provider: ModelProviderOpenAI, |
| 127 | + OpenAI: &OpenAIConfig{MaxCompletionTokens: 1000}, |
| 128 | + }, |
| 129 | + } |
| 130 | + }, |
| 131 | + }, |
| 132 | + { |
| 133 | + name: "neither token cap accepted", |
| 134 | + build: func() ctrl_client.Object { |
| 135 | + return &ModelConfig{ |
| 136 | + ObjectMeta: metav1.ObjectMeta{Name: "mc-no-token-caps", Namespace: ns}, |
| 137 | + Spec: ModelConfigSpec{ |
| 138 | + Model: "gpt-4", |
| 139 | + Provider: ModelProviderOpenAI, |
| 140 | + OpenAI: &OpenAIConfig{}, |
| 141 | + }, |
| 142 | + } |
| 143 | + }, |
| 144 | + }, |
| 145 | + } |
| 146 | + |
| 147 | + for _, c := range cases { |
| 148 | + t.Run(c.name, func(t *testing.T) { |
| 149 | + err := cl.Create(ctx, c.build()) |
| 150 | + if c.wantReject == "" { |
| 151 | + require.NoError(t, err) |
| 152 | + return |
| 153 | + } |
| 154 | + require.Error(t, err) |
| 155 | + require.Contains(t, err.Error(), c.wantReject) |
| 156 | + }) |
| 157 | + } |
| 158 | +} |
0 commit comments