test: use google cmp library to do the deep reflect

The cmp library has two advantages:

- it shows diffs between compared values
- it allows ignoring fields based on field name or type
This commit is contained in:
Ondřej Budai 2019-11-28 10:52:43 +01:00 committed by Tom Gundersen
parent 268befa435
commit 613b659b95
4 changed files with 8 additions and 2 deletions

1
go.mod
View file

@ -7,6 +7,7 @@ require (
github.com/aws/aws-sdk-go v1.25.37 github.com/aws/aws-sdk-go v1.25.37
github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f
github.com/gobwas/glob v0.2.3 github.com/gobwas/glob v0.2.3
github.com/google/go-cmp v0.3.1
github.com/google/uuid v1.1.1 github.com/google/uuid v1.1.1
github.com/julienschmidt/httprouter v1.2.0 github.com/julienschmidt/httprouter v1.2.0
) )

2
go.sum
View file

@ -8,6 +8,8 @@ github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f h1:JOrtw2xFKzlg+
github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y= github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y=
github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8= github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8=
github.com/google/go-cmp v0.3.1 h1:Xye71clBPdm5HgqGwUkwhbynsUJZhDbS20FvLhQ2izg=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY= github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5imkbgOkpRUYLnmbU7UEFbjtDA2hxJ1ichM= github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5imkbgOkpRUYLnmbU7UEFbjtDA2hxJ1ichM=

View file

@ -27,6 +27,7 @@ BuildRequires: golang(github.com/coreos/go-systemd/activation)
BuildRequires: golang(github.com/google/uuid) BuildRequires: golang(github.com/google/uuid)
BuildRequires: golang(github.com/julienschmidt/httprouter) BuildRequires: golang(github.com/julienschmidt/httprouter)
BuildRequires: golang(github.com/gobwas/glob) BuildRequires: golang(github.com/gobwas/glob)
BuildRequires: golang(github.com/google/go-cmp/cmp)
Requires: systemd Requires: systemd
Requires: osbuild Requires: osbuild

View file

@ -12,6 +12,8 @@ import (
"reflect" "reflect"
"strings" "strings"
"testing" "testing"
"github.com/google/go-cmp/cmp"
) )
type API interface { type API interface {
@ -137,8 +139,8 @@ func TestRoute(t *testing.T, api API, external bool, method, path, body string,
dropFields(reply, ignoreFields...) dropFields(reply, ignoreFields...)
dropFields(expected, ignoreFields...) dropFields(expected, ignoreFields...)
if !reflect.DeepEqual(reply, expected) { if diff := cmp.Diff(expected, reply); diff != "" {
t.Errorf("%s: reply != expected:\n reply: %s\nexpected: %s", path, strings.TrimSpace(string(replyJSON)), expectedJSON) t.Errorf("%s: reply != expected:\n reply: %s\nexpected: %s\ndiff: %s", path, strings.TrimSpace(string(replyJSON)), expectedJSON, diff)
return return
} }
} }