Skip to content

Commit

Permalink
Merge pull request projectcontour#14 from lrouquette/v2cc3c38-adobe-u…
Browse files Browse the repository at this point in the history
…nit-tests

Restoring unit-tests
  • Loading branch information
phylake authored Dec 19, 2019
2 parents 866e189 + 2a1051e commit e56e37a
Show file tree
Hide file tree
Showing 25 changed files with 569 additions and 296 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ test: install
test-race: | test
go test -race -mod=readonly $(MODULE)/...

test-adobe:
CIDR_LIST_PATH= ZZZ_NO_SYNC_XDS=true go test -count=1 -mod=readonly $(MODULE)/...

vet: | test
go vet $(MODULE)/...

Expand Down
56 changes: 56 additions & 0 deletions adobe/adobe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package adobe

import (
v2 "github.com/envoyproxy/go-control-plane/envoy/api/v2"
envoy_api_v2_core "github.com/envoyproxy/go-control-plane/envoy/api/v2/core"
envoy_api_v2_route "github.com/envoyproxy/go-control-plane/envoy/api/v2/route"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
ingressroutev1 "github.com/projectcontour/contour/apis/contour/v1beta1"
"k8s.io/api/extensions/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// ignore properties added/removed by our customization
var ignoreProperties = []cmp.Option{
cmpopts.IgnoreFields(v2.Cluster{}, "CircuitBreakers", "DrainConnectionsOnHostRemoval"),
cmpopts.IgnoreFields(envoy_api_v2_core.HealthCheck_HttpHealthCheck{}, "ExpectedStatuses"),
cmpopts.IgnoreFields(envoy_api_v2_route.RouteAction{}, "RetryPolicy", "Timeout", "HashPolicy"),
cmpopts.IgnoreFields(envoy_api_v2_route.VirtualHost{}, "RetryPolicy"),
}

// list of tests to ignore (assuming names are unique across suites)
var ignoreTests = map[string]struct{}{
"ingressroute root delegates to another ingressroute root": {}, // root to root delegation
"root ingress delegating to another root w/ different hostname": {}, // root to root delegation
"self-edge produces a cycle": {}, // root to root delegation
"multiple tls ingress with secrets should be sorted": {}, // we group the filter chains together
}

func IgnoreFields() []cmp.Option {
return ignoreProperties
}

func ShouldSkipTest(name string) bool {
if _, ignore := ignoreTests[name]; ignore {
return true
}
return false
}

// Object resources
func AdobefyObject(data interface{}) {
switch obj := data.(type) {
case *v1beta1.Ingress:
addClassAnnotation(&obj.ObjectMeta)
case *ingressroutev1.IngressRoute:
addClassAnnotation(&obj.ObjectMeta)
}
}

func addClassAnnotation(om *metav1.ObjectMeta) {
if metav1.HasAnnotation(*om, "kubernetes.io/ingress.class") {
return
}
metav1.SetMetaDataAnnotation(om, "kubernetes.io/ingress.class", "contour")
}
4 changes: 3 additions & 1 deletion internal/contour/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"testing"
"time"

"github.com/projectcontour/contour/adobe"

v2 "github.com/envoyproxy/go-control-plane/envoy/api/v2"
envoy_api_v2_cluster "github.com/envoyproxy/go-control-plane/envoy/api/v2/cluster"
envoy_api_v2_core "github.com/envoyproxy/go-control-plane/envoy/api/v2/core"
Expand Down Expand Up @@ -855,7 +857,7 @@ func TestClusterVisit(t *testing.T) {
t.Run(name, func(t *testing.T) {
root := buildDAG(tc.objs...)
got := visitClusters(root)
if diff := cmp.Diff(tc.want, got); diff != "" {
if diff := cmp.Diff(tc.want, got, adobe.IgnoreFields()...); diff != "" {
t.Fatal(diff)
}
})
Expand Down
21 changes: 13 additions & 8 deletions internal/contour/listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ package contour
import (
"testing"

"github.com/projectcontour/contour/adobe"

v2 "github.com/envoyproxy/go-control-plane/envoy/api/v2"
envoy_api_v2_auth "github.com/envoyproxy/go-control-plane/envoy/api/v2/auth"
envoy_api_v2_listener "github.com/envoyproxy/go-control-plane/envoy/api/v2/listener"
Expand Down Expand Up @@ -270,7 +272,7 @@ func TestListenerVisit(t *testing.T) {
FilterChainMatch: &envoy_api_v2_listener.FilterChainMatch{
ServerNames: []string{"whatever.example.com"},
},
TlsContext: tlscontext(envoy_api_v2_auth.TlsParameters_TLSv1_1, "h2", "http/1.1"),
TlsContext: tlscontext(envoy_api_v2_auth.TlsParameters_TLSv1_2, "h2", "http/1.1"),
Filters: envoy.Filters(envoy.HTTPConnectionManager(ENVOY_HTTPS_LISTENER, envoy.FileAccessLogEnvoy(DEFAULT_HTTP_ACCESS_LOG))),
}},
}),
Expand Down Expand Up @@ -357,13 +359,13 @@ func TestListenerVisit(t *testing.T) {
FilterChainMatch: &envoy_api_v2_listener.FilterChainMatch{
ServerNames: []string{"sortedfirst.example.com"},
},
TlsContext: tlscontext(envoy_api_v2_auth.TlsParameters_TLSv1_1, "h2", "http/1.1"),
TlsContext: tlscontext(envoy_api_v2_auth.TlsParameters_TLSv1_2, "h2", "http/1.1"),
Filters: envoy.Filters(envoy.HTTPConnectionManager(ENVOY_HTTPS_LISTENER, envoy.FileAccessLogEnvoy(DEFAULT_HTTP_ACCESS_LOG))),
}, {
FilterChainMatch: &envoy_api_v2_listener.FilterChainMatch{
ServerNames: []string{"sortedsecond.example.com"},
},
TlsContext: tlscontext(envoy_api_v2_auth.TlsParameters_TLSv1_1, "h2", "http/1.1"),
TlsContext: tlscontext(envoy_api_v2_auth.TlsParameters_TLSv1_2, "h2", "http/1.1"),
Filters: envoy.Filters(envoy.HTTPConnectionManager(ENVOY_HTTPS_LISTENER, envoy.FileAccessLogEnvoy(DEFAULT_HTTP_ACCESS_LOG))),
}},
}),
Expand Down Expand Up @@ -479,7 +481,7 @@ func TestListenerVisit(t *testing.T) {
FilterChainMatch: &envoy_api_v2_listener.FilterChainMatch{
ServerNames: []string{"www.example.com"},
},
TlsContext: tlscontext(envoy_api_v2_auth.TlsParameters_TLSv1_1, "h2", "http/1.1"),
TlsContext: tlscontext(envoy_api_v2_auth.TlsParameters_TLSv1_2, "h2", "http/1.1"),
Filters: envoy.Filters(envoy.HTTPConnectionManager(ENVOY_HTTPS_LISTENER, envoy.FileAccessLogEnvoy(DEFAULT_HTTP_ACCESS_LOG))),
}},
ListenerFilters: envoy.ListenerFilters(
Expand Down Expand Up @@ -560,7 +562,7 @@ func TestListenerVisit(t *testing.T) {
FilterChainMatch: &envoy_api_v2_listener.FilterChainMatch{
ServerNames: []string{"www.example.com"},
},
TlsContext: tlscontext(envoy_api_v2_auth.TlsParameters_TLSv1_1, "h2", "http/1.1"),
TlsContext: tlscontext(envoy_api_v2_auth.TlsParameters_TLSv1_2, "h2", "http/1.1"),
Filters: envoy.Filters(envoy.HTTPConnectionManager(ENVOY_HTTPS_LISTENER, envoy.FileAccessLogEnvoy(DEFAULT_HTTP_ACCESS_LOG))),
}},
ListenerFilters: envoy.ListenerFilters(
Expand Down Expand Up @@ -634,7 +636,7 @@ func TestListenerVisit(t *testing.T) {
FilterChainMatch: &envoy_api_v2_listener.FilterChainMatch{
ServerNames: []string{"whatever.example.com"},
},
TlsContext: tlscontext(envoy_api_v2_auth.TlsParameters_TLSv1_1, "h2", "http/1.1"),
TlsContext: tlscontext(envoy_api_v2_auth.TlsParameters_TLSv1_2, "h2", "http/1.1"),
Filters: envoy.Filters(envoy.HTTPConnectionManager(ENVOY_HTTPS_LISTENER, envoy.FileAccessLogEnvoy(DEFAULT_HTTP_ACCESS_LOG))),
}},
}),
Expand Down Expand Up @@ -706,7 +708,7 @@ func TestListenerVisit(t *testing.T) {
FilterChainMatch: &envoy_api_v2_listener.FilterChainMatch{
ServerNames: []string{"whatever.example.com"},
},
TlsContext: tlscontext(envoy_api_v2_auth.TlsParameters_TLSv1_1, "h2", "http/1.1"),
TlsContext: tlscontext(envoy_api_v2_auth.TlsParameters_TLSv1_2, "h2", "http/1.1"),
Filters: envoy.Filters(envoy.HTTPConnectionManager(ENVOY_HTTPS_LISTENER, envoy.FileAccessLogEnvoy(DEFAULT_HTTP_ACCESS_LOG))),
}},
}),
Expand Down Expand Up @@ -775,7 +777,7 @@ func TestListenerVisit(t *testing.T) {
FilterChainMatch: &envoy_api_v2_listener.FilterChainMatch{
ServerNames: []string{"whatever.example.com"},
},
TlsContext: tlscontext(envoy_api_v2_auth.TlsParameters_TLSv1_1, "h2", "http/1.1"),
TlsContext: tlscontext(envoy_api_v2_auth.TlsParameters_TLSv1_2, "h2", "http/1.1"),
Filters: envoy.Filters(envoy.HTTPConnectionManager(ENVOY_HTTPS_LISTENER, envoy.FileAccessLogEnvoy("/tmp/https_access.log"))),
}},
}),
Expand Down Expand Up @@ -993,6 +995,9 @@ func TestListenerVisit(t *testing.T) {
}

