Skip to content

Commit 8806489

Browse files
add test scenario
1 parent 7de69b1 commit 8806489

File tree

2 files changed

+59
-10
lines changed

2 files changed

+59
-10
lines changed

azure-ipam/ipam_test.go

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,33 @@ func (c *MockCNSClient) RequestIPAddress(ctx context.Context, ipconfig cns.IPCon
5858
},
5959
}
6060
return result, nil
61+
case "nilGateway":
62+
result := &cns.IPConfigResponse{
63+
PodIpInfo: cns.PodIpInfo{
64+
PodIPConfig: cns.IPSubnet{
65+
IPAddress: "10.0.1.10",
66+
PrefixLength: 24,
67+
},
68+
NetworkContainerPrimaryIPConfig: cns.IPConfiguration{
69+
IPSubnet: cns.IPSubnet{
70+
IPAddress: "10.0.1.0",
71+
PrefixLength: 24,
72+
},
73+
DNSServers: nil,
74+
GatewayIPAddress: "", // nil/empty gateway
75+
},
76+
HostPrimaryIPInfo: cns.HostIPInfo{
77+
Gateway: "",
78+
PrimaryIP: "10.0.0.1",
79+
Subnet: "10.0.0.0/24",
80+
},
81+
},
82+
Response: cns.Response{
83+
ReturnCode: 0,
84+
Message: "",
85+
},
86+
}
87+
return result, nil
6188
default:
6289
result := &cns.IPConfigResponse{
6390
PodIpInfo: cns.PodIpInfo{
@@ -92,7 +119,7 @@ func (c *MockCNSClient) RequestIPs(ctx context.Context, ipconfig cns.IPConfigsRe
92119
switch ipconfig.InfraContainerID {
93120
case "failRequestCNSArgs":
94121
return nil, errFoo
95-
case "happyArgsSingle", "failProcessCNSRespSingleIP", "failRequestCNSArgsSingleIP":
122+
case "happyArgsSingle", "failProcessCNSRespSingleIP", "failRequestCNSArgsSingleIP", "nilGateway":
96123
e := &client.CNSClientError{}
97124
e.Code = types.UnsupportedAPI
98125
e.Err = errUnsupportedAPI
@@ -129,8 +156,8 @@ func (c *MockCNSClient) RequestIPs(ctx context.Context, ipconfig cns.IPConfigsRe
129156
IPAddress: "fd11:1234::",
130157
PrefixLength: 112,
131158
},
132-
DNSServers: nil,
133-
GatewayIPAddress: "fe80::1234:5678:9abc",
159+
DNSServers: nil,
160+
GatewayIPv6Address: "fe80::1234:5678:9abc",
134161
},
135162
HostPrimaryIPInfo: cns.HostIPInfo{
136163
Gateway: "fe80::1234:5678:9abc",
@@ -177,8 +204,8 @@ func (c *MockCNSClient) RequestIPs(ctx context.Context, ipconfig cns.IPConfigsRe
177204
IPAddress: "fd11:1234::",
178205
PrefixLength: 120,
179206
},
180-
DNSServers: nil,
181-
GatewayIPAddress: "fe80::1234:5678:9abc",
207+
DNSServers: nil,
208+
GatewayIPv6Address: "fe80::1234:5678:9abc",
182209
},
183210
HostPrimaryIPInfo: cns.HostIPInfo{
184211
Gateway: "fe80::1234:5678:9abc",
@@ -287,6 +314,7 @@ func TestCmdAdd(t *testing.T) {
287314
IP: net.IPv4(10, 0, 1, 10),
288315
Mask: net.CIDRMask(24, 32),
289316
},
317+
Gateway: net.IPv4(10, 0, 0, 1),
290318
},
291319
},
292320
DNS: cniTypes.DNS{},
@@ -304,12 +332,31 @@ func TestCmdAdd(t *testing.T) {
304332
IP: net.IPv4(10, 0, 1, 10),
305333
Mask: net.CIDRMask(24, 32),
306334
},
335+
Gateway: net.IPv4(10, 0, 0, 1),
307336
},
308337
{
309338
Address: net.IPNet{
310339
IP: net.ParseIP("fd11:1234::1"),
311340
Mask: net.CIDRMask(120, 128),
312341
},
342+
Gateway: net.ParseIP("fe80::1234:5678:9abc"),
343+
},
344+
},
345+
DNS: cniTypes.DNS{},
346+
},
347+
wantErr: false,
348+
},
349+
{
350+
name: "CNI add with nil gateway IP",
351+
args: buildArgs("nilGateway", happyPodArgs, happyNetConfByteArr),
352+
want: &types100.Result{
353+
CNIVersion: "1.0.0",
354+
IPs: []*types100.IPConfig{
355+
{
356+
Address: net.IPNet{
357+
IP: net.IPv4(10, 0, 1, 10),
358+
Mask: net.CIDRMask(24, 32),
359+
},
313360
},
314361
},
315362
DNS: cniTypes.DNS{},

azure-ipam/ipconfig/ipconfig.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,16 @@ func ProcessIPConfigsResp(resp *cns.IPConfigsResponse) (*[]netip.Prefix, *[]net.
8484

8585
var gatewayStr string
8686
if podIPNet.Addr().Is4() {
87-
gatewayStr = podInfo.NetworkContainerPrimaryIPConfig.GatewayIPAddress
87+
gatewayStr = resp.PodIPInfo[i].NetworkContainerPrimaryIPConfig.GatewayIPAddress
8888
} else if podIPNet.Addr().Is6() {
89-
gatewayStr = podInfo.NetworkContainerPrimaryIPConfig.GatewayIPv6Address
89+
gatewayStr = resp.PodIPInfo[i].NetworkContainerPrimaryIPConfig.GatewayIPv6Address
9090
}
9191

92-
gatewayIP := net.ParseIP(gatewayStr)
93-
if gatewayIP == nil {
94-
return nil, nil, errors.Errorf("failed to parse gateway IP %q for pod ip %s", gatewayStr, podInfo.PodIPConfig.IPAddress)
92+
if gatewayStr != "" {
93+
gatewayIP = net.ParseIP(gatewayStr)
94+
if gatewayIP == nil {
95+
return nil, nil, errors.Errorf("failed to parse gateway IP %q for pod ip %s", gatewayStr, resp.PodIPInfo[i].PodIPConfig.IPAddress)
96+
}
9597
}
9698
gatewaysIPs[i] = gatewayIP
9799
}

0 commit comments

Comments
 (0)