Skip to content

Commit 2623dd6

Browse files
committed
[Tests] Add test code
1 parent a0c9341 commit 2623dd6

File tree

3 files changed

+146
-5
lines changed

3 files changed

+146
-5
lines changed

ray-operator/controllers/ray/metrics/ray_cluster_metrics_test.go

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,76 @@ func TestRayClusterInfo(t *testing.T) {
9696
}
9797
}
9898

99+
func TestDeleteRayClusterMetrics(t *testing.T) {
100+
k8sScheme := runtime.NewScheme()
101+
require.NoError(t, rayv1.AddToScheme(k8sScheme))
102+
client := fake.NewClientBuilder().WithScheme(k8sScheme).Build()
103+
manager := NewRayClusterMetricsManager(context.Background(), client)
104+
reg := prometheus.NewRegistry()
105+
reg.MustRegister(manager)
106+
107+
// Test case 1: Delete specific cluster metrics
108+
// Manually add some metrics
109+
manager.rayClusterProvisionedDurationSeconds.With(prometheus.Labels{"name": "cluster1", "namespace": "ns1"}).Set(10.5)
110+
manager.rayClusterProvisionedDurationSeconds.With(prometheus.Labels{"name": "cluster2", "namespace": "ns2"}).Set(20.3)
111+
manager.rayClusterProvisionedDurationSeconds.With(prometheus.Labels{"name": "cluster3", "namespace": "ns1"}).Set(5.7)
112+
113+
// Test deleting metrics for cluster1 in ns1
114+
manager.DeleteRayClusterMetrics("cluster1", "ns1")
115+
116+
// Verify metrics
117+
req, err := http.NewRequestWithContext(t.Context(), http.MethodGet, "/metrics", nil)
118+
require.NoError(t, err)
119+
recorder := httptest.NewRecorder()
120+
handler := promhttp.HandlerFor(reg, promhttp.HandlerOpts{Registry: reg})
121+
handler.ServeHTTP(recorder, req)
122+
123+
assert.Equal(t, http.StatusOK, recorder.Code)
124+
body := recorder.Body.String()
125+
assert.NotContains(t, body, `kuberay_cluster_provisioned_duration_seconds{name="cluster1",namespace="ns1"}`)
126+
assert.Contains(t, body, `kuberay_cluster_provisioned_duration_seconds{name="cluster2",namespace="ns2"}`)
127+
assert.Contains(t, body, `kuberay_cluster_provisioned_duration_seconds{name="cluster3",namespace="ns1"}`)
128+
129+
// Test case 2: Delete with empty name
130+
manager.DeleteRayClusterMetrics("", "ns1")
131+
132+
// Verify metrics again
133+
recorder2 := httptest.NewRecorder()
134+
handler.ServeHTTP(recorder2, req)
135+
136+
assert.Equal(t, http.StatusOK, recorder2.Code)
137+
body2 := recorder2.Body.String()
138+
assert.NotContains(t, body2, `kuberay_cluster_provisioned_duration_seconds{name="cluster1",namespace="ns1"}`)
139+
assert.Contains(t, body2, `kuberay_cluster_provisioned_duration_seconds{name="cluster3",namespace="ns1"}`)
140+
assert.Contains(t, body2, `kuberay_cluster_provisioned_duration_seconds{name="cluster2",namespace="ns2"}`)
141+
142+
// Test case 3: Delete with empty name and namespace
143+
manager.DeleteRayClusterMetrics("", "")
144+
145+
// Verify all metrics are deleted
146+
recorder3 := httptest.NewRecorder()
147+
handler.ServeHTTP(recorder3, req)
148+
149+
assert.Equal(t, http.StatusOK, recorder3.Code)
150+
body3 := recorder3.Body.String()
151+
assert.NotContains(t, body3, `kuberay_cluster_provisioned_duration_seconds{name="cluster1",namespace="ns1"}`)
152+
assert.Contains(t, body3, `kuberay_cluster_provisioned_duration_seconds{name="cluster3",namespace="ns1"}`)
153+
assert.Contains(t, body3, `kuberay_cluster_provisioned_duration_seconds{name="cluster2",namespace="ns2"}`)
154+
155+
// Test case 4: Delete with false name and namespace
156+
manager.DeleteRayClusterMetrics("ns2", "cluster2")
157+
158+
// Verify all metrics are deleted
159+
recorder4 := httptest.NewRecorder()
160+
handler.ServeHTTP(recorder4, req)
161+
162+
assert.Equal(t, http.StatusOK, recorder4.Code)
163+
body4 := recorder4.Body.String()
164+
assert.NotContains(t, body4, `kuberay_cluster_provisioned_duration_seconds{name="cluster1",namespace="ns1"}`)
165+
assert.Contains(t, body4, `kuberay_cluster_provisioned_duration_seconds{name="cluster3",namespace="ns1"}`)
166+
assert.Contains(t, body4, `kuberay_cluster_provisioned_duration_seconds{name="cluster2",namespace="ns2"}`)
167+
}
168+
99169
func TestRayClusterConditionProvisioned(t *testing.T) {
100170
tests := []struct {
101171
name string
@@ -155,7 +225,7 @@ func TestRayClusterConditionProvisioned(t *testing.T) {
155225
reg := prometheus.NewRegistry()
156226
reg.MustRegister(manager)
157227

158-
req, err := http.NewRequestWithContext(t.Context(), http.MethodGet, "/metrics", nil)
228+
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "/metrics", nil)
159229
require.NoError(t, err)
160230
rr := httptest.NewRecorder()
161231
handler := promhttp.HandlerFor(reg, promhttp.HandlerOpts{})
@@ -168,7 +238,7 @@ func TestRayClusterConditionProvisioned(t *testing.T) {
168238
}
169239

170240
if len(tc.clusters) > 0 {
171-
err = client.Delete(t.Context(), &tc.clusters[0])
241+
err = client.Delete(context.Background(), &tc.clusters[0])
172242
require.NoError(t, err)
173243
}
174244

ray-operator/controllers/ray/metrics/ray_job_metrics.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func (r *RayJobMetricsManager) ObserveRayJobExecutionDuration(name, namespace st
8484
r.rayJobExecutionDurationSeconds.WithLabelValues(name, namespace, string(jobDeploymentStatus), strconv.Itoa(retryCount)).Set(duration)
8585
}
8686

87-
// deleteRayJobMetrics removes metrics that belongs to the specified RayJob.
87+
// DeleteRayJobMetrics removes metrics that belongs to the specified RayJob.
8888
func (r *RayJobMetricsManager) DeleteRayJobMetrics(name, namespace string) {
8989
numCleanedUpMetrics := r.rayJobExecutionDurationSeconds.DeletePartialMatch(prometheus.Labels{"name": name, "namespace": namespace})
9090
r.log.Info("Cleaned up expired rayJob metric", "name", name, "namespace", namespace, "numCleanedUpMetrics", numCleanedUpMetrics)

ray-operator/controllers/ray/metrics/ray_job_metrics_test.go

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,77 @@ func TestMetricRayJobInfo(t *testing.T) {
9292
}
9393
}
9494

95+
func TestDeleteRayJobMetrics(t *testing.T) {
96+
k8sScheme := runtime.NewScheme()
97+
require.NoError(t, rayv1.AddToScheme(k8sScheme))
98+
client := fake.NewClientBuilder().WithScheme(k8sScheme).Build()
99+
manager := NewRayJobMetricsManager(context.Background(), client)
100+
reg := prometheus.NewRegistry()
101+
reg.MustRegister(manager)
102+
103+
// Test case 1: Delete specific job metrics
104+
// Manually add some metrics
105+
manager.ObserveRayJobExecutionDuration("job1", "ns1", rayv1.JobDeploymentStatusComplete, 0, 10.5)
106+
manager.ObserveRayJobExecutionDuration("job2", "ns2", rayv1.JobDeploymentStatusFailed, 1, 20.3)
107+
manager.ObserveRayJobExecutionDuration("job3", "ns1", rayv1.JobDeploymentStatusRunning, 0, 5.7)
108+
109+
// Test deleting metrics for job1 in ns1
110+
manager.DeleteRayJobMetrics("job1", "ns1")
111+
112+
// Verify metrics
113+
req, err := http.NewRequestWithContext(t.Context(), http.MethodGet, "/metrics", nil)
114+
require.NoError(t, err)
115+
recorder := httptest.NewRecorder()
116+
handler := promhttp.HandlerFor(reg, promhttp.HandlerOpts{Registry: reg})
117+
handler.ServeHTTP(recorder, req)
118+
119+
assert.Equal(t, http.StatusOK, recorder.Code)
120+
body := recorder.Body.String()
121+
assert.NotContains(t, body, `kuberay_job_execution_duration_seconds{job_deployment_status="Complete",name="job1",namespace="ns1",retry_count="0"}`)
122+
assert.Contains(t, body, `kuberay_job_execution_duration_seconds{job_deployment_status="Failed",name="job2",namespace="ns2",retry_count="1"}`)
123+
assert.Contains(t, body, `kuberay_job_execution_duration_seconds{job_deployment_status="Running",name="job3",namespace="ns1",retry_count="0"}`)
124+
125+
// Test case 2: Delete with empty name
126+
manager.DeleteRayJobMetrics("", "ns1")
127+
128+
// Verify metrics again
129+
recorder2 := httptest.NewRecorder()
130+
handler.ServeHTTP(recorder2, req)
131+
132+
assert.Equal(t, http.StatusOK, recorder2.Code)
133+
body2 := recorder2.Body.String()
134+
assert.NotContains(t, body2, `kuberay_job_execution_duration_seconds{job_deployment_status="Complete",name="job1",namespace="ns1",retry_count="0"}`)
135+
assert.Contains(t, body2, `kuberay_job_execution_duration_seconds{job_deployment_status="Failed",name="job2",namespace="ns2",retry_count="1"}`)
136+
assert.Contains(t, body2, `kuberay_job_execution_duration_seconds{job_deployment_status="Running",name="job3",namespace="ns1",retry_count="0"}`)
137+
138+
// Test case 3: Delete with empty name and namespace
139+
manager.DeleteRayJobMetrics("", "")
140+
141+
// Verify all metrics are deleted
142+
recorder3 := httptest.NewRecorder()
143+
handler.ServeHTTP(recorder3, req)
144+
145+
assert.Equal(t, http.StatusOK, recorder3.Code)
146+
body3 := recorder3.Body.String()
147+
assert.NotContains(t, body3, `kuberay_job_execution_duration_seconds{job_deployment_status="Complete",name="job1",namespace="ns1",retry_count="0"}`)
148+
assert.Contains(t, body3, `kuberay_job_execution_duration_seconds{job_deployment_status="Failed",name="job2",namespace="ns2",retry_count="1"}`)
149+
assert.Contains(t, body3, `kuberay_job_execution_duration_seconds{job_deployment_status="Running",name="job3",namespace="ns1",retry_count="0"}`)
150+
151+
// Test case 4: Delete with false name and namespace
152+
manager.DeleteRayJobMetrics("ns2", "job2")
153+
154+
// Verify all metrics are deleted
155+
recorder4 := httptest.NewRecorder()
156+
handler.ServeHTTP(recorder4, req)
157+
158+
assert.Equal(t, http.StatusOK, recorder4.Code)
159+
body4 := recorder4.Body.String()
160+
assert.NotContains(t, body4, `kuberay_job_execution_duration_seconds{job_deployment_status="Complete",name="job1",namespace="ns1",retry_count="0"}`)
161+
assert.Contains(t, body4, `kuberay_job_execution_duration_seconds{job_deployment_status="Failed",name="job2",namespace="ns2",retry_count="1"}`)
162+
assert.Contains(t, body4, `kuberay_job_execution_duration_seconds{job_deployment_status="Running",name="job3",namespace="ns1",retry_count="0"}`)
163+
164+
}
165+
95166
func TestMetricRayJobDeploymentStatus(t *testing.T) {
96167
tests := []struct {
97168
name string
@@ -141,7 +212,7 @@ func TestMetricRayJobDeploymentStatus(t *testing.T) {
141212
reg := prometheus.NewRegistry()
142213
reg.MustRegister(manager)
143214

144-
req, err := http.NewRequestWithContext(t.Context(), http.MethodGet, "/metrics", nil)
215+
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "/metrics", nil)
145216
require.NoError(t, err)
146217
rr := httptest.NewRecorder()
147218
handler := promhttp.HandlerFor(reg, promhttp.HandlerOpts{})
@@ -154,7 +225,7 @@ func TestMetricRayJobDeploymentStatus(t *testing.T) {
154225
}
155226

156227
if len(tc.rayJobs) > 0 {
157-
err = client.Delete(t.Context(), &tc.rayJobs[0])
228+
err = client.Delete(context.Background(), &tc.rayJobs[0])
158229
require.NoError(t, err)
159230
}
160231

0 commit comments

Comments
 (0)