|
| 1 | +package metrics_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/prometheus/client_golang/prometheus/testutil" |
| 7 | + "github.com/stretchr/testify/suite" |
| 8 | + |
| 9 | + "github.com/platform-mesh/platform-mesh-operator/internal/metrics" |
| 10 | +) |
| 11 | + |
| 12 | +type MetricsTestSuite struct { |
| 13 | + suite.Suite |
| 14 | +} |
| 15 | + |
| 16 | +func TestMetricsTestSuite(t *testing.T) { |
| 17 | + suite.Run(t, new(MetricsTestSuite)) |
| 18 | +} |
| 19 | + |
| 20 | +// TestReconcileTotal verifies that the ReconcileTotal counter increments |
| 21 | +// correctly for each controller/result label combination. |
| 22 | +func (s *MetricsTestSuite) TestReconcileTotal() { |
| 23 | + before := testutil.ToFloat64(metrics.ReconcileTotal.WithLabelValues("PlatformMeshReconciler", "success")) |
| 24 | + metrics.ReconcileTotal.WithLabelValues("PlatformMeshReconciler", "success").Inc() |
| 25 | + s.Require().Equal(before+1, testutil.ToFloat64(metrics.ReconcileTotal.WithLabelValues("PlatformMeshReconciler", "success"))) |
| 26 | + |
| 27 | + before = testutil.ToFloat64(metrics.ReconcileTotal.WithLabelValues("ResourceReconciler", "error")) |
| 28 | + metrics.ReconcileTotal.WithLabelValues("ResourceReconciler", "error").Inc() |
| 29 | + s.Require().Equal(before+1, testutil.ToFloat64(metrics.ReconcileTotal.WithLabelValues("ResourceReconciler", "error"))) |
| 30 | +} |
| 31 | + |
| 32 | +// TestSubroutineTotal verifies that the SubroutineTotal counter increments |
| 33 | +// correctly for each subroutine/result label combination. |
| 34 | +func (s *MetricsTestSuite) TestSubroutineTotal() { |
| 35 | + before := testutil.ToFloat64(metrics.SubroutineTotal.WithLabelValues("deployment", "success")) |
| 36 | + metrics.SubroutineTotal.WithLabelValues("deployment", "success").Inc() |
| 37 | + s.Require().Equal(before+1, testutil.ToFloat64(metrics.SubroutineTotal.WithLabelValues("deployment", "success"))) |
| 38 | + |
| 39 | + before = testutil.ToFloat64(metrics.SubroutineTotal.WithLabelValues("kcpsetup", "error")) |
| 40 | + metrics.SubroutineTotal.WithLabelValues("kcpsetup", "error").Inc() |
| 41 | + s.Require().Equal(before+1, testutil.ToFloat64(metrics.SubroutineTotal.WithLabelValues("kcpsetup", "error"))) |
| 42 | +} |
| 43 | + |
| 44 | +// TestSubroutineDuration verifies that the SubroutineDuration histogram records |
| 45 | +// observations per subroutine label. |
| 46 | +func (s *MetricsTestSuite) TestSubroutineDuration() { |
| 47 | + before := testutil.CollectAndCount(metrics.SubroutineDuration) |
| 48 | + metrics.SubroutineDuration.WithLabelValues("deployment").Observe(0.1) |
| 49 | + s.Assert().Greater(testutil.CollectAndCount(metrics.SubroutineDuration), before) |
| 50 | +} |
0 commit comments