Skip to content

Commit

Permalink
test: Harvest should use go-cmp instead of reflect.DeepEqual (#3025)
Browse files Browse the repository at this point in the history
go-cmp makes it easier to tell what failed between got and want

chore: bump gopsutil
  • Loading branch information
cgrinds committed Jul 2, 2024
1 parent 8099c24 commit 50a7d40
Show file tree
Hide file tree
Showing 30 changed files with 5,055 additions and 67 deletions.
7 changes: 4 additions & 3 deletions cmd/collectors/rest/rest_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package rest

import (
"github.com/google/go-cmp/cmp"
"github.com/netapp/harvest/v2/cmd/collectors"
"github.com/netapp/harvest/v2/cmd/poller/collector"
"github.com/netapp/harvest/v2/cmd/poller/options"
"github.com/netapp/harvest/v2/pkg/conf"
"github.com/netapp/harvest/v2/pkg/matrix"
"github.com/tidwall/gjson"
"os"
"reflect"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -431,8 +431,9 @@ func TestFields(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := tt.r.Fields(tt.p)
if !reflect.DeepEqual(result, tt.expectedResult) {
t.Errorf("Expected %v, got %v", tt.expectedResult, result)
diff := cmp.Diff(result, tt.expectedResult)
if diff != "" {
t.Errorf("Mismatch (-got +want):\n%s", diff)
}
})
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/collectors/restperf/restperf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package restperf

import (
"fmt"
"github.com/google/go-cmp/cmp"
"github.com/netapp/harvest/v2/cmd/collectors"
rest2 "github.com/netapp/harvest/v2/cmd/collectors/rest"
"github.com/netapp/harvest/v2/cmd/poller/collector"
Expand All @@ -13,7 +14,6 @@ import (
"github.com/netapp/harvest/v2/pkg/tree/node"
"github.com/tidwall/gjson"
"os"
"reflect"
"sort"
"testing"
"time"
Expand Down Expand Up @@ -59,8 +59,9 @@ func Test_parseMetricResponse(t *testing.T) {
}
metricResponses := parseMetricResponses(tt.args.instanceData, metrics)
got := metricResponses[tt.args.metric]
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("\ngot %v\nwant %v", got, tt.want)
diff := cmp.Diff(got, tt.want, cmp.AllowUnexported(metricResponse{}))
if diff != "" {
t.Errorf("Mismatch (-got +want):\n%s", diff)
}
})
}
Expand Down
8 changes: 5 additions & 3 deletions cmd/poller/poller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package main

import (
"errors"
"github.com/google/go-cmp/cmp"
"github.com/netapp/harvest/v2/pkg/conf"
"github.com/netapp/harvest/v2/pkg/tree/node"
"os"
"reflect"
"testing"
)

Expand Down Expand Up @@ -194,8 +194,10 @@ func Test_nonOverlappingCollectors(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := nonOverlappingCollectors(tt.args); !reflect.DeepEqual(got, tt.want) {
t.Errorf("got=[%v], want=[%v]", got, tt.want)
got := nonOverlappingCollectors(tt.args)
diff := cmp.Diff(got, tt.want, cmp.AllowUnexported(objectCollector{}))
if diff != "" {
t.Errorf("Mismatch (-got +want):\n%s", diff)
}
})
}
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ go 1.22

