Debian Codegraph @

routing/balance_test.go

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