bgp_test.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package dynamicrouting
  2. import (
  3. "strings"
  4. "testing"
  5. )
  6. func TestBGPConfigGeneration(t *testing.T) {
  7. m := &Manager{}
  8. m.config.BGPInstance = BGPInstance{
  9. Enabled: true,
  10. AS: 65001,
  11. RouterID: "192.168.131.56",
  12. RedistributeStatic: true,
  13. }
  14. m.config.BGPNetworks = []BGPNetwork{
  15. {
  16. ID: "net-1",
  17. Network: "192.168.100.0/24",
  18. Disabled: false,
  19. },
  20. }
  21. m.config.BGPPeers = []BGPPeer{
  22. {
  23. ID: "peer-1",
  24. Name: "TestPeer",
  25. RemoteAddress: "192.168.129.225",
  26. RemoteAS: 65555,
  27. Passive: true,
  28. NexthopSelf: true,
  29. RedistStatic: true,
  30. OutputNetworks: []string{"192.168.200.0/24"},
  31. },
  32. }
  33. // Mock bird and birdc
  34. dir := t.TempDir()
  35. t.Setenv("PATH", dir+":"+dir)
  36. // Test configuration generation
  37. sb := &strings.Builder{}
  38. // Test the export filter logic directly
  39. exportSources := []string{`source = RTS_BGP`}
  40. if m.config.BGPPeers[0].RedistStatic || m.config.BGPInstance.RedistributeStatic {
  41. exportSources = append(exportSources, `source ~ [ RTS_STATIC, RTS_INHERIT ]`)
  42. }
  43. exportRule := strings.Join(exportSources, ") || (")
  44. if !strings.Contains(exportRule, "RTS_INHERIT") {
  45. t.Fatalf("Expected exportRule to contain RTS_INHERIT for kernel static routes, got %s", exportRule)
  46. }
  47. _ = sb
  48. }