Skip to content

Commit

Permalink
testing: Migrate as many tests as possible to internal/assert
Browse files Browse the repository at this point in the history
Needed to update some aliases in internal/assert to cover all the cases.

Updates projectcontour#2786.

Signed-off-by: Nick Young <ynick@vmware.com>
  • Loading branch information
Nick Young committed Aug 20, 2020
1 parent a6279c3 commit c58a9a1
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 182 deletions.
8 changes: 8 additions & 0 deletions internal/assert/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ var (
Equal = tassert.Equal
Equalf = tassert.Equalf
ElementsMatch = tassert.ElementsMatch
Error = tassert.Error
Errorf = tassert.Errorf
NoError = tassert.NoError
NoErrorf = tassert.NoErrorf
True = tassert.True
Truef = tassert.Truef
False = tassert.False
Falsef = tassert.Falsef
)

// EqualProto will test that want == got for protobufs, call t.Error if it does not,
Expand Down
23 changes: 8 additions & 15 deletions internal/certgen/certgen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"fmt"
"testing"
"time"

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

func TestGeneratedCertsValid(t *testing.T) {
Expand All @@ -27,24 +29,17 @@ func TestGeneratedCertsValid(t *testing.T) {
expiry := now.Add(24 * 365 * time.Hour)

cacert, cakey, err := NewCA("contour", expiry)
if err != nil {
t.Fatalf("Failed to generate CA cert: %s", err)
}
assert.NoErrorf(t, err, "Failed to generate CA cert")

contourcert, _, err := NewCert(cacert, cakey, expiry, "contour", "projectcontour")
if err != nil {
t.Fatalf("Failed to generate Contour cert: %s", err)
}
assert.NoErrorf(t, err, "Failed to generate Contour cert")

roots := x509.NewCertPool()
ok := roots.AppendCertsFromPEM(cacert)
if !ok {
t.Fatal("Failed to set up CA cert for testing, maybe it's an invalid PEM")
}
assert.Truef(t, ok, "Failed to set up CA cert for testing, maybe it's an invalid PEM")

envoycert, _, err := NewCert(cacert, cakey, expiry, "envoy", "projectcontour")
if err != nil {
t.Fatalf("Failed to generate Envoy cert: %s", err)
}
assert.NoErrorf(t, err, "Failed to generate Envoy cert")

tests := map[string]struct {
cert []byte
Expand All @@ -63,9 +58,7 @@ func TestGeneratedCertsValid(t *testing.T) {
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
err := verifyCert(tc.cert, roots, tc.dnsname)
if err != nil {
t.Fatalf("Validating %s failed: %s", name, err)
}
assert.NoErrorf(t, err, "Validating %s failed", name)
})
}

Expand Down
34 changes: 12 additions & 22 deletions internal/contour/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
v2 "github.com/envoyproxy/go-control-plane/envoy/api/v2"
discovery "github.com/envoyproxy/go-control-plane/envoy/service/discovery/v2"
resource "github.com/envoyproxy/go-control-plane/pkg/resource/v2"
"github.com/projectcontour/contour/internal/assert"
"github.com/projectcontour/contour/internal/dag"
"github.com/projectcontour/contour/internal/xds"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -63,7 +64,7 @@ func TestGRPC(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
stream, err := sds.StreamClusters(ctx)
check(t, err)
assert.NoError(t, err)
sendreq(t, stream, resource.ClusterType) // send initial notification
checkrecv(t, stream) // check we receive one notification
checktimeout(t, stream) // check that the second receive times out
Expand All @@ -90,7 +91,7 @@ func TestGRPC(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
stream, err := eds.StreamEndpoints(ctx)
check(t, err)
assert.NoError(t, err)
sendreq(t, stream, resource.EndpointType) // send initial notification
checkrecv(t, stream) // check we receive one notification
checktimeout(t, stream) // check that the second receive times out
Expand Down Expand Up @@ -123,7 +124,7 @@ func TestGRPC(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
stream, err := lds.StreamListeners(ctx)
check(t, err)
assert.NoError(t, err)
sendreq(t, stream, resource.ListenerType) // send initial notification
checkrecv(t, stream) // check we receive one notification
checktimeout(t, stream) // check that the second receive times out
Expand Down Expand Up @@ -155,7 +156,7 @@ func TestGRPC(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
stream, err := rds.StreamRoutes(ctx)
check(t, err)
assert.NoError(t, err)
sendreq(t, stream, resource.RouteType) // send initial notification
checkrecv(t, stream) // check we receive one notification
checktimeout(t, stream) // check that the second receive times out
Expand All @@ -176,7 +177,7 @@ func TestGRPC(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
stream, err := sds.StreamSecrets(ctx)
check(t, err)
assert.NoError(t, err)
sendreq(t, stream, resource.SecretType) // send initial notification
checkrecv(t, stream) // check we receive one notification
checktimeout(t, stream) // check that the second receive times out
Expand Down Expand Up @@ -206,7 +207,7 @@ func TestGRPC(t *testing.T) {

srv := xds.RegisterServer(xds.NewContourServer(log, ResourcesOf(resources)...), nil)
l, err := net.Listen("tcp", "127.0.0.1:0")
check(t, err)
assert.NoError(t, err)
done := make(chan error, 1)
stop := make(chan struct{})
run := eh.Start()
Expand All @@ -220,50 +221,39 @@ func TestGRPC(t *testing.T) {
<-done
}()
cc, err := grpc.Dial(l.Addr().String(), grpc.WithInsecure())
check(t, err)
assert.NoError(t, err)
defer cc.Close()
fn(t, cc)
})
}
}

func check(t *testing.T, err error) {
t.Helper()
if err != nil {
t.Fatal(err)
}
}

func sendreq(t *testing.T, stream interface {
Send(*v2.DiscoveryRequest) error
}, typeurl string) {
t.Helper()
err := stream.Send(&v2.DiscoveryRequest{
TypeUrl: typeurl,
})
check(t, err)
assert.NoError(t, err)
}

func checkrecv(t *testing.T, stream interface {
Recv() (*v2.DiscoveryResponse, error)
}) {
t.Helper()
_, err := stream.Recv()
check(t, err)
assert.NoError(t, err)
}

func checktimeout(t *testing.T, stream interface {
Recv() (*v2.DiscoveryResponse, error)
}) {
t.Helper()
_, err := stream.Recv()
if err == nil {
t.Fatal("expected timeout")
}
assert.Errorf(t, err, "expected timeout")
s, ok := status.FromError(err)
if !ok {
t.Fatalf("%T %v", err, err)
}
assert.True(t, ok, "Error wasn't what was expected: %T %v", err, err)

// Work around grpc/grpc-go#1645 which sometimes seems to
// set the status code to Unknown, even when the message is derived from context.DeadlineExceeded.
Expand Down
42 changes: 9 additions & 33 deletions internal/dag/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import (
"time"

envoy_api_v2_auth "github.com/envoyproxy/go-control-plane/envoy/api/v2/auth"
"github.com/google/go-cmp/cmp"
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"
v1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -6025,13 +6025,7 @@ func TestDAGInsert(t *testing.T) {
want[l.Port] = l
}
}
opts := []cmp.Option{
cmp.AllowUnexported(VirtualHost{}),
cmp.AllowUnexported(timeout.Setting{}),
}
if diff := cmp.Diff(want, got, opts...); diff != "" {
t.Fatal(diff)
}
assert.Equal(t, want, got)
})
}
}
Expand Down Expand Up @@ -6129,14 +6123,8 @@ func TestBuilderLookupService(t *testing.T) {
b.reset()

got, gotErr := b.lookupService(tc.NamespacedName, tc.port)
if diff := cmp.Diff(tc.want, got); diff != "" {
t.Fatal(diff)
}
if (tc.wantErr != nil) != (gotErr != nil) {
t.Errorf("got err = %v, wanted %v", gotErr, tc.wantErr)
} else if tc.wantErr != nil && gotErr != nil && tc.wantErr.Error() != gotErr.Error() {
t.Errorf("got err = %v, wanted %v", gotErr, tc.wantErr)
}
assert.Equal(t, tc.want, got)
assert.Equal(t, tc.wantErr, gotErr)
})
}
}
Expand Down Expand Up @@ -6423,9 +6411,7 @@ func TestHttpPaths(t *testing.T) {
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
got := httppaths(tc.rule)
if diff := cmp.Diff(tc.want, got); diff != "" {
t.Fatalf(diff)
}
assert.Equal(t, tc.want, got)
})
}
}
Expand Down Expand Up @@ -6502,9 +6488,7 @@ func TestDetermineSNI(t *testing.T) {
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
got := determineSNI(tc.routeRequestHeaders, tc.clusterRequestHeaders, tc.service)
if diff := cmp.Diff(tc.want, got); diff != "" {
t.Fatalf(diff)
}
assert.Equal(t, tc.want, got)
})
}
}
Expand Down Expand Up @@ -6540,9 +6524,7 @@ func TestEnforceRoute(t *testing.T) {
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
got := routeEnforceTLS(tc.tlsEnabled, tc.permitInsecure)
if diff := cmp.Diff(tc.want, got); diff != "" {
t.Fatalf(diff)
}
assert.Equal(t, tc.want, got)
})
}
}
Expand Down Expand Up @@ -6642,14 +6624,8 @@ func TestValidateHeaderAlteration(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got, gotErr := headersPolicy(test.in, false)
if !cmp.Equal(test.want, got) {
t.Errorf("set (-want, +got) = %s", cmp.Diff(test.want, got))
}
if (test.wantErr != nil) != (gotErr != nil) {
t.Errorf("err = %v, wanted %v", gotErr, test.wantErr)
} else if test.wantErr != nil && gotErr != nil && test.wantErr.Error() != gotErr.Error() {
t.Errorf("err = %v, wanted %v", gotErr, test.wantErr)
}
assert.Equal(t, test.want, got)
assert.Equal(t, test.wantErr, gotErr)
})
}
}
Expand Down
9 changes: 3 additions & 6 deletions internal/dag/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ 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"
v1 "k8s.io/api/core/v1"
"k8s.io/api/networking/v1beta1"
Expand Down Expand Up @@ -742,9 +743,7 @@ func TestKubernetesCacheInsert(t *testing.T) {
cache.Insert(p)
}
got := cache.Insert(tc.obj)
if tc.want != got {
t.Fatalf("Insert(%v): expected %v, got %v", tc.obj, tc.want, got)
}
assert.Equal(t, tc.want, got)
})
}
}
Expand Down Expand Up @@ -952,9 +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)
if tc.want != got {
t.Fatalf("Remove(%v): expected %v, got %v", tc.obj, tc.want, got)
}
assert.Equal(t, tc.want, got)
})
}
}
6 changes: 2 additions & 4 deletions internal/envoy/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ 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"
matcher "github.com/envoyproxy/go-control-plane/envoy/type/matcher"
"github.com/google/go-cmp/cmp"
"github.com/projectcontour/contour/internal/assert"
"github.com/projectcontour/contour/internal/dag"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -108,9 +108,7 @@ func TestUpstreamTLSContext(t *testing.T) {
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
got := UpstreamTLSContext(tc.validation, tc.externalName, tc.alpnProtocols...)
if diff := cmp.Diff(tc.want, got); diff != "" {
t.Fatal(diff)
}
assert.EqualProto(t, tc.want, got)
})
}
}
22 changes: 6 additions & 16 deletions internal/envoy/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (

v2 "github.com/envoyproxy/go-control-plane/envoy/api/v2"
envoy_api_v2_endpoint "github.com/envoyproxy/go-control-plane/envoy/api/v2/endpoint"
"github.com/google/go-cmp/cmp"
"github.com/projectcontour/contour/internal/assert"
)

