Skip to content

Commit

Permalink
tests: consolidate envoy Cluster defaults
Browse files Browse the repository at this point in the history
Many tests need to specify default fields for envoy `Cluster` structs
so that struct equality tests can succeed. Specifying these defaults
in many places is fragile and tends to make it harder to see the
field values that actually matter to the tests.  Consolidating the
defaults into a handful of helpers reduces the overally clutter.
We still need a couple of duplicates of the helper so that we can
avoid package dependency cycles.

This updates projectcontour#1375.

Signed-off-by: James Peach <jpeach@vmware.com>
  • Loading branch information
jpeach committed Oct 23, 2019
1 parent 947d6e9 commit f363090
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 209 deletions.
102 changes: 23 additions & 79 deletions internal/contour/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,23 +52,17 @@ func TestClusterCacheContents(t *testing.T) {
EdsConfig: envoy.ConfigSource("contour"),
ServiceName: "default/kuard",
},
ConnectTimeout: protobuf.Duration(250 * time.Millisecond),
LbPolicy: v2.Cluster_ROUND_ROBIN,
CommonLbConfig: envoy.ClusterCommonLBConfig(),
}),
want: []proto.Message{
&v2.Cluster{
cluster(&v2.Cluster{
Name: "default/kuard/443/da39a3ee5e",
AltStatName: "default_kuard_443",
ClusterDiscoveryType: envoy.ClusterDiscoveryType(v2.Cluster_EDS),
EdsClusterConfig: &v2.Cluster_EdsClusterConfig{
EdsConfig: envoy.ConfigSource("contour"),
ServiceName: "default/kuard",
},
ConnectTimeout: protobuf.Duration(250 * time.Millisecond),
LbPolicy: v2.Cluster_ROUND_ROBIN,
CommonLbConfig: envoy.ClusterCommonLBConfig(),
},
}),
},
},
}
Expand Down Expand Up @@ -99,24 +93,18 @@ func TestClusterCacheQuery(t *testing.T) {
EdsConfig: envoy.ConfigSource("contour"),
ServiceName: "default/kuard",
},
ConnectTimeout: protobuf.Duration(250 * time.Millisecond),
LbPolicy: v2.Cluster_ROUND_ROBIN,
CommonLbConfig: envoy.ClusterCommonLBConfig(),
}),
query: []string{"default/kuard/443/da39a3ee5e"},
want: []proto.Message{
&v2.Cluster{
cluster(&v2.Cluster{
Name: "default/kuard/443/da39a3ee5e",
AltStatName: "default_kuard_443",
ClusterDiscoveryType: envoy.ClusterDiscoveryType(v2.Cluster_EDS),
EdsClusterConfig: &v2.Cluster_EdsClusterConfig{
EdsConfig: envoy.ConfigSource("contour"),
ServiceName: "default/kuard",
},
ConnectTimeout: protobuf.Duration(250 * time.Millisecond),
LbPolicy: v2.Cluster_ROUND_ROBIN,
CommonLbConfig: envoy.ClusterCommonLBConfig(),
},
}),
},
},
"partial match": {
Expand All @@ -129,24 +117,18 @@ func TestClusterCacheQuery(t *testing.T) {
EdsConfig: envoy.ConfigSource("contour"),
ServiceName: "default/kuard",
},
ConnectTimeout: protobuf.Duration(250 * time.Millisecond),
LbPolicy: v2.Cluster_ROUND_ROBIN,
CommonLbConfig: envoy.ClusterCommonLBConfig(),
}),
query: []string{"default/kuard/443/da39a3ee5e", "foo/bar/baz"},
want: []proto.Message{
&v2.Cluster{
cluster(&v2.Cluster{
Name: "default/kuard/443/da39a3ee5e",
AltStatName: "default_kuard_443",
ClusterDiscoveryType: envoy.ClusterDiscoveryType(v2.Cluster_EDS),
EdsClusterConfig: &v2.Cluster_EdsClusterConfig{
EdsConfig: envoy.ConfigSource("contour"),
ServiceName: "default/kuard",
},
ConnectTimeout: protobuf.Duration(250 * time.Millisecond),
LbPolicy: v2.Cluster_ROUND_ROBIN,
CommonLbConfig: envoy.ClusterCommonLBConfig(),
},
}),
},
},
"no match": {
Expand All @@ -159,9 +141,6 @@ func TestClusterCacheQuery(t *testing.T) {
EdsConfig: envoy.ConfigSource("contour"),
ServiceName: "default/kuard",
},
ConnectTimeout: protobuf.Duration(250 * time.Millisecond),
LbPolicy: v2.Cluster_ROUND_ROBIN,
CommonLbConfig: envoy.ClusterCommonLBConfig(),
}),
query: []string{"foo/bar/baz"},
want: nil,
Expand Down Expand Up @@ -215,9 +194,6 @@ func TestClusterVisit(t *testing.T) {
EdsConfig: envoy.ConfigSource("contour"),
ServiceName: "default/kuard",
},
ConnectTimeout: protobuf.Duration(250 * time.Millisecond),
LbPolicy: v2.Cluster_ROUND_ROBIN,
CommonLbConfig: envoy.ClusterCommonLBConfig(),
}),
},
"single named service": {
Expand Down Expand Up @@ -252,9 +228,6 @@ func TestClusterVisit(t *testing.T) {
EdsConfig: envoy.ConfigSource("contour"),
ServiceName: "default/kuard/https",
},
ConnectTimeout: protobuf.Duration(250 * time.Millisecond),
LbPolicy: v2.Cluster_ROUND_ROBIN,
CommonLbConfig: envoy.ClusterCommonLBConfig(),
}),
},
"h2c upstream": {
Expand Down Expand Up @@ -293,10 +266,7 @@ func TestClusterVisit(t *testing.T) {
EdsConfig: envoy.ConfigSource("contour"),
ServiceName: "default/kuard/http",
},
ConnectTimeout: protobuf.Duration(250 * time.Millisecond),
LbPolicy: v2.Cluster_ROUND_ROBIN,
Http2ProtocolOptions: &envoy_api_v2_core.Http2ProtocolOptions{},
CommonLbConfig: envoy.ClusterCommonLBConfig(),
},
),
},
Expand Down Expand Up @@ -329,9 +299,6 @@ func TestClusterVisit(t *testing.T) {
EdsConfig: envoy.ConfigSource("contour"),
ServiceName: "beurocratic-company-test-domain-1/tiny-cog-department-test-instance/svc-0",
},
ConnectTimeout: protobuf.Duration(250 * time.Millisecond),
LbPolicy: v2.Cluster_ROUND_ROBIN,
CommonLbConfig: envoy.ClusterCommonLBConfig(),
}),
},
"two service ports": {
Expand Down Expand Up @@ -378,9 +345,6 @@ func TestClusterVisit(t *testing.T) {
EdsConfig: envoy.ConfigSource("contour"),
ServiceName: "default/backend/http",
},
ConnectTimeout: protobuf.Duration(250 * time.Millisecond),
LbPolicy: v2.Cluster_ROUND_ROBIN,
CommonLbConfig: envoy.ClusterCommonLBConfig(),
},
&v2.Cluster{
Name: "default/backend/8080/da39a3ee5e",
Expand All @@ -390,9 +354,6 @@ func TestClusterVisit(t *testing.T) {
EdsConfig: envoy.ConfigSource("contour"),
ServiceName: "default/backend/alt",
},
ConnectTimeout: protobuf.Duration(250 * time.Millisecond),
LbPolicy: v2.Cluster_ROUND_ROBIN,
CommonLbConfig: envoy.ClusterCommonLBConfig(),
},
),
},
Expand Down Expand Up @@ -435,8 +396,6 @@ func TestClusterVisit(t *testing.T) {
EdsConfig: envoy.ConfigSource("contour"),
ServiceName: "default/backend/http",
},
ConnectTimeout: protobuf.Duration(250 * time.Millisecond),
LbPolicy: v2.Cluster_ROUND_ROBIN,
HealthChecks: []*envoy_api_v2_core.HealthCheck{{
Timeout: &duration.Duration{Seconds: 2},
Interval: &duration.Duration{Seconds: 10},
Expand All @@ -449,7 +408,6 @@ func TestClusterVisit(t *testing.T) {
},
},
}},
CommonLbConfig: envoy.ClusterCommonLBConfig(),
DrainConnectionsOnHostRemoval: true,
},
),
Expand Down Expand Up @@ -498,8 +456,6 @@ func TestClusterVisit(t *testing.T) {
EdsConfig: envoy.ConfigSource("contour"),
ServiceName: "default/backend/http",
},
ConnectTimeout: protobuf.Duration(250 * time.Millisecond),
LbPolicy: v2.Cluster_ROUND_ROBIN,
HealthChecks: []*envoy_api_v2_core.HealthCheck{{
Timeout: &duration.Duration{Seconds: 99},
Interval: &duration.Duration{Seconds: 98},
Expand All @@ -512,7 +468,6 @@ func TestClusterVisit(t *testing.T) {
},
},
}},
CommonLbConfig: envoy.ClusterCommonLBConfig(),
DrainConnectionsOnHostRemoval: true,
},
),
Expand Down Expand Up @@ -554,9 +509,6 @@ func TestClusterVisit(t *testing.T) {
EdsConfig: envoy.ConfigSource("contour"),
ServiceName: "default/backend/http",
},
ConnectTimeout: protobuf.Duration(250 * time.Millisecond),
LbPolicy: v2.Cluster_ROUND_ROBIN,
CommonLbConfig: envoy.ClusterCommonLBConfig(),
},
),
},
Expand Down Expand Up @@ -597,9 +549,7 @@ func TestClusterVisit(t *testing.T) {
EdsConfig: envoy.ConfigSource("contour"),
ServiceName: "default/backend/http",
},
ConnectTimeout: protobuf.Duration(250 * time.Millisecond),
LbPolicy: v2.Cluster_LEAST_REQUEST,
CommonLbConfig: envoy.ClusterCommonLBConfig(),
LbPolicy: v2.Cluster_LEAST_REQUEST,
},
),
},
Expand Down Expand Up @@ -640,9 +590,7 @@ func TestClusterVisit(t *testing.T) {
EdsConfig: envoy.ConfigSource("contour"),
ServiceName: "default/backend/http",
},
ConnectTimeout: protobuf.Duration(250 * time.Millisecond),
LbPolicy: v2.Cluster_RANDOM,
CommonLbConfig: envoy.ClusterCommonLBConfig(),
LbPolicy: v2.Cluster_RANDOM,
},
),
},
Expand Down Expand Up @@ -690,9 +638,7 @@ func TestClusterVisit(t *testing.T) {
EdsConfig: envoy.ConfigSource("contour"),
ServiceName: "default/backend/http",
},
ConnectTimeout: protobuf.Duration(250 * time.Millisecond),
LbPolicy: v2.Cluster_RANDOM,
CommonLbConfig: envoy.ClusterCommonLBConfig(),
LbPolicy: v2.Cluster_RANDOM,
},
&v2.Cluster{
Name: "default/backend/80/8bf87fefba",
Expand All @@ -702,9 +648,7 @@ func TestClusterVisit(t *testing.T) {
EdsConfig: envoy.ConfigSource("contour"),
ServiceName: "default/backend/http",
},
ConnectTimeout: protobuf.Duration(250 * time.Millisecond),
LbPolicy: v2.Cluster_LEAST_REQUEST,
CommonLbConfig: envoy.ClusterCommonLBConfig(),
LbPolicy: v2.Cluster_LEAST_REQUEST,
},
),
},
Expand Down Expand Up @@ -745,9 +689,6 @@ func TestClusterVisit(t *testing.T) {
EdsConfig: envoy.ConfigSource("contour"),
ServiceName: "default/backend/http",
},
ConnectTimeout: protobuf.Duration(250 * time.Millisecond),
LbPolicy: v2.Cluster_ROUND_ROBIN,
CommonLbConfig: envoy.ClusterCommonLBConfig(),
},
),
},
Expand Down Expand Up @@ -790,8 +731,6 @@ func TestClusterVisit(t *testing.T) {
EdsConfig: envoy.ConfigSource("contour"),
ServiceName: "default/kuard/http",
},
ConnectTimeout: protobuf.Duration(250 * time.Millisecond),
LbPolicy: v2.Cluster_ROUND_ROBIN,
CircuitBreakers: &envoy_api_v2_cluster.CircuitBreakers{
Thresholds: []*envoy_api_v2_cluster.CircuitBreakers_Thresholds{{
MaxConnections: protobuf.UInt32(9000),
Expand All @@ -800,7 +739,6 @@ func TestClusterVisit(t *testing.T) {
MaxRetries: protobuf.UInt32(7),
}},
},
CommonLbConfig: envoy.ClusterCommonLBConfig(),
},
),
},
Expand Down Expand Up @@ -840,9 +778,6 @@ func TestClusterVisit(t *testing.T) {
EdsConfig: envoy.ConfigSource("contour"),
ServiceName: "default/kuard/https",
},
ConnectTimeout: protobuf.Duration(250 * time.Millisecond),
LbPolicy: v2.Cluster_ROUND_ROBIN,
CommonLbConfig: envoy.ClusterCommonLBConfig(),
}),
},

