dynamographdeploymentrequest_test.go 13 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
	nvidiacomv1beta1 "github.com/ai-dynamo/dynamo/deploy/operator/api/v1beta1"
25
26
27
28
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestDynamoGraphDeploymentRequestValidator_Validate(t *testing.T) {
29
30
	vram := float64(81920)
	gpuCount := int32(8)
31

32
	// errMsg: if non-empty, an error is expected and each newline-separated substring must appear in it.
33
	tests := []struct {
34
		name                string
35
		request             *nvidiacomv1beta1.DynamoGraphDeploymentRequest
36
37
38
		isClusterWide       bool
		gpuDiscoveryEnabled bool
		errMsg              string
39
40
41
	}{
		{
			name: "valid request",
42
43
44
			request: &nvidiacomv1beta1.DynamoGraphDeploymentRequest{
				ObjectMeta: metav1.ObjectMeta{Name: "test-dgdr", Namespace: "default"},
				Spec: nvidiacomv1beta1.DynamoGraphDeploymentRequestSpec{
45
					Model:   "llama-3-8b",
46
47
					Backend: nvidiacomv1beta1.BackendTypeVllm,
					Image:   "profiler:latest",
48
49
50
51
52
				},
			},
			isClusterWide: true,
		},
		{
53
54
55
56
			name: "missing image",
			request: &nvidiacomv1beta1.DynamoGraphDeploymentRequest{
				ObjectMeta: metav1.ObjectMeta{Name: "test-dgdr", Namespace: "default"},
				Spec: nvidiacomv1beta1.DynamoGraphDeploymentRequestSpec{
57
					Model:   "llama-3-8b",
58
59
					Backend: nvidiacomv1beta1.BackendTypeVllm,
					Image:   "",
60
61
62
				},
			},
			isClusterWide: true,
63
			errMsg:        "spec.image is required",
64
65
		},
		{
66
67
68
69
70
71
72
73
			name: "thorough + auto is invalid",
			request: &nvidiacomv1beta1.DynamoGraphDeploymentRequest{
				ObjectMeta: metav1.ObjectMeta{Name: "test-dgdr", Namespace: "default"},
				Spec: nvidiacomv1beta1.DynamoGraphDeploymentRequestSpec{
					Model:          "llama-3-8b",
					Image:          "profiler:latest",
					Backend:        nvidiacomv1beta1.BackendTypeAuto,
					SearchStrategy: nvidiacomv1beta1.SearchStrategyThorough,
74
				},
75
76
77
78
79
80
81
82
83
84
85
86
87
			},
			isClusterWide: true,
			errMsg:        `spec.searchStrategy "thorough" is incompatible with spec.backend "auto"`,
		},
		{
			name: "rapid + auto is valid (default combination)",
			request: &nvidiacomv1beta1.DynamoGraphDeploymentRequest{
				ObjectMeta: metav1.ObjectMeta{Name: "test-dgdr", Namespace: "default"},
				Spec: nvidiacomv1beta1.DynamoGraphDeploymentRequestSpec{
					Model:          "llama-3-8b",
					Image:          "profiler:latest",
					Backend:        nvidiacomv1beta1.BackendTypeAuto,
					SearchStrategy: nvidiacomv1beta1.SearchStrategyRapid,
88
89
90
91
92
				},
			},
			isClusterWide: true,
		},
		{
93
94
95
96
97
98
99
100
			name: "thorough + vllm is valid",
			request: &nvidiacomv1beta1.DynamoGraphDeploymentRequest{
				ObjectMeta: metav1.ObjectMeta{Name: "test-dgdr", Namespace: "default"},
				Spec: nvidiacomv1beta1.DynamoGraphDeploymentRequestSpec{
					Model:          "llama-3-8b",
					Image:          "profiler:latest",
					Backend:        nvidiacomv1beta1.BackendTypeVllm,
					SearchStrategy: nvidiacomv1beta1.SearchStrategyThorough,
101
				},
102
103
104
105
106
107
108
109
110
111
112
113
			},
			isClusterWide: true,
		},
		{
			name: "thorough + trtllm is valid",
			request: &nvidiacomv1beta1.DynamoGraphDeploymentRequest{
				ObjectMeta: metav1.ObjectMeta{Name: "test-dgdr", Namespace: "default"},
				Spec: nvidiacomv1beta1.DynamoGraphDeploymentRequestSpec{
					Model:          "llama-3-8b",
					Image:          "profiler:latest",
					Backend:        nvidiacomv1beta1.BackendTypeTrtllm,
					SearchStrategy: nvidiacomv1beta1.SearchStrategyThorough,
114
115
116
117
118
				},
			},
			isClusterWide: true,
		},
		{
119
120
121
122
123
124
125
126
			name: "thorough + sglang is valid",
			request: &nvidiacomv1beta1.DynamoGraphDeploymentRequest{
				ObjectMeta: metav1.ObjectMeta{Name: "test-dgdr", Namespace: "default"},
				Spec: nvidiacomv1beta1.DynamoGraphDeploymentRequestSpec{
					Model:          "llama-3-8b",
					Image:          "profiler:latest",
					Backend:        nvidiacomv1beta1.BackendTypeSglang,
					SearchStrategy: nvidiacomv1beta1.SearchStrategyThorough,
127
				},
128
129
130
131
132
133
134
135
			},
			isClusterWide: true,
		},
		{
			name: "namespace-scoped operator with manual hardware config (should pass)",
			request: &nvidiacomv1beta1.DynamoGraphDeploymentRequest{
				ObjectMeta: metav1.ObjectMeta{Name: "test-dgdr", Namespace: "default"},
				Spec: nvidiacomv1beta1.DynamoGraphDeploymentRequestSpec{
136
					Model:   "llama-3-8b",
137
138
139
140
141
142
					Backend: nvidiacomv1beta1.BackendTypeVllm,
					Image:   "profiler:latest",
					Hardware: &nvidiacomv1beta1.HardwareSpec{
						GPUSKU:         "H100-SXM5-80GB",
						VRAMMB:         &vram,
						NumGPUsPerNode: &gpuCount,
143
144
145
					},
				},
			},
146
147
148
149
150
			isClusterWide:       false,
			gpuDiscoveryEnabled: false,
		},
		{
			name: "namespace-scoped operator with GPU discovery enabled (should pass without manual config)",
151
152
153
			request: &nvidiacomv1beta1.DynamoGraphDeploymentRequest{
				ObjectMeta: metav1.ObjectMeta{Name: "test-dgdr", Namespace: "default"},
				Spec: nvidiacomv1beta1.DynamoGraphDeploymentRequestSpec{
154
					Model:   "llama-3-8b",
155
156
					Backend: nvidiacomv1beta1.BackendTypeVllm,
					Image:   "profiler:latest",
157
158
159
160
161
162
163
				},
			},
			isClusterWide:       false,
			gpuDiscoveryEnabled: true,
		},
		{
			name: "namespace-scoped operator with GPU discovery disabled and no hardware config (should error)",
164
165
166
			request: &nvidiacomv1beta1.DynamoGraphDeploymentRequest{
				ObjectMeta: metav1.ObjectMeta{Name: "test-dgdr", Namespace: "default"},
				Spec: nvidiacomv1beta1.DynamoGraphDeploymentRequestSpec{
167
					Model:   "llama-3-8b",
168
169
					Backend: nvidiacomv1beta1.BackendTypeVllm,
					Image:   "profiler:latest",
170
171
172
173
174
				},
			},
			isClusterWide:       false,
			gpuDiscoveryEnabled: false,
			errMsg:              "GPU hardware configuration required: GPU discovery is disabled",
175
176
		},
		{
177
178
179
180
181
182
183
184
			name: "multiple errors (missing image and thorough+auto)",
			request: &nvidiacomv1beta1.DynamoGraphDeploymentRequest{
				ObjectMeta: metav1.ObjectMeta{Name: "test-dgdr", Namespace: "default"},
				Spec: nvidiacomv1beta1.DynamoGraphDeploymentRequestSpec{
					Model:          "llama-3-8b",
					Backend:        nvidiacomv1beta1.BackendTypeAuto,
					SearchStrategy: nvidiacomv1beta1.SearchStrategyThorough,
					Image:          "",
185
186
187
				},
			},
			isClusterWide: true,
188
			errMsg:        "spec.image is required\nspec.searchStrategy",
189
190
191
192
193
		},
	}

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

197
198
199
			wantErr := tt.errMsg != ""
			if (err != nil) != wantErr {
				t.Errorf("Validate() error = %v, wantErr %v", err, wantErr)
200
201
				return
			}
202
203
204
205
			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)
