|
| 1 | +package kaischeduler |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + corev1 "k8s.io/api/core/v1" |
| 9 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 10 | + |
| 11 | + rayv1 "github.com/ray-project/kuberay/ray-operator/apis/ray/v1" |
| 12 | +) |
| 13 | + |
| 14 | +func createTestRayCluster(name, namespace string, labels map[string]string) *rayv1.RayCluster { |
| 15 | + return &rayv1.RayCluster{ |
| 16 | + ObjectMeta: metav1.ObjectMeta{ |
| 17 | + Name: name, |
| 18 | + Namespace: namespace, |
| 19 | + Labels: labels, |
| 20 | + }, |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +func createTestPod(name, namespace string) *corev1.Pod { |
| 25 | + return &corev1.Pod{ |
| 26 | + ObjectMeta: metav1.ObjectMeta{ |
| 27 | + Name: name, |
| 28 | + Namespace: namespace, |
| 29 | + Labels: map[string]string{ |
| 30 | + "ray.io/cluster": "test-cluster", |
| 31 | + "ray.io/node-type": "worker", |
| 32 | + "app": "ray", |
| 33 | + }, |
| 34 | + }, |
| 35 | + Spec: corev1.PodSpec{ |
| 36 | + Containers: []corev1.Container{{ |
| 37 | + Name: "ray-worker", |
| 38 | + Image: "rayproject/ray:latest", |
| 39 | + }}, |
| 40 | + }, |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +func TestAddMetadataToPod_WithQueueLabel(t *testing.T) { |
| 45 | + a := assert.New(t) |
| 46 | + scheduler := &KaiScheduler{} |
| 47 | + ctx := context.Background() |
| 48 | + |
| 49 | + // Create RayCluster with queue label |
| 50 | + rayCluster := createTestRayCluster("test-cluster", "default", map[string]string{ |
| 51 | + QueueLabelName: "test-queue", |
| 52 | + }) |
| 53 | + pod := createTestPod("test-pod", "default") |
| 54 | + |
| 55 | + // Call AddMetadataToPod |
| 56 | + scheduler.AddMetadataToPod(ctx, rayCluster, "test-group", pod) |
| 57 | + |
| 58 | + // Assert scheduler name is set to kai-scheduler |
| 59 | + a.Equal("kai-scheduler", pod.Spec.SchedulerName) |
| 60 | + |
| 61 | + // Assert queue label is propagated to pod |
| 62 | + a.NotNil(pod.Labels) |
| 63 | + a.Equal("test-queue", pod.Labels[QueueLabelName]) |
| 64 | +} |
| 65 | + |
| 66 | +func TestAddMetadataToPod_WithoutQueueLabel(t *testing.T) { |
| 67 | + a := assert.New(t) |
| 68 | + scheduler := &KaiScheduler{} |
| 69 | + ctx := context.Background() |
| 70 | + |
| 71 | + // Create RayCluster without queue label |
| 72 | + rayCluster := createTestRayCluster("test-cluster", "default", map[string]string{}) |
| 73 | + pod := createTestPod("test-pod", "default") |
| 74 | + |
| 75 | + // Call AddMetadataToPod |
| 76 | + scheduler.AddMetadataToPod(ctx, rayCluster, "test-group", pod) |
| 77 | + |
| 78 | + // Assert scheduler name is still set (always required) |
| 79 | + a.Equal("kai-scheduler", pod.Spec.SchedulerName) |
| 80 | + |
| 81 | + // Assert queue label is not added to pod when missing from RayCluster |
| 82 | + if pod.Labels != nil { |
| 83 | + _, exists := pod.Labels[QueueLabelName] |
| 84 | + a.False(exists) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func TestAddMetadataToPod_WithEmptyQueueLabel(t *testing.T) { |
| 89 | + a := assert.New(t) |
| 90 | + scheduler := &KaiScheduler{} |
| 91 | + ctx := context.Background() |
| 92 | + |
| 93 | + // Create RayCluster with empty queue label |
| 94 | + rayCluster := createTestRayCluster("test-cluster", "default", map[string]string{ |
| 95 | + QueueLabelName: "", |
| 96 | + }) |
| 97 | + pod := createTestPod("test-pod", "default") |
| 98 | + |
| 99 | + // Call AddMetadataToPod |
| 100 | + scheduler.AddMetadataToPod(ctx, rayCluster, "test-group", pod) |
| 101 | + |
| 102 | + // Assert scheduler name is still set |
| 103 | + a.Equal("kai-scheduler", pod.Spec.SchedulerName) |
| 104 | + |
| 105 | + // Assert empty queue label is treated as missing |
| 106 | + if pod.Labels != nil { |
| 107 | + _, exists := pod.Labels[QueueLabelName] |
| 108 | + a.False(exists) |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +func TestAddMetadataToPod_PreservesExistingPodLabels(t *testing.T) { |
| 113 | + a := assert.New(t) |
| 114 | + scheduler := &KaiScheduler{} |
| 115 | + ctx := context.Background() |
| 116 | + |
| 117 | + // Create RayCluster with queue label |
| 118 | + rayCluster := createTestRayCluster("test-cluster", "default", map[string]string{ |
| 119 | + QueueLabelName: "test-queue", |
| 120 | + }) |
| 121 | + |
| 122 | + // Create pod with existing labels |
| 123 | + pod := createTestPod("test-pod", "default") |
| 124 | + pod.Labels = map[string]string{ |
| 125 | + "existing-label": "existing-value", |
| 126 | + "app": "ray", |
| 127 | + } |
| 128 | + |
| 129 | + // Call AddMetadataToPod |
| 130 | + scheduler.AddMetadataToPod(ctx, rayCluster, "test-group", pod) |
| 131 | + |
| 132 | + // Assert scheduler name is set |
| 133 | + a.Equal("kai-scheduler", pod.Spec.SchedulerName) |
| 134 | + |
| 135 | + // Assert queue label is added |
| 136 | + a.Equal("test-queue", pod.Labels[QueueLabelName]) |
| 137 | + |
| 138 | + // Assert existing labels are preserved |
| 139 | + a.Equal("existing-value", pod.Labels["existing-label"]) |
| 140 | + a.Equal("ray", pod.Labels["app"]) |
| 141 | +} |
0 commit comments