|
| 1 | +/* |
| 2 | +Copyright 2024 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package metrics |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/prometheus/client_golang/prometheus" |
| 23 | + "github.com/prometheus/client_golang/prometheus/testutil" |
| 24 | +) |
| 25 | + |
| 26 | +// TestServerCountMetric tests the server count metric. |
| 27 | +func TestServerCountMetric(t *testing.T) { |
| 28 | + // Reset metrics to ensure clean state |
| 29 | + Metrics.Reset() |
| 30 | + |
| 31 | + // Test setting server count |
| 32 | + expectedCount := 3 |
| 33 | + Metrics.SetServerCount(expectedCount) |
| 34 | + |
| 35 | + // Verify the metric value |
| 36 | + actualCount := testutil.ToFloat64(Metrics.serverCount) |
| 37 | + if actualCount != float64(expectedCount) { |
| 38 | + t.Errorf("Expected server count %d, got %f", expectedCount, actualCount) |
| 39 | + } |
| 40 | + |
| 41 | + // Test updating server count |
| 42 | + newCount := 5 |
| 43 | + Metrics.SetServerCount(newCount) |
| 44 | + actualCount = testutil.ToFloat64(Metrics.serverCount) |
| 45 | + if actualCount != float64(newCount) { |
| 46 | + t.Errorf("Expected updated server count %d, got %f", newCount, actualCount) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +// TestServerCountMetricRegistration tests the registration of the server count metric. |
| 51 | +func TestServerCountMetricRegistration(t *testing.T) { |
| 52 | + // Verify the metric is properly registered |
| 53 | + registry := prometheus.NewRegistry() |
| 54 | + registry.MustRegister(Metrics.serverCount) |
| 55 | + |
| 56 | + // Set a value and check if it's accessible |
| 57 | + Metrics.SetServerCount(2) |
| 58 | + actualCount := testutil.ToFloat64(Metrics.serverCount) |
| 59 | + if actualCount != 2.0 { |
| 60 | + t.Errorf("Expected server count 2, got %f", actualCount) |
| 61 | + } |
| 62 | +} |
0 commit comments