206
207
208
209
210
211
212
213
214
215
					}
				}
			}
		})
	}
}

func TestDynamoGraphDeploymentRequestValidator_ValidateUpdate(t *testing.T) {
	tests := []struct {
		name         string
216
217
		oldRequest   *nvidiacomv1beta1.DynamoGraphDeploymentRequest
		newRequest   *nvidiacomv1beta1.DynamoGraphDeploymentRequest
218
		wantErr      bool
219
		errMsg       string
220
221
222
223
		wantWarnings bool
	}{
		{
			name: "no changes",
224
225
			oldRequest: &nvidiacomv1beta1.DynamoGraphDeploymentRequest{
				Spec: nvidiacomv1beta1.DynamoGraphDeploymentRequestSpec{
226
					Model:   "llama-3-8b",
227
228
					Backend: nvidiacomv1beta1.BackendTypeVllm,
					Image:   "profiler:latest",
229
230
				},
			},
231
232
			newRequest: &nvidiacomv1beta1.DynamoGraphDeploymentRequest{
				Spec: nvidiacomv1beta1.DynamoGraphDeploymentRequestSpec{
233
					Model:   "llama-3-8b",
234
235
					Backend: nvidiacomv1beta1.BackendTypeVllm,
					Image:   "profiler:latest",
236
237
238
239
240
				},
			},
			wantErr: false,
		},
		{
241
242
243
			name: "changing model name is allowed when not in immutable phase",
			oldRequest: &nvidiacomv1beta1.DynamoGraphDeploymentRequest{
				Spec: nvidiacomv1beta1.DynamoGraphDeploymentRequestSpec{
244
					Model:   "llama-3-8b",
245
246
					Backend: nvidiacomv1beta1.BackendTypeVllm,
					Image:   "profiler:latest",
247
248
				},
			},
249
250
			newRequest: &nvidiacomv1beta1.DynamoGraphDeploymentRequest{
				Spec: nvidiacomv1beta1.DynamoGraphDeploymentRequestSpec{
251
					Model:   "llama-3-70b",
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
					Backend: nvidiacomv1beta1.BackendTypeVllm,
					Image:   "profiler:latest",
				},
			},
			wantErr: false,
		},
		{
			name: "spec change rejected during Profiling phase",
			oldRequest: &nvidiacomv1beta1.DynamoGraphDeploymentRequest{
				Spec: nvidiacomv1beta1.DynamoGraphDeploymentRequestSpec{
					Model:   "llama-3-8b",
					Backend: nvidiacomv1beta1.BackendTypeVllm,
					Image:   "profiler:latest",
				},
				Status: nvidiacomv1beta1.DynamoGraphDeploymentRequestStatus{
					Phase: nvidiacomv1beta1.DGDRPhaseProfiling,
				},
			},
			newRequest: &nvidiacomv1beta1.DynamoGraphDeploymentRequest{
				Spec: nvidiacomv1beta1.DynamoGraphDeploymentRequestSpec{
					Model:   "llama-3-70b",
					Backend: nvidiacomv1beta1.BackendTypeVllm,
					Image:   "profiler:latest",
				},
			},
			wantErr: true,
			errMsg:  "spec updates are forbidden while the resource is in phase",
		},
		{
			name: "spec change rejected during Deploying phase",
			oldRequest: &nvidiacomv1beta1.DynamoGraphDeploymentRequest{
				Spec: nvidiacomv1beta1.DynamoGraphDeploymentRequestSpec{
					Model:   "llama-3-8b",
					Backend: nvidiacomv1beta1.BackendTypeVllm,
					Image:   "profiler:latest",
				},
				Status: nvidiacomv1beta1.DynamoGraphDeploymentRequestStatus{
					Phase: nvidiacomv1beta1.DGDRPhaseDeploying,
				},
			},
			newRequest: &nvidiacomv1beta1.DynamoGraphDeploymentRequest{
				Spec: nvidiacomv1beta1.DynamoGraphDeploymentRequestSpec{
					Model:   "llama-3-70b",
					Backend: nvidiacomv1beta1.BackendTypeVllm,
					Image:   "profiler:latest",
				},
			},
			wantErr: true,
			errMsg:  "spec updates are forbidden while the resource is in phase",
		},
		{
			name: "spec change rejected during Deployed phase",
			oldRequest: &nvidiacomv1beta1.DynamoGraphDeploymentRequest{
				Spec: nvidiacomv1beta1.DynamoGraphDeploymentRequestSpec{
					Model:   "llama-3-8b",
					Backend: nvidiacomv1beta1.BackendTypeVllm,
					Image:   "profiler:latest",
				},
				Status: nvidiacomv1beta1.DynamoGraphDeploymentRequestStatus{
					Phase: nvidiacomv1beta1.DGDRPhaseDeployed,
				},
			},
			newRequest: &nvidiacomv1beta1.DynamoGraphDeploymentRequest{
				Spec: nvidiacomv1beta1.DynamoGraphDeploymentRequestSpec{
					Model:   "llama-3-70b",
					Backend: nvidiacomv1beta1.BackendTypeVllm,
					Image:   "profiler:latest",
				},
			},
			wantErr: true,
			errMsg:  "spec updates are forbidden while the resource is in phase",
		},
		{
			name: "no spec change during immutable phase is allowed",
			oldRequest: &nvidiacomv1beta1.DynamoGraphDeploymentRequest{
				Spec: nvidiacomv1beta1.DynamoGraphDeploymentRequestSpec{
					Model:   "llama-3-8b",
					Backend: nvidiacomv1beta1.BackendTypeVllm,
					Image:   "profiler:latest",
				},
				Status: nvidiacomv1beta1.DynamoGraphDeploymentRequestStatus{
					Phase: nvidiacomv1beta1.DGDRPhaseProfiling,
				},
			},
			newRequest: &nvidiacomv1beta1.DynamoGraphDeploymentRequest{
				Spec: nvidiacomv1beta1.DynamoGraphDeploymentRequestSpec{
					Model:   "llama-3-8b",
					Backend: nvidiacomv1beta1.BackendTypeVllm,
					Image:   "profiler:latest",
				},
			},
			wantErr: false,
		},
		{
			name: "spec change allowed during Failed phase",
			oldRequest: &nvidiacomv1beta1.DynamoGraphDeploymentRequest{
				Spec: nvidiacomv1beta1.DynamoGraphDeploymentRequestSpec{
					Model:   "llama-3-8b",
					Backend: nvidiacomv1beta1.BackendTypeVllm,
					Image:   "profiler:latest",
				},
				Status: nvidiacomv1beta1.DynamoGraphDeploymentRequestStatus{
					Phase: nvidiacomv1beta1.DGDRPhaseFailed,
				},
			},
			newRequest: &nvidiacomv1beta1.DynamoGraphDeploymentRequest{
				Spec: nvidiacomv1beta1.DynamoGraphDeploymentRequestSpec{
					Model:   "llama-3-70b",
					Backend: nvidiacomv1beta1.BackendTypeVllm,
					Image:   "profiler:latest",
362
363
364
365
366
367
368
369
				},
			},
			wantErr: false,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
370
			validator := NewDynamoGraphDeploymentRequestValidator(tt.newRequest, true, true)
371
372
373
			warnings, err := validator.ValidateUpdate(tt.oldRequest)

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

378
379
380
381
382
383
			if tt.wantErr && tt.errMsg != "" {
				if !strings.Contains(err.Error(), tt.errMsg) {
					t.Errorf("ValidateUpdate() error %q does not contain %q", err.Error(), tt.errMsg)
				}
			}

384
			if tt.wantWarnings && len(warnings) == 0 {
385
386
387
388
				t.Errorf("ValidateUpdate() expected warnings but got none")
			}
			if !tt.wantWarnings && len(warnings) > 0 {
				t.Errorf("ValidateUpdate() unexpected warnings: %v", warnings)
389
390
391
392
			}
		})
	}
}