Skip to content

Commit 9dc3145

Browse files
Add unit tests
1 parent ade10dc commit 9dc3145

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

ray-operator/controllers/ray/raycluster_controller_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ var _ = Context("Inside the default namespace", func() {
175175
Eventually(
176176
getClusterState(ctx, namespace, rayCluster.Name),
177177
time.Second*3, time.Millisecond*500).Should(Equal(rayv1.Ready))
178+
178179
})
179180

180181
// The following tests focus on checking whether KubeRay creates the correct number of Pods.
@@ -470,6 +471,15 @@ var _ = Context("Inside the default namespace", func() {
470471
getClusterState(ctx, namespace, rayCluster.Name),
471472
time.Second*3, time.Millisecond*500).Should(Equal(rayv1.Ready))
472473
})
474+
475+
It("RayCluster's .status.stateTransitionTimes should include a time for ready state", func() {
476+
Eventually(
477+
func() *metav1.Time {
478+
status := getClusterStatus(ctx, namespace, rayCluster.Name)()
479+
return status.StateTransitionTimes[rayv1.Ready]
480+
},
481+
time.Second*3, time.Millisecond*500).Should(Not(BeNil()))
482+
})
473483
})
474484

475485
Describe("RayCluster with a multi-host worker group", func() {

ray-operator/controllers/ray/raycluster_controller_unit_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1650,6 +1650,58 @@ func TestCalculateStatus(t *testing.T) {
16501650
assert.Equal(t, headNodeIP, newInstance.Status.Head.PodIP)
16511651
assert.Equal(t, headServiceIP, newInstance.Status.Head.ServiceIP)
16521652
assert.Equal(t, headService.Name, newInstance.Status.Head.ServiceName)
1653+
assert.NotNil(t, newInstance.Status.StateTransitionTimes, "Cluster state transition timestamp should be created")
1654+
assert.Equal(t, newInstance.Status.LastUpdateTime, newInstance.Status.StateTransitionTimes[rayv1.Unhealthy])
1655+
}
1656+
1657+
func TestStateTransitionTimes_NoStateChange(t *testing.T) {
1658+
setupTest(t)
1659+
1660+
// Create a new scheme with CRDs, Pod, Service schemes.
1661+
newScheme := runtime.NewScheme()
1662+
_ = rayv1.AddToScheme(newScheme)
1663+
_ = corev1.AddToScheme(newScheme)
1664+
1665+
// Mock data
1666+
headServiceIP := "aaa.bbb.ccc.ddd"
1667+
headService, err := common.BuildServiceForHeadPod(context.Background(), *testRayCluster, nil, nil)
1668+
assert.Nil(t, err, "Failed to build head service.")
1669+
headService.Spec.ClusterIP = headServiceIP
1670+
// headService.Spec.cont
1671+
headPod := &corev1.Pod{
1672+
ObjectMeta: metav1.ObjectMeta{
1673+
Name: "headNode",
1674+
Namespace: namespaceStr,
1675+
Labels: map[string]string{
1676+
utils.RayClusterLabelKey: instanceName,
1677+
utils.RayNodeTypeLabelKey: string(rayv1.HeadNode),
1678+
},
1679+
},
1680+
Status: corev1.PodStatus{
1681+
PodIP: headNodeIP,
1682+
},
1683+
}
1684+
runtimeObjects := []runtime.Object{headPod, headService}
1685+
1686+
// Initialize a fake client with newScheme and runtimeObjects.
1687+
fakeClient := clientFake.NewClientBuilder().WithScheme(newScheme).WithRuntimeObjects(runtimeObjects...).Build()
1688+
ctx := context.Background()
1689+
1690+
// Initialize a RayCluster reconciler.
1691+
r := &RayClusterReconciler{
1692+
Client: fakeClient,
1693+
Recorder: &record.FakeRecorder{},
1694+
Scheme: scheme.Scheme,
1695+
}
1696+
1697+
// prevent unhealthy state status
1698+
delete(testRayCluster.Spec.HeadGroupSpec.RayStartParams, "object-store-memory")
1699+
preUpdateTime := metav1.Now()
1700+
testRayCluster.Status.State = rayv1.Ready
1701+
testRayCluster.Status.StateTransitionTimes = map[rayv1.ClusterState]*metav1.Time{rayv1.Ready: &preUpdateTime}
1702+
newInstance, err := r.calculateStatus(ctx, testRayCluster)
1703+
assert.Nil(t, err)
1704+
assert.Equal(t, preUpdateTime, *newInstance.Status.StateTransitionTimes[rayv1.Ready], "Cluster state transition timestamp should not be updated")
16531705
}
16541706

16551707
func Test_TerminatedWorkers_NoAutoscaler(t *testing.T) {

ray-operator/controllers/ray/suite_helpers_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ func getClusterState(ctx context.Context, namespace string, clusterName string)
4949
}
5050
}
5151

52+
func getClusterStatus(ctx context.Context, namespace string, clusterName string) func() rayv1.RayClusterStatus {
53+
return func() rayv1.RayClusterStatus {
54+
var cluster rayv1.RayCluster
55+
if err := k8sClient.Get(ctx, client.ObjectKey{Namespace: namespace, Name: clusterName}, &cluster); err != nil {
56+
log.Fatal(err)
57+
}
58+
return cluster.Status
59+
}
60+
}
61+
5262
func isAllPodsRunningByFilters(ctx context.Context, podlist corev1.PodList, opt ...client.ListOption) bool {
5363
err := k8sClient.List(ctx, &podlist, opt...)
5464
gomega.Expect(err).ShouldNot(gomega.HaveOccurred(), "failed to list Pods")

0 commit comments

Comments
 (0)