From 9fe4da98b3be87d512a5b0b4952ebb6ae0db53bb Mon Sep 17 00:00:00 2001 From: paulyufan2 Date: Tue, 24 Sep 2024 21:15:26 -0400 Subject: [PATCH] add an ut --- cni/network/network.go | 1 + cns/middlewares/k8sSwiftV2_windows.go | 8 +------- cns/middlewares/k8sSwiftV2_windows_test.go | 16 +++++++++++++++- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/cni/network/network.go b/cni/network/network.go index 0f0ce23dbb1..cf1677d023a 100644 --- a/cni/network/network.go +++ b/cni/network/network.go @@ -856,6 +856,7 @@ func (plugin *NetPlugin) createEpInfo(opt *createEpInfoOpt) (*network.EndpointIn } setEndpointOptions(opt.cnsNetworkConfig, &endpointInfo, vethName) + logger.Info("Generated endpoint info from fields", zap.String("epInfo", endpointInfo.PrettyString())) // now our ep info should have the full combined information from both the network and endpoint structs diff --git a/cns/middlewares/k8sSwiftV2_windows.go b/cns/middlewares/k8sSwiftV2_windows.go index 54960aaea32..8ceb97f89d4 100644 --- a/cns/middlewares/k8sSwiftV2_windows.go +++ b/cns/middlewares/k8sSwiftV2_windows.go @@ -1,8 +1,6 @@ package middlewares import ( - "fmt" - "github.com/Azure/azure-container-networking/cns" "github.com/Azure/azure-container-networking/cns/logger" "github.com/Azure/azure-container-networking/cns/middlewares/utils" @@ -17,7 +15,6 @@ func (k *K8sSWIFTv2Middleware) setRoutes(podIPInfo *cns.PodIpInfo) error { logger.Printf("[SWIFTv2Middleware] skip setting default route on InfraNIC interface") podIPInfo.SkipDefaultRoutes = true } - return nil } @@ -44,14 +41,11 @@ func (k *K8sSWIFTv2Middleware) assignSubnetPrefixLengthFields(podIPInfo *cns.Pod GatewayIPAddress: interfaceInfo.GatewayIP, } // assign default route - defaultRoute := cns.Route{ - IPAddress: fmt.Sprintf("%s/%d", interfaceInfo.GatewayIP, prefixLength), - } route := cns.Route{ IPAddress: "0.0.0.0/0", GatewayIPAddress: interfaceInfo.GatewayIP, } - podIPInfo.Routes = append(podIPInfo.Routes, defaultRoute, route) + podIPInfo.Routes = append(podIPInfo.Routes, route) return nil } diff --git a/cns/middlewares/k8sSwiftV2_windows_test.go b/cns/middlewares/k8sSwiftV2_windows_test.go index 945d650ae9f..6df52494e26 100644 --- a/cns/middlewares/k8sSwiftV2_windows_test.go +++ b/cns/middlewares/k8sSwiftV2_windows_test.go @@ -1,6 +1,7 @@ package middlewares import ( + "reflect" "testing" "github.com/Azure/azure-container-networking/cns" @@ -53,11 +54,19 @@ func TestAssignSubnetPrefixSuccess(t *testing.T) { MacAddress: "12:34:56:78:9a:bc", } + gatewayIP := "20.240.1.1" intInfo := v1alpha1.InterfaceInfo{ - GatewayIP: "20.240.1.1", + GatewayIP: gatewayIP, SubnetAddressSpace: "20.240.1.0/16", } + routes := []cns.Route{ + { + IPAddress: "0.0.0.0/0", + GatewayIPAddress: gatewayIP, + }, + } + ipInfo := podIPInfo err := middleware.assignSubnetPrefixLengthFields(&ipInfo, intInfo, ipInfo.PodIPConfig.IPAddress) assert.Equal(t, err, nil) @@ -65,4 +74,9 @@ func TestAssignSubnetPrefixSuccess(t *testing.T) { assert.Equal(t, ipInfo.PodIPConfig.PrefixLength, uint8(16)) assert.Equal(t, ipInfo.HostPrimaryIPInfo.Gateway, intInfo.GatewayIP) assert.Equal(t, ipInfo.HostPrimaryIPInfo.Subnet, intInfo.SubnetAddressSpace) + + // compare two slices of routes + if !reflect.DeepEqual(ipInfo.Routes, routes) { + t.Errorf("got '%+v', expected '%+v'", ipInfo.Routes, routes) + } }