k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/pkg/proxy/ipvs/util/ipvs_test.go (about) 1 //go:build linux 2 // +build linux 3 4 /* 5 Copyright 2017 The Kubernetes Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 */ 19 20 package ipvs 21 22 import ( 23 "testing" 24 25 netutils "k8s.io/utils/net" 26 ) 27 28 func TestVirtualServerEqual(t *testing.T) { 29 Tests := []struct { 30 svcA *VirtualServer 31 svcB *VirtualServer 32 equal bool 33 reason string 34 }{ 35 { 36 svcA: &VirtualServer{ 37 Address: netutils.ParseIPSloppy("10.20.30.40"), 38 Protocol: "", 39 Port: 0, 40 Scheduler: "wrr", 41 Flags: 0, 42 Timeout: 0, 43 }, 44 svcB: &VirtualServer{ 45 Address: netutils.ParseIPSloppy("10.20.30.41"), 46 Protocol: "", 47 Port: 0, 48 Scheduler: "wrr", 49 Flags: 0, 50 Timeout: 0, 51 }, 52 equal: false, 53 reason: "IPv4 address not equal", 54 }, 55 { 56 svcA: &VirtualServer{ 57 Address: netutils.ParseIPSloppy("2012::beef"), 58 Protocol: "", 59 Port: 0, 60 Scheduler: "wrr", 61 Flags: 0, 62 Timeout: 0, 63 }, 64 svcB: &VirtualServer{ 65 Address: netutils.ParseIPSloppy("2017::beef"), 66 Protocol: "", 67 Port: 0, 68 Scheduler: "wrr", 69 Flags: 0, 70 Timeout: 0, 71 }, 72 equal: false, 73 reason: "IPv6 address not equal", 74 }, 75 { 76 svcA: &VirtualServer{ 77 Address: netutils.ParseIPSloppy("2012::beef"), 78 Protocol: "TCP", 79 Port: 0, 80 Scheduler: "wrr", 81 Flags: 0, 82 Timeout: 0, 83 }, 84 svcB: &VirtualServer{ 85 Address: netutils.ParseIPSloppy("2012::beef"), 86 Protocol: "UDP", 87 Port: 0, 88 Scheduler: "wrr", 89 Flags: 0, 90 Timeout: 0, 91 }, 92 equal: false, 93 reason: "Protocol not equal", 94 }, 95 { 96 svcA: &VirtualServer{ 97 Address: netutils.ParseIPSloppy("2012::beef"), 98 Protocol: "TCP", 99 Port: 80, 100 Scheduler: "wrr", 101 Flags: 0, 102 Timeout: 0, 103 }, 104 svcB: &VirtualServer{ 105 Address: netutils.ParseIPSloppy("2012::beef"), 106 Protocol: "TCP", 107 Port: 8080, 108 Scheduler: "wrr", 109 Flags: 0, 110 Timeout: 0, 111 }, 112 equal: false, 113 reason: "Port not equal", 114 }, 115 { 116 svcA: &VirtualServer{ 117 Address: netutils.ParseIPSloppy("1.2.3.4"), 118 Protocol: "TCP", 119 Port: 80, 120 Scheduler: "rr", 121 Flags: 0, 122 Timeout: 0, 123 }, 124 svcB: &VirtualServer{ 125 Address: netutils.ParseIPSloppy("1.2.3.4"), 126 Protocol: "TCP", 127 Port: 80, 128 Scheduler: "wlc", 129 Flags: 0, 130 Timeout: 0, 131 }, 132 equal: false, 133 reason: "Scheduler not equal", 134 }, 135 { 136 svcA: &VirtualServer{ 137 Address: netutils.ParseIPSloppy("1.2.3.4"), 138 Protocol: "TCP", 139 Port: 80, 140 Scheduler: "rr", 141 Flags: 2, 142 Timeout: 0, 143 }, 144 svcB: &VirtualServer{ 145 Address: netutils.ParseIPSloppy("1.2.3.4"), 146 Protocol: "TCP", 147 Port: 80, 148 Scheduler: "rr", 149 Flags: 3, 150 Timeout: 0, 151 }, 152 equal: false, 153 reason: "Flags not equal", 154 }, 155 { 156 svcA: &VirtualServer{ 157 Address: netutils.ParseIPSloppy("2012::beef"), 158 Protocol: "", 159 Port: 0, 160 Scheduler: "wrr", 161 Flags: 0, 162 Timeout: 0, 163 }, 164 svcB: &VirtualServer{ 165 Address: netutils.ParseIPSloppy("2012::beef"), 166 Protocol: "", 167 Port: 0, 168 Scheduler: "wrr", 169 Flags: 0, 170 Timeout: 10800, 171 }, 172 equal: false, 173 reason: "Timeout not equal", 174 }, 175 { 176 svcA: &VirtualServer{ 177 Address: netutils.ParseIPSloppy("1.2.3.4"), 178 Protocol: "TCP", 179 Port: 80, 180 Scheduler: "rr", 181 Flags: 0x1, 182 Timeout: 10800, 183 }, 184 svcB: &VirtualServer{ 185 Address: netutils.ParseIPSloppy("1.2.3.4"), 186 Protocol: "TCP", 187 Port: 80, 188 Scheduler: "rr", 189 Flags: 0x1, 190 Timeout: 10800, 191 }, 192 equal: true, 193 reason: "All fields equal", 194 }, 195 { 196 svcA: &VirtualServer{ 197 Address: netutils.ParseIPSloppy("2012::beef"), 198 Protocol: "TCP", 199 Port: 0, 200 Scheduler: "wrr", 201 Flags: 0, 202 Timeout: 0, 203 }, 204 svcB: &VirtualServer{ 205 Address: netutils.ParseIPSloppy("2012::beef"), 206 Protocol: "SCTP", 207 Port: 0, 208 Scheduler: "wrr", 209 Flags: 0, 210 Timeout: 0, 211 }, 212 equal: false, 213 reason: "Protocol not equal", 214 }, 215 { 216 svcA: &VirtualServer{ 217 Address: netutils.ParseIPSloppy("1.2.3.4"), 218 Protocol: "SCTP", 219 Port: 80, 220 Scheduler: "rr", 221 Flags: 0x1, 222 Timeout: 10800, 223 }, 224 svcB: &VirtualServer{ 225 Address: netutils.ParseIPSloppy("1.2.3.4"), 226 Protocol: "SCTP", 227 Port: 80, 228 Scheduler: "rr", 229 Flags: 0x1, 230 Timeout: 10800, 231 }, 232 equal: true, 233 reason: "All fields equal", 234 }, 235 } 236 237 for i := range Tests { 238 equal := Tests[i].svcA.Equal(Tests[i].svcB) 239 if equal != Tests[i].equal { 240 t.Errorf("case: %d got %v, expected %v, reason: %s", i, equal, Tests[i].equal, Tests[i].reason) 241 } 242 } 243 } 244 245 func TestRealServerEqual(t *testing.T) { 246 Tests := []struct { 247 rsA *RealServer 248 rsB *RealServer 249 equal bool 250 reason string 251 }{ 252 { 253 rsA: &RealServer{ 254 Address: netutils.ParseIPSloppy("10.20.30.40"), 255 Port: 80, 256 }, 257 rsB: &RealServer{ 258 Address: netutils.ParseIPSloppy("10.20.30.41"), 259 Port: 80, 260 }, 261 equal: false, 262 reason: "IPv4 address not equal", 263 }, 264 { 265 rsA: &RealServer{ 266 Address: netutils.ParseIPSloppy("2012::beef"), 267 Port: 80, 268 }, 269 rsB: &RealServer{ 270 Address: netutils.ParseIPSloppy("2017::beef"), 271 Port: 80, 272 }, 273 equal: false, 274 reason: "IPv6 address not equal", 275 }, 276 { 277 rsA: &RealServer{ 278 Address: netutils.ParseIPSloppy("2012::beef"), 279 Port: 80, 280 }, 281 rsB: &RealServer{ 282 Address: netutils.ParseIPSloppy("2012::beef"), 283 Port: 8080, 284 }, 285 equal: false, 286 reason: "Port not equal", 287 }, 288 { 289 rsA: &RealServer{ 290 Address: netutils.ParseIPSloppy("1.2.3.4"), 291 Port: 3080, 292 }, 293 rsB: &RealServer{ 294 Address: netutils.ParseIPSloppy("1.2.3.4"), 295 Port: 3080, 296 }, 297 equal: true, 298 reason: "All fields equal", 299 }, 300 { 301 rsA: &RealServer{ 302 Address: netutils.ParseIPSloppy("2012::beef"), 303 Port: 3080, 304 }, 305 rsB: &RealServer{ 306 Address: netutils.ParseIPSloppy("2012::beef"), 307 Port: 3080, 308 }, 309 equal: true, 310 reason: "All fields equal", 311 }, 312 } 313 314 for i := range Tests { 315 equal := Tests[i].rsA.Equal(Tests[i].rsB) 316 if equal != Tests[i].equal { 317 t.Errorf("case: %d got %v, expected %v, reason: %s", i, equal, Tests[i].equal, Tests[i].reason) 318 } 319 } 320 } 321 322 func TestFrontendServiceString(t *testing.T) { 323 Tests := []struct { 324 svc *VirtualServer 325 expected string 326 }{ 327 { 328 svc: &VirtualServer{ 329 Address: netutils.ParseIPSloppy("10.20.30.40"), 330 Protocol: "TCP", 331 Port: 80, 332 }, 333 expected: "10.20.30.40:80/TCP", 334 }, 335 { 336 svc: &VirtualServer{ 337 Address: netutils.ParseIPSloppy("2012::beef"), 338 Protocol: "UDP", 339 Port: 8080, 340 }, 341 expected: "[2012::beef]:8080/UDP", 342 }, 343 { 344 svc: &VirtualServer{ 345 Address: netutils.ParseIPSloppy("10.20.30.41"), 346 Protocol: "ESP", 347 Port: 1234, 348 }, 349 expected: "10.20.30.41:1234/ESP", 350 }, 351 } 352 353 for i := range Tests { 354 if Tests[i].expected != Tests[i].svc.String() { 355 t.Errorf("case: %d got %v, expected %v", i, Tests[i].svc.String(), Tests[i].expected) 356 } 357 } 358 } 359 360 func TestFrontendDestinationString(t *testing.T) { 361 Tests := []struct { 362 svc *RealServer 363 expected string 364 }{ 365 { 366 svc: &RealServer{ 367 Address: netutils.ParseIPSloppy("10.20.30.40"), 368 Port: 80, 369 }, 370 expected: "10.20.30.40:80", 371 }, 372 { 373 svc: &RealServer{ 374 Address: netutils.ParseIPSloppy("2012::beef"), 375 Port: 8080, 376 }, 377 expected: "[2012::beef]:8080", 378 }, 379 } 380 381 for i := range Tests { 382 if Tests[i].expected != Tests[i].svc.String() { 383 t.Errorf("case: %d got %v, expected %v", i, Tests[i].svc.String(), Tests[i].expected) 384 } 385 } 386 }