From 9a1e4121d322e7d6f8757f2099be48d0ad133493 Mon Sep 17 00:00:00 2001 From: Joachim Bauch Date: Mon, 2 Feb 2026 11:57:44 +0100 Subject: [PATCH] Add tests for prometheus registry. --- metrics/prometheus_test.go | 67 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 metrics/prometheus_test.go diff --git a/metrics/prometheus_test.go b/metrics/prometheus_test.go new file mode 100644 index 0000000..801ae3f --- /dev/null +++ b/metrics/prometheus_test.go @@ -0,0 +1,67 @@ +/** + * Standalone signaling server for the Nextcloud Spreed app. + * Copyright (C) 2026 struktur AG + * + * @author Joachim Bauch + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package metrics + +import ( + "testing" + + "github.com/prometheus/client_golang/prometheus" + "github.com/stretchr/testify/assert" +) + +func TestRegistration(t *testing.T) { + t.Parallel() + + collectors := []prometheus.Collector{ + prometheus.NewCounter(prometheus.CounterOpts{ + Namespace: "signaling", + Subsystem: "test", + Name: "value_total", + Help: "Total value.", + }), + prometheus.NewGaugeVec(prometheus.GaugeOpts{ + Namespace: "signaling", + Subsystem: "test", + Name: "value", + Help: "Current value.", + }, []string{"foo", "bar"}), + } + // Can unregister without previous registration + UnregisterAll(collectors...) + RegisterAll(collectors...) + // Can register multiple times + RegisterAll(collectors...) + UnregisterAll(collectors...) +} + +func TestRegistrationError(t *testing.T) { + t.Parallel() + + defer func() { + value := recover() + if err, ok := value.(error); assert.True(t, ok) { + assert.ErrorContains(t, err, "is not a valid metric name") + } + }() + + RegisterAll(prometheus.NewCounter(prometheus.CounterOpts{})) +}