Tests: use replacement DeepEquals in more places

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
This commit is contained in:
Bryan Boreham 2024-01-24 16:48:22 +00:00
parent d0dee51aac
commit 17f48f2b3b
11 changed files with 37 additions and 20 deletions

View file

@ -22,6 +22,7 @@ import (
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
"github.com/prometheus/prometheus/model/labels" "github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/util/testutil"
) )
func TestRelabel(t *testing.T) { func TestRelabel(t *testing.T) {
@ -591,7 +592,7 @@ func TestRelabel(t *testing.T) {
res, keep := Process(test.input, test.relabel...) res, keep := Process(test.input, test.relabel...)
require.Equal(t, !test.drop, keep) require.Equal(t, !test.drop, keep)
if keep { if keep {
require.Equal(t, test.output, res) testutil.RequireEqual(t, test.output, res)
} }
} }
} }

View file

@ -23,6 +23,7 @@ import (
"github.com/prometheus/prometheus/model/exemplar" "github.com/prometheus/prometheus/model/exemplar"
"github.com/prometheus/prometheus/model/labels" "github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/util/testutil"
) )
func TestOpenMetricsParse(t *testing.T) { func TestOpenMetricsParse(t *testing.T) {
@ -268,12 +269,12 @@ foo_total 17.0 1520879607.789 # {id="counter-test"} 5`
require.Equal(t, exp[i].m, string(m)) require.Equal(t, exp[i].m, string(m))
require.Equal(t, exp[i].t, ts) require.Equal(t, exp[i].t, ts)
require.Equal(t, exp[i].v, v) require.Equal(t, exp[i].v, v)
require.Equal(t, exp[i].lset, res) testutil.RequireEqual(t, exp[i].lset, res)
if exp[i].e == nil { if exp[i].e == nil {
require.False(t, found) require.False(t, found)
} else { } else {
require.True(t, found) require.True(t, found)
require.Equal(t, *exp[i].e, e) testutil.RequireEqual(t, *exp[i].e, e)
} }
case EntryType: case EntryType:

View file

@ -26,6 +26,7 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/prometheus/prometheus/model/labels" "github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/util/testutil"
) )
func TestPromParse(t *testing.T) { func TestPromParse(t *testing.T) {
@ -191,7 +192,7 @@ testmetric{label="\"bar\""} 1`
require.Equal(t, exp[i].m, string(m)) require.Equal(t, exp[i].m, string(m))
require.Equal(t, exp[i].t, ts) require.Equal(t, exp[i].t, ts)
require.Equal(t, exp[i].v, v) require.Equal(t, exp[i].v, v)
require.Equal(t, exp[i].lset, res) testutil.RequireEqual(t, exp[i].lset, res)
case EntryType: case EntryType:
m, typ := p.Type() m, typ := p.Type()

View file

@ -27,6 +27,7 @@ import (
"github.com/prometheus/prometheus/model/exemplar" "github.com/prometheus/prometheus/model/exemplar"
"github.com/prometheus/prometheus/model/histogram" "github.com/prometheus/prometheus/model/histogram"
"github.com/prometheus/prometheus/model/labels" "github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/util/testutil"
dto "github.com/prometheus/prometheus/prompb/io/prometheus/client" dto "github.com/prometheus/prometheus/prompb/io/prometheus/client"
) )
@ -1993,12 +1994,12 @@ func TestProtobufParse(t *testing.T) {
require.Equal(t, int64(0), exp[i].t, "i: %d", i) require.Equal(t, int64(0), exp[i].t, "i: %d", i)
} }
require.Equal(t, exp[i].v, v, "i: %d", i) require.Equal(t, exp[i].v, v, "i: %d", i)
require.Equal(t, exp[i].lset, res, "i: %d", i) testutil.RequireEqual(t, exp[i].lset, res, "i: %d", i)
if len(exp[i].e) == 0 { if len(exp[i].e) == 0 {
require.False(t, eFound, "i: %d", i) require.False(t, eFound, "i: %d", i)
} else { } else {
require.True(t, eFound, "i: %d", i) require.True(t, eFound, "i: %d", i)
require.Equal(t, exp[i].e[0], e, "i: %d", i) testutil.RequireEqual(t, exp[i].e[0], e, "i: %d", i)
require.False(t, p.Exemplar(&e), "too many exemplars returned, i: %d", i) require.False(t, p.Exemplar(&e), "too many exemplars returned, i: %d", i)
} }
if exp[i].ct != 0 { if exp[i].ct != 0 {
@ -2017,7 +2018,7 @@ func TestProtobufParse(t *testing.T) {
} else { } else {
require.Equal(t, int64(0), exp[i].t, "i: %d", i) require.Equal(t, int64(0), exp[i].t, "i: %d", i)
} }
require.Equal(t, exp[i].lset, res, "i: %d", i) testutil.RequireEqual(t, exp[i].lset, res, "i: %d", i)
require.Equal(t, exp[i].m, string(m), "i: %d", i) require.Equal(t, exp[i].m, string(m), "i: %d", i)
if shs != nil { if shs != nil {
require.Equal(t, exp[i].shs, shs, "i: %d", i) require.Equal(t, exp[i].shs, shs, "i: %d", i)
@ -2026,7 +2027,7 @@ func TestProtobufParse(t *testing.T) {
} }
j := 0 j := 0
for e := (exemplar.Exemplar{}); p.Exemplar(&e); j++ { for e := (exemplar.Exemplar{}); p.Exemplar(&e); j++ {
require.Equal(t, exp[i].e[j], e, "i: %d", i) testutil.RequireEqual(t, exp[i].e[j], e, "i: %d", i)
e = exemplar.Exemplar{} e = exemplar.Exemplar{}
} }
require.Len(t, exp[i].e, j, "not enough exemplars found, i: %d", i) require.Len(t, exp[i].e, j, "not enough exemplars found, i: %d", i)

View file

@ -26,6 +26,7 @@ import (
"github.com/prometheus/prometheus/model/histogram" "github.com/prometheus/prometheus/model/histogram"
"github.com/prometheus/prometheus/model/labels" "github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/util/testutil"
"github.com/prometheus/prometheus/promql/parser/posrange" "github.com/prometheus/prometheus/promql/parser/posrange"
) )
@ -4018,7 +4019,7 @@ func TestParseSeries(t *testing.T) {
if !test.fail { if !test.fail {
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, test.expectedMetric, metric, "error on input '%s'", test.input) testutil.RequireEqual(t, test.expectedMetric, metric, "error on input '%s'", test.input)
require.Equal(t, test.expectedValues, vals, "error in input '%s'", test.input) require.Equal(t, test.expectedValues, vals, "error in input '%s'", test.input)
} else { } else {
require.Error(t, err) require.Error(t, err)

View file

@ -719,7 +719,7 @@ func TestSendAlertsDontAffectActiveAlerts(t *testing.T) {
// The relabel rule changes a1=1 to a1=bug. // The relabel rule changes a1=1 to a1=bug.
// But the labels with the AlertingRule should not be changed. // But the labels with the AlertingRule should not be changed.
require.Equal(t, labels.FromStrings("a1", "1"), rule.active[h].Labels) testutil.RequireEqual(t, labels.FromStrings("a1", "1"), rule.active[h].Labels)
} }
func TestKeepFiringFor(t *testing.T) { func TestKeepFiringFor(t *testing.T) {

View file

@ -40,6 +40,7 @@ import (
"github.com/prometheus/prometheus/model/labels" "github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/model/relabel" "github.com/prometheus/prometheus/model/relabel"
"github.com/prometheus/prometheus/util/runutil" "github.com/prometheus/prometheus/util/runutil"
"github.com/prometheus/prometheus/util/testutil"
) )
func TestPopulateLabels(t *testing.T) { func TestPopulateLabels(t *testing.T) {
@ -449,8 +450,8 @@ func TestPopulateLabels(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
} }
require.Equal(t, c.in, in) require.Equal(t, c.in, in)
require.Equal(t, c.res, res) testutil.RequireEqual(t, c.res, res)
require.Equal(t, c.resOrig, orig) testutil.RequireEqual(t, c.resOrig, orig)
} }
} }

View file

@ -44,6 +44,7 @@ import (
"github.com/prometheus/prometheus/scrape" "github.com/prometheus/prometheus/scrape"
"github.com/prometheus/prometheus/tsdb/chunks" "github.com/prometheus/prometheus/tsdb/chunks"
"github.com/prometheus/prometheus/tsdb/record" "github.com/prometheus/prometheus/tsdb/record"
"github.com/prometheus/prometheus/util/testutil"
) )
const defaultFlushDeadline = 1 * time.Minute const defaultFlushDeadline = 1 * time.Minute
@ -1077,7 +1078,7 @@ func TestProcessExternalLabels(t *testing.T) {
} { } {
b.Reset(tc.labels) b.Reset(tc.labels)
processExternalLabels(b, tc.externalLabels) processExternalLabels(b, tc.externalLabels)
require.Equal(t, tc.expected, b.Labels(), "test %d", i) testutil.RequireEqual(t, tc.expected, b.Labels(), "test %d", i)
} }
} }

View file

@ -28,6 +28,7 @@ import (
"github.com/prometheus/prometheus/model/labels" "github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/prompb" "github.com/prometheus/prometheus/prompb"
"github.com/prometheus/prometheus/util/annotations" "github.com/prometheus/prometheus/util/annotations"
"github.com/prometheus/prometheus/util/testutil"
) )
func TestNoDuplicateReadConfigs(t *testing.T) { func TestNoDuplicateReadConfigs(t *testing.T) {
@ -484,7 +485,7 @@ func TestSampleAndChunkQueryableClient(t *testing.T) {
got = append(got, ss.At().Labels()) got = append(got, ss.At().Labels())
} }
require.NoError(t, ss.Err()) require.NoError(t, ss.Err())
require.Equal(t, tc.expectedSeries, got) testutil.RequireEqual(t, tc.expectedSeries, got)
}) })
} }
} }

View file

@ -26,6 +26,7 @@ import (
"time" "time"
"github.com/go-kit/log" "github.com/go-kit/log"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/prometheus/prometheus/model/exemplar" "github.com/prometheus/prometheus/model/exemplar"
@ -35,6 +36,7 @@ import (
"github.com/prometheus/prometheus/prompb" "github.com/prometheus/prometheus/prompb"
"github.com/prometheus/prometheus/storage" "github.com/prometheus/prometheus/storage"
"github.com/prometheus/prometheus/tsdb" "github.com/prometheus/prometheus/tsdb"
"github.com/prometheus/prometheus/util/testutil"
) )
func TestRemoteWriteHandler(t *testing.T) { func TestRemoteWriteHandler(t *testing.T) {
@ -59,23 +61,23 @@ func TestRemoteWriteHandler(t *testing.T) {
for _, ts := range writeRequestFixture.Timeseries { for _, ts := range writeRequestFixture.Timeseries {
labels := labelProtosToLabels(ts.Labels) labels := labelProtosToLabels(ts.Labels)
for _, s := range ts.Samples { for _, s := range ts.Samples {
require.Equal(t, mockSample{labels, s.Timestamp, s.Value}, appendable.samples[i]) requireEqual(t, mockSample{labels, s.Timestamp, s.Value}, appendable.samples[i])
i++ i++
} }
for _, e := range ts.Exemplars { for _, e := range ts.Exemplars {
exemplarLabels := labelProtosToLabels(e.Labels) exemplarLabels := labelProtosToLabels(e.Labels)
require.Equal(t, mockExemplar{labels, exemplarLabels, e.Timestamp, e.Value}, appendable.exemplars[j]) requireEqual(t, mockExemplar{labels, exemplarLabels, e.Timestamp, e.Value}, appendable.exemplars[j])
j++ j++
} }
for _, hp := range ts.Histograms { for _, hp := range ts.Histograms {
if hp.IsFloatHistogram() { if hp.IsFloatHistogram() {
fh := FloatHistogramProtoToFloatHistogram(hp) fh := FloatHistogramProtoToFloatHistogram(hp)
require.Equal(t, mockHistogram{labels, hp.Timestamp, nil, fh}, appendable.histograms[k]) requireEqual(t, mockHistogram{labels, hp.Timestamp, nil, fh}, appendable.histograms[k])
} else { } else {
h := HistogramProtoToHistogram(hp) h := HistogramProtoToHistogram(hp)
require.Equal(t, mockHistogram{labels, hp.Timestamp, h, nil}, appendable.histograms[k]) requireEqual(t, mockHistogram{labels, hp.Timestamp, h, nil}, appendable.histograms[k])
} }
k++ k++
@ -293,6 +295,13 @@ type mockHistogram struct {
fh *histogram.FloatHistogram fh *histogram.FloatHistogram
} }
// Wrapper to instruct go-cmp package to compare a list of structs with unexported fields.
func requireEqual(t *testing.T, expected, actual interface{}, msgAndArgs ...interface{}) {
testutil.RequireEqualWithOptions(t, expected, actual,
[]cmp.Option{cmp.AllowUnexported(mockSample{}), cmp.AllowUnexported(mockExemplar{}), cmp.AllowUnexported(mockHistogram{})},
msgAndArgs...)
}
func (m *mockAppendable) Appender(_ context.Context) storage.Appender { func (m *mockAppendable) Appender(_ context.Context) storage.Appender {
return m return m
} }

View file

@ -205,7 +205,7 @@ func TestIndexRW_Postings(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
require.Empty(t, c) require.Empty(t, c)
require.Equal(t, series[i], builder.Labels()) testutil.RequireEqual(t, series[i], builder.Labels())
} }
require.NoError(t, p.Err()) require.NoError(t, p.Err())
@ -488,7 +488,7 @@ func TestPersistence_index_e2e(t *testing.T) {
err = mi.Series(expp.At(), &eBuilder, &expchks) err = mi.Series(expp.At(), &eBuilder, &expchks)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, eBuilder.Labels(), builder.Labels()) testutil.RequireEqual(t, eBuilder.Labels(), builder.Labels())
require.Equal(t, expchks, chks) require.Equal(t, expchks, chks)
} }
require.False(t, expp.Next(), "Expected no more postings for %q=%q", p.Name, p.Value) require.False(t, expp.Next(), "Expected no more postings for %q=%q", p.Name, p.Value)