|
1 | 1 | package lvs |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "syscall" |
4 | 5 | "testing" |
5 | 6 |
|
6 | 7 | "github.com/easzlab/ezlb/pkg/config" |
@@ -488,6 +489,107 @@ func TestReconcile_BackendRecovery(t *testing.T) { |
488 | 489 | } |
489 | 490 | } |
490 | 491 |
|
| 492 | +// --- UDP protocol tests --- |
| 493 | + |
| 494 | +func TestReconcile_UDPService(t *testing.T) { |
| 495 | + mgr, _, reconciler := newReconcilerTestEnv(t) |
| 496 | + defer mgr.Close() |
| 497 | + |
| 498 | + configs := []config.ServiceConfig{ |
| 499 | + { |
| 500 | + Name: "dns-svc", |
| 501 | + Listen: "10.0.0.1:53", |
| 502 | + Protocol: "udp", |
| 503 | + Scheduler: "rr", |
| 504 | + HealthCheck: config.HealthCheckConfig{ |
| 505 | + Enabled: boolPtr(false), |
| 506 | + }, |
| 507 | + Backends: []config.BackendConfig{ |
| 508 | + makeBackend("192.168.1.1:53", 1), |
| 509 | + makeBackend("192.168.1.2:53", 1), |
| 510 | + }, |
| 511 | + }, |
| 512 | + } |
| 513 | + |
| 514 | + if err := reconciler.Reconcile(configs); err != nil { |
| 515 | + t.Fatalf("Reconcile failed: %v", err) |
| 516 | + } |
| 517 | + |
| 518 | + services, err := mgr.GetServices() |
| 519 | + if err != nil { |
| 520 | + t.Fatalf("GetServices failed: %v", err) |
| 521 | + } |
| 522 | + if len(services) != 1 { |
| 523 | + t.Fatalf("expected 1 service, got %d", len(services)) |
| 524 | + } |
| 525 | + if services[0].Protocol != syscall.IPPROTO_UDP { |
| 526 | + t.Errorf("expected protocol IPPROTO_UDP (%d), got %d", syscall.IPPROTO_UDP, services[0].Protocol) |
| 527 | + } |
| 528 | + |
| 529 | + dests, err := mgr.GetDestinations(services[0]) |
| 530 | + if err != nil { |
| 531 | + t.Fatalf("GetDestinations failed: %v", err) |
| 532 | + } |
| 533 | + if len(dests) != 2 { |
| 534 | + t.Fatalf("expected 2 destinations, got %d", len(dests)) |
| 535 | + } |
| 536 | +} |
| 537 | + |
| 538 | +func TestReconcile_TCPAndUDPSameAddress(t *testing.T) { |
| 539 | + mgr, _, reconciler := newReconcilerTestEnv(t) |
| 540 | + defer mgr.Close() |
| 541 | + |
| 542 | + configs := []config.ServiceConfig{ |
| 543 | + { |
| 544 | + Name: "dns-tcp", |
| 545 | + Listen: "10.0.0.1:53", |
| 546 | + Protocol: "tcp", |
| 547 | + Scheduler: "rr", |
| 548 | + HealthCheck: config.HealthCheckConfig{ |
| 549 | + Enabled: boolPtr(false), |
| 550 | + }, |
| 551 | + Backends: []config.BackendConfig{ |
| 552 | + makeBackend("192.168.1.1:53", 1), |
| 553 | + }, |
| 554 | + }, |
| 555 | + { |
| 556 | + Name: "dns-udp", |
| 557 | + Listen: "10.0.0.1:53", |
| 558 | + Protocol: "udp", |
| 559 | + Scheduler: "rr", |
| 560 | + HealthCheck: config.HealthCheckConfig{ |
| 561 | + Enabled: boolPtr(false), |
| 562 | + }, |
| 563 | + Backends: []config.BackendConfig{ |
| 564 | + makeBackend("192.168.1.2:53", 2), |
| 565 | + }, |
| 566 | + }, |
| 567 | + } |
| 568 | + |
| 569 | + if err := reconciler.Reconcile(configs); err != nil { |
| 570 | + t.Fatalf("Reconcile failed: %v", err) |
| 571 | + } |
| 572 | + |
| 573 | + services, err := mgr.GetServices() |
| 574 | + if err != nil { |
| 575 | + t.Fatalf("GetServices failed: %v", err) |
| 576 | + } |
| 577 | + if len(services) != 2 { |
| 578 | + t.Fatalf("expected 2 services (TCP + UDP), got %d", len(services)) |
| 579 | + } |
| 580 | + |
| 581 | + // Verify each service has its own destinations |
| 582 | + for _, svc := range services { |
| 583 | + dests, err := mgr.GetDestinations(svc) |
| 584 | + if err != nil { |
| 585 | + t.Fatalf("GetDestinations failed: %v", err) |
| 586 | + } |
| 587 | + if len(dests) != 1 { |
| 588 | + t.Errorf("expected 1 destination per service, got %d for protocol %d", len(dests), svc.Protocol) |
| 589 | + } |
| 590 | + } |
| 591 | +} |
| 592 | + |
491 | 593 | // --- Error handling --- |
492 | 594 |
|
493 | 595 | func TestReconcile_InvalidListenAddress(t *testing.T) { |
|
0 commit comments