Skip to content

Commit 529b021

Browse files
committed
Test: Add unit test for replica validation.
This is added since we moved the validation logic.
1 parent 2a1f87f commit 529b021

File tree

1 file changed

+95
-7
lines changed

1 file changed

+95
-7
lines changed

ray-operator/controllers/ray/utils/validation_test.go

Lines changed: 95 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,9 @@ func TestValidateRayClusterSpecEmptyContainers(t *testing.T) {
395395
Template: podTemplateSpec(nil, nil),
396396
}
397397
workerGroupSpecWithOneContainer := rayv1.WorkerGroupSpec{
398-
Template: podTemplateSpec(nil, nil),
398+
Template: podTemplateSpec(nil, nil),
399+
MinReplicas: ptr.To(int32(0)),
400+
MaxReplicas: ptr.To(int32(5)),
399401
}
400402
headGroupSpecWithNoContainers := *headGroupSpecWithOneContainer.DeepCopy()
401403
headGroupSpecWithNoContainers.Template.Spec.Containers = []corev1.Container{}
@@ -459,8 +461,10 @@ func TestValidateRayClusterSpecSuspendingWorkerGroup(t *testing.T) {
459461
Template: podTemplateSpec(nil, nil),
460462
}
461463
workerGroupSpecSuspended := rayv1.WorkerGroupSpec{
462-
GroupName: "worker-group-1",
463-
Template: podTemplateSpec(nil, nil),
464+
GroupName: "worker-group-1",
465+
Template: podTemplateSpec(nil, nil),
466+
MinReplicas: ptr.To(int32(0)),
467+
MaxReplicas: ptr.To(int32(5)),
464468
}
465469
workerGroupSpecSuspended.Suspend = ptr.To(true)
466470

@@ -692,8 +696,10 @@ func TestValidateRayClusterSpec_Resources(t *testing.T) {
692696
},
693697
WorkerGroupSpecs: []rayv1.WorkerGroupSpec{
694698
{
695-
GroupName: "worker-group",
696-
Template: podTemplateSpec(nil, nil),
699+
GroupName: "worker-group",
700+
Template: podTemplateSpec(nil, nil),
701+
MinReplicas: ptr.To(int32(0)),
702+
MaxReplicas: ptr.To(int32(5)),
697703
},
698704
},
699705
}
@@ -773,8 +779,10 @@ func TestValidateRayClusterSpec_Labels(t *testing.T) {
773779
},
774780
WorkerGroupSpecs: []rayv1.WorkerGroupSpec{
775781
{
776-
GroupName: "worker-group",
777-
Template: podTemplateSpec(nil, nil),
782+
GroupName: "worker-group",
783+
Template: podTemplateSpec(nil, nil),
784+
MinReplicas: ptr.To(int32(0)),
785+
MaxReplicas: ptr.To(int32(5)),
778786
},
779787
},
780788
}
@@ -2473,3 +2481,83 @@ func TestValidateRayClusterUpgradeOptions(t *testing.T) {
24732481
})
24742482
}
24752483
}
2484+
2485+
func TestValidateRayClusterSpec_WorkerGroupReplicaValidation(t *testing.T) {
2486+
createSpec := func() rayv1.RayClusterSpec {
2487+
return rayv1.RayClusterSpec{
2488+
EnableInTreeAutoscaling: ptr.To(false),
2489+
HeadGroupSpec: rayv1.HeadGroupSpec{
2490+
Template: podTemplateSpec(nil, nil),
2491+
},
2492+
}
2493+
}
2494+
2495+
tests := []struct {
2496+
name string
2497+
errorMsg string
2498+
expectError bool
2499+
spec rayv1.RayClusterSpec
2500+
}{
2501+
{
2502+
name: "minReplicas greater than maxReplicas",
2503+
spec: func() rayv1.RayClusterSpec {
2504+
s := createSpec()
2505+
s.WorkerGroupSpecs = []rayv1.WorkerGroupSpec{
2506+
{
2507+
GroupName: "worker-group-3",
2508+
Template: podTemplateSpec(nil, nil),
2509+
MinReplicas: ptr.To(int32(5)),
2510+
MaxReplicas: ptr.To(int32(3)),
2511+
},
2512+
}
2513+
return s
2514+
}(),
2515+
expectError: true,
2516+
errorMsg: "worker group worker-group-3 has minReplicas 5 greater than maxReplicas 3",
2517+
},
2518+
{
2519+
name: "replicas smaller than minReplicas when autoscaling disabled",
2520+
spec: func() rayv1.RayClusterSpec {
2521+
s := createSpec()
2522+
s.WorkerGroupSpecs = []rayv1.WorkerGroupSpec{
2523+
{
2524+
GroupName: "worker-group-3",
2525+
Template: podTemplateSpec(nil, nil),
2526+
Replicas: ptr.To(int32(1)),
2527+
MinReplicas: ptr.To(int32(2)),
2528+
MaxReplicas: ptr.To(int32(5)),
2529+
},
2530+
}
2531+
return s
2532+
}(),
2533+
expectError: false,
2534+
},
2535+
{
2536+
name: "valid when autoscaling enabled",
2537+
spec: func() rayv1.RayClusterSpec {
2538+
s := createSpec()
2539+
s.EnableInTreeAutoscaling = ptr.To(true)
2540+
s.WorkerGroupSpecs = []rayv1.WorkerGroupSpec{
2541+
{
2542+
GroupName: "worker-group-3",
2543+
Template: podTemplateSpec(nil, nil),
2544+
},
2545+
}
2546+
return s
2547+
}(),
2548+
expectError: false,
2549+
},
2550+
}
2551+
2552+
for _, tt := range tests {
2553+
t.Run(tt.name, func(t *testing.T) {
2554+
err := ValidateRayClusterSpec(&tt.spec, nil)
2555+
if tt.expectError {
2556+
require.Error(t, err)
2557+
require.EqualError(t, err, tt.errorMsg)
2558+
} else {
2559+
require.NoError(t, err)
2560+
}
2561+
})
2562+
}
2563+
}

0 commit comments

Comments
 (0)