@@ -3,7 +3,6 @@ package overlay
3
3
import (
4
4
"fmt"
5
5
"net/netip"
6
- "reflect"
7
6
"testing"
8
7
9
8
"github.com/slackhq/nebula/config"
@@ -155,26 +154,44 @@ func Test_parseUnsafeRoutes(t *testing.T) {
155
154
156
155
// invalid via
157
156
for _ , invalidValue := range []interface {}{
158
- 127 , false , nil , 1.0 ,
157
+ 127 , false , nil , 1.0 , [] string { "1" , "2" },
159
158
} {
160
159
c .Settings ["tun" ] = map [interface {}]interface {}{"unsafe_routes" : []interface {}{map [interface {}]interface {}{"via" : invalidValue }}}
161
160
routes , err = parseUnsafeRoutes (c , []netip.Prefix {n })
162
161
assert .Nil (t , routes )
163
- require .EqualError (t , err , fmt .Sprintf ("entry 1.via in tun.unsafe_routes is not a string or array of string : found %T" , invalidValue ))
162
+ require .EqualError (t , err , fmt .Sprintf ("entry 1.via in tun.unsafe_routes is not a string or list of gateways : found %T" , invalidValue ))
164
163
}
165
164
166
165
// Unparsable list of via
167
166
c .Settings ["tun" ] = map [interface {}]interface {}{"unsafe_routes" : []interface {}{map [interface {}]interface {}{"via" : []string {"1" , "2" }}}}
168
167
routes , err = parseUnsafeRoutes (c , []netip.Prefix {n })
169
168
assert .Nil (t , routes )
170
- assert .EqualError (t , err , fmt . Sprintf ( "entry 1.via in tun.unsafe_routes failed to parse address: ParseAddr( \" 1 \" ): unable to parse IP" ) )
169
+ require .EqualError (t , err , "entry 1.via in tun.unsafe_routes is not a string or list of gateways: found []string" )
171
170
172
171
// unparsable via
173
172
c .Settings ["tun" ] = map [interface {}]interface {}{"unsafe_routes" : []interface {}{map [interface {}]interface {}{"mtu" : "500" , "via" : "nope" }}}
174
173
routes , err = parseUnsafeRoutes (c , []netip.Prefix {n })
175
174
assert .Nil (t , routes )
176
175
require .EqualError (t , err , "entry 1.via in tun.unsafe_routes failed to parse address: ParseAddr(\" nope\" ): unable to parse IP" )
177
176
177
+ // unparsable gateway
178
+ c .Settings ["tun" ] = map [interface {}]interface {}{"unsafe_routes" : []interface {}{map [interface {}]interface {}{"mtu" : "500" , "via" : []interface {}{map [interface {}]interface {}{"gateway" : "1" }}}}}
179
+ routes , err = parseUnsafeRoutes (c , []netip.Prefix {n })
180
+ assert .Nil (t , routes )
181
+ require .EqualError (t , err , "entry .gateway in tun.unsafe_routes[1].via[1] failed to parse address: ParseAddr(\" 1\" ): unable to parse IP" )
182
+
183
+ // missing gateway element
184
+ c .Settings ["tun" ] = map [interface {}]interface {}{"unsafe_routes" : []interface {}{map [interface {}]interface {}{"mtu" : "500" , "via" : []interface {}{map [interface {}]interface {}{"weight" : "1" }}}}}
185
+ routes , err = parseUnsafeRoutes (c , []netip.Prefix {n })
186
+ assert .Nil (t , routes )
187
+ require .EqualError (t , err , "entry .gateway in tun.unsafe_routes[1].via[1] is not present" )
188
+
189
+ // unparsable weight element
190
+ c .Settings ["tun" ] = map [interface {}]interface {}{"unsafe_routes" : []interface {}{map [interface {}]interface {}{"mtu" : "500" , "via" : []interface {}{map [interface {}]interface {}{"gateway" : "10.0.0.1" , "weight" : "a" }}}}}
191
+ routes , err = parseUnsafeRoutes (c , []netip.Prefix {n })
192
+ assert .Nil (t , routes )
193
+ require .EqualError (t , err , "entry .weight in tun.unsafe_routes[1].via[1] is not an integer" )
194
+
178
195
// missing route
179
196
c .Settings ["tun" ] = map [interface {}]interface {}{"unsafe_routes" : []interface {}{map [interface {}]interface {}{"via" : "127.0.0.1" , "mtu" : "500" }}}
180
197
routes , err = parseUnsafeRoutes (c , []netip.Prefix {n })
@@ -305,51 +322,83 @@ func Test_makeRouteTree(t *testing.T) {
305
322
assert .False (t , ok )
306
323
}
307
324
308
- func sameElements [T comparable ](arr1 []T , arr2 []T ) bool {
309
- if len (arr1 ) != len (arr2 ) {
310
- return false
311
- }
312
-
313
- counts1 := make (map [T ]int )
314
- counts2 := make (map [T ]int )
315
-
316
- for _ , v := range arr1 {
317
- counts1 [v ]++
318
- }
319
- for _ , v := range arr2 {
320
- counts2 [v ]++
321
- }
322
-
323
- return reflect .DeepEqual (counts1 , counts2 )
324
- }
325
-
326
- func Test_multipath (t * testing.T ) {
325
+ func Test_makeMultipathUnsafeRouteTree (t * testing.T ) {
327
326
l := test .NewLogger ()
328
327
c := config .NewC (l )
329
328
n , err := netip .ParsePrefix ("10.0.0.0/24" )
330
- assert .NoError (t , err )
329
+ require .NoError (t , err )
331
330
332
- c .Settings ["tun" ] = map [interface {}]interface {}{"unsafe_routes" : []interface {}{
333
- map [interface {}]interface {}{"via" : []string {"192.168.0.1" , "192.168.0.2" , "192.168.0.3" }, "route" : "1.0.0.0/16" },
334
- }}
331
+ c .Settings ["tun" ] = map [interface {}]interface {}{
332
+ "unsafe_routes" : []interface {}{
333
+ map [interface {}]interface {}{
334
+ "route" : "192.168.86.0/24" ,
335
+ "via" : "192.168.100.10" ,
336
+ },
337
+ map [interface {}]interface {}{
338
+ "route" : "192.168.87.0/24" ,
339
+ "via" : []interface {}{
340
+ map [interface {}]interface {}{
341
+ "gateway" : "10.0.0.1" ,
342
+ },
343
+ map [interface {}]interface {}{
344
+ "gateway" : "10.0.0.2" ,
345
+ },
346
+ map [interface {}]interface {}{
347
+ "gateway" : "10.0.0.3" ,
348
+ },
349
+ },
350
+ },
351
+ map [interface {}]interface {}{
352
+ "route" : "192.168.89.0/24" ,
353
+ "via" : []interface {}{
354
+ map [interface {}]interface {}{
355
+ "gateway" : "10.0.0.1" ,
356
+ "weight" : 10 ,
357
+ },
358
+ map [interface {}]interface {}{
359
+ "gateway" : "10.0.0.2" ,
360
+ "weight" : 5 ,
361
+ },
362
+ },
363
+ },
364
+ },
365
+ }
335
366
336
367
routes , err := parseUnsafeRoutes (c , []netip.Prefix {n })
337
- assert .NoError (t , err )
338
- assert .Len (t , routes , 1 )
368
+ require .NoError (t , err )
369
+ assert .Len (t , routes , 3 )
339
370
routeTree , err := makeRouteTree (l , routes , true )
340
- assert .NoError (t , err )
371
+ require .NoError (t , err )
341
372
342
- ip , err := netip .ParseAddr ("1.0.0.2 " )
343
- assert .NoError (t , err )
373
+ ip , err := netip .ParseAddr ("192.168.86.1 " )
374
+ require .NoError (t , err )
344
375
r , ok := routeTree .Lookup (ip )
345
376
assert .True (t , ok )
346
377
347
- nips := []routing.Gateway {}
348
- nips = append (nips , routing .NewGateway (netip .MustParseAddr ("192.168.0.1" ), 1 ))
349
- nips = append (nips , routing .NewGateway (netip .MustParseAddr ("192.168.0.2" ), 1 ))
350
- nips = append (nips , routing .NewGateway (netip .MustParseAddr ("192.168.0.3" ), 1 ))
378
+ nip , err := netip .ParseAddr ("192.168.100.10" )
379
+ require .NoError (t , err )
380
+ assert .Equal (t , nip , r [0 ].Ip ())
381
+
382
+ ip , err = netip .ParseAddr ("192.168.87.1" )
383
+ require .NoError (t , err )
384
+ r , ok = routeTree .Lookup (ip )
385
+ assert .True (t , ok )
386
+
387
+ expectedGateways := []routing.Gateway {routing .NewGateway (netip .MustParseAddr ("10.0.0.1" ), 1 ),
388
+ routing .NewGateway (netip .MustParseAddr ("10.0.0.2" ), 1 ),
389
+ routing .NewGateway (netip .MustParseAddr ("10.0.0.3" ), 1 )}
390
+
391
+ routing .RebalanceGateways (expectedGateways )
392
+ assert .ElementsMatch (t , expectedGateways , r )
393
+
394
+ ip , err = netip .ParseAddr ("192.168.89.1" )
395
+ require .NoError (t , err )
396
+ r , ok = routeTree .Lookup (ip )
397
+ assert .True (t , ok )
351
398
352
- routing .RebalanceGateways (nips )
399
+ expectedGateways = []routing.Gateway {routing .NewGateway (netip .MustParseAddr ("10.0.0.1" ), 10 ),
400
+ routing .NewGateway (netip .MustParseAddr ("10.0.0.2" ), 5 )}
353
401
354
- assert .ElementsMatch (t , nips , r )
402
+ routing .RebalanceGateways (expectedGateways )
403
+ assert .ElementsMatch (t , expectedGateways , r )
355
404
}
0 commit comments