Sort series by labels in requireEqual()

Tests that look at samples with StaleNaN values will fail because these samples are generated from map iteration and so the order can be unstable.

Signed-off-by: Lukasz Mierzwa <l.mierzwa@gmail.com>
This commit is contained in:
Lukasz Mierzwa 2025-04-28 16:04:18 +01:00
parent e2193f634f
commit c75768739a

View File

@ -36,6 +36,7 @@ import (
"github.com/gogo/protobuf/proto" "github.com/gogo/protobuf/proto"
"github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/grafana/regexp" "github.com/grafana/regexp"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
prom_testutil "github.com/prometheus/client_golang/prometheus/testutil" prom_testutil "github.com/prometheus/client_golang/prometheus/testutil"
@ -1992,7 +1993,16 @@ func TestScrapeLoopAppend(t *testing.T) {
func requireEqual(t *testing.T, expected, actual interface{}, msgAndArgs ...interface{}) { func requireEqual(t *testing.T, expected, actual interface{}, msgAndArgs ...interface{}) {
t.Helper() t.Helper()
testutil.RequireEqualWithOptions(t, expected, actual, testutil.RequireEqualWithOptions(t, expected, actual,
[]cmp.Option{cmp.Comparer(equalFloatSamples), cmp.AllowUnexported(histogramSample{})}, []cmp.Option{
cmp.Comparer(equalFloatSamples),
cmp.AllowUnexported(histogramSample{}),
// StaleNaN samples are generated by iterating over a map, which means that the order
// of samples might be different on every test run. Sort series by label to avoid
// test failures because of that.
cmpopts.SortSlices(func(a, b floatSample) int {
return labels.Compare(a.metric, b.metric)
}),
},
msgAndArgs...) msgAndArgs...)
} }