dynamographdeploymentrequest_test.go 12.6 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
				},
			},
			isClusterWide: true,
		},
52

53
		{
54
55
56
57
58
59
60
61
			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,
62
				},
63
64
65
66
67
68
69
70
71
72
73
74
75
			},
			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,
76
77
78
79
80
				},
			},
			isClusterWide: true,
		},
		{
81
82
83
84
85
86
87
88
			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,
89
				},
90
91
92
93
94
95
96
97
98
99
100
101
			},
			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,
102
103
104
105
106
				},
			},
			isClusterWide: true,
		},
		{
107
108
109
110
111
112
113
114
			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,
115
				},
116
117
118
119
120
121
122
123
			},
			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{
124
					Model:   "llama-3-8b",
125
126
127
128
129
130
					Backend: nvidiacomv1beta1.BackendTypeVllm,
					Image:   "profiler:latest",
					Hardware: &nvidiacomv1beta1.HardwareSpec{
						GPUSKU:         "H100-SXM5-80GB",
						VRAMMB:         &vram,
						NumGPUsPerNode: &gpuCount,
131
132
133
					},
				},
			},
134
135
136
137
138
			isClusterWide:       false,
			gpuDiscoveryEnabled: false,
		},
		{
			name: "namespace-scoped operator with GPU discovery enabled (should pass without manual config)",
139
140
141
			request: &nvidiacomv1beta1.DynamoGraphDeploymentRequest{
				ObjectMeta: metav1.ObjectMeta{Name: "test-dgdr", Namespace: "default"},
				Spec: nvidiacomv1beta1.DynamoGraphDeploymentRequestSpec{
142
					Model:   "llama-3-8b",
143
144
					Backend: nvidiacomv1beta1.BackendTypeVllm,
					Image:   "profiler:latest",
145
146
147
148
149
150
151
				},
			},
			isClusterWide:       false,
			gpuDiscoveryEnabled: true,
		},
		{
			name: "namespace-scoped operator with GPU discovery disabled and no hardware config (should error)",
152
153
154
			request: &nvidiacomv1beta1.DynamoGraphDeploymentRequest{
				ObjectMeta: metav1.ObjectMeta{Name: "test-dgdr", Namespace: "default"},
				Spec: nvidiacomv1beta1.DynamoGraphDeploymentRequestSpec{
155
					Model:   "llama-3-8b",
156
157
					Backend: nvidiacomv1beta1.BackendTypeVllm,
					Image:   "profiler:latest",
158
159
160
161
162
				},
			},
			isClusterWide:       false,
			gpuDiscoveryEnabled: false,
			errMsg:              "GPU hardware configuration required: GPU discovery is disabled",
163
164
		},
		{
165
			name: "thorough+auto is invalid regardless of image",
166
167
168
169
170
171
172
			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:          "",
173
174
175
				},
			},
			isClusterWide: true,
176
			errMsg:        "spec.searchStrategy",
177
178
179
180
181
		},
	}

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

185
186
187
			wantErr := tt.errMsg != ""
			if (err != nil) != wantErr {
				t.Errorf("Validate() error = %v, wantErr %v", err, wantErr)
188
189
				return
			}
190
191
192
193
			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)
194
195
196
197
198
199
200
201
202
203
					}
				}
			}
		})
	}
}

func TestDynamoGraphDeploymentRequestValidator_ValidateUpdate(t *testing.T) {
	tests := []struct {
		name         string
204
205
		oldRequest   *nvidiacomv1beta1.DynamoGraphDeploymentRequest
		newRequest   *nvidiacomv1beta1.DynamoGraphDeploymentRequest
206
		wantErr      bool
207
		errMsg       string
208
209
210
211
		wantWarnings bool
	}{
		{
			name: "no changes",
212
213
			oldRequest: &nvidiacomv1beta1.DynamoGraphDeploymentRequest{
				Spec: nvidiacomv1beta1.DynamoGraphDeploymentRequestSpec{
214
					Model:   "llama-3-8b",
215
216
					Backend: nvidiacomv1beta1.BackendTypeVllm,
					Image:   "profiler:latest",
217
218
				},
			},
219
220
			newRequest: &nvidiacomv1beta1.DynamoGraphDeploymentRequest{
				Spec: nvidiacomv1beta1.DynamoGraphDeploymentRequestSpec{
221
					Model:   "llama-3-8b",
222
223
					Backend: nvidiacomv1beta1.BackendTypeVllm,
					Image:   "profiler:latest",
224
225
226
227
228
				},
			},
			wantErr: false,
		},
		{
229
230
231
			name: "changing model name is allowed when not in immutable phase",
			oldRequest: &nvidiacomv1beta1.DynamoGraphDeploymentRequest{
				Spec: nvidiacomv1beta1.DynamoGraphDeploymentRequestSpec{
232
					Model:   "llama-3-8b",
233
234
					Backend: nvidiacomv1beta1.BackendTypeVllm,
					Image:   "profiler:latest",
235
236
				},
			},
237
238
			newRequest: &nvidiacomv1beta1.DynamoGraphDeploymentRequest{
				Spec: nvidiacomv1beta1.DynamoGraphDeploymentRequestSpec{
239
					Model:   "llama-3-70b",
240
241
242
243
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
					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",
350
351
352
353
354
355
356
357
				},
			},
			wantErr: false,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
358
			validator := NewDynamoGraphDeploymentRequestValidator(tt.newRequest, true, true)
359
360
361
			warnings, err := validator.ValidateUpdate(tt.oldRequest)

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

366
367
368
369
370
371
			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)
				}
			}

372
			if tt.wantWarnings && len(warnings) == 0 {
373
374
375
376
				t.Errorf("ValidateUpdate() expected warnings but got none")
			}
			if !tt.wantWarnings && len(warnings) > 0 {
				t.Errorf("ValidateUpdate() unexpected warnings: %v", warnings)
377
378
379
380
			}
		})
	}
}