@@ -3,11 +3,9 @@ package metrics
3
3
import (
4
4
"context"
5
5
"net/http"
6
- "net/http/httptest"
7
6
"testing"
8
7
9
8
"github.com/prometheus/client_golang/prometheus"
10
- "github.com/prometheus/client_golang/prometheus/promhttp"
11
9
"github.com/stretchr/testify/assert"
12
10
"github.com/stretchr/testify/require"
13
11
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -16,6 +14,7 @@ import (
16
14
"sigs.k8s.io/controller-runtime/pkg/client/fake"
17
15
18
16
rayv1 "github.com/ray-project/kuberay/ray-operator/apis/ray/v1"
17
+ "github.com/ray-project/kuberay/ray-operator/test/support"
19
18
)
20
19
21
20
func TestMetricRayJobInfo (t * testing.T ) {
@@ -61,29 +60,21 @@ func TestMetricRayJobInfo(t *testing.T) {
61
60
reg := prometheus .NewRegistry ()
62
61
reg .MustRegister (manager )
63
62
64
- req , err := http .NewRequestWithContext (t .Context (), http .MethodGet , "/metrics" , nil )
65
- require .NoError (t , err )
66
- rr := httptest .NewRecorder ()
67
- handler := promhttp .HandlerFor (reg , promhttp.HandlerOpts {})
68
- handler .ServeHTTP (rr , req )
63
+ body , statusCode := support .GetMetricsResponseAndCode (t , reg )
69
64
70
- assert .Equal (t , http .StatusOK , rr .Code )
71
- body := rr .Body .String ()
65
+ assert .Equal (t , http .StatusOK , statusCode )
72
66
for _ , label := range tc .expectedMetrics {
73
67
assert .Contains (t , body , label )
74
68
}
75
69
76
70
if len (tc .rayJobs ) > 0 {
77
- err = client .Delete (t .Context (), & tc .rayJobs [0 ])
71
+ err : = client .Delete (t .Context (), & tc .rayJobs [0 ])
78
72
require .NoError (t , err )
79
73
}
80
74
81
- rr2 := httptest .NewRecorder ()
82
- handler .ServeHTTP (rr2 , req )
83
-
84
- assert .Equal (t , http .StatusOK , rr2 .Code )
85
- body2 := rr2 .Body .String ()
75
+ body2 , statusCode := support .GetMetricsResponseAndCode (t , reg )
86
76
77
+ assert .Equal (t , http .StatusOK , statusCode )
87
78
assert .NotContains (t , body2 , tc .expectedMetrics [0 ])
88
79
for _ , label := range tc .expectedMetrics [1 :] {
89
80
assert .Contains (t , body2 , label )
@@ -92,6 +83,65 @@ func TestMetricRayJobInfo(t *testing.T) {
92
83
}
93
84
}
94
85
86
+ func TestDeleteRayJobMetrics (t * testing.T ) {
87
+ k8sScheme := runtime .NewScheme ()
88
+ require .NoError (t , rayv1 .AddToScheme (k8sScheme ))
89
+ client := fake .NewClientBuilder ().WithScheme (k8sScheme ).Build ()
90
+ manager := NewRayJobMetricsManager (context .Background (), client )
91
+ reg := prometheus .NewRegistry ()
92
+ reg .MustRegister (manager )
93
+
94
+ // Test case 1: Delete specific job metrics
95
+ // Manually add some metrics
96
+ manager .ObserveRayJobExecutionDuration ("job1" , "ns1" , rayv1 .JobDeploymentStatusComplete , 0 , 10.5 )
97
+ manager .ObserveRayJobExecutionDuration ("job2" , "ns2" , rayv1 .JobDeploymentStatusFailed , 1 , 20.3 )
98
+ manager .ObserveRayJobExecutionDuration ("job3" , "ns1" , rayv1 .JobDeploymentStatusRunning , 0 , 5.7 )
99
+
100
+ // Test deleting metrics for job1 in ns1
101
+ manager .DeleteRayJobMetrics ("job1" , "ns1" )
102
+
103
+ // Verify metrics
104
+ body , statusCode := support .GetMetricsResponseAndCode (t , reg )
105
+
106
+ assert .Equal (t , http .StatusOK , statusCode )
107
+ assert .NotContains (t , body , `kuberay_job_execution_duration_seconds{job_deployment_status="Complete",name="job1",namespace="ns1",retry_count="0"}` )
108
+ assert .Contains (t , body , `kuberay_job_execution_duration_seconds{job_deployment_status="Failed",name="job2",namespace="ns2",retry_count="1"}` )
109
+ assert .Contains (t , body , `kuberay_job_execution_duration_seconds{job_deployment_status="Running",name="job3",namespace="ns1",retry_count="0"}` )
110
+
111
+ // Test case 2: Delete with empty name
112
+ manager .DeleteRayJobMetrics ("" , "ns1" )
113
+
114
+ // Verify metrics again
115
+ body2 , statusCode := support .GetMetricsResponseAndCode (t , reg )
116
+
117
+ assert .Equal (t , http .StatusOK , statusCode )
118
+ assert .NotContains (t , body2 , `kuberay_job_execution_duration_seconds{job_deployment_status="Complete",name="job1",namespace="ns1",retry_count="0"}` )
119
+ assert .Contains (t , body2 , `kuberay_job_execution_duration_seconds{job_deployment_status="Failed",name="job2",namespace="ns2",retry_count="1"}` )
120
+ assert .Contains (t , body2 , `kuberay_job_execution_duration_seconds{job_deployment_status="Running",name="job3",namespace="ns1",retry_count="0"}` )
121
+
122
+ // Test case 3: Delete with empty name and namespace
123
+ manager .DeleteRayJobMetrics ("" , "" )
124
+
125
+ // Verify no metrics were deleted
126
+ body3 , statusCode := support .GetMetricsResponseAndCode (t , reg )
127
+
128
+ assert .Equal (t , http .StatusOK , statusCode )
129
+ assert .NotContains (t , body3 , `kuberay_job_execution_duration_seconds{job_deployment_status="Complete",name="job1",namespace="ns1",retry_count="0"}` )
130
+ assert .Contains (t , body3 , `kuberay_job_execution_duration_seconds{job_deployment_status="Failed",name="job2",namespace="ns2",retry_count="1"}` )
131
+ assert .Contains (t , body3 , `kuberay_job_execution_duration_seconds{job_deployment_status="Running",name="job3",namespace="ns1",retry_count="0"}` )
132
+
133
+ // Test case 4: Delete with false name and namespace
134
+ manager .DeleteRayJobMetrics ("ns2" , "job2" )
135
+
136
+ // Verify no metrics were deleted
137
+ body4 , statusCode := support .GetMetricsResponseAndCode (t , reg )
138
+
139
+ assert .Equal (t , http .StatusOK , statusCode )
140
+ assert .NotContains (t , body4 , `kuberay_job_execution_duration_seconds{job_deployment_status="Complete",name="job1",namespace="ns1",retry_count="0"}` )
141
+ assert .Contains (t , body4 , `kuberay_job_execution_duration_seconds{job_deployment_status="Failed",name="job2",namespace="ns2",retry_count="1"}` )
142
+ assert .Contains (t , body4 , `kuberay_job_execution_duration_seconds{job_deployment_status="Running",name="job3",namespace="ns1",retry_count="0"}` )
143
+ }
144
+
95
145
func TestMetricRayJobDeploymentStatus (t * testing.T ) {
96
146
tests := []struct {
97
147
name string
@@ -141,28 +191,21 @@ func TestMetricRayJobDeploymentStatus(t *testing.T) {
141
191
reg := prometheus .NewRegistry ()
142
192
reg .MustRegister (manager )
143
193
144
- req , err := http .NewRequestWithContext (t .Context (), http .MethodGet , "/metrics" , nil )
145
- require .NoError (t , err )
146
- rr := httptest .NewRecorder ()
147
- handler := promhttp .HandlerFor (reg , promhttp.HandlerOpts {})
148
- handler .ServeHTTP (rr , req )
194
+ body , statusCode := support .GetMetricsResponseAndCode (t , reg )
149
195
150
- assert .Equal (t , http .StatusOK , rr .Code )
151
- body := rr .Body .String ()
196
+ assert .Equal (t , http .StatusOK , statusCode )
152
197
for _ , label := range tc .expectedMetrics {
153
198
assert .Contains (t , body , label )
154
199
}
155
200
156
201
if len (tc .rayJobs ) > 0 {
157
- err = client .Delete (t . Context (), & tc .rayJobs [0 ])
202
+ err : = client .Delete (context . Background (), & tc .rayJobs [0 ])
158
203
require .NoError (t , err )
159
204
}
160
205
161
- rr2 := httptest .NewRecorder ()
162
- handler .ServeHTTP (rr2 , req )
206
+ body2 , statusCode := support .GetMetricsResponseAndCode (t , reg )
163
207
164
- assert .Equal (t , http .StatusOK , rr2 .Code )
165
- body2 := rr2 .Body .String ()
208
+ assert .Equal (t , http .StatusOK , statusCode )
166
209
167
210
assert .NotContains (t , body2 , tc .expectedMetrics [0 ])
168
211
for _ , label := range tc .expectedMetrics [1 :] {
0 commit comments