istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pilot/test/xdstest/endpoints.go (about) 1 // Copyright Istio Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package xdstest 16 17 import ( 18 "fmt" 19 "sort" 20 "testing" 21 22 endpointv3 "github.com/envoyproxy/go-control-plane/envoy/config/endpoint/v3" 23 ) 24 25 type LbEpInfo struct { 26 Address string 27 // nolint: structcheck 28 Weight uint32 29 } 30 31 type LocLbEpInfo struct { 32 LbEps []LbEpInfo 33 Weight uint32 34 } 35 36 func (i LocLbEpInfo) GetAddrs() []string { 37 addrs := make([]string, 0) 38 for _, ep := range i.LbEps { 39 addrs = append(addrs, ep.Address) 40 } 41 return addrs 42 } 43 44 func CompareEndpointsOrFail(t *testing.T, cluster string, got []*endpointv3.LocalityLbEndpoints, want []LocLbEpInfo) { 45 if err := CompareEndpoints(cluster, got, want); err != nil { 46 t.Error(err) 47 } 48 } 49 50 func CompareEndpoints(cluster string, got []*endpointv3.LocalityLbEndpoints, want []LocLbEpInfo) error { 51 if len(got) != len(want) { 52 return fmt.Errorf("unexpected number of filtered endpoints for %s: got %v, want %v", cluster, len(got), len(want)) 53 } 54 55 sort.Slice(got, func(i, j int) bool { 56 addrI := got[i].LbEndpoints[0].GetEndpoint().Address.GetSocketAddress().Address 57 addrJ := got[j].LbEndpoints[0].GetEndpoint().Address.GetSocketAddress().Address 58 return addrI < addrJ 59 }) 60 61 for i, ep := range got { 62 if len(ep.LbEndpoints) != len(want[i].LbEps) { 63 return fmt.Errorf("unexpected number of LB endpoints within endpoint %d: %v, want %v", 64 i, getLbEndpointAddrs(ep), want[i].GetAddrs()) 65 } 66 67 if ep.LoadBalancingWeight.GetValue() != want[i].Weight { 68 return fmt.Errorf("unexpected weight for endpoint %d: got %v, want %v", i, ep.LoadBalancingWeight.GetValue(), want[i].Weight) 69 } 70 71 for _, lbEp := range ep.LbEndpoints { 72 addr := lbEp.GetEndpoint().Address.GetSocketAddress().Address 73 found := false 74 for _, wantLbEp := range want[i].LbEps { 75 if addr == wantLbEp.Address { 76 found = true 77 78 // Now compare the weight. 79 if lbEp.GetLoadBalancingWeight().Value != wantLbEp.Weight { 80 return fmt.Errorf("unexpected weight for endpoint %s: got %v, want %v", 81 addr, lbEp.GetLoadBalancingWeight().Value, wantLbEp.Weight) 82 } 83 break 84 } 85 } 86 if !found { 87 return fmt.Errorf("unexpected address for endpoint %d: %v", i, addr) 88 } 89 } 90 } 91 return nil 92 } 93 94 func getLbEndpointAddrs(ep *endpointv3.LocalityLbEndpoints) []string { 95 addrs := make([]string, 0) 96 for _, lbEp := range ep.LbEndpoints { 97 addrs = append(addrs, lbEp.GetEndpoint().Address.GetSocketAddress().Address) 98 } 99 return addrs 100 }