istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pilot/pkg/xds/endpoints/endpoint_builder_test.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 endpoints 16 17 import ( 18 "reflect" 19 "testing" 20 21 "google.golang.org/protobuf/types/known/wrapperspb" 22 23 meshconfig "istio.io/api/mesh/v1alpha1" 24 networking "istio.io/api/networking/v1alpha3" 25 "istio.io/istio/pilot/pkg/model" 26 "istio.io/istio/pkg/config" 27 ) 28 29 func TestPopulateFailoverPriorityLabels(t *testing.T) { 30 tests := []struct { 31 name string 32 dr *config.Config 33 mesh *meshconfig.MeshConfig 34 expectedLabels []byte 35 }{ 36 { 37 name: "no dr", 38 expectedLabels: nil, 39 }, 40 { 41 name: "simple", 42 dr: &config.Config{ 43 Spec: &networking.DestinationRule{ 44 TrafficPolicy: &networking.TrafficPolicy{ 45 OutlierDetection: &networking.OutlierDetection{ 46 ConsecutiveErrors: 5, 47 }, 48 LoadBalancer: &networking.LoadBalancerSettings{ 49 LocalityLbSetting: &networking.LocalityLoadBalancerSetting{ 50 FailoverPriority: []string{ 51 "a", 52 "b", 53 }, 54 }, 55 }, 56 }, 57 }, 58 }, 59 expectedLabels: []byte("a:a b:b "), 60 }, 61 { 62 name: "no outlier detection", 63 dr: &config.Config{ 64 Spec: &networking.DestinationRule{ 65 TrafficPolicy: &networking.TrafficPolicy{ 66 LoadBalancer: &networking.LoadBalancerSettings{ 67 LocalityLbSetting: &networking.LocalityLoadBalancerSetting{ 68 FailoverPriority: []string{ 69 "a", 70 "b", 71 }, 72 }, 73 }, 74 }, 75 }, 76 }, 77 expectedLabels: nil, 78 }, 79 { 80 name: "no failover priority", 81 dr: &config.Config{ 82 Spec: &networking.DestinationRule{ 83 TrafficPolicy: &networking.TrafficPolicy{ 84 OutlierDetection: &networking.OutlierDetection{ 85 ConsecutiveErrors: 5, 86 }, 87 }, 88 }, 89 }, 90 expectedLabels: nil, 91 }, 92 { 93 name: "failover priority disabled", 94 dr: &config.Config{ 95 Spec: &networking.DestinationRule{ 96 TrafficPolicy: &networking.TrafficPolicy{ 97 OutlierDetection: &networking.OutlierDetection{ 98 ConsecutiveErrors: 5, 99 }, 100 LoadBalancer: &networking.LoadBalancerSettings{ 101 LocalityLbSetting: &networking.LocalityLoadBalancerSetting{ 102 FailoverPriority: []string{ 103 "a", 104 "b", 105 }, 106 Enabled: &wrapperspb.BoolValue{Value: false}, 107 }, 108 }, 109 }, 110 }, 111 }, 112 expectedLabels: nil, 113 }, 114 { 115 name: "mesh LocalityLoadBalancerSetting", 116 dr: &config.Config{ 117 Spec: &networking.DestinationRule{ 118 TrafficPolicy: &networking.TrafficPolicy{ 119 OutlierDetection: &networking.OutlierDetection{ 120 ConsecutiveErrors: 5, 121 }, 122 }, 123 }, 124 }, 125 mesh: &meshconfig.MeshConfig{ 126 LocalityLbSetting: &networking.LocalityLoadBalancerSetting{ 127 FailoverPriority: []string{ 128 "a", 129 "b", 130 }, 131 }, 132 }, 133 expectedLabels: []byte("a:a b:b "), 134 }, 135 { 136 name: "mesh LocalityLoadBalancerSetting(no outlier detection)", 137 dr: &config.Config{ 138 Spec: &networking.DestinationRule{ 139 TrafficPolicy: &networking.TrafficPolicy{}, 140 }, 141 }, 142 mesh: &meshconfig.MeshConfig{ 143 LocalityLbSetting: &networking.LocalityLoadBalancerSetting{ 144 FailoverPriority: []string{ 145 "a", 146 "b", 147 }, 148 }, 149 }, 150 expectedLabels: nil, 151 }, 152 { 153 name: "mesh LocalityLoadBalancerSetting(no failover priority)", 154 dr: &config.Config{ 155 Spec: &networking.DestinationRule{ 156 TrafficPolicy: &networking.TrafficPolicy{ 157 OutlierDetection: &networking.OutlierDetection{ 158 ConsecutiveErrors: 5, 159 }, 160 }, 161 }, 162 }, 163 mesh: &meshconfig.MeshConfig{ 164 LocalityLbSetting: &networking.LocalityLoadBalancerSetting{}, 165 }, 166 expectedLabels: nil, 167 }, 168 { 169 name: "mesh LocalityLoadBalancerSetting(failover priority disabled)", 170 dr: &config.Config{ 171 Spec: &networking.DestinationRule{ 172 TrafficPolicy: &networking.TrafficPolicy{ 173 OutlierDetection: &networking.OutlierDetection{ 174 ConsecutiveErrors: 5, 175 }, 176 }, 177 }, 178 }, 179 mesh: &meshconfig.MeshConfig{ 180 LocalityLbSetting: &networking.LocalityLoadBalancerSetting{ 181 FailoverPriority: []string{ 182 "a", 183 "b", 184 }, 185 Enabled: &wrapperspb.BoolValue{Value: false}, 186 }, 187 }, 188 expectedLabels: nil, 189 }, 190 { 191 name: "both dr and mesh LocalityLoadBalancerSetting", 192 dr: &config.Config{ 193 Spec: &networking.DestinationRule{ 194 TrafficPolicy: &networking.TrafficPolicy{ 195 OutlierDetection: &networking.OutlierDetection{ 196 ConsecutiveErrors: 5, 197 }, 198 LoadBalancer: &networking.LoadBalancerSettings{ 199 LocalityLbSetting: &networking.LocalityLoadBalancerSetting{ 200 FailoverPriority: []string{ 201 "a", 202 "b", 203 }, 204 }, 205 }, 206 }, 207 }, 208 }, 209 mesh: &meshconfig.MeshConfig{ 210 LocalityLbSetting: &networking.LocalityLoadBalancerSetting{ 211 FailoverPriority: []string{ 212 "c", 213 }, 214 }, 215 }, 216 expectedLabels: []byte("a:a b:b "), 217 }, 218 } 219 for _, tt := range tests { 220 t.Run(tt.name, func(t *testing.T) { 221 b := EndpointBuilder{ 222 proxy: &model.Proxy{ 223 Metadata: &model.NodeMetadata{}, 224 Labels: map[string]string{ 225 "app": "foo", 226 "a": "a", 227 "b": "b", 228 }, 229 }, 230 push: &model.PushContext{ 231 Mesh: tt.mesh, 232 }, 233 } 234 if tt.dr != nil { 235 b.destinationRule = model.ConvertConsolidatedDestRule(tt.dr) 236 } 237 b.populateFailoverPriorityLabels() 238 if !reflect.DeepEqual(b.failoverPriorityLabels, tt.expectedLabels) { 239 t.Fatalf("expected priorityLabels %v but got %v", tt.expectedLabels, b.failoverPriorityLabels) 240 } 241 }) 242 } 243 }