for name, tc := range tests {
if adobe.ShouldSkipTest(name) {
t.SkipNow()
}
t.Run(name, func(t *testing.T) {
root := buildDAG(tc.objs...)
got := visitListeners(root, &tc.ListenerVisitorConfig)
Expand Down
3 changes: 3 additions & 0 deletions internal/contour/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ package contour
import (
"testing"

"github.com/projectcontour/contour/adobe"

"github.com/google/go-cmp/cmp"
ingressroutev1 "github.com/projectcontour/contour/apis/contour/v1beta1"
projcontour "github.com/projectcontour/contour/apis/projectcontour/v1alpha1"
Expand Down Expand Up @@ -537,6 +539,7 @@ func TestIngressRouteMetrics(t *testing.T) {
},
}
for _, o := range tc.objs {
adobe.AdobefyObject(o)
builder.Source.Insert(o)
}
dag := builder.Build()
Expand Down
8 changes: 4 additions & 4 deletions internal/contour/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,15 @@ func (v *routeVisitor) visit(vertex dag.Vertex) {
vh.Visit(func(v dag.Vertex) {
switch r := v.(type) {
case *dag.PrefixRoute:
rr := envoy.Route(envoy.RoutePrefix(r.Prefix), envoy.RouteRoute(&r.Route), &r.Route)
rr := envoy.RouteAdobe(envoy.RoutePrefix(r.Prefix), envoy.RouteRoute(&r.Route), &r.Route)

if r.HTTPSUpgrade {
rr.Action = envoy.UpgradeHTTPS()
rr.RequestHeadersToAdd = nil
}
routes = append(routes, rr)
case *dag.RegexRoute:
rr := envoy.Route(envoy.RouteRegex(r.Regex), envoy.RouteRoute(&r.Route), &r.Route)
rr := envoy.RouteAdobe(envoy.RouteRegex(r.Regex), envoy.RouteRoute(&r.Route), &r.Route)

if r.HTTPSUpgrade {
rr.Action = envoy.UpgradeHTTPS()
Expand All @@ -147,12 +147,12 @@ func (v *routeVisitor) visit(vertex dag.Vertex) {
case *dag.PrefixRoute:
routes = append(
routes,
envoy.Route(envoy.RoutePrefix(r.Prefix), envoy.RouteRoute(&r.Route), &r.Route),
envoy.RouteAdobe(envoy.RoutePrefix(r.Prefix), envoy.RouteRoute(&r.Route), &r.Route),
)
case *dag.RegexRoute:
routes = append(
routes,
envoy.Route(envoy.RouteRegex(r.Regex), envoy.RouteRoute(&r.Route), &r.Route),
envoy.RouteAdobe(envoy.RouteRegex(r.Regex), envoy.RouteRoute(&r.Route), &r.Route),
)
}
})
Expand Down
4 changes: 3 additions & 1 deletion internal/contour/route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"testing"
"time"

"github.com/projectcontour/contour/adobe"

v2 "github.com/envoyproxy/go-control-plane/envoy/api/v2"
envoy_api_v2_route "github.com/envoyproxy/go-control-plane/envoy/api/v2/route"
"github.com/golang/protobuf/proto"
Expand Down Expand Up @@ -1324,7 +1326,7 @@ func TestRouteVisit(t *testing.T) {
t.Run(name, func(t *testing.T) {
root := buildDAG(tc.objs...)
got := visitRoutes(root)
if diff := cmp.Diff(tc.want, got); diff != "" {
if diff := cmp.Diff(tc.want, got, adobe.IgnoreFields()...); diff != "" {
t.Fatal(diff)
}
})
Expand Down
3 changes: 3 additions & 0 deletions internal/contour/secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"reflect"
"testing"

"github.com/projectcontour/contour/adobe"

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"
"github.com/golang/protobuf/proto"
Expand Down Expand Up @@ -473,6 +475,7 @@ func TestSecretVisit(t *testing.T) {
func buildDAG(objs ...interface{}) *dag.DAG {
var builder dag.Builder
for _, o := range objs {
adobe.AdobefyObject(o)
builder.Source.Insert(o)
}
return builder.Build()
Expand Down
4 changes: 3 additions & 1 deletion internal/contour/visitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"testing"
"time"

"github.com/projectcontour/contour/adobe"

envoy_api_v2 "github.com/envoyproxy/go-control-plane/envoy/api/v2"
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"
Expand Down Expand Up @@ -80,7 +82,7 @@ func TestVisitClusters(t *testing.T) {
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
got := visitClusters(tc.root)
if diff := cmp.Diff(tc.want, got); diff != "" {
if diff := cmp.Diff(tc.want, got, adobe.IgnoreFields()...); diff != "" {
t.Fatal(diff)
}
})
Expand Down
14 changes: 11 additions & 3 deletions internal/dag/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"testing"
"time"

"github.com/projectcontour/contour/adobe"

"github.com/envoyproxy/go-control-plane/envoy/api/v2/auth"
"github.com/google/go-cmp/cmp"
ingressroutev1 "github.com/projectcontour/contour/apis/contour/v1beta1"
Expand Down Expand Up @@ -2427,7 +2429,7 @@ func TestDAGInsert(t *testing.T) {
),
},
Secret: secret(sec1),
MinProtoVersion: envoy_api_v2_auth.TlsParameters_TLSv1_1,
MinProtoVersion: envoy_api_v2_auth.TlsParameters_TLSv1_2,
},
),
},
Expand Down Expand Up @@ -3554,7 +3556,7 @@ func TestDAGInsert(t *testing.T) {
VirtualHost: VirtualHost{
Name: "example.com",
},
MinProtoVersion: envoy_api_v2_auth.TlsParameters_TLSv1_1,
MinProtoVersion: envoy_api_v2_auth.TlsParameters_TLSv1_2,
Secret: secret(sec1),
TCPProxy: &TCPProxy{
Clusters: clusters(service(s9)),
Expand Down Expand Up @@ -3827,7 +3829,11 @@ func TestDAGInsert(t *testing.T) {
FieldLogger: testLogger(t),
},
}
if adobe.ShouldSkipTest(name) {
t.SkipNow()
}
for _, o := range tc.objs {
adobe.AdobefyObject(o)
if !builder.Source.Insert(o) {
t.Logf("insert %v: failed", o)
}
Expand All @@ -3847,6 +3853,7 @@ func TestDAGInsert(t *testing.T) {
opts := []cmp.Option{
cmp.AllowUnexported(VirtualHost{}),
}
opts = append(opts, adobe.IgnoreFields()...)
if diff := cmp.Diff(want, got, opts...); diff != "" {
t.Fatal(diff)
}
Expand Down Expand Up @@ -4147,6 +4154,7 @@ func TestDAGRootNamespaces(t *testing.T) {
}

for _, o := range tc.objs {
adobe.AdobefyObject(o)
builder.Source.Insert(o)
}
dag := builder.Build()
Expand Down Expand Up @@ -4505,7 +4513,7 @@ func securevirtualhost(name string, sec *v1.Secret, v ...Vertex) *SecureVirtualH
Name: name,
routes: routes(v...),
},
MinProtoVersion: envoy_api_v2_auth.TlsParameters_TLSv1_1,
MinProtoVersion: envoy_api_v2_auth.TlsParameters_TLSv1_2,
Secret: secret(sec),
}
}
Expand Down
9 changes: 9 additions & 0 deletions internal/dag/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ package dag
import (
"testing"

"github.com/projectcontour/contour/adobe"

ingressroutev1 "github.com/projectcontour/contour/apis/contour/v1beta1"
projcontour "github.com/projectcontour/contour/apis/projectcontour/v1alpha1"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -817,9 +819,14 @@ func TestKubernetesCacheInsert(t *testing.T) {
cache := KubernetesCache{
FieldLogger: testLogger(t),
}
if adobe.ShouldSkipTest(name) {
t.SkipNow()
}
for _, p := range tc.pre {
adobe.AdobefyObject(p)
cache.Insert(p)
}
adobe.AdobefyObject(tc.obj)
got := cache.Insert(tc.obj)
if tc.want != got {
t.Fatalf("Insert(%v): expected %v, got %v", tc.obj, tc.want, got)
Expand All @@ -834,6 +841,7 @@ func TestKubernetesCacheRemove(t *testing.T) {
FieldLogger: testLogger(t),
}
for _, o := range objs {
adobe.AdobefyObject(o)
cache.Insert(o)
}
return &cache
Expand Down Expand Up @@ -993,6 +1001,7 @@ func TestKubernetesCacheRemove(t *testing.T) {

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
adobe.AdobefyObject(tc.obj)
got := tc.cache.Remove(tc.obj)
if tc.want != got {
t.Fatalf("Remove(%v): expected %v, got %v", tc.obj, tc.want, got)
Expand Down
Loading

0 comments on commit e56e37a

Please sign in to comment.