balance_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package routing
  2. import (
  3. "net/netip"
  4. "testing"
  5. "github.com/slackhq/nebula/firewall"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func TestPacketsAreBalancedEqually(t *testing.T) {
  9. gateways := []Gateway{}
  10. gw1Addr := netip.MustParseAddr("1.0.0.1")
  11. gw2Addr := netip.MustParseAddr("1.0.0.2")
  12. gw3Addr := netip.MustParseAddr("1.0.0.3")
  13. gateways = append(gateways, NewGateway(gw1Addr, 1))
  14. gateways = append(gateways, NewGateway(gw2Addr, 1))
  15. gateways = append(gateways, NewGateway(gw3Addr, 1))
  16. CalculateBucketsForGateways(gateways)
  17. gw1count := 0
  18. gw2count := 0
  19. gw3count := 0
  20. iterationCount := uint16(65535)
  21. for i := uint16(0); i < iterationCount; i++ {
  22. packet := firewall.Packet{
  23. LocalAddr: netip.MustParseAddr("192.168.1.1"),
  24. RemoteAddr: netip.MustParseAddr("10.0.0.1"),
  25. LocalPort: i,
  26. RemotePort: 65535 - i,
  27. Protocol: 6, // TCP
  28. Fragment: false,
  29. }
  30. selectedGw, ok := BalancePacket(&packet, gateways)
  31. assert.True(t, ok)
  32. switch selectedGw {
  33. case gw1Addr:
  34. gw1count += 1
  35. case gw2Addr:
  36. gw2count += 1
  37. case gw3Addr:
  38. gw3count += 1
  39. }
  40. }
  41. // Assert packets are balanced, allow variation of up to 100 packets per gateway
  42. assert.InDeltaf(t, iterationCount/3, gw1count, 100, "Expected %d +/- 100, but got %d", iterationCount/3, gw1count)
  43. assert.InDeltaf(t, iterationCount/3, gw2count, 100, "Expected %d +/- 100, but got %d", iterationCount/3, gw1count)
  44. assert.InDeltaf(t, iterationCount/3, gw3count, 100, "Expected %d +/- 100, but got %d", iterationCount/3, gw1count)
  45. }
  46. func TestPacketsAreBalancedByPriority(t *testing.T) {
  47. gateways := []Gateway{}
  48. gw1Addr := netip.MustParseAddr("1.0.0.1")
  49. gw2Addr := netip.MustParseAddr("1.0.0.2")
  50. gateways = append(gateways, NewGateway(gw1Addr, 10))
  51. gateways = append(gateways, NewGateway(gw2Addr, 5))
  52. CalculateBucketsForGateways(gateways)
  53. gw1count := 0
  54. gw2count := 0
  55. iterationCount := uint16(65535)
  56. for i := uint16(0); i < iterationCount; i++ {
  57. packet := firewall.Packet{
  58. LocalAddr: netip.MustParseAddr("192.168.1.1"),
  59. RemoteAddr: netip.MustParseAddr("10.0.0.1"),
  60. LocalPort: i,
  61. RemotePort: 65535 - i,
  62. Protocol: 6, // TCP
  63. Fragment: false,
  64. }
  65. selectedGw, ok := BalancePacket(&packet, gateways)
  66. assert.True(t, ok)
  67. switch selectedGw {
  68. case gw1Addr:
  69. gw1count += 1
  70. case gw2Addr:
  71. gw2count += 1
  72. }
  73. }
  74. iterationCountAsFloat := float32(iterationCount)
  75. assert.InDeltaf(t, iterationCountAsFloat*(2.0/3.0), gw1count, 100, "Expected %d +/- 100, but got %d", iterationCountAsFloat*(2.0/3.0), gw1count)
  76. assert.InDeltaf(t, iterationCountAsFloat*(1.0/3.0), gw2count, 100, "Expected %d +/- 100, but got %d", iterationCountAsFloat*(1.0/3.0), gw2count)
  77. }
  78. func TestBalancePacketDistributsRandomlyAndReturnsFalseIfBucketsNotCalculated(t *testing.T) {
  79. gateways := []Gateway{}
  80. gw1Addr := netip.MustParseAddr("1.0.0.1")
  81. gw2Addr := netip.MustParseAddr("1.0.0.2")
  82. gateways = append(gateways, NewGateway(gw1Addr, 10))
  83. gateways = append(gateways, NewGateway(gw2Addr, 5))
  84. iterationCount := uint16(65535)
  85. gw1count := 0
  86. gw2count := 0
  87. for i := uint16(0); i < iterationCount; i++ {
  88. packet := firewall.Packet{
  89. LocalAddr: netip.MustParseAddr("192.168.1.1"),
  90. RemoteAddr: netip.MustParseAddr("10.0.0.1"),
  91. LocalPort: i,
  92. RemotePort: 65535 - i,
  93. Protocol: 6, // TCP
  94. Fragment: false,
  95. }
  96. selectedGw, ok := BalancePacket(&packet, gateways)
  97. assert.False(t, ok)
  98. switch selectedGw {
  99. case gw1Addr:
  100. gw1count += 1
  101. case gw2Addr:
  102. gw2count += 1
  103. }
  104. }
  105. assert.Equal(t, int(iterationCount), (gw1count + gw2count))
  106. assert.NotEqual(t, 0, gw1count)
  107. assert.NotEqual(t, 0, gw2count)
  108. }