@@ -4,48 +4,89 @@ import (
44 "net"
55 "testing"
66
7+ xds_core "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
8+ xds_endpoint "github.com/envoyproxy/go-control-plane/envoy/config/endpoint/v3"
9+ "github.com/golang/protobuf/ptypes/wrappers"
10+ "github.com/google/go-cmp/cmp"
711 tassert "github.com/stretchr/testify/assert"
12+ "google.golang.org/protobuf/testing/protocmp"
813
914 "github.com/openservicemesh/osm/pkg/endpoint"
15+ "github.com/openservicemesh/osm/pkg/envoy"
1016 "github.com/openservicemesh/osm/pkg/service"
1117)
1218
1319func TestNewClusterLoadAssignment (t * testing.T ) {
14- assert := tassert .New (t )
15-
16- namespacedServices := []service.MeshService {
17- {Namespace : "ns1" , Name : "bookstore-1" , TargetPort : 80 },
18- {Namespace : "ns2" , Name : "bookstore-2" , TargetPort : 90 },
19- }
20-
21- allServiceEndpoints := map [service.MeshService ][]endpoint.Endpoint {
22- namespacedServices [0 ]: {
23- {IP : net .IP ("0.0.0.0" )},
20+ testCases := []struct {
21+ name string
22+ svc service.MeshService
23+ endpoints []endpoint.Endpoint
24+ expected * xds_endpoint.ClusterLoadAssignment
25+ }{
26+ {
27+ name : "multiple endpoints per cluster within the same locality" ,
28+ svc : service.MeshService {Namespace : "ns1" , Name : "bookstore-1" , TargetPort : 80 },
29+ endpoints : []endpoint.Endpoint {
30+ {IP : net .ParseIP ("1.1.1.1" ), Port : 80 },
31+ {IP : net .ParseIP ("2.2.2.2" ), Port : 80 },
32+ },
33+ expected : & xds_endpoint.ClusterLoadAssignment {
34+ ClusterName : "ns1/bookstore-1|80" ,
35+ Endpoints : []* xds_endpoint.LocalityLbEndpoints {
36+ {
37+ Locality : & xds_core.Locality {
38+ Zone : zone ,
39+ },
40+ LbEndpoints : []* xds_endpoint.LbEndpoint {
41+ {
42+ HostIdentifier : & xds_endpoint.LbEndpoint_Endpoint {
43+ Endpoint : & xds_endpoint.Endpoint {
44+ Address : envoy .GetAddress ("1.1.1.1" , 80 ),
45+ },
46+ },
47+ LoadBalancingWeight : & wrappers.UInt32Value {
48+ Value : 50 ,
49+ },
50+ },
51+ {
52+ HostIdentifier : & xds_endpoint.LbEndpoint_Endpoint {
53+ Endpoint : & xds_endpoint.Endpoint {
54+ Address : envoy .GetAddress ("2.2.2.2" , 80 ),
55+ },
56+ },
57+ LoadBalancingWeight : & wrappers.UInt32Value {
58+ Value : 50 ,
59+ },
60+ },
61+ },
62+ },
63+ },
64+ },
2465 },
25- namespacedServices [1 ]: {
26- {IP : net .IP ("0.0.0.1" )},
27- {IP : net .IP ("0.0.0.2" )},
66+ {
67+ name : "no endpoints for cluster" ,
68+ svc : service.MeshService {Namespace : "ns1" , Name : "bookstore-1" , TargetPort : 80 },
69+ endpoints : nil ,
70+ expected : & xds_endpoint.ClusterLoadAssignment {
71+ ClusterName : "ns1/bookstore-1|80" ,
72+ Endpoints : []* xds_endpoint.LocalityLbEndpoints {
73+ {
74+ Locality : & xds_core.Locality {
75+ Zone : zone ,
76+ },
77+ LbEndpoints : []* xds_endpoint.LbEndpoint {},
78+ },
79+ },
80+ },
2881 },
2982 }
3083
31- cla := newClusterLoadAssignment (namespacedServices [0 ], allServiceEndpoints [namespacedServices [0 ]])
32- assert .NotNil (cla )
33- assert .Equal (cla .ClusterName , "ns1/bookstore-1|80" )
34- assert .Len (cla .Endpoints , 1 )
35- assert .Len (cla .Endpoints [0 ].LbEndpoints , 1 )
36- assert .Equal (cla .Endpoints [0 ].LbEndpoints [0 ].GetLoadBalancingWeight ().Value , uint32 (100 ))
84+ for _ , tc := range testCases {
85+ t .Run (tc .name , func (t * testing.T ) {
86+ assert := tassert .New (t )
3787
38- cla2 := newClusterLoadAssignment (namespacedServices [1 ], allServiceEndpoints [namespacedServices [1 ]])
39- assert .NotNil (cla2 )
40- assert .Equal (cla2 .ClusterName , "ns2/bookstore-2|90" )
41- assert .Len (cla2 .Endpoints , 1 )
42- assert .Len (cla2 .Endpoints [0 ].LbEndpoints , 2 )
43- assert .Equal (cla2 .Endpoints [0 ].LbEndpoints [0 ].GetLoadBalancingWeight ().Value , uint32 (50 ))
44- assert .Equal (cla2 .Endpoints [0 ].LbEndpoints [1 ].GetLoadBalancingWeight ().Value , uint32 (50 ))
45-
46- cla3 := newClusterLoadAssignment (namespacedServices [0 ], []endpoint.Endpoint {})
47- assert .NotNil (cla3 )
48- assert .Equal (cla3 .ClusterName , "ns1/bookstore-1|80" )
49- assert .Len (cla3 .Endpoints , 1 )
50- assert .Len (cla3 .Endpoints [0 ].LbEndpoints , 0 )
88+ actual := newClusterLoadAssignment (tc .svc , tc .endpoints )
89+ assert .True (cmp .Equal (tc .expected , actual , protocmp .Transform ()), cmp .Diff (tc .expected , actual , protocmp .Transform ()))
90+ })
91+ }
5192}
0 commit comments