Skip to content

Commit

Permalink
tighten up tests
Browse files Browse the repository at this point in the history
Signed-off-by: Sotiris Nanopoulos <sotiris.nanopoulos@reddit.com>
  • Loading branch information
davinci26 committed Oct 23, 2023
1 parent 7eb02e6 commit 1f7925e
Show file tree
Hide file tree
Showing 2 changed files with 279 additions and 0 deletions.
167 changes: 167 additions & 0 deletions internal/dag/builder_sorted_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
// Copyright Project Contour Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package dag

import (
"testing"

contour_api_v1 "github.com/projectcontour/contour/apis/projectcontour/v1"
"github.com/projectcontour/contour/internal/fixture"
"github.com/stretchr/testify/assert"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestOriginalRouting(t *testing.T) {
proxyObject := &contour_api_v1.HTTPProxy{
ObjectMeta: metav1.ObjectMeta{
Name: "bar-com",
Namespace: "default",
},
Spec: contour_api_v1.HTTPProxySpec{
VirtualHost: &contour_api_v1.VirtualHost{
Fqdn: "unique.com",
},
Routes: []contour_api_v1.Route{
{
Conditions: []contour_api_v1.MatchCondition{{
Prefix: "/",
}},
Services: []contour_api_v1.Service{{
Name: "kuard",
Port: 8080,
}},
},
{
Conditions: []contour_api_v1.MatchCondition{{
Regex: "/fizzbuzz.*",
}},
Services: []contour_api_v1.Service{{
Name: "kuard",
Port: 8080,
}},
},
{
Conditions: []contour_api_v1.MatchCondition{{
Exact: "/foobar",
}},
Services: []contour_api_v1.Service{{
Name: "kuarder",
Port: 8080,
}},
},
},
},
}

s1 := &v1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "kuard",
Namespace: "default",
},
Spec: v1.ServiceSpec{
Ports: []v1.ServicePort{makeServicePort("http", "TCP", 8080, 8080)},
},
}

// s2 is like s1 but with a different name
s2 := &v1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "kuarder",
Namespace: "default",
},
Spec: v1.ServiceSpec{
Ports: []v1.ServicePort{makeServicePort("http", "TCP", 8080, 8080)},
},
}

tests := []struct {
name string
objs []any
want []*Listener
ingressReqHp *HeadersPolicy
ingressRespHp *HeadersPolicy
httpProxyReqHp *HeadersPolicy
httpProxyRespHp *HeadersPolicy
wantErr error
}{
{
name: "VHost should be marked as needs to be sorted",
objs: []any{
proxyObject, s1, s2,
},
want: listeners(
&Listener{
Name: HTTP_LISTENER_NAME,
Port: 8080,
VirtualHosts: virtualhosts(
virtualhost("unique.com", true,
&Route{
PathMatchCondition: prefixString("/"),
Clusters: clusters(service(s1)),
},
&Route{
PathMatchCondition: regex("/fizzbuzz.*"),
Clusters: clusters(service(s1)),
},
&Route{
PathMatchCondition: exact("/foobar"),
Clusters: clusters(service(s2)),
},
),
),
},
),
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
builder := Builder{
Source: KubernetesCache{
FieldLogger: fixture.NewTestLogger(t),
},
Processors: []Processor{
&ListenerProcessor{},
&IngressProcessor{
FieldLogger: fixture.NewTestLogger(t),
RequestHeadersPolicy: tc.ingressReqHp,
ResponseHeadersPolicy: tc.ingressRespHp,
},
&HTTPProxyProcessor{
ShouldSortRoutes: true,
RequestHeadersPolicy: tc.httpProxyReqHp,
ResponseHeadersPolicy: tc.httpProxyRespHp,
},
},
}

for _, o := range tc.objs {
builder.Source.Insert(o)
}
dag := builder.Build()

got := make(map[int]*Listener)
for _, l := range dag.Listeners {
got[l.Port] = l
}

want := make(map[int]*Listener)
for _, l := range tc.want {
want[l.Port] = l
}
assert.Equal(t, want, got)
})
}
}
112 changes: 112 additions & 0 deletions internal/xdscache/v3/route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3459,6 +3459,118 @@ func TestRouteVisit(t *testing.T) {
}
}

func TestRouteVisit_WithSortingEnabled(t *testing.T) {
tests := map[string]struct {
objs []any
want map[string]*envoy_route_v3.RouteConfiguration
}{
"Routes should be sorted with sorter enabled": {
objs: []any{
&contour_api_v1.HTTPProxy{
ObjectMeta: metav1.ObjectMeta{
Name: "simple",
Namespace: "default",
},
Spec: contour_api_v1.HTTPProxySpec{
VirtualHost: &contour_api_v1.VirtualHost{
Fqdn: "www.unique.com",
},
Routes: []contour_api_v1.Route{
{
Conditions: []contour_api_v1.MatchCondition{{
Prefix: "/",
}},
Services: []contour_api_v1.Service{{
Name: "backend",
Port: 80,
}},
},
{
Conditions: []contour_api_v1.MatchCondition{{
Regex: "/fizzbuzz.*",
}},
Services: []contour_api_v1.Service{{
Name: "backend",
Port: 80,
}},
},
{
Conditions: []contour_api_v1.MatchCondition{{
Exact: "/foobar",
}},
Services: []contour_api_v1.Service{{
Name: "backend",
Port: 80,
}},
},
},
},
},
&v1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "backend",
Namespace: "default",
},
Spec: v1.ServiceSpec{
Ports: []v1.ServicePort{{
Protocol: "TCP",
Port: 80,
TargetPort: intstr.FromInt(8080),
}},
},
},
},
want: routeConfigurations(
envoy_v3.RouteConfiguration("ingress_http",
envoy_v3.VirtualHost("www.unique.com",
&envoy_route_v3.Route{
Match: routeExact("/foobar"),
Action: routecluster("default/backend/80/da39a3ee5e"),
},
&envoy_route_v3.Route{
Match: routeRegex("/fizzbuzz.*"),
Action: routecluster("default/backend/80/da39a3ee5e"),
},
&envoy_route_v3.Route{
Match: routePrefix("/"),
Action: routecluster("default/backend/80/da39a3ee5e"),
},
),
),
),
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
var rc RouteCache
builder := dag.Builder{
Source: dag.KubernetesCache{
FieldLogger: fixture.NewTestLogger(t),
},
Processors: []dag.Processor{
&dag.ListenerProcessor{
HTTPAddress: "0.0.0.0",
HTTPPort: 8080,
HTTPSAddress: "0.0.0.0",
HTTPSPort: 8443,
},
&dag.IngressProcessor{
FieldLogger: fixture.NewTestLogger(t),
},
&dag.HTTPProxyProcessor{
ShouldSortRoutes: true,
},
},
}
for _, o := range tc.objs {
builder.Source.Insert(o)
}
rc.OnChange(builder.Build())
protobuf.ExpectEqual(t, tc.want, rc.values)
})
}
}

func TestRouteVisit_GlobalExternalAuthorization(t *testing.T) {
tests := map[string]struct {
objs []any
Expand Down

0 comments on commit 1f7925e

Please sign in to comment.