From 5a6c8f9c152dfab5f96f5b6f14703b801b014255 Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Wed, 24 Jan 2024 09:52:16 +0000 Subject: [PATCH] promtool: use go-cmp instead of DeepEqual go-cmp allows more control over unexported fields and implementation details. Signed-off-by: Bryan Boreham --- cmd/promtool/sd.go | 4 ++-- cmd/promtool/unittest.go | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/cmd/promtool/sd.go b/cmd/promtool/sd.go index 4892743fc0..e65262d439 100644 --- a/cmd/promtool/sd.go +++ b/cmd/promtool/sd.go @@ -18,10 +18,10 @@ import ( "encoding/json" "fmt" "os" - "reflect" "time" "github.com/go-kit/log" + "github.com/google/go-cmp/cmp" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/prometheus/config" @@ -153,7 +153,7 @@ func getSDCheckResult(targetGroups []*targetgroup.Group, scrapeConfig *config.Sc duplicateRes := false for _, sdCheckRes := range sdCheckResults { - if reflect.DeepEqual(sdCheckRes, result) { + if cmp.Equal(sdCheckRes, result, cmp.Comparer(labels.Equal)) { duplicateRes = true break } diff --git a/cmd/promtool/unittest.go b/cmd/promtool/unittest.go index a89288c44a..4777b88098 100644 --- a/cmd/promtool/unittest.go +++ b/cmd/promtool/unittest.go @@ -20,13 +20,13 @@ import ( "fmt" "os" "path/filepath" - "reflect" "sort" "strconv" "strings" "time" "github.com/go-kit/log" + "github.com/google/go-cmp/cmp" "github.com/grafana/regexp" "github.com/nsf/jsondiff" "github.com/prometheus/common/model" @@ -340,7 +340,7 @@ func (tg *testGroup) test(evalInterval time.Duration, groupOrderMap map[string]i sort.Sort(gotAlerts) sort.Sort(expAlerts) - if !reflect.DeepEqual(expAlerts, gotAlerts) { + if !cmp.Equal(expAlerts, gotAlerts, cmp.Comparer(labels.Equal)) { var testName string if tg.TestGroupName != "" { testName = fmt.Sprintf(" name: %s,\n", tg.TestGroupName) @@ -448,7 +448,7 @@ Outer: sort.Slice(gotSamples, func(i, j int) bool { return labels.Compare(gotSamples[i].Labels, gotSamples[j].Labels) <= 0 }) - if !reflect.DeepEqual(expSamples, gotSamples) { + if !cmp.Equal(expSamples, gotSamples, cmp.Comparer(labels.Equal)) { errs = append(errs, fmt.Errorf(" expr: %q, time: %s,\n exp: %v\n got: %v", testCase.Expr, testCase.EvalTime.String(), parsedSamplesString(expSamples), parsedSamplesString(gotSamples))) }