diff --git a/pkg/envoy/cds/cluster_test.go b/pkg/envoy/cds/cluster_test.go index 1f39768880..2b8d12f9ec 100644 --- a/pkg/envoy/cds/cluster_test.go +++ b/pkg/envoy/cds/cluster_test.go @@ -1,23 +1,30 @@ package cds import ( + "net" "testing" xds_cluster "github.com/envoyproxy/go-control-plane/envoy/config/cluster/v3" - + xds_core "github.com/envoyproxy/go-control-plane/envoy/config/core/v3" + xds_endpoint "github.com/envoyproxy/go-control-plane/envoy/config/endpoint/v3" "github.com/golang/mock/gomock" - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" + "github.com/golang/protobuf/ptypes" + "github.com/golang/protobuf/ptypes/wrappers" tassert "github.com/stretchr/testify/assert" + "github.com/openservicemesh/osm/pkg/catalog" "github.com/openservicemesh/osm/pkg/configurator" + "github.com/openservicemesh/osm/pkg/constants" + "github.com/openservicemesh/osm/pkg/endpoint" + "github.com/openservicemesh/osm/pkg/envoy" + "github.com/openservicemesh/osm/pkg/service" "github.com/openservicemesh/osm/pkg/tests" ) func TestGetUpstreamServiceCluster(t *testing.T) { assert := tassert.New(t) - mockCtrl := gomock.NewController(GinkgoT()) + mockCtrl := gomock.NewController(t) mockConfigurator := configurator.NewMockConfigurator(mockCtrl) downstreamSvc := tests.BookbuyerService upstreamSvc := tests.BookstoreV1Service @@ -56,3 +63,88 @@ func TestGetUpstreamServiceCluster(t *testing.T) { }) } } + +func TestGetLocalServiceCluster(t *testing.T) { + assert := tassert.New(t) + + clusterName := "bookbuyer-local" + proxyService := service.MeshService{ + Name: "bookbuyer", + Namespace: "bookbuyer-ns", + } + + mockCtrl := gomock.NewController(t) + mockCatalog := catalog.NewMockMeshCataloger(mockCtrl) + + testCases := []struct { + name string + endpoints []endpoint.Endpoint + expectedLocalityLbEndpoints []*xds_endpoint.LocalityLbEndpoints + expectedLbPolicy xds_cluster.Cluster_LbPolicy + expectedProtocolSelection xds_cluster.Cluster_ClusterProtocolSelection + }{ + { + name: "when service returns one endpoint", + endpoints: []endpoint.Endpoint{tests.Endpoint}, + expectedLocalityLbEndpoints: []*xds_endpoint.LocalityLbEndpoints{ + { + Locality: &xds_core.Locality{ + Zone: "zone", + }, + LbEndpoints: []*xds_endpoint.LbEndpoint{{ + HostIdentifier: &xds_endpoint.LbEndpoint_Endpoint{ + Endpoint: &xds_endpoint.Endpoint{ + Address: envoy.GetAddress(constants.WildcardIPAddr, uint32(tests.ServicePort)), + }, + }, + LoadBalancingWeight: &wrappers.UInt32Value{ + Value: constants.ClusterWeightAcceptAll, // Local cluster accepts all traffic + }, + }}, + }, + }, + }, + { + name: "when service returns two endpoints with same port", + endpoints: []endpoint.Endpoint{tests.Endpoint, { + IP: net.ParseIP("1.2.3.4"), + Port: endpoint.Port(tests.ServicePort), + }}, + expectedLocalityLbEndpoints: []*xds_endpoint.LocalityLbEndpoints{ + { + Locality: &xds_core.Locality{ + Zone: "zone", + }, + LbEndpoints: []*xds_endpoint.LbEndpoint{{ + HostIdentifier: &xds_endpoint.LbEndpoint_Endpoint{ + Endpoint: &xds_endpoint.Endpoint{ + Address: envoy.GetAddress(constants.WildcardIPAddr, uint32(tests.ServicePort)), + }, + }, + LoadBalancingWeight: &wrappers.UInt32Value{ + Value: constants.ClusterWeightAcceptAll, // Local cluster accepts all traffic + }, + }}, + }, + }, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + mockCatalog.EXPECT().ListEndpointsForService(proxyService).Return(tc.endpoints, nil).Times(1) + cluster, err := getLocalServiceCluster(mockCatalog, proxyService, clusterName) + assert.Nil(err) + assert.Equal(clusterName, cluster.Name) + assert.Equal(clusterName, cluster.AltStatName) + assert.Equal(ptypes.DurationProto(clusterConnectTimeout), cluster.ConnectTimeout) + assert.Equal(xds_cluster.Cluster_ROUND_ROBIN, cluster.LbPolicy) + assert.Equal(&xds_cluster.Cluster_Type{Type: xds_cluster.Cluster_STRICT_DNS}, cluster.ClusterDiscoveryType) + assert.Equal(true, cluster.RespectDnsTtl) + assert.Equal(xds_cluster.Cluster_V4_ONLY, cluster.DnsLookupFamily) + assert.Equal(xds_cluster.Cluster_USE_DOWNSTREAM_PROTOCOL, cluster.ProtocolSelection) + assert.Equal(len(tc.expectedLocalityLbEndpoints), len(cluster.LoadAssignment.Endpoints)) + //assert.Equal(tc.expectedLocalityLbEndpoints, cluster.LoadAssignment.Endpoints) + }) + } +}