@@ -2,8 +2,9 @@ package middlewares
22
33import (
44 "context"
5- // "fmt"
5+ "fmt"
66 "testing"
7+ "reflect"
78
89 "github.com/Azure/azure-container-networking/cns"
910 "github.com/Azure/azure-container-networking/cns/configuration"
@@ -242,112 +243,112 @@ func TestGetSWIFTv2IPConfigFailure(t *testing.T) {
242243 assert .Error (t , err , errMTPNCNotReady .Error ())
243244}
244245
245- // func TestSetRoutesSuccess(t *testing.T) {
246- // middleware := K8sSWIFTv2Middleware{Cli: mock.NewClient()}
247- // t.Setenv(configuration.EnvPodCIDRs, "10.0.1.10/24,16A0:0010:AB00:001E::2/32")
248- // t.Setenv(configuration.EnvServiceCIDRs, "10.0.0.0/16,16A0:0010:AB00:0000::/32")
249- // t.Setenv(configuration.EnvInfraVNETCIDRs, "10.240.0.1/16,16A0:0020:AB00:0000::/32")
250-
251- // podIPInfo := []cns.PodIpInfo{
252- // {
253- // PodIPConfig: cns.IPSubnet{
254- // IPAddress: "10.0.1.10",
255- // PrefixLength: 32,
256- // },
257- // NICType: cns.InfraNIC,
258- // },
259- // {
260- // PodIPConfig: cns.IPSubnet{
261- // IPAddress: "2001:0db8:abcd:0015::0",
262- // PrefixLength: 64,
263- // },
264- // NICType: cns.InfraNIC,
265- // },
266- // {
267- // PodIPConfig: cns.IPSubnet{
268- // IPAddress: "20.240.1.242",
269- // PrefixLength: 32,
270- // },
271- // NICType: cns.DelegatedVMNIC,
272- // MacAddress: "12:34:56:78:9a:bc",
273- // },
274- // }
275- // desiredPodIPInfo := []cns.PodIpInfo{
276- // {
277- // PodIPConfig: cns.IPSubnet{
278- // IPAddress: "10.0.1.10",
279- // PrefixLength: 32,
280- // },
281- // NICType: cns.InfraNIC,
282- // Routes: []cns.Route{
283- // {
284- // IPAddress: "10.0.1.10/24",
285- // GatewayIPAddress: overlayGatewayv4,
286- // },
287- // {
288- // IPAddress: "10.0.0.0/16",
289- // GatewayIPAddress: overlayGatewayv4,
290- // },
291- // {
292- // IPAddress: "10.240.0.1/16",
293- // GatewayIPAddress: overlayGatewayv4,
294- // },
295- // },
296- // },
297- // {
298- // PodIPConfig: cns.IPSubnet{
299- // IPAddress: "2001:0db8:abcd:0015::0",
300- // PrefixLength: 64,
301- // },
302- // NICType: cns.InfraNIC,
303- // Routes: []cns.Route{
304- // {
305- // IPAddress: "16A0:0010:AB00:001E::2/32",
306- // GatewayIPAddress: overlayGatewayV6,
307- // },
308- // {
309- // IPAddress: "16A0:0010:AB00:0000::/32",
310- // GatewayIPAddress: overlayGatewayV6,
311- // },
312- // {
313- // IPAddress: "16A0:0020:AB00:0000::/32",
314- // GatewayIPAddress: overlayGatewayV6,
315- // },
316- // },
317- // },
318- // {
319- // PodIPConfig: cns.IPSubnet{
320- // IPAddress: "20.240.1.242",
321- // PrefixLength: 32,
322- // },
323- // NICType: cns.DelegatedVMNIC,
324- // MacAddress: "12:34:56:78:9a:bc",
325- // Routes: []cns.Route{
326- // {
327- // IPAddress: fmt.Sprintf("%s/%d", virtualGW, prefixLength),
328- // },
329- // {
330- // IPAddress: "0.0.0.0/0",
331- // GatewayIPAddress: virtualGW,
332- // },
333- // },
334- // },
335- // }
336- // for i := range podIPInfo {
337- // ipInfo := &podIPInfo[i]
338- // err := middleware.setRoutes(ipInfo)
339- // assert.Equal(t, err, nil)
340- // if ipInfo.NICType == cns.InfraNIC {
341- // assert.Equal(t, ipInfo.SkipDefaultRoutes, true)
342- // } else {
343- // assert.Equal(t, ipInfo.SkipDefaultRoutes, false)
344- // }
345-
346- // }
347- // for i := range podIPInfo {
348- // assert .DeepEqual(t, podIPInfo[i].Routes, desiredPodIPInfo[i].Routes)
349- // }
350- // }
246+ func TestSetRoutesSuccess (t * testing.T ) {
247+ middleware := K8sSWIFTv2Middleware {Cli : mock .NewClient ()}
248+ t .Setenv (configuration .EnvPodCIDRs , "10.0.1.10/24,16A0:0010:AB00:001E::2/32" )
249+ t .Setenv (configuration .EnvServiceCIDRs , "10.0.0.0/16,16A0:0010:AB00:0000::/32" )
250+ t .Setenv (configuration .EnvInfraVNETCIDRs , "10.240.0.1/16,16A0:0020:AB00:0000::/32" )
251+
252+ podIPInfo := []cns.PodIpInfo {
253+ {
254+ PodIPConfig : cns.IPSubnet {
255+ IPAddress : "10.0.1.10" ,
256+ PrefixLength : 32 ,
257+ },
258+ NICType : cns .InfraNIC ,
259+ },
260+ {
261+ PodIPConfig : cns.IPSubnet {
262+ IPAddress : "2001:0db8:abcd:0015::0" ,
263+ PrefixLength : 64 ,
264+ },
265+ NICType : cns .InfraNIC ,
266+ },
267+ {
268+ PodIPConfig : cns.IPSubnet {
269+ IPAddress : "20.240.1.242" ,
270+ PrefixLength : 32 ,
271+ },
272+ NICType : cns .DelegatedVMNIC ,
273+ MacAddress : "12:34:56:78:9a:bc" ,
274+ },
275+ }
276+ desiredPodIPInfo := []cns.PodIpInfo {
277+ {
278+ PodIPConfig : cns.IPSubnet {
279+ IPAddress : "10.0.1.10" ,
280+ PrefixLength : 32 ,
281+ },
282+ NICType : cns .InfraNIC ,
283+ Routes : []cns.Route {
284+ {
285+ IPAddress : "10.0.1.10/24" ,
286+ GatewayIPAddress : overlayGatewayv4 ,
287+ },
288+ {
289+ IPAddress : "10.0.0.0/16" ,
290+ GatewayIPAddress : overlayGatewayv4 ,
291+ },
292+ {
293+ IPAddress : "10.240.0.1/16" ,
294+ GatewayIPAddress : overlayGatewayv4 ,
295+ },
296+ },
297+ },
298+ {
299+ PodIPConfig : cns.IPSubnet {
300+ IPAddress : "2001:0db8:abcd:0015::0" ,
301+ PrefixLength : 64 ,
302+ },
303+ NICType : cns .InfraNIC ,
304+ Routes : []cns.Route {
305+ {
306+ IPAddress : "16A0:0010:AB00:001E::2/32" ,
307+ GatewayIPAddress : overlayGatewayV6 ,
308+ },
309+ {
310+ IPAddress : "16A0:0010:AB00:0000::/32" ,
311+ GatewayIPAddress : overlayGatewayV6 ,
312+ },
313+ {
314+ IPAddress : "16A0:0020:AB00:0000::/32" ,
315+ GatewayIPAddress : overlayGatewayV6 ,
316+ },
317+ },
318+ },
319+ {
320+ PodIPConfig : cns.IPSubnet {
321+ IPAddress : "20.240.1.242" ,
322+ PrefixLength : 32 ,
323+ },
324+ NICType : cns .DelegatedVMNIC ,
325+ MacAddress : "12:34:56:78:9a:bc" ,
326+ Routes : []cns.Route {
327+ {
328+ IPAddress : fmt .Sprintf ("%s/%d" , virtualGW , prefixLength ),
329+ },
330+ {
331+ IPAddress : "0.0.0.0/0" ,
332+ GatewayIPAddress : virtualGW ,
333+ },
334+ },
335+ },
336+ }
337+ for i := range podIPInfo {
338+ ipInfo := & podIPInfo [i ]
339+ err := middleware .setRoutes (ipInfo )
340+ assert .Equal (t , err , nil )
341+ if ipInfo .NICType == cns .InfraNIC {
342+ assert .Equal (t , ipInfo .SkipDefaultRoutes , true )
343+ } else {
344+ assert .Equal (t , ipInfo .SkipDefaultRoutes , false )
345+ }
346+ }
347+
348+ for i := range podIPInfo {
349+ reflect .DeepEqual (podIPInfo [i ].Routes , desiredPodIPInfo [i ].Routes )
350+ }
351+ }
351352
352353func TestSetRoutesFailure (t * testing.T ) {
353354 // Failure due to env var not set
0 commit comments