|
5 | 5 | "net/http"
|
6 | 6 | "net/http/httptest"
|
7 | 7 | "testing"
|
| 8 | + "time" |
8 | 9 |
|
9 | 10 | "github.com/prometheus/client_golang/prometheus"
|
10 | 11 | "github.com/prometheus/client_golang/prometheus/promhttp"
|
@@ -96,6 +97,106 @@ func TestRayClusterInfo(t *testing.T) {
|
96 | 97 | }
|
97 | 98 | }
|
98 | 99 |
|
| 100 | +// TestScheduleRayClusterMetricForCleanup tests scheduling a metric for cleanup |
| 101 | +func TestScheduleRayClusterMetricForCleanup(t *testing.T) { |
| 102 | + ctx := context.Background() |
| 103 | + k8sScheme := runtime.NewScheme() |
| 104 | + require.NoError(t, rayv1.AddToScheme(k8sScheme)) |
| 105 | + |
| 106 | + client := fake.NewClientBuilder().WithScheme(k8sScheme).Build() |
| 107 | + manager := NewRayClusterMetricsManager(ctx, client) |
| 108 | + |
| 109 | + // Schedule a metric for cleanup |
| 110 | + manager.ScheduleRayClusterMetricForCleanup("test-cluster", "test-namespace") |
| 111 | + |
| 112 | + // Verify the cleanup queue has one item |
| 113 | + assert.Len(t, manager.cleanupQueue, 1) |
| 114 | + assert.Equal(t, "test-cluster", manager.cleanupQueue[0].Name) |
| 115 | + assert.Equal(t, "test-namespace", manager.cleanupQueue[0].Namespace) |
| 116 | + assert.WithinDuration(t, time.Now().Add(5*time.Minute), manager.cleanupQueue[0].DeleteAt, 1*time.Second) |
| 117 | +} |
| 118 | + |
| 119 | +// TestCleanupExpiredRayClusterMetrics tests cleaning up expired metrics |
| 120 | +func TestCleanupExpiredRayClusterMetrics(t *testing.T) { |
| 121 | + ctx := context.Background() |
| 122 | + k8sScheme := runtime.NewScheme() |
| 123 | + require.NoError(t, rayv1.AddToScheme(k8sScheme)) |
| 124 | + |
| 125 | + client := fake.NewClientBuilder().WithScheme(k8sScheme).Build() |
| 126 | + manager := NewRayClusterMetricsManager(ctx, client) |
| 127 | + |
| 128 | + // Set up a metric |
| 129 | + manager.ObserveRayClusterProvisionedDuration("expired-cluster", "test-namespace", 123.45) |
| 130 | + |
| 131 | + // Add an expired item to the cleanup queue |
| 132 | + manager.queueMutex.Lock() |
| 133 | + manager.cleanupQueue = append(manager.cleanupQueue, RayClusterMetricCleanupItem{ |
| 134 | + Name: "expired-cluster", |
| 135 | + Namespace: "test-namespace", |
| 136 | + DeleteAt: time.Now().Add(-1 * time.Minute), // Expired 1 minute ago |
| 137 | + }) |
| 138 | + manager.queueMutex.Unlock() |
| 139 | + |
| 140 | + // Clean up expired metrics |
| 141 | + manager.cleanupExpiredRayClusterMetrics() |
| 142 | + |
| 143 | + // Verify the metric was deleted by checking the registry |
| 144 | + reg := prometheus.NewRegistry() |
| 145 | + reg.MustRegister(manager) |
| 146 | + req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "/metrics", nil) |
| 147 | + require.NoError(t, err) |
| 148 | + recorder := httptest.NewRecorder() |
| 149 | + handler := promhttp.HandlerFor(reg, promhttp.HandlerOpts{}) |
| 150 | + handler.ServeHTTP(recorder, req) |
| 151 | + assert.Equal(t, http.StatusOK, recorder.Code) |
| 152 | + body := recorder.Body.String() |
| 153 | + assert.NotContains(t, body, `kuberay_cluster_provisioned_duration_seconds{name="expired-cluster",namespace="test-namespace"}`) |
| 154 | + |
| 155 | + // Verify the cleanup queue is empty |
| 156 | + assert.Empty(t, manager.cleanupQueue) |
| 157 | +} |
| 158 | + |
| 159 | +// TestCleanupLoop tests the background cleanup loop |
| 160 | +func TestCleanupLoop(t *testing.T) { |
| 161 | + // Create a context that we can cancel |
| 162 | + ctx, cancel := context.WithCancel(context.Background()) |
| 163 | + defer cancel() |
| 164 | + |
| 165 | + k8sScheme := runtime.NewScheme() |
| 166 | + require.NoError(t, rayv1.AddToScheme(k8sScheme)) |
| 167 | + |
| 168 | + client := fake.NewClientBuilder().WithScheme(k8sScheme).Build() |
| 169 | + manager := NewRayClusterMetricsManager(ctx, client) |
| 170 | + |
| 171 | + // Set up a metric |
| 172 | + manager.ObserveRayClusterProvisionedDuration("test-cluster", "test-namespace", 123.45) |
| 173 | + |
| 174 | + // Add an item to the cleanup queue with a very short TTL |
| 175 | + manager.queueMutex.Lock() |
| 176 | + manager.metricTTL = 1 * time.Second // Set TTL to 1 second for testing |
| 177 | + manager.cleanupQueue = append(manager.cleanupQueue, RayClusterMetricCleanupItem{ |
| 178 | + Name: "test-cluster", |
| 179 | + Namespace: "test-namespace", |
| 180 | + DeleteAt: time.Now().Add(manager.metricTTL), |
| 181 | + }) |
| 182 | + manager.queueMutex.Unlock() |
| 183 | + |
| 184 | + // Wait for the cleanup loop to run and process the item |
| 185 | + time.Sleep(2 * time.Second) |
| 186 | + |
| 187 | + // Verify the metric was deleted by checking the registry |
| 188 | + reg := prometheus.NewRegistry() |
| 189 | + reg.MustRegister(manager) |
| 190 | + req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "/metrics", nil) |
| 191 | + require.NoError(t, err) |
| 192 | + recorder := httptest.NewRecorder() |
| 193 | + handler := promhttp.HandlerFor(reg, promhttp.HandlerOpts{}) |
| 194 | + handler.ServeHTTP(recorder, req) |
| 195 | + assert.Equal(t, http.StatusOK, recorder.Code) |
| 196 | + body := recorder.Body.String() |
| 197 | + assert.NotContains(t, body, `kuberay_cluster_provisioned_duration_seconds{name="test-cluster",namespace="test-namespace"}`) |
| 198 | +} |
| 199 | + |
99 | 200 | func TestRayClusterConditionProvisioned(t *testing.T) {
|
100 | 201 | tests := []struct {
|
101 | 202 | name string
|
|
0 commit comments