dynamographdeploymentscalingadapter_controller_test.go 14.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
 * SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
 * 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 controller

import (
	"context"
	"testing"

24
25
	"github.com/ai-dynamo/dynamo/deploy/operator/api/v1alpha1"
	"github.com/ai-dynamo/dynamo/deploy/operator/internal/consts"
26
27
28
29
30
31
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
132
133
134
135
136
137
138
139
140
141
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
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
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
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/types"
	"k8s.io/client-go/kubernetes/scheme"
	"k8s.io/client-go/tools/record"
	"k8s.io/utils/ptr"
	ctrl "sigs.k8s.io/controller-runtime"
	"sigs.k8s.io/controller-runtime/pkg/client"
	"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

func TestDynamoGraphDeploymentScalingAdapterReconciler_Reconcile(t *testing.T) {
	// Register custom types with the scheme
	if err := v1alpha1.AddToScheme(scheme.Scheme); err != nil {
		t.Fatalf("Failed to add v1alpha1 to scheme: %v", err)
	}

	tests := []struct {
		name                   string
		adapter                *v1alpha1.DynamoGraphDeploymentScalingAdapter
		dgd                    *v1alpha1.DynamoGraphDeployment
		expectedDGDReplicas    int32
		expectedStatusReplicas int32
		expectError            bool
		expectRequeue          bool
	}{
		{
			name: "updates DGD replicas when DGDSA spec differs",
			adapter: &v1alpha1.DynamoGraphDeploymentScalingAdapter{
				ObjectMeta: metav1.ObjectMeta{
					Name:      "test-dgd-frontend",
					Namespace: "default",
				},
				Spec: v1alpha1.DynamoGraphDeploymentScalingAdapterSpec{
					Replicas: 5,
					DGDRef: v1alpha1.DynamoGraphDeploymentServiceRef{
						Name:        "test-dgd",
						ServiceName: "Frontend",
					},
				},
			},
			dgd: &v1alpha1.DynamoGraphDeployment{
				ObjectMeta: metav1.ObjectMeta{
					Name:      "test-dgd",
					Namespace: "default",
				},
				Spec: v1alpha1.DynamoGraphDeploymentSpec{
					Services: map[string]*v1alpha1.DynamoComponentDeploymentSharedSpec{
						"Frontend": {
							Replicas: ptr.To(int32(2)),
						},
					},
				},
			},
			expectedDGDReplicas:    5,
			expectedStatusReplicas: 5,
			expectError:            false,
		},
		{
			name: "no update when replicas already match",
			adapter: &v1alpha1.DynamoGraphDeploymentScalingAdapter{
				ObjectMeta: metav1.ObjectMeta{
					Name:      "test-dgd-frontend",
					Namespace: "default",
				},
				Spec: v1alpha1.DynamoGraphDeploymentScalingAdapterSpec{
					Replicas: 3,
					DGDRef: v1alpha1.DynamoGraphDeploymentServiceRef{
						Name:        "test-dgd",
						ServiceName: "Frontend",
					},
				},
			},
			dgd: &v1alpha1.DynamoGraphDeployment{
				ObjectMeta: metav1.ObjectMeta{
					Name:      "test-dgd",
					Namespace: "default",
				},
				Spec: v1alpha1.DynamoGraphDeploymentSpec{
					Services: map[string]*v1alpha1.DynamoComponentDeploymentSharedSpec{
						"Frontend": {
							Replicas: ptr.To(int32(3)),
						},
					},
				},
			},
			expectedDGDReplicas:    3,
			expectedStatusReplicas: 3,
			expectError:            false,
		},
		{
			name: "uses default replicas (1) when DGD service has no replicas set",
			adapter: &v1alpha1.DynamoGraphDeploymentScalingAdapter{
				ObjectMeta: metav1.ObjectMeta{
					Name:      "test-dgd-worker",
					Namespace: "default",
				},
				Spec: v1alpha1.DynamoGraphDeploymentScalingAdapterSpec{
					Replicas: 4,
					DGDRef: v1alpha1.DynamoGraphDeploymentServiceRef{
						Name:        "test-dgd",
						ServiceName: "worker",
					},
				},
			},
			dgd: &v1alpha1.DynamoGraphDeployment{
				ObjectMeta: metav1.ObjectMeta{
					Name:      "test-dgd",
					Namespace: "default",
				},
				Spec: v1alpha1.DynamoGraphDeploymentSpec{
					Services: map[string]*v1alpha1.DynamoComponentDeploymentSharedSpec{
						"worker": {}, // no replicas set
					},
				},
			},
			expectedDGDReplicas:    4,
			expectedStatusReplicas: 4,
			expectError:            false,
		},
		{
			name: "error when service not found in DGD",
			adapter: &v1alpha1.DynamoGraphDeploymentScalingAdapter{
				ObjectMeta: metav1.ObjectMeta{
					Name:      "test-dgd-missing",
					Namespace: "default",
				},
				Spec: v1alpha1.DynamoGraphDeploymentScalingAdapterSpec{
					Replicas: 2,
					DGDRef: v1alpha1.DynamoGraphDeploymentServiceRef{
						Name:        "test-dgd",
						ServiceName: "nonexistent",
					},
				},
			},
			dgd: &v1alpha1.DynamoGraphDeployment{
				ObjectMeta: metav1.ObjectMeta{
					Name:      "test-dgd",
					Namespace: "default",
				},
				Spec: v1alpha1.DynamoGraphDeploymentSpec{
					Services: map[string]*v1alpha1.DynamoComponentDeploymentSharedSpec{
						"Frontend": {
							Replicas: ptr.To(int32(1)),
						},
					},
				},
			},
			expectError: true,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			// Build initial objects
			var initObjs []client.Object
			initObjs = append(initObjs, tt.adapter, tt.dgd)

			// Create fake client with status subresource support
			fakeClient := fake.NewClientBuilder().
				WithScheme(scheme.Scheme).
				WithObjects(initObjs...).
				WithStatusSubresource(&v1alpha1.DynamoGraphDeploymentScalingAdapter{}).
				Build()

			// Create reconciler
			r := &DynamoGraphDeploymentScalingAdapterReconciler{
				Client:   fakeClient,
				Scheme:   scheme.Scheme,
				Recorder: record.NewFakeRecorder(10),
			}

			// Run Reconcile
			ctx := context.Background()
			req := ctrl.Request{
				NamespacedName: types.NamespacedName{
					Name:      tt.adapter.Name,
					Namespace: tt.adapter.Namespace,
				},
			}

			result, err := r.Reconcile(ctx, req)

			// Check error expectation
			if tt.expectError && err == nil {
				t.Errorf("Expected error, but got none")
			}
			if !tt.expectError && err != nil {
				t.Errorf("Unexpected error: %v", err)
			}

			// Skip further checks if error was expected
			if tt.expectError {
				return
			}

			// Check requeue
			if tt.expectRequeue && result.RequeueAfter == 0 {
				t.Errorf("Expected requeue, but got none")
			}

			// Verify DGD replicas were updated
			updatedDGD := &v1alpha1.DynamoGraphDeployment{}
			if err := fakeClient.Get(ctx, types.NamespacedName{Name: tt.dgd.Name, Namespace: tt.dgd.Namespace}, updatedDGD); err != nil {
				t.Fatalf("Failed to get updated DGD: %v", err)
			}

			service, exists := updatedDGD.Spec.Services[tt.adapter.Spec.DGDRef.ServiceName]
			if !exists {
				t.Fatalf("Service %s not found in updated DGD", tt.adapter.Spec.DGDRef.ServiceName)
			}

			actualReplicas := int32(1)
			if service.Replicas != nil {
				actualReplicas = *service.Replicas
			}

			if actualReplicas != tt.expectedDGDReplicas {
				t.Errorf("DGD service replicas = %d, expected %d", actualReplicas, tt.expectedDGDReplicas)
			}

			// Verify adapter status was updated
			updatedAdapter := &v1alpha1.DynamoGraphDeploymentScalingAdapter{}
			if err := fakeClient.Get(ctx, types.NamespacedName{Name: tt.adapter.Name, Namespace: tt.adapter.Namespace}, updatedAdapter); err != nil {
				t.Fatalf("Failed to get updated adapter: %v", err)
			}

			if updatedAdapter.Status.Replicas != tt.expectedStatusReplicas {
				t.Errorf("Adapter status.replicas = %d, expected %d", updatedAdapter.Status.Replicas, tt.expectedStatusReplicas)
			}

			// Verify selector is set
			if updatedAdapter.Status.Selector == "" {
				t.Errorf("Adapter status.selector is empty, expected non-empty")
			}
		})
	}
}