require (
github.com/go-openapi/spec v0.21.0
github.com/google/go-cmp v0.6.0
github.com/mattn/go-runewidth v0.0.15
github.com/rs/zerolog v1.33.0
github.com/shirou/gopsutil/v4 v4.24.5
github.com/shirou/gopsutil/v4 v4.24.6
github.com/spf13/cobra v1.8.1
github.com/tidwall/gjson v1.17.1
github.com/zekroTJA/timedmap/v2 v2.0.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shirou/gopsutil/v4 v4.24.5 h1:gGsArG5K6vmsh5hcFOHaPm87UD003CaDMkAOweSQjhM=
github.com/shirou/gopsutil/v4 v4.24.5/go.mod h1:aoebb2vxetJ/yIDZISmduFvVNPHqXQ9SEJwRXxkf0RA=
github.com/shirou/gopsutil/v4 v4.24.6 h1:9qqCSYF2pgOU+t+NgJtp7Co5+5mHF/HyKBUckySQL64=
github.com/shirou/gopsutil/v4 v4.24.6/go.mod h1:aoebb2vxetJ/yIDZISmduFvVNPHqXQ9SEJwRXxkf0RA=
github.com/shoenig/go-m1cpu v0.1.6 h1:nxdKQNcEB6vzgA2E2bvzKIYRuNj7XNJ4S/aRSwKzFtM=
github.com/shoenig/go-m1cpu v0.1.6/go.mod h1:1JJMcUBvfNwpq05QDQVAnx3gUHr9IYF7GNg9SUEw2VQ=
github.com/shoenig/test v0.6.4 h1:kVTaSd7WLz5WZ2IaoM0RSzRsUD+m8wRR+5qvntpn4LU=
Expand Down
12 changes: 7 additions & 5 deletions pkg/auth/auth_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package auth

import (
"github.com/google/go-cmp/cmp"
"github.com/netapp/harvest/v2/pkg/conf"
"github.com/netapp/harvest/v2/pkg/logging"
"os"
"reflect"
"testing"
)

Expand Down Expand Up @@ -587,11 +587,13 @@ Pollers:
tt.want.HasCredentialScript,
)
}
if !reflect.DeepEqual(tt.want.PemCert, got.PemCert) {
t.Errorf("got PemCert=[%s], want PemCert=[%s]", got.PemCert, tt.want.PemCert)
diff1 := cmp.Diff(tt.want.PemCert, got.PemCert)
if diff1 != "" {
t.Errorf("Mismatch (-got +want):\n%s", diff1)
}
if !reflect.DeepEqual(tt.want.PemKey, got.PemKey) {
t.Errorf("got PemKey=[%s], want PemKey=[%s]", got.PemKey, tt.want.PemKey)
diff2 := cmp.Diff(tt.want.PemKey, got.PemKey)
if diff2 != "" {
t.Errorf("Mismatch (-got +want):\n%s", diff2)
}
if tt.want.CertPath != got.CertPath && got.CertPath != hostCertPath {
t.Errorf("got CertPath=[%s], want CertPath=[%s]", got.CertPath, tt.want.CertPath)
Expand Down
21 changes: 11 additions & 10 deletions pkg/conf/conf_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package conf

import (
"github.com/google/go-cmp/cmp"
"github.com/netapp/harvest/v2/pkg/tree/node"
"os"
"reflect"
"slices"
"sort"
"strconv"
Expand Down Expand Up @@ -66,9 +66,9 @@ func TestPollerStructDefaults(t *testing.T) {
if len(poller.Exporters) != 1 {
t.Fatalf(`expected 1 exporter but got %v`, poller.Exporters)
}
expected := []string{"prometheusrange"}
if !reflect.DeepEqual(poller.Exporters, expected) {
t.Fatalf(`expected collectors to be %v but was %v`, expected, poller.Exporters)
diff := cmp.Diff(poller.Exporters, []string{"prometheusrange"})
if diff != "" {
t.Errorf("Mismatch (-got +want):\n%s", diff)
}
})

Expand All @@ -85,9 +85,10 @@ func TestPollerStructDefaults(t *testing.T) {
t.Fatalf(`expected 2 collectors but got %v`, poller.Collectors)
}
defaultT := []string{"default.yaml", "custom.yaml"}
expected := []Collector{{Name: "Zapi", Templates: &defaultT}, {Name: "ZapiPerf", Templates: &defaultT}}
if !reflect.DeepEqual(poller.Collectors, expected) {
t.Fatalf(`expected collectors to be %v but was %v`, expected, poller.Collectors)
want := []Collector{{Name: "Zapi", Templates: &defaultT}, {Name: "ZapiPerf", Templates: &defaultT}}
diff := cmp.Diff(poller.Collectors, want)
if diff != "" {
t.Errorf("Mismatch (-got +want):\n%s", diff)
}
})

Expand Down Expand Up @@ -197,9 +198,9 @@ func TestUniqueExportersByType(t *testing.T) {
t.Run("Exporters are unique by type", func(t *testing.T) {
exporters := GetUniqueExporters(poller2.Exporters)
sort.Strings(exporters)
want := []string{"foo1", "foo2", "influxy", "influxz", "prometheus"}
if !reflect.DeepEqual(want, exporters) {
t.Fatalf(`expected %v but got %v`, want, exporters)
diff := cmp.Diff([]string{"foo1", "foo2", "influxy", "influxz", "prometheus"}, exporters)
if diff != "" {
t.Errorf("Mismatch (-got +want):\n%s", diff)
}
})
}
Expand Down
27 changes: 27 additions & 0 deletions vendor/github.com/google/go-cmp/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 50a7d40

Please sign in to comment.