@@ -2,10 +2,13 @@ package network
22
33import (
44 "errors"
5+ "fmt"
56 "net"
7+ "runtime"
68 "testing"
79
810 "github.com/Azure/azure-container-networking/cni"
11+ "github.com/Azure/azure-container-networking/cni/util"
912 "github.com/Azure/azure-container-networking/cns"
1013 "github.com/Azure/azure-container-networking/iptables"
1114 "github.com/Azure/azure-container-networking/network"
@@ -25,19 +28,29 @@ func getTestIPConfigRequest() cns.IPConfigRequest {
2528 }
2629}
2730
31+ func getTestOverlayGateway () net.IP {
32+ if runtime .GOOS == "windows" {
33+ return net .ParseIP ("10.240.0.1" )
34+ }
35+
36+ return net .ParseIP ("169.254.1.1" )
37+ }
38+
2839func TestCNSIPAMInvoker_Add (t * testing.T ) {
2940 require := require .New (t ) //nolint further usage of require without passing t
3041 type fields struct {
3142 podName string
3243 podNamespace string
3344 cnsClient cnsclient
45+ ipamMode util.IpamMode
3446 }
3547 type args struct {
3648 nwCfg * cni.NetworkConfig
3749 args * cniSkel.CmdArgs
3850 hostSubnetPrefix * net.IPNet
3951 options map [string ]interface {}
4052 }
53+
4154 tests := []struct {
4255 name string
4356 fields fields
@@ -127,6 +140,76 @@ func TestCNSIPAMInvoker_Add(t *testing.T) {
127140 },
128141 wantErr : true ,
129142 },
143+ {
144+ name : "Test happy CNI Overlay add" ,
145+ fields : fields {
146+ podName : testPodInfo .PodName ,
147+ podNamespace : testPodInfo .PodNamespace ,
148+ ipamMode : util .V4Overlay ,
149+ cnsClient : & MockCNSClient {
150+ require : require ,
151+ request : requestIPAddressHandler {
152+ ipconfigArgument : cns.IPConfigRequest {
153+ PodInterfaceID : "testcont-testifname3" ,
154+ InfraContainerID : "testcontainerid3" ,
155+ OrchestratorContext : marshallPodInfo (testPodInfo ),
156+ },
157+ result : & cns.IPConfigResponse {
158+ PodIpInfo : cns.PodIpInfo {
159+ PodIPConfig : cns.IPSubnet {
160+ IPAddress : "10.240.1.242" ,
161+ PrefixLength : 16 ,
162+ },
163+ NetworkContainerPrimaryIPConfig : cns.IPConfiguration {
164+ IPSubnet : cns.IPSubnet {
165+ IPAddress : "10.240.1.0" ,
166+ PrefixLength : 16 ,
167+ },
168+ DNSServers : nil ,
169+ GatewayIPAddress : "" ,
170+ },
171+ HostPrimaryIPInfo : cns.HostIPInfo {
172+ Gateway : "10.224.0.1" ,
173+ PrimaryIP : "10.224.0.5" ,
174+ Subnet : "10.224.0.0/16" ,
175+ },
176+ },
177+ Response : cns.Response {
178+ ReturnCode : 0 ,
179+ Message : "" ,
180+ },
181+ },
182+ err : nil ,
183+ },
184+ },
185+ },
186+ args : args {
187+ nwCfg : & cni.NetworkConfig {},
188+ args : & cniSkel.CmdArgs {
189+ ContainerID : "testcontainerid3" ,
190+ Netns : "testnetns3" ,
191+ IfName : "testifname3" ,
192+ },
193+ hostSubnetPrefix : getCIDRNotationForAddress ("10.224.0.0/16" ),
194+ options : map [string ]interface {}{},
195+ },
196+ want : & cniTypesCurr.Result {
197+ IPs : []* cniTypesCurr.IPConfig {
198+ {
199+ Address : * getCIDRNotationForAddress ("10.240.1.242/16" ),
200+ Gateway : getTestOverlayGateway (),
201+ },
202+ },
203+ Routes : []* cniTypes.Route {
204+ {
205+ Dst : network .Ipv4DefaultRouteDstPrefix ,
206+ GW : getTestOverlayGateway (),
207+ },
208+ },
209+ },
210+ want1 : nil ,
211+ wantErr : false ,
212+ },
130213 }
131214 for _ , tt := range tests {
132215 tt := tt
@@ -136,13 +219,17 @@ func TestCNSIPAMInvoker_Add(t *testing.T) {
136219 podNamespace : tt .fields .podNamespace ,
137220 cnsClient : tt .fields .cnsClient ,
138221 }
222+ if tt .fields .ipamMode != "" {
223+ invoker .ipamMode = tt .fields .ipamMode
224+ }
139225 ipamAddResult , err := invoker .Add (IPAMAddConfig {nwCfg : tt .args .nwCfg , args : tt .args .args , options : tt .args .options })
140226 if tt .wantErr {
141227 require .Error (err )
142228 } else {
143229 require .NoError (err )
144230 }
145231
232+ fmt .Printf ("want:%+v\n rest:%+v\n " , tt .want , ipamAddResult .ipv4Result )
146233 require .Equalf (tt .want , ipamAddResult .ipv4Result , "incorrect ipv4 response" )
147234 require .Equalf (tt .want1 , ipamAddResult .ipv6Result , "incorrect ipv6 response" )
148235 })
0 commit comments