func TestDynamoGraphDeploymentScalingAdapterReconciler_Reconcile_NotFound(t *testing.T) {
	// Register custom types with the scheme
	if err := v1alpha1.AddToScheme(scheme.Scheme); err != nil {
		t.Fatalf("Failed to add v1alpha1 to scheme: %v", err)
	}

	// Create fake client with no objects
	fakeClient := fake.NewClientBuilder().
		WithScheme(scheme.Scheme).
		Build()

	r := &DynamoGraphDeploymentScalingAdapterReconciler{
		Client:   fakeClient,
		Scheme:   scheme.Scheme,
		Recorder: record.NewFakeRecorder(10),
	}

	ctx := context.Background()
	req := ctrl.Request{
		NamespacedName: types.NamespacedName{
			Name:      "nonexistent",
			Namespace: "default",
		},
	}

	// Should return no error when adapter not found (client.IgnoreNotFound)
	result, err := r.Reconcile(ctx, req)
	if err != nil {
		t.Errorf("Expected no error for not found adapter, got: %v", err)
	}
	if result.RequeueAfter != 0 {
		t.Errorf("Expected no requeueAfter for not found adapter, got: %v", result.RequeueAfter)
	}
}

func TestDynamoGraphDeploymentScalingAdapterReconciler_Reconcile_DGDNotFound(t *testing.T) {
	// Register custom types with the scheme
	if err := v1alpha1.AddToScheme(scheme.Scheme); err != nil {
		t.Fatalf("Failed to add v1alpha1 to scheme: %v", err)
	}

	adapter := &v1alpha1.DynamoGraphDeploymentScalingAdapter{
		ObjectMeta: metav1.ObjectMeta{
			Name:      "test-dgd-frontend",
			Namespace: "default",
		},
		Spec: v1alpha1.DynamoGraphDeploymentScalingAdapterSpec{
			Replicas: 5,
			DGDRef: v1alpha1.DynamoGraphDeploymentServiceRef{
				Name:        "nonexistent-dgd",
				ServiceName: "Frontend",
			},
		},
	}

	fakeClient := fake.NewClientBuilder().
		WithScheme(scheme.Scheme).
		WithObjects(adapter).
		Build()

	r := &DynamoGraphDeploymentScalingAdapterReconciler{
		Client:   fakeClient,
		Scheme:   scheme.Scheme,
		Recorder: record.NewFakeRecorder(10),
	}

	ctx := context.Background()
	req := ctrl.Request{
		NamespacedName: types.NamespacedName{
			Name:      adapter.Name,
			Namespace: adapter.Namespace,
		},
	}

	// Should return error when DGD not found
	_, err := r.Reconcile(ctx, req)
	if err == nil {
		t.Errorf("Expected error when DGD not found, got none")
	}
}

