@@ -778,26 +778,30 @@ func Test_ExtractRayCluster(t *testing.T) {
778
778
779
779
func Test_GenDNSHostnames (t * testing.T ) {
780
780
tests := map [string ]struct {
781
+ clusterName string
781
782
replicaIndex int
782
783
numOfHosts int32
783
784
expectedHostnames string
784
785
expectedError error
785
786
}{
786
787
"genDNSHostnames with NumOfHosts == 0" : {
787
788
// a workergroup can't have NumOfHosts set to 0 so this should error out
789
+ clusterName : "test-cluster" ,
788
790
replicaIndex : 0 ,
789
791
numOfHosts : int32 (0 ),
790
792
expectedError : errors .New ("workerGroupSpec NumOfHosts not set" ),
791
793
},
792
794
"genDNSHostnames with NumOfHosts == 1" : {
793
795
// Single-host worker group, should return a single DNS hostname. This function will
794
796
// never be called for single-host groups, but we don't necessarily want it to error if it does.
797
+ clusterName : "test-cluster" ,
795
798
replicaIndex : 0 ,
796
799
numOfHosts : int32 (1 ),
797
800
expectedHostnames : fmt .Sprintf ("%s-%d-%d.%s-%s" , "test-group" , 0 , 0 , "test-cluster" , headlessServiceSuffix ),
798
801
},
799
802
"genDNSHostnames with NumOfHosts > 1" : {
800
803
// multi-host worker group, should return a string list of DNS hostnames for the given replica
804
+ clusterName : "test-cluster" ,
801
805
replicaIndex : 1 ,
802
806
numOfHosts : int32 (4 ),
803
807
expectedHostnames : strings .Join ([]string {fmt .Sprintf ("%s-%d-%d.%s-%s" , "test-group" , 1 , 0 , "test-cluster" , headlessServiceSuffix ),
@@ -806,12 +810,21 @@ func Test_GenDNSHostnames(t *testing.T) {
806
810
fmt .Sprintf ("%s-%d-%d.%s-%s" , "test-group" , 1 , 3 , "test-cluster" , headlessServiceSuffix ),
807
811
}, "," ),
808
812
},
813
+ "genDNSHostnames with long RayCluster name" : {
814
+ // Multi-host worker group in a RayCluster with a name that will be truncated
815
+ clusterName : "long-raycluster-name-to-be-truncated" ,
816
+ replicaIndex : 1 ,
817
+ numOfHosts : int32 (2 ),
818
+ expectedHostnames : strings .Join ([]string {fmt .Sprintf ("%s-%d-%d.%s-%s" , "test-group" , 1 , 0 , "aycluster-name-to-be-truncated" , headlessServiceSuffix ),
819
+ fmt .Sprintf ("%s-%d-%d.%s-%s" , "test-group" , 1 , 1 , "aycluster-name-to-be-truncated" , headlessServiceSuffix ),
820
+ }, "," ),
821
+ },
809
822
}
810
823
811
824
// validate that genDNSHostnames correctly returns a string list of DNS addressable hostnames
812
825
for name , tc := range tests {
813
826
t .Run (name , func (t * testing.T ) {
814
- hostnames , err := genDNSHostnames (tc .numOfHosts , "test-group" , "test-cluster" , "test-namespace" , tc .replicaIndex )
827
+ hostnames , err := genDNSHostnames (tc .numOfHosts , "test-group" , tc . clusterName , "test-namespace" , tc .replicaIndex )
815
828
if err != nil {
816
829
assert .Equal (t , tc .expectedError , err )
817
830
} else {
@@ -823,21 +836,15 @@ func Test_GenDNSHostnames(t *testing.T) {
823
836
824
837
func Test_InjectHostnames (t * testing.T ) {
825
838
tests := map [string ]struct {
826
- numOfHosts int
839
+ clusterName string
827
840
groupName string
828
841
expectedSubdomain string
829
842
expectedHostnames string
830
843
}{
831
- "injectHostnames for single-host worker group" : {
832
- // should create a patch to set the subdomain and a single TPU_WORKER_HOSTNAMES DNS hostname
833
- numOfHosts : 1 ,
834
- groupName : "test-group-name" ,
835
- expectedSubdomain : fmt .Sprintf ("%s-%s" , "test-cluster" , headlessServiceSuffix ),
836
- expectedHostnames : fmt .Sprintf ("%s-%d-%d.%s-%s" , "test-group" , 0 , 0 , "test-cluster" , headlessServiceSuffix ),
837
- },
838
844
"injectHostnames for multi-host worker group" : {
839
- // should create a patch to set the subdomain and TPU_WORKER_HOSTNAMES for all hosts
840
- numOfHosts : 1 ,
845
+ // Should create a patch to set the subdomain and TPU_WORKER_HOSTNAMES for all hosts.
846
+ // This function is only called for multi-host TPU worker groups.
847
+ clusterName : "test-cluster" ,
841
848
groupName : "test-group-name" ,
842
849
expectedSubdomain : fmt .Sprintf ("%s-%s" , "test-cluster" , headlessServiceSuffix ),
843
850
expectedHostnames : strings .Join ([]string {fmt .Sprintf ("%s-%d-%d.%s-%s" , "test-group" , 1 , 0 , "test-cluster" , headlessServiceSuffix ),
@@ -846,21 +853,33 @@ func Test_InjectHostnames(t *testing.T) {
846
853
fmt .Sprintf ("%s-%d-%d.%s-%s" , "test-group" , 1 , 3 , "test-cluster" , headlessServiceSuffix ),
847
854
}, "," ),
848
855
},
856
+ "injectHostnames for multi-host worker group with truncated service name" : {
857
+ // Should create a patch to set the subdomain and TPU_WORKER_HOSTNAMES for all hosts, with the
858
+ // correct subdomain truncated to match the created service name.
859
+ clusterName : "extremely-long-test-raycluster-name" ,
860
+ groupName : "test-group-name" ,
861
+ expectedSubdomain : fmt .Sprintf ("%s-%s" , "mely-long-test-raycluster-name" , headlessServiceSuffix ),
862
+ expectedHostnames : strings .Join ([]string {fmt .Sprintf ("%s-%d-%d.%s-%s" , "test-group" , 1 , 0 , "mely-long-test-raycluster-name" , headlessServiceSuffix ),
863
+ fmt .Sprintf ("%s-%d-%d.%s-%s" , "test-group" , 1 , 1 , "mely-long-test-raycluster-name" , headlessServiceSuffix ),
864
+ fmt .Sprintf ("%s-%d-%d.%s-%s" , "test-group" , 1 , 2 , "mely-long-test-raycluster-name" , headlessServiceSuffix ),
865
+ fmt .Sprintf ("%s-%d-%d.%s-%s" , "test-group" , 1 , 3 , "mely-long-test-raycluster-name" , headlessServiceSuffix ),
866
+ }, "," ),
867
+ },
849
868
}
850
869
851
870
// check that a valid subdomain and TPU_WORKER_HOSTNAMES are injected into the Pod
852
871
for name , tc := range tests {
853
872
t .Run (name , func (t * testing.T ) {
854
- testPod := getTestTPUWorker ("test-cluster" , "test-group" , "test-namespace" , "tpu-v4-podslice" , "2x2x1 " , "4" )
873
+ testPod := getTestTPUWorker (tc . clusterName , tc . groupName , "test-namespace" , "tpu-v4-podslice" , "2x2x2 " , "4" )
855
874
expectedEnv := []corev1.EnvVar {corev1.EnvVar {Name : "TPU_WORKER_HOSTNAMES" , Value : tc .expectedHostnames }}
856
- expectedPatches := []patch {}
857
- injectHostnames ("test-cluster" , tc .expectedHostnames , "/spec/containers/0/env" , testPod .Spec .Containers [0 ], & expectedPatches )
875
+ patches := []patch {}
876
+ injectHostnames (tc . clusterName , tc .expectedHostnames , "/spec/containers/0/env" , testPod .Spec .Containers [0 ], & patches )
858
877
// check subdomain patch
859
- assert .Equal (t , "/spec/subdomain" , expectedPatches [0 ]["path" ])
860
- assert .Equal (t , tc .expectedSubdomain , expectedPatches [0 ]["value" ])
878
+ assert .Equal (t , "/spec/subdomain" , patches [0 ]["path" ])
879
+ assert .Equal (t , tc .expectedSubdomain , patches [0 ]["value" ])
861
880
// check hostnames patch
862
- assert .Equal (t , "/spec/containers/0/env" , expectedPatches [1 ]["path" ])
863
- assert .Equal (t , expectedEnv , expectedPatches [1 ]["value" ])
881
+ assert .Equal (t , "/spec/containers/0/env" , patches [1 ]["path" ])
882
+ assert .Equal (t , expectedEnv , patches [1 ]["value" ])
864
883
})
865
884
}
866
885
}
@@ -1464,3 +1483,26 @@ func Test_MutatePod(t *testing.T) {
1464
1483
})
1465
1484
}
1466
1485
}
1486
+
1487
+ func Test_GenerateHeadlessServiceName (t * testing.T ) {
1488
+ tests := map [string ]struct {
1489
+ testRayClusterName string
1490
+ expectedServiceName string
1491
+ }{
1492
+ "RayCluster name + headless-worker-svc is less than 50 chars, no truncation" : {
1493
+ testRayClusterName : "test-raycluster" , // 15 chars
1494
+ expectedServiceName : "test-raycluster-headless-worker-svc" , // 35 chars
1495
+ },
1496
+ "RayCluster name + headless-worker-svc is more than 50 chars, name is truncated" : {
1497
+ testRayClusterName : "extremely-long-test-raycluster-name" , // 35 chars
1498
+ expectedServiceName : "mely-long-test-raycluster-name-headless-worker-svc" , // 50 chars
1499
+ },
1500
+ }
1501
+
1502
+ for name , tc := range tests {
1503
+ t .Run (name , func (t * testing.T ) {
1504
+ serviceName := generateHeadlessServiceName (tc .testRayClusterName )
1505
+ assert .Equal (t , tc .expectedServiceName , serviceName )
1506
+ })
1507
+ }
1508
+ }
0 commit comments