Skip to content

Commit 7cc5010

Browse files
committed
feat: add tests for SkipDefaultRoutes handling in network container requests
1 parent 776430c commit 7cc5010

File tree

3 files changed

+113
-4
lines changed

3 files changed

+113
-4
lines changed

cns/NetworkContainerContract_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,3 +240,40 @@ func TestPostNetworkContainersRequest_Validate(t *testing.T) {
240240
})
241241
}
242242
}
243+
244+
func TestCreateNetworkContainerRequest_SkipDefaultRoutes(t *testing.T) {
245+
tests := []struct {
246+
name string
247+
req CreateNetworkContainerRequest
248+
expected bool
249+
}{
250+
{
251+
name: "SkipDefaultRoutesTrue",
252+
req: CreateNetworkContainerRequest{
253+
NetworkContainerid: "f47ac10b-58cc-0372-8567-0e02b2c3d479",
254+
SkipDefaultRoutes: true,
255+
},
256+
expected: true,
257+
},
258+
{
259+
name: "SkipDefaultRoutesFalse",
260+
req: CreateNetworkContainerRequest{
261+
NetworkContainerid: "f47ac10b-58cc-0372-8567-0e02b2c3d479",
262+
SkipDefaultRoutes: false,
263+
},
264+
expected: false,
265+
},
266+
{
267+
name: "SkipDefaultRoutesIgnored",
268+
req: CreateNetworkContainerRequest{
269+
NetworkContainerid: "f47ac10b-58cc-0372-8567-0e02b2c3d479",
270+
},
271+
expected: false,
272+
},
273+
}
274+
for _, tt := range tests {
275+
t.Run(tt.name, func(t *testing.T) {
276+
assert.Equal(t, tt.expected, tt.req.SkipDefaultRoutes, "SkipDefaultRoutes value should match expected")
277+
})
278+
}
279+
}

network/transparent_vlan_endpointclient_linux.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -649,11 +649,11 @@ func (client *TransparentVlanEndpointClient) addDefaultRoutes(linkToName string,
649649
}
650650

651651
// Helper that creates routing rules for the current NS which direct packets
652-
// to the virtual gateway ip on linkToName device interface
653-
// Route 1: 169.254.2.1 dev <linkToName>
654-
// Route 2: default via 169.254.2.1 dev <linkToName>
652+
// to the subnet gateway ip on linkToName device interface
653+
// Route 1: <gatewayIP> dev <linkToName>
654+
// Route 2: <subnetCIDR> via <gatewayIP> dev <linkToName>
655655
func (client *TransparentVlanEndpointClient) addCustomRoutes(linkToName string, gatewayIP net.IP, subnetCIDR net.IPNet, table int) error {
656-
// Add route for virtualgwip (ip route add <gatewayIP> dev <linkToName>)
656+
// Add route for subnetgwIP (ip route add <gatewayIP> dev <linkToName>)
657657
gWIP, gwNet, _ := net.ParseCIDR(gatewayIP.String() + "/32")
658658
routeInfo := RouteInfo{
659659
Dst: *gwNet,

network/transparent_vlan_endpointclient_linux_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,74 @@ func TestTransparentVlanConfigureContainerInterfacesAndRoutes(t *testing.T) {
867867
wantErr: true,
868868
wantErrMsg: "failed container ns add default routes: addRoutes failed: " + netio.ErrMockNetIOFail.Error() + ":B1veth0",
869869
},
870+
{
871+
name: "Configure interface and routes good path with SkipDefaultRoutes set to true for container",
872+
client: &TransparentVlanEndpointClient{
873+
primaryHostIfName: "eth0",
874+
vlanIfName: "eth0.1",
875+
vnetVethName: "A1veth0",
876+
containerVethName: "B1veth0",
877+
vnetNSName: "az_ns_1",
878+
vnetMac: vnetMac,
879+
netlink: netlink.NewMockNetlink(false, ""),
880+
plClient: platform.NewMockExecClient(false),
881+
netUtilsClient: networkutils.NewNetworkUtils(nl, plc),
882+
netioshim: netio.NewMockNetIO(false, 0),
883+
},
884+
epInfo: &EndpointInfo{
885+
SkipDefaultRoutes: true,
886+
IPAddresses: []net.IPNet{
887+
{
888+
IP: net.ParseIP("192.168.0.4"),
889+
Mask: net.CIDRMask(subnetv4Mask, ipv4Bits),
890+
},
891+
},
892+
Subnets: []SubnetInfo{
893+
{
894+
Gateway: net.ParseIP("192.168.0.1"),
895+
Prefix: net.IPNet{
896+
IP: net.ParseIP("192.168.0.0"),
897+
Mask: net.CIDRMask(subnetv4Mask, ipv4Bits),
898+
},
899+
},
900+
},
901+
},
902+
wantErr: false,
903+
},
904+
{
905+
name: "Configure interface and routes good path with SkipDefaultRoutes set to false for container",
906+
client: &TransparentVlanEndpointClient{
907+
primaryHostIfName: "eth0",
908+
vlanIfName: "eth0.1",
909+
vnetVethName: "A1veth0",
910+
containerVethName: "B1veth0",
911+
vnetNSName: "az_ns_1",
912+
vnetMac: vnetMac,
913+
netlink: netlink.NewMockNetlink(false, ""),
914+
plClient: platform.NewMockExecClient(false),
915+
netUtilsClient: networkutils.NewNetworkUtils(nl, plc),
916+
netioshim: netio.NewMockNetIO(false, 0),
917+
},
918+
epInfo: &EndpointInfo{
919+
SkipDefaultRoutes: true,
920+
IPAddresses: []net.IPNet{
921+
{
922+
IP: net.ParseIP("192.168.0.4"),
923+
Mask: net.CIDRMask(subnetv4Mask, ipv4Bits),
924+
},
925+
},
926+
Subnets: []SubnetInfo{
927+
{
928+
Gateway: net.ParseIP("192.168.0.1"),
929+
Prefix: net.IPNet{
930+
IP: net.ParseIP("192.168.0.0"),
931+
Mask: net.CIDRMask(subnetv4Mask, ipv4Bits),
932+
},
933+
},
934+
},
935+
},
936+
wantErr: false,
937+
},
870938
}
871939

872940
for _, tt := range tests {
@@ -1008,3 +1076,7 @@ func TestRunWithRetries(t *testing.T) {
10081076
})
10091077
}
10101078
}
1079+
1080+
func TestAddCustomRoutes(t *testing.T) {
1081+
1082+
}

0 commit comments

Comments
 (0)