func TestDynamoGraphDeploymentScalingAdapterReconciler_Reconcile_BeingDeleted(t *testing.T) {
	// Register custom types with the scheme
	if err := v1alpha1.AddToScheme(scheme.Scheme); err != nil {
		t.Fatalf("Failed to add v1alpha1 to scheme: %v", err)
	}

	now := metav1.Now()
	adapter := &v1alpha1.DynamoGraphDeploymentScalingAdapter{
		ObjectMeta: metav1.ObjectMeta{
			Name:              "test-dgd-frontend",
			Namespace:         "default",
			DeletionTimestamp: &now,
			Finalizers:        []string{"test-finalizer"}, // Required for deletion timestamp to be set
		},
		Spec: v1alpha1.DynamoGraphDeploymentScalingAdapterSpec{
			Replicas: 5,
			DGDRef: v1alpha1.DynamoGraphDeploymentServiceRef{
				Name:        "test-dgd",
				ServiceName: "Frontend",
			},
		},
	}

	dgd := &v1alpha1.DynamoGraphDeployment{
		ObjectMeta: metav1.ObjectMeta{
			Name:      "test-dgd",
			Namespace: "default",
		},
		Spec: v1alpha1.DynamoGraphDeploymentSpec{
			Services: map[string]*v1alpha1.DynamoComponentDeploymentSharedSpec{
				"Frontend": {
					Replicas: ptr.To(int32(2)),
				},
			},
		},
	}

	fakeClient := fake.NewClientBuilder().
		WithScheme(scheme.Scheme).
		WithObjects(adapter, dgd).
		Build()

	r := &DynamoGraphDeploymentScalingAdapterReconciler{
		Client:   fakeClient,
		Scheme:   scheme.Scheme,
		Recorder: record.NewFakeRecorder(10),
	}

	ctx := context.Background()
	req := ctrl.Request{
		NamespacedName: types.NamespacedName{
			Name:      adapter.Name,
			Namespace: adapter.Namespace,
		},
	}

	// Should return no error and skip reconciliation
	result, err := r.Reconcile(ctx, req)
	if err != nil {
		t.Errorf("Expected no error for deleting adapter, got: %v", err)
	}
	if result.RequeueAfter != 0 {
		t.Errorf("Expected no requeueAfter for deleting adapter, got: %v", result.RequeueAfter)
	}

	// DGD replicas should NOT be updated (still 2)
	updatedDGD := &v1alpha1.DynamoGraphDeployment{}
	if err := fakeClient.Get(ctx, types.NamespacedName{Name: dgd.Name, Namespace: dgd.Namespace}, updatedDGD); err != nil {
		t.Fatalf("Failed to get DGD: %v", err)
	}

	if *updatedDGD.Spec.Services["Frontend"].Replicas != 2 {
		t.Errorf("DGD replicas should remain unchanged, got %d", *updatedDGD.Spec.Services["Frontend"].Replicas)
	}
}

