Skip to content

Commit f26cc9f

Browse files
Add unit tests
1 parent 5e741e2 commit f26cc9f

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

ray-operator/controllers/ray/raycluster_controller_fake_test.go

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

16501702
func Test_TerminatedWorkers_NoAutoscaler(t *testing.T) {

ray-operator/controllers/ray/raycluster_controller_test.go

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

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

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

ray-operator/controllers/ray/suite_helpers_test.go

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

50+
func getClusterStatus(ctx context.Context, namespace string, clusterName string) func() rayv1.RayClusterStatus {
51+
return func() rayv1.RayClusterStatus {
52+
var cluster rayv1.RayCluster
53+
if err := k8sClient.Get(ctx, client.ObjectKey{Namespace: namespace, Name: clusterName}, &cluster); err != nil {
54+
log.Fatal(err)
55+
}
56+
return cluster.Status
57+
}
58+
}
59+
5060
func isAllPodsRunning(ctx context.Context, podlist corev1.PodList, filterLabels client.MatchingLabels, namespace string) bool {
5161
return isAllPodsRunningByFilters(ctx, podlist, filterLabels, &client.ListOptions{Namespace: namespace})
5262
}

0 commit comments

Comments
 (0)