dynamographdeploymentrequest_test.go 12.1 KB
Newer Older
1
/*
2
 * SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package validation

import (
	"strings"
	"testing"

24
	nvidiacomv1alpha1 "github.com/ai-dynamo/dynamo/deploy/operator/api/v1alpha1"
25
26
27
28
29
30
	apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestDynamoGraphDeploymentRequestValidator_Validate(t *testing.T) {
	validConfig := `{"engine": {"backend": "vllm"}, "deployment": {"model": "test-model"}}`
31
	validConfigWithHardware := `{"engine": {"backend": "vllm"}, "deployment": {"model": "test-model"}, "hardware": {"numGpusPerNode": 8, "gpuModel": "H100-SXM5-80GB", "gpuVramMib": 81920}}`
32
	minimalConfig := `{"sla": {"ttft": 200.0}}`
33
34
35
36
	configWithDifferentBackend := `{"engine": {"backend": "sglang"}}`
	configWithDifferentModel := `{"deployment": {"model": "different-model"}}`
	invalidYAML := `{invalid yaml`

37
38
	// errMsg: if non-empty, an error is expected and each newline-separated substring must appear in it.
	// expectedWarning: if non-empty, at least one warning must contain this substring.
39
	tests := []struct {
40
41
42
43
44
45
		name                string
		request             *nvidiacomv1alpha1.DynamoGraphDeploymentRequest
		isClusterWide       bool
		gpuDiscoveryEnabled bool
		errMsg              string
		expectedWarning     string
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
	}{
		{
			name: "valid request",
			request: &nvidiacomv1alpha1.DynamoGraphDeploymentRequest{
				ObjectMeta: metav1.ObjectMeta{
					Name:      "test-dgdr",
					Namespace: "default",
				},
				Spec: nvidiacomv1alpha1.DynamoGraphDeploymentRequestSpec{
					Model:   "llama-3-8b",
					Backend: "vllm",
					ProfilingConfig: nvidiacomv1alpha1.ProfilingConfigSpec{
						ProfilerImage: "profiler:latest",
						Config: &apiextensionsv1.JSON{
							Raw: []byte(validConfig),
						},
					},
				},
			},
			isClusterWide: true,
		},
		{
			name: "missing profiler image",
			request: &nvidiacomv1alpha1.DynamoGraphDeploymentRequest{
				ObjectMeta: metav1.ObjectMeta{
					Name:      "test-dgdr",
					Namespace: "default",
				},
				Spec: nvidiacomv1alpha1.DynamoGraphDeploymentRequestSpec{
					Model:   "llama-3-8b",
					Backend: "vllm",
					ProfilingConfig: nvidiacomv1alpha1.ProfilingConfigSpec{
						ProfilerImage: "",
						Config: &apiextensionsv1.JSON{
							Raw: []byte(validConfig),
						},
					},
				},
			},
			isClusterWide: true,
			errMsg:        "spec.profilingConfig.profilerImage is required",
		},
		{
			name: "missing profiling config",
			request: &nvidiacomv1alpha1.DynamoGraphDeploymentRequest{
				ObjectMeta: metav1.ObjectMeta{
					Name:      "test-dgdr",
					Namespace: "default",
				},
				Spec: nvidiacomv1alpha1.DynamoGraphDeploymentRequestSpec{
					Model:   "llama-3-8b",
					Backend: "vllm",
					ProfilingConfig: nvidiacomv1alpha1.ProfilingConfigSpec{
						ProfilerImage: "profiler:latest",
						Config:        nil,
					},
				},
			},
			isClusterWide: true,
			errMsg:        "spec.profilingConfig.config is required and must not be empty",
		},
		{
			name: "empty profiling config",
			request: &nvidiacomv1alpha1.DynamoGraphDeploymentRequest{
				ObjectMeta: metav1.ObjectMeta{
					Name:      "test-dgdr",
					Namespace: "default",
				},
				Spec: nvidiacomv1alpha1.DynamoGraphDeploymentRequestSpec{
					Model:   "llama-3-8b",
					Backend: "vllm",
					ProfilingConfig: nvidiacomv1alpha1.ProfilingConfigSpec{
						ProfilerImage: "profiler:latest",
						Config: &apiextensionsv1.JSON{
							Raw: []byte{},
						},
					},
				},
			},
			isClusterWide: true,
			errMsg:        "spec.profilingConfig.config is required and must not be empty",
		},
		{
129
			name: "namespace-scoped operator with manual hardware config (should pass)",
130
131
132
133
134
135
			request: &nvidiacomv1alpha1.DynamoGraphDeploymentRequest{
				ObjectMeta: metav1.ObjectMeta{
					Name:      "test-dgdr",
					Namespace: "default",
				},
				Spec: nvidiacomv1alpha1.DynamoGraphDeploymentRequestSpec{
136
137
					Model:   "llama-3-8b",
					Backend: "vllm",
138
139
140
					ProfilingConfig: nvidiacomv1alpha1.ProfilingConfigSpec{
						ProfilerImage: "profiler:latest",
						Config: &apiextensionsv1.JSON{
141
							Raw: []byte(validConfigWithHardware),
142
143
144
145
						},
					},
				},
			},
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
			isClusterWide:       false,
			gpuDiscoveryEnabled: false,
		},
		{
			name: "namespace-scoped operator with GPU discovery enabled (should pass without manual config)",
			request: &nvidiacomv1alpha1.DynamoGraphDeploymentRequest{
				ObjectMeta: metav1.ObjectMeta{
					Name:      "test-dgdr",
					Namespace: "default",
				},
				Spec: nvidiacomv1alpha1.DynamoGraphDeploymentRequestSpec{
					Model:   "llama-3-8b",
					Backend: "vllm",
					ProfilingConfig: nvidiacomv1alpha1.ProfilingConfigSpec{
						ProfilerImage: "profiler:latest",
						Config: &apiextensionsv1.JSON{
							Raw: []byte(minimalConfig),
						},
					},
				},
			},
			isClusterWide:       false,
			gpuDiscoveryEnabled: true,
		},
		{
			name: "namespace-scoped operator with GPU discovery disabled and no hardware config (should error)",
			request: &nvidiacomv1alpha1.DynamoGraphDeploymentRequest{
				ObjectMeta: metav1.ObjectMeta{
					Name:      "test-dgdr",
					Namespace: "default",
				},
				Spec: nvidiacomv1alpha1.DynamoGraphDeploymentRequestSpec{
					Model:   "llama-3-8b",
					Backend: "vllm",
					ProfilingConfig: nvidiacomv1alpha1.ProfilingConfigSpec{
						ProfilerImage: "profiler:latest",
						Config: &apiextensionsv1.JSON{
							Raw: []byte(minimalConfig),
						},
					},
				},
			},
			isClusterWide:       false,
			gpuDiscoveryEnabled: false,
			errMsg:              "GPU hardware configuration required: GPU discovery is disabled",
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
		},
		{
			name: "invalid config YAML",
			request: &nvidiacomv1alpha1.DynamoGraphDeploymentRequest{
				ObjectMeta: metav1.ObjectMeta{
					Name:      "test-dgdr",
					Namespace: "default",
				},
				Spec: nvidiacomv1alpha1.DynamoGraphDeploymentRequestSpec{
					Model:   "llama-3-8b",
					Backend: "vllm",
					ProfilingConfig: nvidiacomv1alpha1.ProfilingConfigSpec{
						ProfilerImage: "profiler:latest",
						Config: &apiextensionsv1.JSON{
							Raw: []byte(invalidYAML),
						},
					},
				},
			},
			isClusterWide: true,
211
			errMsg:        "failed to parse spec.profilingConfig.config",
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
		},
		{
			name: "warning for different backend in config",
			request: &nvidiacomv1alpha1.DynamoGraphDeploymentRequest{
				ObjectMeta: metav1.ObjectMeta{
					Name:      "test-dgdr",
					Namespace: "default",
				},
				Spec: nvidiacomv1alpha1.DynamoGraphDeploymentRequestSpec{
					Model:   "llama-3-8b",
					Backend: "vllm",
					ProfilingConfig: nvidiacomv1alpha1.ProfilingConfigSpec{
						ProfilerImage: "profiler:latest",
						Config: &apiextensionsv1.JSON{
							Raw: []byte(configWithDifferentBackend),
						},
					},
				},
			},
			isClusterWide:   true,
			expectedWarning: "spec.profilingConfig.config.engine.backend (sglang) will be overwritten by spec.backend (vllm)",
		},
		{
			name: "warning for different model in config",
			request: &nvidiacomv1alpha1.DynamoGraphDeploymentRequest{
				ObjectMeta: metav1.ObjectMeta{
					Name:      "test-dgdr",
					Namespace: "default",
				},
				Spec: nvidiacomv1alpha1.DynamoGraphDeploymentRequestSpec{
					Model:   "llama-3-8b",
					Backend: "vllm",
					ProfilingConfig: nvidiacomv1alpha1.ProfilingConfigSpec{
						ProfilerImage: "profiler:latest",
						Config: &apiextensionsv1.JSON{
							Raw: []byte(configWithDifferentModel),
						},
					},
				},
			},
			isClusterWide:   true,
			expectedWarning: "spec.profilingConfig.config.deployment.model (different-model) will be overwritten by spec.model (llama-3-8b)",
		},
		{
256
			name: "multiple errors (missing profiler image and missing config)",
257
258
259
260
261
262
			request: &nvidiacomv1alpha1.DynamoGraphDeploymentRequest{
				ObjectMeta: metav1.ObjectMeta{
					Name:      "test-dgdr",
					Namespace: "default",
				},
				Spec: nvidiacomv1alpha1.DynamoGraphDeploymentRequestSpec{
263
264
					Model:   "llama-3-8b",
					Backend: "vllm",
265
266
267
268
269
270
271
					ProfilingConfig: nvidiacomv1alpha1.ProfilingConfigSpec{
						ProfilerImage: "",
						Config:        nil,
					},
				},
			},
			isClusterWide: false,
272
			errMsg:        "spec.profilingConfig.profilerImage is required\nspec.profilingConfig.config is required and must not be empty",
273
274
275
276
277
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
278
			validator := NewDynamoGraphDeploymentRequestValidator(tt.request, tt.isClusterWide, tt.gpuDiscoveryEnabled)
279
280
			warnings, err := validator.Validate()

281
282
283
			wantErr := tt.errMsg != ""
			if (err != nil) != wantErr {
				t.Errorf("Validate() error = %v, wantErr %v", err, wantErr)
284
285
				return
			}
286
287
288
289
			if wantErr {
				for _, msg := range strings.Split(tt.errMsg, "\n") {
					if !strings.Contains(err.Error(), msg) {
						t.Errorf("Validate() error %q does not contain %q", err.Error(), msg)
290
291
292
293
					}
				}
			}

294
295
296
			wantWarning := tt.expectedWarning != ""
			if wantWarning && len(warnings) == 0 {
				t.Errorf("Validate() expected warning %q but got none", tt.expectedWarning)
297
			}
298
299
			if wantWarning && len(warnings) > 0 && !strings.Contains(warnings[0], tt.expectedWarning) {
				t.Errorf("Validate() warning %q does not contain %q", warnings[0], tt.expectedWarning)
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
			}
		})
	}
}

func TestDynamoGraphDeploymentRequestValidator_ValidateUpdate(t *testing.T) {
	validConfig := `{"engine": {"backend": "vllm"}}`

	tests := []struct {
		name         string
		oldRequest   *nvidiacomv1alpha1.DynamoGraphDeploymentRequest
		newRequest   *nvidiacomv1alpha1.DynamoGraphDeploymentRequest
		wantErr      bool
		wantWarnings bool
	}{
		{
			name: "no changes",
			oldRequest: &nvidiacomv1alpha1.DynamoGraphDeploymentRequest{
				Spec: nvidiacomv1alpha1.DynamoGraphDeploymentRequestSpec{
					Model:   "llama-3-8b",
					Backend: "vllm",
					ProfilingConfig: nvidiacomv1alpha1.ProfilingConfigSpec{
						ProfilerImage: "profiler:latest",
						Config: &apiextensionsv1.JSON{
							Raw: []byte(validConfig),
						},
					},
				},
			},
			newRequest: &nvidiacomv1alpha1.DynamoGraphDeploymentRequest{
				Spec: nvidiacomv1alpha1.DynamoGraphDeploymentRequestSpec{
					Model:   "llama-3-8b",
					Backend: "vllm",
					ProfilingConfig: nvidiacomv1alpha1.ProfilingConfigSpec{
						ProfilerImage: "profiler:latest",
						Config: &apiextensionsv1.JSON{
							Raw: []byte(validConfig),
						},
					},
				},
			},
			wantErr: false,
		},
		{
			name: "changing model name is allowed",
			oldRequest: &nvidiacomv1alpha1.DynamoGraphDeploymentRequest{
				Spec: nvidiacomv1alpha1.DynamoGraphDeploymentRequestSpec{
					Model:   "llama-3-8b",
					Backend: "vllm",
					ProfilingConfig: nvidiacomv1alpha1.ProfilingConfigSpec{
						ProfilerImage: "profiler:latest",
						Config: &apiextensionsv1.JSON{
							Raw: []byte(validConfig),
						},
					},
				},
			},
			newRequest: &nvidiacomv1alpha1.DynamoGraphDeploymentRequest{
				Spec: nvidiacomv1alpha1.DynamoGraphDeploymentRequestSpec{
					Model:   "llama-3-70b",
					Backend: "vllm",
					ProfilingConfig: nvidiacomv1alpha1.ProfilingConfigSpec{
						ProfilerImage: "profiler:latest",
						Config: &apiextensionsv1.JSON{
							Raw: []byte(validConfig),
						},
					},
				},
			},
			wantErr: false,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
375
			validator := NewDynamoGraphDeploymentRequestValidator(tt.newRequest, true, true)
376
377
378
379
380
381
382
383
384
385
386
387
388
			warnings, err := validator.ValidateUpdate(tt.oldRequest)

			if (err != nil) != tt.wantErr {
				t.Errorf("DynamoGraphDeploymentRequestValidator.ValidateUpdate() error = %v, wantErr %v", err, tt.wantErr)
				return
			}

			if tt.wantWarnings && len(warnings) == 0 {
				t.Errorf("DynamoGraphDeploymentRequestValidator.ValidateUpdate() expected warnings but got none")
			}
		})
	}
}