@@ -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,12 +28,21 @@ 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
@@ -127,6 +139,76 @@ func TestCNSIPAMInvoker_Add(t *testing.T) {
127139 },
128140 wantErr : true ,
129141 },
142+ {
143+ name : "Test happy CNI Overlay add" ,
144+ fields : fields {
145+ podName : testPodInfo .PodName ,
146+ podNamespace : testPodInfo .PodNamespace ,
147+ ipamMode : util .V4Overlay ,
148+ cnsClient : & MockCNSClient {
149+ require : require ,
150+ request : requestIPAddressHandler {
151+ ipconfigArgument : cns.IPConfigRequest {
152+ PodInterfaceID : "testcont-testifname3" ,
153+ InfraContainerID : "testcontainerid3" ,
154+ OrchestratorContext : marshallPodInfo (testPodInfo ),
155+ },
156+ result : & cns.IPConfigResponse {
157+ PodIpInfo : cns.PodIpInfo {
158+ PodIPConfig : cns.IPSubnet {
159+ IPAddress : "10.240.1.242" ,
160+ PrefixLength : 16 ,
161+ },
162+ NetworkContainerPrimaryIPConfig : cns.IPConfiguration {
163+ IPSubnet : cns.IPSubnet {
164+ IPAddress : "10.240.1.0" ,
165+ PrefixLength : 16 ,
166+ },
167+ DNSServers : nil ,
168+ GatewayIPAddress : "" ,
169+ },
170+ HostPrimaryIPInfo : cns.HostIPInfo {
171+ Gateway : "10.224.0.1" ,
172+ PrimaryIP : "10.224.0.5" ,
173+ Subnet : "10.224.0.0/16" ,
174+ },
175+ },
176+ Response : cns.Response {
177+ ReturnCode : 0 ,
178+ Message : "" ,
179+ },
180+ },
181+ err : nil ,
182+ },
183+ },
184+ },
185+ args : args {
186+ nwCfg : & cni.NetworkConfig {},
187+ args : & cniSkel.CmdArgs {
188+ ContainerID : "testcontainerid3" ,
189+ Netns : "testnetns3" ,
190+ IfName : "testifname3" ,
191+ },
192+ hostSubnetPrefix : getCIDRNotationForAddress ("10.224.0.0/16" ),
193+ options : map [string ]interface {}{},
194+ },
195+ want : & cniTypesCurr.Result {
196+ IPs : []* cniTypesCurr.IPConfig {
197+ {
198+ Address : * getCIDRNotationForAddress ("10.240.1.242/16" ),
199+ Gateway : getTestOverlayGateway (),
200+ },
201+ },
202+ Routes : []* cniTypes.Route {
203+ {
204+ Dst : network .Ipv4DefaultRouteDstPrefix ,
205+ GW : getTestOverlayGateway (),
206+ },
207+ },
208+ },
209+ want1 : nil ,
210+ wantErr : false ,
211+ },
130212 }
131213 for _ , tt := range tests {
132214 tt := tt
@@ -136,13 +218,17 @@ func TestCNSIPAMInvoker_Add(t *testing.T) {
136218 podNamespace : tt .fields .podNamespace ,
137219 cnsClient : tt .fields .cnsClient ,
138220 }
221+ if tt .fields .ipamMode != "" {
222+ invoker .ipamMode = tt .fields .ipamMode
223+ }
139224 ipamAddResult , err := invoker .Add (IPAMAddConfig {nwCfg : tt .args .nwCfg , args : tt .args .args , options : tt .args .options })
140225 if tt .wantErr {
141226 require .Error (err )
142227 } else {
143228 require .NoError (err )
144229 }
145230
231+ fmt .Printf ("want:%+v\n rest:%+v\n " , tt .want , ipamAddResult .ipv4Result )
146232 require .Equalf (tt .want , ipamAddResult .ipv4Result , "incorrect ipv4 response" )
147233 require .Equalf (tt .want1 , ipamAddResult .ipv6Result , "incorrect ipv6 response" )
148234 })
0 commit comments