@@ -2154,3 +2154,122 @@ func createAndSaveMockNCRequest(t *testing.T, svc *HTTPRestService, ncID string,
21542154 require .Equal (t , types .Success , returnCode )
21552155 require .Empty (t , returnMessage )
21562156}
2157+
2158+ // Validate Statefile in Stateless CNI scenarios
2159+ func TestStatelessCNIStateFile (t * testing.T ) {
2160+ svc := getTestService (cns .KubernetesCRD )
2161+ svc .EndpointStateStore = store .NewMockStore ("" )
2162+ // test Case 1 - AKS SIngleTenancy
2163+ endpointInfo1ContainerID := "0a4917617e15d24dc495e407d8eb5c88e4406e58fa209e4eb75a2c2fb7045eea"
2164+ endpointInfo1 := & EndpointInfo {IfnameToIPMap : make (map [string ]* IPInfo )}
2165+ endpointInfo1 .IfnameToIPMap ["eth0" ] = & IPInfo {IPv4 : []net.IPNet {{IP : net .IPv4 (10 , 0 , 0 , 1 ), Mask : net .IPv4Mask (255 , 255 , 255 , 0 )}}}
2166+ req1 := make (map [string ]* IPInfo )
2167+ req1 ["eth0" ] = & IPInfo {IPv4 : []net.IPNet {{IP : net .IPv4 (10 , 0 , 0 , 1 ), Mask : net .IPv4Mask (255 , 255 , 255 , 0 )}}, HnsEndpointID : "5c15cccc-830a-4dff-81f3-4b1e55cb7dcb" , NICType : cns .InfraNIC }
2168+ testPod1Info = cns .NewPodInfo (endpointInfo1ContainerID , endpointInfo1ContainerID , "pod1" , "default" )
2169+ req := cns.IPConfigsRequest {
2170+ PodInterfaceID : testPod1Info .InterfaceID (),
2171+ InfraContainerID : testPod1Info .InfraContainerID (),
2172+ }
2173+ // test Case 2 - ACI
2174+ endpointInfo2ContainerID := "1b4917617e15d24dc495e407d8eb5c88e4406e58fa209e4eb75a2c2fb7045eea"
2175+ endpointInfo2 := & EndpointInfo {IfnameToIPMap : make (map [string ]* IPInfo )}
2176+ endpointInfo2 .IfnameToIPMap ["eth2" ] = & IPInfo {IPv4 : nil , NICType : cns .DelegatedVMNIC , HnsEndpointID : "5c15cccc-830a-4dff-81f3-4b1e55cb7dcb" , HnsNetworkID : "5c0712cd-824c-4898-b1c0-2fcb16ede4fb" , MacAddress : "7c:1e:52:06:d3:4b" }
2177+
2178+ tests := []struct {
2179+ name string
2180+ endpointID string
2181+ req map [string ]* IPInfo
2182+ store store.KeyValueStore
2183+ want * EndpointInfo
2184+ wantErr bool
2185+ }{
2186+ {
2187+ name : "good" ,
2188+ endpointID : endpointInfo1ContainerID ,
2189+ req : req1 ,
2190+ store : svc .EndpointStateStore ,
2191+ want : & EndpointInfo {
2192+ PodName : "pod1" , PodNamespace : "default" , IfnameToIPMap : map [string ]* IPInfo {
2193+ "eth0" : {
2194+ IPv4 : []net.IPNet {{IP : net .IPv4 (10 , 0 , 0 , 1 ), Mask : net .IPv4Mask (255 , 255 , 255 , 0 )}},
2195+ HnsEndpointID : "5c15cccc-830a-4dff-81f3-4b1e55cb7dcb" ,
2196+ NICType : cns .InfraNIC ,
2197+ },
2198+ },
2199+ },
2200+ wantErr : false ,
2201+ },
2202+ {
2203+ name : "good with ACI endpoint" ,
2204+ endpointID : endpointInfo2ContainerID ,
2205+ req : endpointInfo2 .IfnameToIPMap ,
2206+ store : svc .EndpointStateStore ,
2207+ want : endpointInfo2 ,
2208+ wantErr : false ,
2209+ },
2210+ }
2211+ ncStates := []ncState {
2212+ {
2213+ ncID : testNCID ,
2214+ ips : []string {
2215+ testIP1 ,
2216+ },
2217+ },
2218+ }
2219+
2220+ ipconfigs := make (map [string ]cns.IPConfigurationStatus , 0 )
2221+ for i := range ncStates {
2222+ state := NewPodState (ncStates [i ].ips [0 ], ipIDs [i ][0 ], ncStates [i ].ncID , types .Available , 0 )
2223+ ipconfigs [state .ID ] = state
2224+ err := UpdatePodIPConfigState (t , svc , ipconfigs , ncStates [i ].ncID )
2225+ if err != nil {
2226+ t .Fatalf ("Expected to not fail update service with config: %+v" , err )
2227+ }
2228+ }
2229+ t .Log (ipconfigs )
2230+ b , _ := testPod1Info .OrchestratorContext ()
2231+ req .OrchestratorContext = b
2232+ req .Ifname = "eth0"
2233+ podIPInfo , err := requestIPConfigsHelper (svc , req )
2234+ if err != nil {
2235+ t .Fatalf ("Expected to not fail getting pod ip info: %+v" , err )
2236+ }
2237+
2238+ ipInfo := & IPInfo {}
2239+ for i := range podIPInfo {
2240+ ip , ipnet , errIP := net .ParseCIDR (podIPInfo [i ].PodIPConfig .IPAddress + "/" + strconv .FormatUint (uint64 (podIPInfo [i ].PodIPConfig .PrefixLength ), 10 ))
2241+ if errIP != nil {
2242+ t .Fatalf ("failed to parse pod ip address: %+v" , errIP )
2243+ }
2244+ ipconfig := net.IPNet {IP : ip , Mask : ipnet .Mask }
2245+ if ip .To4 () == nil { // is an ipv6 address
2246+ ipInfo .IPv6 = append (ipInfo .IPv6 , ipconfig )
2247+ } else {
2248+ ipInfo .IPv4 = append (ipInfo .IPv4 , ipconfig )
2249+ }
2250+ }
2251+
2252+ // add goalState
2253+ err = svc .updateEndpointState (req , testPod1Info , podIPInfo )
2254+ if err != nil {
2255+ t .Fatalf ("Expected to not fail updating endpoint state: %+v" , err )
2256+ }
2257+ // update State
2258+ for _ , tt := range tests {
2259+ tt := tt
2260+ t .Run (tt .name , func (t * testing.T ) {
2261+ err := svc .UpdateEndpointHelper (tt .endpointID , tt .req )
2262+ if tt .wantErr {
2263+ assert .Error (t , err )
2264+ return
2265+ }
2266+ got , err := svc .GetEndpointHelper (tt .endpointID )
2267+ if tt .wantErr {
2268+ assert .Error (t , err )
2269+ return
2270+ }
2271+ require .NoError (t , err )
2272+ assert .Equal (t , tt .want , got )
2273+ })
2274+ }
2275+ }
0 commit comments