vxlan_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package vpn
  2. import (
  3. "os"
  4. "path/filepath"
  5. "testing"
  6. )
  7. func TestVXLANManagerBasics(t *testing.T) {
  8. tmpDir := t.TempDir()
  9. storePath := filepath.Join(tmpDir, "vxlan.json")
  10. mgr := NewVXLANManager(storePath)
  11. if mgr == nil {
  12. t.Fatal("expected non-nil VXLANManager")
  13. }
  14. // 1. Validation checks
  15. err := mgr.Save(&VXLANInstance{
  16. VNI: 0,
  17. })
  18. if err == nil {
  19. t.Fatal("expected error for VNI <= 0")
  20. }
  21. err = mgr.Save(&VXLANInstance{
  22. VNI: 16777216,
  23. })
  24. if err == nil {
  25. t.Fatal("expected error for VNI > 16777215")
  26. }
  27. // 2. Save a disabled VXLAN instance (does not invoke kernel netlink)
  28. inst1 := &VXLANInstance{
  29. ID: "vxlan-test-1",
  30. Name: "vxlan-test1",
  31. VNI: 100,
  32. Disabled: true,
  33. Comment: "Test VXLAN 1",
  34. }
  35. if err := mgr.Save(inst1); err != nil {
  36. t.Fatalf("failed to save inst1: %v", err)
  37. }
  38. // Validate auto-defaults
  39. if inst1.Port != 4789 {
  40. t.Errorf("expected default port 4789, got %d", inst1.Port)
  41. }
  42. if inst1.MTU != 1500 {
  43. t.Errorf("expected default MTU 1500, got %d", inst1.MTU)
  44. }
  45. if inst1.MACAddress == "" {
  46. t.Error("expected auto-generated MAC address")
  47. }
  48. // 3. Get and List
  49. got, found := mgr.Get("vxlan-test-1")
  50. if !found || got == nil {
  51. t.Fatal("expected to find vxlan-test-1 by ID")
  52. }
  53. if got.Name != "vxlan-test1" {
  54. t.Errorf("expected name vxlan-test1, got %s", got.Name)
  55. }
  56. gotByName, found := mgr.Get("vxlan-test1")
  57. if !found || gotByName == nil {
  58. t.Fatal("expected to find vxlan-test1 by Name")
  59. }
  60. list := mgr.List()
  61. if len(list) != 1 {
  62. t.Fatalf("expected 1 tunnel, got %d", len(list))
  63. }
  64. // 4. Duplicate checks
  65. dupVNI := &VXLANInstance{
  66. ID: "vxlan-test-dup",
  67. Name: "vxlan-test-other",
  68. VNI: 100,
  69. Port: 4789,
  70. Disabled: true,
  71. }
  72. if err := mgr.Save(dupVNI); err == nil {
  73. t.Fatal("expected error on duplicate VNI+Port")
  74. }
  75. dupName := &VXLANInstance{
  76. ID: "vxlan-test-dup-name",
  77. Name: "vxlan-test1",
  78. VNI: 200,
  79. Disabled: true,
  80. }
  81. if err := mgr.Save(dupName); err == nil {
  82. t.Fatal("expected error on duplicate Name")
  83. }
  84. // 5. VTEP management
  85. vtep1 := &VXLANVtep{
  86. RemoteIP: "192.168.1.10",
  87. Comment: "Remote Office VTEP",
  88. }
  89. if err := mgr.AddVtep("vxlan-test-1", vtep1); err != nil {
  90. t.Fatalf("failed to add VTEP: %v", err)
  91. }
  92. // Duplicate VTEP remote IP check
  93. if err := mgr.AddVtep("vxlan-test-1", &VXLANVtep{RemoteIP: "192.168.1.10"}); err == nil {
  94. t.Fatal("expected error for duplicate remote IP VTEP")
  95. }
  96. // Invalid remote IP
  97. if err := mgr.AddVtep("vxlan-test-1", &VXLANVtep{RemoteIP: "invalid-ip"}); err == nil {
  98. t.Fatal("expected error for invalid remote IP")
  99. }
  100. vteps := mgr.ListVteps("vxlan-test1")
  101. if len(vteps) != 1 {
  102. t.Fatalf("expected 1 VTEP, got %d", len(vteps))
  103. }
  104. if vteps[0].RemoteIP != "192.168.1.10" {
  105. t.Errorf("expected 192.168.1.10, got %s", vteps[0].RemoteIP)
  106. }
  107. // Delete VTEP
  108. if err := mgr.DeleteVtep("vxlan-test1", "192.168.1.10"); err != nil {
  109. t.Fatalf("failed to delete VTEP: %v", err)
  110. }
  111. if len(mgr.ListVteps("vxlan-test1")) != 0 {
  112. t.Fatal("expected 0 VTEPs after deletion")
  113. }
  114. // 6. Delete tunnel
  115. if err := mgr.Delete("vxlan-test-1"); err != nil {
  116. t.Fatalf("failed to delete tunnel: %v", err)
  117. }
  118. if len(mgr.List()) != 0 {
  119. t.Fatal("expected 0 tunnels after deletion")
  120. }
  121. // Verify persistence file exists and is valid
  122. if _, err := os.Stat(storePath); err != nil {
  123. t.Fatalf("expected store file to exist: %v", err)
  124. }
  125. }