func TestLBEndpoint(t *testing.T) {
Expand All @@ -30,9 +30,7 @@ func TestLBEndpoint(t *testing.T) {
},
},
}
if diff := cmp.Diff(want, got); diff != "" {
t.Fatal(diff)
}
assert.EqualProto(t, want, got)
}

func TestEndpoints(t *testing.T) {
Expand All @@ -55,9 +53,7 @@ func TestEndpoints(t *testing.T) {
},
}},
}}
if diff := cmp.Diff(want, got); diff != "" {
t.Fatal(diff)
}
assert.EqualProto(t, want, got)
}

func TestClusterLoadAssignment(t *testing.T) {
Expand All @@ -66,19 +62,15 @@ func TestClusterLoadAssignment(t *testing.T) {
ClusterName: "empty",
}

if diff := cmp.Diff(want, got); diff != "" {
t.Fatal(diff)
}
assert.EqualProto(t, want, got)

got = ClusterLoadAssignment("one addr", SocketAddress("microsoft.com", 81))
want = &v2.ClusterLoadAssignment{
ClusterName: "one addr",
Endpoints: Endpoints(SocketAddress("microsoft.com", 81)),
}

if diff := cmp.Diff(want, got); diff != "" {
t.Fatal(diff)
}
assert.EqualProto(t, want, got)

got = ClusterLoadAssignment("two addrs",
SocketAddress("microsoft.com", 81),
Expand All @@ -92,7 +84,5 @@ func TestClusterLoadAssignment(t *testing.T) {
),
}

if diff := cmp.Diff(want, got); diff != "" {
t.Fatal(diff)
}
assert.EqualProto(t, want, got)
}
Loading

0 comments on commit c58a9a1

Please sign in to comment.