From c75768739a3288218a0f2555e99f3ce00b9c33e2 Mon Sep 17 00:00:00 2001 From: Lukasz Mierzwa Date: Mon, 28 Apr 2025 16:04:18 +0100 Subject: [PATCH] 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 --- scrape/scrape_test.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/scrape/scrape_test.go b/scrape/scrape_test.go index ee8bfcef67..d28bd4ed18 100644 --- a/scrape/scrape_test.go +++ b/scrape/scrape_test.go @@ -36,6 +36,7 @@ import ( "github.com/gogo/protobuf/proto" "github.com/google/go-cmp/cmp" + "github.com/google/go-cmp/cmp/cmpopts" "github.com/grafana/regexp" "github.com/prometheus/client_golang/prometheus" 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{}) { t.Helper() 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...) }