dynamographdeploymentrequest_test.go 11.4 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
33
34
35
36
37
38
39
40
41
42
43
44
45
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
129
130
131
	configWithDifferentBackend := `{"engine": {"backend": "sglang"}}`
	configWithDifferentModel := `{"deployment": {"model": "different-model"}}`
	invalidYAML := `{invalid yaml`

	tests := []struct {
		name            string
		request         *nvidiacomv1alpha1.DynamoGraphDeploymentRequest
		isClusterWide   bool
		wantErr         bool
		errMsg          string
		wantWarnings    bool
		expectedWarning string
		errContains     bool
	}{
		{
			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,
			wantErr:       false,
		},
		{
			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,
			wantErr:       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,
			wantErr:       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,
			wantErr:       true,
			errMsg:        "spec.profilingConfig.config is required and must not be empty",
		},
		{
132
			name: "namespace-restricted operator (GPU discovery will fail gracefully)",
133
134
135
136
137
138
			request: &nvidiacomv1alpha1.DynamoGraphDeploymentRequest{
				ObjectMeta: metav1.ObjectMeta{
					Name:      "test-dgdr",
					Namespace: "default",
				},
				Spec: nvidiacomv1alpha1.DynamoGraphDeploymentRequestSpec{
139
140
					Model:   "llama-3-8b",
					Backend: "vllm",
141
142
143
					ProfilingConfig: nvidiacomv1alpha1.ProfilingConfigSpec{
						ProfilerImage: "profiler:latest",
						Config: &apiextensionsv1.JSON{
144
							Raw: []byte(validConfigWithHardware),
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
						},
					},
				},
			},
			isClusterWide: false,
			wantErr:       false,
		},
		{
			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,
			wantErr:       true,
			errMsg:        "failed to parse spec.profilingConfig.config: error converting YAML to JSON: yaml: line 1: did not find expected ',' or '}'",
		},
		{
			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,
			wantErr:         false,
			wantWarnings:    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,
			wantErr:         false,
			wantWarnings:    true,
			expectedWarning: "spec.profilingConfig.config.deployment.model (different-model) will be overwritten by spec.model (llama-3-8b)",
		},
		{
221
			name: "multiple errors (missing profiler image and missing config)",
222
223
224
225
226
227
			request: &nvidiacomv1alpha1.DynamoGraphDeploymentRequest{
				ObjectMeta: metav1.ObjectMeta{
					Name:      "test-dgdr",
					Namespace: "default",
				},
				Spec: nvidiacomv1alpha1.DynamoGraphDeploymentRequestSpec{
228
229
					Model:   "llama-3-8b",
					Backend: "vllm",
230
231
232
233
234
235
236
237
					ProfilingConfig: nvidiacomv1alpha1.ProfilingConfigSpec{
						ProfilerImage: "",
						Config:        nil,
					},
				},
			},
			isClusterWide: false,
			wantErr:       true,
238
			errMsg:        "spec.profilingConfig.profilerImage is required\nspec.profilingConfig.config is required and must not be empty",
239
240
			errContains:   true,
		},
241
242
243
		// TODO: Add test for invalid GPU range (min > max) validation
		// The validation logic is in place (lines 148-152 of dynamographdeploymentrequest.go)
		// but needs proper test coverage
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
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
	}

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

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

			if tt.wantErr {
				if tt.errContains {
					// For multiple errors, check that all expected error messages are present
					errStr := err.Error()
					for _, expectedMsg := range strings.Split(tt.errMsg, "\n") {
						if !strings.Contains(errStr, expectedMsg) {
							t.Errorf("DynamoGraphDeploymentRequestValidator.Validate() error message = %v, want to contain %v", errStr, expectedMsg)
						}
					}
				} else {
					if err.Error() != tt.errMsg {
						t.Errorf("DynamoGraphDeploymentRequestValidator.Validate() error message = %v, want %v", err.Error(), tt.errMsg)
					}
				}
			}

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

			if tt.wantWarnings && len(warnings) > 0 && warnings[0] != tt.expectedWarning {
				t.Errorf("DynamoGraphDeploymentRequestValidator.Validate() warning = %v, want %v", warnings[0], tt.expectedWarning)
			}
		})
	}
}

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) {
			validator := NewDynamoGraphDeploymentRequestValidator(tt.newRequest, true)
			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")
			}
		})
	}
}