func TestDynamoGraphDeploymentScalingAdapterReconciler_findAdaptersForDGD(t *testing.T) {
	// Register custom types with the scheme
	if err := v1alpha1.AddToScheme(scheme.Scheme); err != nil {
		t.Fatalf("Failed to add v1alpha1 to scheme: %v", err)
	}

	dgd := &v1alpha1.DynamoGraphDeployment{
		ObjectMeta: metav1.ObjectMeta{
			Name:      "test-dgd",
			Namespace: "default",
		},
	}

	// Adapters belonging to test-dgd
	adapter1 := &v1alpha1.DynamoGraphDeploymentScalingAdapter{
		ObjectMeta: metav1.ObjectMeta{
			Name:      "test-dgd-frontend",
			Namespace: "default",
			Labels: map[string]string{
				consts.KubeLabelDynamoGraphDeploymentName: "test-dgd",
			},
		},
		Spec: v1alpha1.DynamoGraphDeploymentScalingAdapterSpec{
			DGDRef: v1alpha1.DynamoGraphDeploymentServiceRef{
				Name:        "test-dgd",
				ServiceName: "Frontend",
			},
		},
	}

	adapter2 := &v1alpha1.DynamoGraphDeploymentScalingAdapter{
		ObjectMeta: metav1.ObjectMeta{
			Name:      "test-dgd-decode",
			Namespace: "default",
			Labels: map[string]string{
				consts.KubeLabelDynamoGraphDeploymentName: "test-dgd",
			},
		},
		Spec: v1alpha1.DynamoGraphDeploymentScalingAdapterSpec{
			DGDRef: v1alpha1.DynamoGraphDeploymentServiceRef{
				Name:        "test-dgd",
				ServiceName: "decode",
			},
		},
	}

	// Adapter belonging to different DGD
	adapterOther := &v1alpha1.DynamoGraphDeploymentScalingAdapter{
		ObjectMeta: metav1.ObjectMeta{
			Name:      "other-dgd-frontend",
			Namespace: "default",
			Labels: map[string]string{
				consts.KubeLabelDynamoGraphDeploymentName: "other-dgd",
			},
		},
		Spec: v1alpha1.DynamoGraphDeploymentScalingAdapterSpec{
			DGDRef: v1alpha1.DynamoGraphDeploymentServiceRef{
				Name:        "other-dgd",
				ServiceName: "Frontend",
			},
		},
	}

	fakeClient := fake.NewClientBuilder().
		WithScheme(scheme.Scheme).
		WithObjects(adapter1, adapter2, adapterOther).
		Build()

	r := &DynamoGraphDeploymentScalingAdapterReconciler{
		Client: fakeClient,
	}

	ctx := context.Background()
	requests := r.findAdaptersForDGD(ctx, dgd)

	// Should return 2 requests (for test-dgd adapters only)
	if len(requests) != 2 {
		t.Errorf("findAdaptersForDGD() returned %d requests, expected 2", len(requests))
	}

	// Verify correct adapters are returned
	expectedNames := map[string]bool{
		"test-dgd-frontend": true,
		"test-dgd-decode":   true,
	}

	for _, req := range requests {
		if !expectedNames[req.Name] {
			t.Errorf("Unexpected adapter in results: %s", req.Name)
		}
	}
}