Skip to content

Commit

Permalink
testing: Migrate more things to testify
Browse files Browse the repository at this point in the history
So, in this commit, I have four goals:
- Remove the `internal/assert` package.
- Move `assert.EqualProto` into the `internal/protobuf` package.
- Make `protobuf.ExpectEqual` and `protobuf.RequireEqual` work similarly
to testify `assert` and `require` respectively.
- Replace any remaining `cmp.Diff` calls in tests with `assert.Equal`
- Check all the tests in `internal/envoy` for protobufs and migrate any
remaining to `protobuf.ExpectEqual`.

Sorry, it's a big PR, but there's a lot of cleanup here.

Updates projectcontour#2786

Signed-off-by: Nick Young <ynick@vmware.com>
  • Loading branch information
Nick Young committed Aug 24, 2020
1 parent 0d684bc commit db32587
Show file tree
Hide file tree
Showing 40 changed files with 106 additions and 120 deletions.
2 changes: 1 addition & 1 deletion cmd/contour/certgen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import (
"sort"
"testing"

"github.com/projectcontour/contour/internal/assert"
"github.com/projectcontour/contour/internal/certgen"
"github.com/projectcontour/contour/internal/dag"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
)

Expand Down
2 changes: 1 addition & 1 deletion cmd/contour/serve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import (
"testing"
"time"

"github.com/projectcontour/contour/internal/assert"
"github.com/projectcontour/contour/internal/timeout"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)

func TestGetRequestTimeout(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/contour/servecontext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
"k8s.io/apimachinery/pkg/types"

"github.com/google/go-cmp/cmp"
"github.com/projectcontour/contour/internal/assert"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc"
"gopkg.in/yaml.v2"
)
Expand Down
2 changes: 1 addition & 1 deletion cmd/contour/shutdownmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strings"
"testing"

"github.com/projectcontour/contour/internal/assert"
"github.com/stretchr/testify/assert"
)

