@@ -2115,3 +2115,186 @@ func TestAddNICsToCNIResult(t *testing.T) {
21152115 })
21162116 }
21172117}
2118+
2119+ // Test to add multiple IB NICs to make sure CNI receives all correct IB info from CNS
2120+ func TestMultipleIBNICsToResult (t * testing.T ) {
2121+ require := require .New (t ) //nolint further usage of require without passing t
2122+
2123+ firstMacAddress := "bc:9a:78:56:34:12"
2124+ firstParsedMacAddress , _ := net .ParseMAC (firstMacAddress )
2125+
2126+ secondMacAddress := "bc:9a:78:56:34:13"
2127+ secondParsedMacAddress , _ := net .ParseMAC (secondMacAddress )
2128+
2129+ thirdMacAddress := "bc:9a:78:56:34:14"
2130+ thirdParsedMacAddress , _ := net .ParseMAC (thirdMacAddress )
2131+
2132+ macAddressList := []string {firstMacAddress , secondMacAddress , thirdMacAddress }
2133+
2134+ firstPnpID := "PCI\\ VEN_15B3&DEV_101C&SUBSYS_000715B3&REV_00\\ 5&8c5acce&0&0"
2135+ firstNewPnpID := "PCI\\ VEN_15B3&DEV_101C&SUBSYS_000715B3&REV_00\\ 5&8c5acce&0&1"
2136+ secondPnpID := "PCI\\ VEN_15B3&DEV_101C&SUBSYS_000715B3&REV_00\\ 6&8c5acce&0&1"
2137+ secondNewPnpID := "PCI\\ VEN_15B3&DEV_101C&SUBSYS_000715B3&REV_00\\ 6&8c5acce&0&2"
2138+ thirdPnpID := "PCI\\ VEN_15B3&DEV_101C&SUBSYS_000715B3&REV_00\\ 7&8c5acce&0&2"
2139+ thirdNewPnpID := "PCI\\ VEN_15B3&DEV_101C&SUBSYS_000715B3&REV_00\\ 7&8c5acce&0&3"
2140+
2141+ type fields struct {
2142+ podName string
2143+ podNamespace string
2144+ cnsClient cnsclient
2145+ }
2146+
2147+ type args struct {
2148+ nwCfg * cni.NetworkConfig
2149+ args * cniSkel.CmdArgs
2150+ hostSubnetPrefix * net.IPNet
2151+ options map [string ]interface {}
2152+ info []IPResultInfo
2153+ }
2154+
2155+ tests := []struct {
2156+ name string
2157+ fields fields
2158+ args args
2159+ wantSecondaryInterfacesInfo map [string ]network.InterfaceInfo
2160+ }{
2161+ {
2162+ name : "add three backendNIC to cni Result" ,
2163+ fields : fields {
2164+ podName : testPodInfo .PodName ,
2165+ podNamespace : testPodInfo .PodNamespace ,
2166+ cnsClient : & MockCNSClient {
2167+ require : require ,
2168+ requestIPs : requestIPsHandler {
2169+ ipconfigArgument : cns.IPConfigsRequest {
2170+ PodInterfaceID : "testcont-testifname1" ,
2171+ InfraContainerID : "testcontainerid1" ,
2172+ OrchestratorContext : marshallPodInfo (testPodInfo ),
2173+ },
2174+ result : & cns.IPConfigsResponse {
2175+ PodIPInfo : []cns.PodIpInfo {
2176+ {
2177+ PodIPConfig : cns.IPSubnet {
2178+ IPAddress : "10.0.1.10" ,
2179+ PrefixLength : 24 ,
2180+ },
2181+ NetworkContainerPrimaryIPConfig : cns.IPConfiguration {
2182+ IPSubnet : cns.IPSubnet {
2183+ IPAddress : "10.0.1.0" ,
2184+ PrefixLength : 24 ,
2185+ },
2186+ DNSServers : nil ,
2187+ GatewayIPAddress : "10.0.0.1" ,
2188+ },
2189+ HostPrimaryIPInfo : cns.HostIPInfo {
2190+ Gateway : "10.0.0.1" ,
2191+ PrimaryIP : "10.0.0.1" ,
2192+ Subnet : "10.0.0.0/24" ,
2193+ },
2194+ NICType : cns .InfraNIC ,
2195+ },
2196+ {
2197+ MacAddress : firstMacAddress ,
2198+ NICType : cns .BackendNIC ,
2199+ PnPID : firstPnpID ,
2200+ },
2201+ {
2202+ MacAddress : secondMacAddress ,
2203+ NICType : cns .BackendNIC ,
2204+ PnPID : secondPnpID ,
2205+ },
2206+ {
2207+ MacAddress : thirdMacAddress ,
2208+ NICType : cns .BackendNIC ,
2209+ PnPID : thirdPnpID ,
2210+ },
2211+ },
2212+ Response : cns.Response {
2213+ ReturnCode : 0 ,
2214+ Message : "" ,
2215+ },
2216+ },
2217+ err : nil ,
2218+ },
2219+ },
2220+ },
2221+ args : args {
2222+ nwCfg : & cni.NetworkConfig {},
2223+ args : & cniSkel.CmdArgs {
2224+ ContainerID : "testcontainerid1" ,
2225+ Netns : "testnetns1" ,
2226+ IfName : "testifname1" ,
2227+ },
2228+ hostSubnetPrefix : getCIDRNotationForAddress ("10.0.0.1/24" ),
2229+ options : map [string ]interface {}{},
2230+ info : []IPResultInfo {
2231+ {
2232+ pnpID : firstNewPnpID , // update pnp ID
2233+ macAddress : firstMacAddress ,
2234+ nicType : cns .BackendNIC ,
2235+ },
2236+ {
2237+ pnpID : secondNewPnpID , // update pnp ID
2238+ macAddress : secondMacAddress ,
2239+ nicType : cns .BackendNIC ,
2240+ },
2241+ {
2242+ pnpID : thirdNewPnpID , // update pnp ID
2243+ macAddress : thirdMacAddress ,
2244+ nicType : cns .BackendNIC ,
2245+ },
2246+ },
2247+ },
2248+ wantSecondaryInterfacesInfo : map [string ]network.InterfaceInfo {
2249+ firstMacAddress : {
2250+ MacAddress : firstParsedMacAddress ,
2251+ PnPID : firstNewPnpID ,
2252+ NICType : cns .BackendNIC ,
2253+ },
2254+ secondMacAddress : {
2255+ MacAddress : secondParsedMacAddress ,
2256+ PnPID : secondNewPnpID ,
2257+ NICType : cns .BackendNIC ,
2258+ },
2259+ thirdMacAddress : {
2260+ MacAddress : thirdParsedMacAddress ,
2261+ PnPID : thirdNewPnpID ,
2262+ NICType : cns .BackendNIC ,
2263+ },
2264+ },
2265+ },
2266+ }
2267+ for _ , tt := range tests {
2268+ tt := tt
2269+ t .Run (tt .name , func (t * testing.T ) {
2270+ invoker := & CNSIPAMInvoker {
2271+ podName : tt .fields .podName ,
2272+ podNamespace : tt .fields .podNamespace ,
2273+ cnsClient : tt .fields .cnsClient ,
2274+ }
2275+ ipamAddResult , err := invoker .Add (IPAMAddConfig {nwCfg : tt .args .nwCfg , args : tt .args .args , options : tt .args .options })
2276+ if err != nil {
2277+ t .Fatalf ("Failed to create ipamAddResult due to error: %v" , err )
2278+ }
2279+
2280+ // add three new backendNICs info to cni Result
2281+ err = addBackendNICToResult (& tt .args .info [0 ], & ipamAddResult , firstMacAddress )
2282+ if err != nil {
2283+ t .Fatalf ("Failed to add first backend NIC to cni Result due to error %v" , err )
2284+ }
2285+ err = addBackendNICToResult (& tt .args .info [1 ], & ipamAddResult , secondMacAddress )
2286+ if err != nil {
2287+ t .Fatalf ("Failed to add second backend NIC to cni Result due to error %v" , err )
2288+ }
2289+ err = addBackendNICToResult (& tt .args .info [2 ], & ipamAddResult , thirdMacAddress )
2290+ if err != nil {
2291+ t .Fatalf ("Failed to add third backend NIC to cni Result due to error %v" , err )
2292+ }
2293+
2294+ for _ , macAddress := range macAddressList {
2295+ t .Logf ("want:%+v\n rest:%+v\n " , tt .wantSecondaryInterfacesInfo [macAddress ], ipamAddResult .interfaceInfo [macAddress ])
2296+ require .EqualValues (tt .wantSecondaryInterfacesInfo [macAddress ], ipamAddResult .interfaceInfo [macAddress ], "incorrect response for IB" )
2297+ }
2298+ })
2299+ }
2300+ }
0 commit comments