bgp_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package dynamicrouting
  2. import (
  3. "strings"
  4. "testing"
  5. "time"
  6. )
  7. func TestBGPConfigGeneration(t *testing.T) {
  8. m := &Manager{}
  9. m.config.BGPInstance = BGPInstance{
  10. Enabled: true,
  11. AS: 65001,
  12. RouterID: "192.168.131.56",
  13. RedistributeStatic: true,
  14. }
  15. m.config.BGPNetworks = []BGPNetwork{
  16. {
  17. ID: "net-1",
  18. Network: "192.168.100.0/24",
  19. Disabled: false,
  20. },
  21. }
  22. m.config.BGPPeers = []BGPPeer{
  23. {
  24. ID: "peer-1",
  25. Name: "TestPeer",
  26. RemoteAddress: "192.168.129.225",
  27. RemoteAS: 65555,
  28. Passive: true,
  29. NexthopSelf: true,
  30. RedistStatic: true,
  31. OutputNetworks: []string{"192.168.200.0/24"},
  32. },
  33. }
  34. // Mock bird and birdc
  35. dir := t.TempDir()
  36. t.Setenv("PATH", dir+":"+dir)
  37. // Test configuration generation
  38. sb := &strings.Builder{}
  39. // Test the export filter logic directly
  40. exportSources := []string{`source = RTS_BGP`}
  41. if m.config.BGPPeers[0].RedistStatic || m.config.BGPInstance.RedistributeStatic {
  42. exportSources = append(exportSources, `source ~ [ RTS_STATIC, RTS_INHERIT ]`)
  43. }
  44. exportRule := strings.Join(exportSources, ") || (")
  45. if !strings.Contains(exportRule, "RTS_INHERIT") {
  46. t.Fatalf("Expected exportRule to contain RTS_INHERIT for kernel static routes, got %s", exportRule)
  47. }
  48. _ = sb
  49. }
  50. func TestParseBirdSinceToUptime(t *testing.T) {
  51. refTime := time.Date(2026, 11, 2, 12, 0, 0, 0, time.Local)
  52. tests := []struct {
  53. name string
  54. input string
  55. expected string
  56. }{
  57. {
  58. name: "ISO long same day",
  59. input: "2026-11-02 10:06:15",
  60. expected: "01h 53m 45s",
  61. },
  62. {
  63. name: "ISO long with milliseconds",
  64. input: "2026-11-02 10:06:15.123",
  65. expected: "01h 53m 44s",
  66. },
  67. {
  68. name: "ISO long previous day",
  69. input: "2026-10-31 13:15:43",
  70. expected: "1d 22h 44m 17s",
  71. },
  72. {
  73. name: "Date only",
  74. input: "2026-10-31",
  75. expected: "2d 12h 00m 00s",
  76. },
  77. {
  78. name: "Time only with ms",
  79. input: "10:06:15.631",
  80. expected: "01h 53m 45s",
  81. },
  82. {
  83. name: "Empty",
  84. input: "",
  85. expected: "",
  86. },
  87. {
  88. name: "Invalid",
  89. input: "not-a-date",
  90. expected: "",
  91. },
  92. }
  93. for _, tc := range tests {
  94. t.Run(tc.name, func(t *testing.T) {
  95. got := parseBirdSinceToUptime(tc.input, refTime)
  96. if got != tc.expected {
  97. t.Errorf("%s: got %q, want %q", tc.name, got, tc.expected)
  98. }
  99. })
  100. }
  101. }