func TestShutdownManager_HealthzHandler(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion internal/annotation/annotations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"testing"

projectcontour "github.com/projectcontour/contour/apis/projectcontour/v1"
"github.com/projectcontour/contour/internal/assert"
"github.com/stretchr/testify/assert"
v1 "k8s.io/api/core/v1"
"k8s.io/api/networking/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down
7 changes: 3 additions & 4 deletions internal/contour/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes/duration"
projcontour "github.com/projectcontour/contour/apis/projectcontour/v1"
"github.com/projectcontour/contour/internal/assert"
"github.com/projectcontour/contour/internal/envoy"
"github.com/projectcontour/contour/internal/protobuf"
v1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -71,7 +70,7 @@ func TestClusterCacheContents(t *testing.T) {
var cc ClusterCache
cc.Update(tc.contents)
got := cc.Contents()
assert.EqualProto(t, tc.want, got)
protobuf.ExpectEqual(t, tc.want, got)
})
}
}
Expand Down Expand Up @@ -151,7 +150,7 @@ func TestClusterCacheQuery(t *testing.T) {
var cc ClusterCache
cc.Update(tc.contents)
got := cc.Query(tc.query)
assert.EqualProto(t, tc.want, got)
protobuf.ExpectEqual(t, tc.want, got)
})
}
}
Expand Down Expand Up @@ -790,7 +789,7 @@ func TestClusterVisit(t *testing.T) {
t.Run(name, func(t *testing.T) {
root := buildDAG(t, tc.objs...)
got := visitClusters(root)
assert.EqualProto(t, tc.want, got)
protobuf.ExpectEqual(t, tc.want, got)
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/contour/endpointstranslator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import (

v2 "github.com/envoyproxy/go-control-plane/envoy/api/v2"
"github.com/golang/protobuf/proto"
"github.com/projectcontour/contour/internal/assert"
"github.com/projectcontour/contour/internal/envoy"
"github.com/projectcontour/contour/internal/fixture"
"github.com/stretchr/testify/assert"
v1 "k8s.io/api/core/v1"
)

Expand Down
8 changes: 4 additions & 4 deletions internal/contour/listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import (
envoy_api_v2_listener "github.com/envoyproxy/go-control-plane/envoy/api/v2/listener"
"github.com/golang/protobuf/proto"
projcontour "github.com/projectcontour/contour/apis/projectcontour/v1"
"github.com/projectcontour/contour/internal/assert"
"github.com/projectcontour/contour/internal/dag"
"github.com/projectcontour/contour/internal/envoy"
"github.com/projectcontour/contour/internal/protobuf"
"github.com/projectcontour/contour/internal/timeout"
v1 "k8s.io/api/core/v1"
"k8s.io/api/networking/v1beta1"
Expand Down Expand Up @@ -66,7 +66,7 @@ func TestListenerCacheContents(t *testing.T) {
var lc ListenerCache
lc.Update(tc.contents)
got := lc.Contents()
assert.EqualProto(t, tc.want, got)
protobuf.ExpectEqual(t, tc.want, got)
})
}
}
Expand Down Expand Up @@ -128,7 +128,7 @@ func TestListenerCacheQuery(t *testing.T) {
var lc ListenerCache
lc.Update(tc.contents)
got := lc.Query(tc.query)
assert.EqualProto(t, tc.want, got)
protobuf.ExpectEqual(t, tc.want, got)
})
}
}
Expand Down Expand Up @@ -1998,7 +1998,7 @@ func TestListenerVisit(t *testing.T) {
t.Run(name, func(t *testing.T) {
root := buildDAGFallback(t, tc.fallbackCertificate, tc.objs...)
got := visitListeners(root, &tc.ListenerConfig)
assert.EqualProto(t, tc.want, got)
protobuf.ExpectEqual(t, tc.want, got)
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/contour/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import (
"testing"

projcontour "github.com/projectcontour/contour/apis/projectcontour/v1"
"github.com/projectcontour/contour/internal/assert"
"github.com/projectcontour/contour/internal/dag"
"github.com/projectcontour/contour/internal/fixture"
"github.com/projectcontour/contour/internal/metrics"
"github.com/stretchr/testify/assert"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand Down
9 changes: 4 additions & 5 deletions internal/contour/route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes/wrappers"
projcontour "github.com/projectcontour/contour/apis/projectcontour/v1"
"github.com/projectcontour/contour/internal/assert"
"github.com/projectcontour/contour/internal/dag"
"github.com/projectcontour/contour/internal/envoy"
"github.com/projectcontour/contour/internal/protobuf"
Expand Down Expand Up @@ -68,7 +67,7 @@ func TestRouteCacheContents(t *testing.T) {
var rc RouteCache
rc.Update(tc.contents)
got := rc.Contents()
assert.EqualProto(t, tc.want, got)
protobuf.ExpectEqual(t, tc.want, got)
})
}
}
Expand Down Expand Up @@ -128,7 +127,7 @@ func TestRouteCacheQuery(t *testing.T) {
var rc RouteCache
rc.Update(tc.contents)
got := rc.Query(tc.query)
assert.EqualProto(t, tc.want, got)
protobuf.ExpectEqual(t, tc.want, got)
})
}
}
Expand Down Expand Up @@ -2756,7 +2755,7 @@ func TestRouteVisit(t *testing.T) {
t.Run(name, func(t *testing.T) {
root := buildDAGFallback(t, tc.fallbackCertificate, tc.objs...)
got := visitRoutes(root)
assert.EqualProto(t, tc.want, got)
protobuf.ExpectEqual(t, tc.want, got)
})
}
}
Expand Down Expand Up @@ -2926,7 +2925,7 @@ func TestSortLongestRouteFirst(t *testing.T) {
t.Run(name, func(t *testing.T) {
got := append([]*envoy_api_v2_route.Route{}, tc.routes...) // shallow copy
sortRoutes(got)
assert.EqualProto(t, tc.want, got)
protobuf.ExpectEqual(t, tc.want, got)
})
}
}
Expand Down
8 changes: 4 additions & 4 deletions internal/contour/secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import (
envoy_api_v2_core "github.com/envoyproxy/go-control-plane/envoy/api/v2/core"
"github.com/golang/protobuf/proto"
projcontour "github.com/projectcontour/contour/apis/projectcontour/v1"
"github.com/projectcontour/contour/internal/assert"
"github.com/projectcontour/contour/internal/dag"
"github.com/projectcontour/contour/internal/fixture"
"github.com/projectcontour/contour/internal/protobuf"
v1 "k8s.io/api/core/v1"
"k8s.io/api/networking/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -54,7 +54,7 @@ func TestSecretCacheContents(t *testing.T) {
var sc SecretCache
sc.Update(tc.contents)
got := sc.Contents()
assert.EqualProto(t, tc.want, got)
protobuf.ExpectEqual(t, tc.want, got)
})
}
}
Expand Down Expand Up @@ -98,7 +98,7 @@ func TestSecretCacheQuery(t *testing.T) {
var sc SecretCache
sc.Update(tc.contents)
got := sc.Query(tc.query)
assert.EqualProto(t, tc.want, got)
protobuf.ExpectEqual(t, tc.want, got)
})
}
}
Expand Down Expand Up @@ -471,7 +471,7 @@ func TestSecretVisit(t *testing.T) {
t.Run(name, func(t *testing.T) {
root := buildDAG(t, tc.objs...)
got := visitSecrets(root)
assert.EqualProto(t, tc.want, got)
protobuf.ExpectEqual(t, tc.want, got)
})
}
}
Expand Down
8 changes: 4 additions & 4 deletions internal/contour/visitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import (
envoy_api_v2_auth "github.com/envoyproxy/go-control-plane/envoy/api/v2/auth"
envoy_api_v2_core "github.com/envoyproxy/go-control-plane/envoy/api/v2/core"
envoy_api_v2_listener "github.com/envoyproxy/go-control-plane/envoy/api/v2/listener"
"github.com/projectcontour/contour/internal/assert"
"github.com/projectcontour/contour/internal/dag"
"github.com/projectcontour/contour/internal/envoy"
"github.com/projectcontour/contour/internal/protobuf"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
Expand Down Expand Up @@ -75,7 +75,7 @@ func TestVisitClusters(t *testing.T) {
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
got := visitClusters(tc.root)
assert.EqualProto(t, tc.want, got)
protobuf.ExpectEqual(t, tc.want, got)
})
}
}
Expand Down Expand Up @@ -144,7 +144,7 @@ func TestVisitListeners(t *testing.T) {
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
got := visitListeners(tc.root, new(ListenerConfig))
assert.EqualProto(t, tc.want, got)
protobuf.ExpectEqual(t, tc.want, got)
})
}
}
Expand Down Expand Up @@ -210,7 +210,7 @@ func TestVisitSecrets(t *testing.T) {
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
got := visitSecrets(tc.root)
assert.EqualProto(t, tc.want, got)
protobuf.ExpectEqual(t, tc.want, got)
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/dag/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import (

envoy_api_v2_auth "github.com/envoyproxy/go-control-plane/envoy/api/v2/auth"
projcontour "github.com/projectcontour/contour/apis/projectcontour/v1"
"github.com/projectcontour/contour/internal/assert"
"github.com/projectcontour/contour/internal/fixture"
"github.com/projectcontour/contour/internal/timeout"
"github.com/stretchr/testify/assert"
v1 "k8s.io/api/core/v1"
"k8s.io/api/networking/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down
6 changes: 3 additions & 3 deletions internal/dag/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import (
projcontour "github.com/projectcontour/contour/apis/projectcontour/v1"
projectcontourv1alpha1 "github.com/projectcontour/contour/apis/projectcontour/v1alpha1"
"github.com/projectcontour/contour/internal/annotation"
"github.com/projectcontour/contour/internal/assert"
"github.com/projectcontour/contour/internal/fixture"
"github.com/stretchr/testify/assert"
v1 "k8s.io/api/core/v1"
"k8s.io/api/networking/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -743,7 +743,7 @@ func TestKubernetesCacheInsert(t *testing.T) {
cache.Insert(p)
}
got := cache.Insert(tc.obj)
assert.Equal(t, tc.want, got)
assert.Equalf(t, tc.want, got, "Insert failed for object %v ", tc.obj)
})
}
}
Expand Down Expand Up @@ -951,7 +951,7 @@ func TestKubernetesCacheRemove(t *testing.T) {
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
got := tc.cache.Remove(tc.obj)
assert.Equal(t, tc.want, got)
assert.Equalf(t, tc.want, got, "Remove failed for object %v ", tc.obj)
})
}
}
11 changes: 6 additions & 5 deletions internal/dag/conditions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"testing"

