github.com/sl1pm4t/consul@v1.4.5-0.20190325224627-74c31c540f9c/agent/xds/clusters_test.go (about)

     1  package xds
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	envoy "github.com/envoyproxy/go-control-plane/envoy/api/v2"
     8  	envoyauth "github.com/envoyproxy/go-control-plane/envoy/api/v2/auth"
     9  	"github.com/envoyproxy/go-control-plane/envoy/api/v2/cluster"
    10  	envoycore "github.com/envoyproxy/go-control-plane/envoy/api/v2/core"
    11  	"github.com/stretchr/testify/require"
    12  
    13  	"github.com/hashicorp/consul/agent/proxycfg"
    14  	"github.com/hashicorp/consul/agent/structs"
    15  )
    16  
    17  func Test_makeUpstreamCluster(t *testing.T) {
    18  	tests := []struct {
    19  		name     string
    20  		snap     proxycfg.ConfigSnapshot
    21  		upstream structs.Upstream
    22  		want     *envoy.Cluster
    23  	}{
    24  		{
    25  			name:     "timeout override",
    26  			snap:     proxycfg.ConfigSnapshot{},
    27  			upstream: structs.TestUpstreams(t)[0],
    28  			want: &envoy.Cluster{
    29  				Name: "service:db",
    30  				Type: envoy.Cluster_EDS,
    31  				EdsClusterConfig: &envoy.Cluster_EdsClusterConfig{
    32  					EdsConfig: &envoycore.ConfigSource{
    33  						ConfigSourceSpecifier: &envoycore.ConfigSource_Ads{
    34  							Ads: &envoycore.AggregatedConfigSource{},
    35  						},
    36  					},
    37  				},
    38  				ConnectTimeout:   1 * time.Second, // TestUpstreams overrides to 1000ms
    39  				OutlierDetection: &cluster.OutlierDetection{},
    40  				TlsContext: &envoyauth.UpstreamTlsContext{
    41  					CommonTlsContext: makeCommonTLSContext(&proxycfg.ConfigSnapshot{}),
    42  				},
    43  			},
    44  		},
    45  		{
    46  			name:     "timeout default",
    47  			snap:     proxycfg.ConfigSnapshot{},
    48  			upstream: structs.TestUpstreams(t)[1],
    49  			want: &envoy.Cluster{
    50  				Name: "prepared_query:geo-cache",
    51  				Type: envoy.Cluster_EDS,
    52  				EdsClusterConfig: &envoy.Cluster_EdsClusterConfig{
    53  					EdsConfig: &envoycore.ConfigSource{
    54  						ConfigSourceSpecifier: &envoycore.ConfigSource_Ads{
    55  							Ads: &envoycore.AggregatedConfigSource{},
    56  						},
    57  					},
    58  				},
    59  				ConnectTimeout:   5 * time.Second, // Default
    60  				OutlierDetection: &cluster.OutlierDetection{},
    61  				TlsContext: &envoyauth.UpstreamTlsContext{
    62  					CommonTlsContext: makeCommonTLSContext(&proxycfg.ConfigSnapshot{}),
    63  				},
    64  			},
    65  		},
    66  	}
    67  	for _, tt := range tests {
    68  		t.Run(tt.name, func(t *testing.T) {
    69  			require := require.New(t)
    70  			got, err := makeUpstreamCluster(tt.upstream, &tt.snap)
    71  			require.NoError(err)
    72  
    73  			require.Equal(tt.want, got)
    74  		})
    75  	}
    76  }