@@ -3,7 +3,6 @@ package overlay
33import (
44 "fmt"
55 "net/netip"
6- "reflect"
76 "testing"
87
98 "github.com/slackhq/nebula/config"
@@ -155,26 +154,44 @@ func Test_parseUnsafeRoutes(t *testing.T) {
155154
156155 // invalid via
157156 for _ , invalidValue := range []interface {}{
158- 127 , false , nil , 1.0 ,
157+ 127 , false , nil , 1.0 , [] string { "1" , "2" },
159158 } {
160159 c .Settings ["tun" ] = map [interface {}]interface {}{"unsafe_routes" : []interface {}{map [interface {}]interface {}{"via" : invalidValue }}}
161160 routes , err = parseUnsafeRoutes (c , []netip.Prefix {n })
162161 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 ))
164163 }
165164
166165 // Unparsable list of via
167166 c .Settings ["tun" ] = map [interface {}]interface {}{"unsafe_routes" : []interface {}{map [interface {}]interface {}{"via" : []string {"1" , "2" }}}}
168167 routes , err = parseUnsafeRoutes (c , []netip.Prefix {n })
169168 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" )
171170
172171 // unparsable via
173172 c .Settings ["tun" ] = map [interface {}]interface {}{"unsafe_routes" : []interface {}{map [interface {}]interface {}{"mtu" : "500" , "via" : "nope" }}}
174173 routes , err = parseUnsafeRoutes (c , []netip.Prefix {n })
175174 assert .Nil (t , routes )
176175 require .EqualError (t , err , "entry 1.via in tun.unsafe_routes failed to parse address: ParseAddr(\" nope\" ): unable to parse IP" )
177176
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+
178195 // missing route
179196 c .Settings ["tun" ] = map [interface {}]interface {}{"unsafe_routes" : []interface {}{map [interface {}]interface {}{"via" : "127.0.0.1" , "mtu" : "500" }}}
180197 routes , err = parseUnsafeRoutes (c , []netip.Prefix {n })
@@ -305,51 +322,83 @@ func Test_makeRouteTree(t *testing.T) {
305322 assert .False (t , ok )
306323}
307324
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 ) {
327326 l := test .NewLogger ()
328327 c := config .NewC (l )
329328 n , err := netip .ParsePrefix ("10.0.0.0/24" )
330- assert .NoError (t , err )
329+ require .NoError (t , err )
331330
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+ }
335366
336367 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 )
339370 routeTree , err := makeRouteTree (l , routes , true )
340- assert .NoError (t , err )
371+ require .NoError (t , err )
341372
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 )
344375 r , ok := routeTree .Lookup (ip )
345376 assert .True (t , ok )
346377
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 )
351398
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 )}
353401
354- assert .ElementsMatch (t , nips , r )
402+ routing .RebalanceGateways (expectedGateways )
403+ assert .ElementsMatch (t , expectedGateways , r )
355404}
0 commit comments