Expand Down Expand Up @@ -882,9 +817,6 @@ func TestClusterVisit(t *testing.T) {
EdsConfig: envoy.ConfigSource("contour"),
ServiceName: "default/kuard/https",
},
ConnectTimeout: protobuf.Duration(250 * time.Millisecond),
LbPolicy: v2.Cluster_ROUND_ROBIN,
CommonLbConfig: envoy.ClusterCommonLBConfig(),
}),
},
}
Expand Down Expand Up @@ -915,10 +847,22 @@ func serviceWithAnnotations(ns, name string, annotations map[string]string, port
}
}

func cluster(c *v2.Cluster) *v2.Cluster {
// NOTE: Keep this in sync with envoy.defaultCluster().
defaults := &v2.Cluster{
ConnectTimeout: protobuf.Duration(250 * time.Millisecond),
CommonLbConfig: envoy.ClusterCommonLBConfig(),
LbPolicy: v2.Cluster_ROUND_ROBIN,
}

proto.Merge(defaults, c)
return defaults
}

func clustermap(clusters ...*v2.Cluster) map[string]*v2.Cluster {
m := make(map[string]*v2.Cluster)
for _, c := range clusters {
m[c.Name] = c
m[c.Name] = cluster(c)
}
return m
}
5 changes: 0 additions & 5 deletions internal/contour/visitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ package contour

import (
"testing"
"time"

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"
Expand All @@ -24,7 +23,6 @@ import (
"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 @@ -69,9 +67,6 @@ func TestVisitClusters(t *testing.T) {
EdsConfig: envoy.ConfigSource("contour"),
ServiceName: "default/example",
},
ConnectTimeout: protobuf.Duration(250 * time.Millisecond),
LbPolicy: envoy_api_v2.Cluster_ROUND_ROBIN,
CommonLbConfig: envoy.ClusterCommonLBConfig(),
},
),
},
Expand Down
Loading

0 comments on commit f363090

Please sign in to comment.