Skip to content

Commit

Permalink
add an ut
Browse files Browse the repository at this point in the history
  • Loading branch information
paulyufan2 committed Sep 25, 2024
1 parent 5424407 commit 9fe4da9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
1 change: 1 addition & 0 deletions cni/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 1 addition & 7 deletions cns/middlewares/k8sSwiftV2_windows.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
}

Expand All @@ -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
}
16 changes: 15 additions & 1 deletion cns/middlewares/k8sSwiftV2_windows_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package middlewares

import (
"reflect"
"testing"

"github.com/Azure/azure-container-networking/cns"
Expand Down Expand Up @@ -53,16 +54,29 @@ 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)
// assert that the function for windows modifies all the expected fields with prefix-length
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)
}
}

0 comments on commit 9fe4da9

Please sign in to comment.