projcontour "github.com/projectcontour/contour/apis/projectcontour/v1"
"github.com/projectcontour/contour/internal/assert"
"github.com/stretchr/testify/assert"
)

func TestPathMatchCondition(t *testing.T) {
Expand Down Expand Up @@ -494,11 +494,12 @@ func TestValidateHeaderMatchConditions(t *testing.T) {
t.Run(name, func(t *testing.T) {
gotErr := headerMatchConditionsValid(tc.matchconditions)

if !tc.wantErr && gotErr != nil {
t.Fatalf("Expected no error, got (%v)", gotErr)
if !tc.wantErr {
assert.NoError(t, gotErr)
}
if tc.wantErr && gotErr == nil {
t.Fatalf("Expected error, got none")

if tc.wantErr {
assert.Error(t, gotErr)
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion internal/dag/dag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package dag
import (
"testing"

"github.com/projectcontour/contour/internal/assert"
"github.com/stretchr/testify/assert"
v1 "k8s.io/api/core/v1"
)

Expand Down
2 changes: 1 addition & 1 deletion internal/dag/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (
"time"

projcontour "github.com/projectcontour/contour/apis/projectcontour/v1"
"github.com/projectcontour/contour/internal/assert"
"github.com/projectcontour/contour/internal/timeout"
"github.com/stretchr/testify/assert"
"k8s.io/api/networking/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand Down
2 changes: 1 addition & 1 deletion internal/dag/secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"fmt"
"testing"

"github.com/projectcontour/contour/internal/assert"
"github.com/stretchr/testify/assert"
v1 "k8s.io/api/core/v1"
)

Expand Down
2 changes: 1 addition & 1 deletion internal/dag/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import (
"testing"

projcontour "github.com/projectcontour/contour/apis/projectcontour/v1"
"github.com/projectcontour/contour/internal/assert"
"github.com/projectcontour/contour/internal/fixture"
"github.com/projectcontour/contour/internal/k8s"
"github.com/stretchr/testify/assert"
v1 "k8s.io/api/core/v1"
"k8s.io/api/networking/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down
5 changes: 2 additions & 3 deletions internal/envoy/accesslog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
envoy_accesslog "github.com/envoyproxy/go-control-plane/envoy/config/filter/accesslog/v2"
"github.com/envoyproxy/go-control-plane/pkg/wellknown"
_struct "github.com/golang/protobuf/ptypes/struct"
"github.com/projectcontour/contour/internal/assert"
"github.com/projectcontour/contour/internal/protobuf"
)

Expand All @@ -44,7 +43,7 @@ func TestFileAccessLog(t *testing.T) {
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
got := FileAccessLogEnvoy(tc.path)
assert.Equal(t, tc.want, got)
protobuf.ExpectEqual(t, tc.want, got)
})
}
}
Expand Down Expand Up @@ -104,7 +103,7 @@ func TestJSONFileAccessLog(t *testing.T) {
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
got := FileAccessLogJSON(tc.path, tc.headers)
assert.EqualProto(t, tc.want, got)
protobuf.ExpectEqual(t, tc.want, got)
})
}
}
Loading

0 comments on commit db32587

Please sign in to comment.