@@ -187,8 +187,28 @@ func getIPNetWithString(ipaddrwithcidr string) *net.IPNet {
187187 return ipnet
188188}
189189
190+ package < yourpkg> // adjust
191+
192+ import (
193+ "net"
194+ "testing"
195+
196+ cni "github.com/Azure/azure-container-networking/cni"
197+ cniTypesCurr "github.com/containernetworking/cni/pkg/types/100"
198+ "github.com/stretchr/testify/require"
199+
200+ "github.com/Azure/azure-container-networking/cns"
201+ "github.com/Azure/azure-container-networking/network"
202+ )
203+
204+ func defaultIPNetV4 () * net.IPNet {
205+ _ , ipn , _ := net .ParseCIDR ("0.0.0.0/0" )
206+ return ipn
207+ }
208+
190209func TestSetupRoutingForMultitenancy (t * testing.T ) {
191210 require := require .New (t ) //nolint:gocritic
211+
192212 type args struct {
193213 nwCfg * cni.NetworkConfig
194214 cnsNetworkConfig * cns.GetNetworkContainerResponse
@@ -204,57 +224,99 @@ func TestSetupRoutingForMultitenancy(t *testing.T) {
204224 expected args
205225 }{
206226 {
207- name : "test happy path " ,
227+ name : "adds default v4 route when SNAT disabled and SkipDefaultRoutes=false " ,
208228 args : args {
209229 nwCfg : & cni.NetworkConfig {
210230 MultiTenancy : true ,
211231 EnableSnatOnHost : false ,
212232 },
213233 cnsNetworkConfig : & cns.GetNetworkContainerResponse {
214234 IPConfiguration : cns.IPConfiguration {
215- IPSubnet : cns.IPSubnet {},
216- DNSServers : nil ,
217235 GatewayIPAddress : "10.0.0.1" ,
218236 },
219237 },
220- epInfo : & network.EndpointInfo {},
238+ epInfo : & network.EndpointInfo {}, // SkipDefaultRoutes defaults to false
221239 result : & network.InterfaceInfo {},
222240 },
241+ multitenancyClient : & Multitenancy {},
223242 expected : args {
224243 nwCfg : & cni.NetworkConfig {
225244 MultiTenancy : true ,
226245 EnableSnatOnHost : false ,
227246 },
228247 cnsNetworkConfig : & cns.GetNetworkContainerResponse {
229248 IPConfiguration : cns.IPConfiguration {
230- IPSubnet : cns.IPSubnet {},
231- DNSServers : nil ,
232249 GatewayIPAddress : "10.0.0.1" ,
233250 },
234251 },
235252 epInfo : & network.EndpointInfo {
236253 Routes : []network.RouteInfo {
237254 {
238- Dst : net.IPNet {IP : net .ParseIP ("0.0.0.0" ), Mask : defaultIPNet ().Mask },
255+ Dst : net.IPNet {IP : net .ParseIP ("0.0.0.0" ), Mask : defaultIPNetV4 ().Mask },
239256 Gw : net .ParseIP ("10.0.0.1" ),
240257 },
241258 },
242259 },
243260 result : & network.InterfaceInfo {
244261 Routes : []network.RouteInfo {
245262 {
246- Dst : net.IPNet {IP : net .ParseIP ("0.0.0.0" ), Mask : defaultIPNet ().Mask },
263+ Dst : net.IPNet {IP : net .ParseIP ("0.0.0.0" ), Mask : defaultIPNetV4 ().Mask },
247264 Gw : net .ParseIP ("10.0.0.1" ),
248265 },
249266 },
250267 },
251268 },
252269 },
270+ {
271+ name : "does not add default route when SkipDefaultRoutes=true" ,
272+ args : args {
273+ nwCfg : & cni.NetworkConfig {
274+ MultiTenancy : true ,
275+ EnableSnatOnHost : false ,
276+ },
277+ cnsNetworkConfig : & cns.GetNetworkContainerResponse {
278+ IPConfiguration : cns.IPConfiguration {
279+ GatewayIPAddress : "10.0.0.1" ,
280+ },
281+ },
282+ epInfo : & network.EndpointInfo {
283+ SkipDefaultRoutes : true ,
284+ },
285+ result : & network.InterfaceInfo {},
286+ },
287+ multitenancyClient : & Multitenancy {},
288+ expected : args {
289+ nwCfg : & cni.NetworkConfig {
290+ MultiTenancy : true ,
291+ EnableSnatOnHost : false ,
292+ },
293+ cnsNetworkConfig : & cns.GetNetworkContainerResponse {
294+ IPConfiguration : cns.IPConfiguration {
295+ GatewayIPAddress : "10.0.0.1" ,
296+ },
297+ },
298+ epInfo : & network.EndpointInfo {
299+ SkipDefaultRoutes : true ,
300+ Routes : nil , // unchanged
301+ },
302+ result : & network.InterfaceInfo {
303+ Routes : nil , // unchanged
304+ },
305+ },
306+ },
253307 }
308+
254309 for _ , tt := range tests {
255310 tt := tt
256311 t .Run (tt .name , func (t * testing.T ) {
257- tt .multitenancyClient .SetupRoutingForMultitenancy (tt .args .nwCfg , tt .args .cnsNetworkConfig , tt .args .azIpamResult , tt .args .epInfo , tt .args .result )
312+ tt .multitenancyClient .SetupRoutingForMultitenancy (
313+ tt .args .nwCfg ,
314+ tt .args .cnsNetworkConfig ,
315+ tt .args .azIpamResult ,
316+ tt .args .epInfo ,
317+ tt .args .result ,
318+ )
319+
258320 require .Exactly (tt .expected .nwCfg , tt .args .nwCfg )
259321 require .Exactly (tt .expected .cnsNetworkConfig , tt .args .cnsNetworkConfig )
260322 require .Exactly (tt .expected .azIpamResult , tt .args .azIpamResult )
0 commit comments