@@ -2154,3 +2154,115 @@ 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" ,
2177+ HnsNetworkID : "5c0712cd-824c-4898-b1c0-2fcb16ede4fb" , MacAddress : "7c:1e:52:06:d3:4b" }
2178+
2179+ tests := []struct {
2180+ name string
2181+ endpointID string
2182+ req map [string ]* IPInfo
2183+ store store.KeyValueStore
2184+ want * EndpointInfo
2185+ wantErr bool
2186+ }{
2187+ {
2188+ name : "good" ,
2189+ endpointID : endpointInfo1ContainerID ,
2190+ req : req1 ,
2191+ store : svc .EndpointStateStore ,
2192+ want : & EndpointInfo {PodName : "pod1" , PodNamespace : "default" , IfnameToIPMap : map [string ]* IPInfo {"eth0" : {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 }}},
2193+ wantErr : false ,
2194+ },
2195+ {
2196+ name : "good with ACI endpoint" ,
2197+ endpointID : endpointInfo2ContainerID ,
2198+ req : endpointInfo2 .IfnameToIPMap ,
2199+ store : svc .EndpointStateStore ,
2200+ want : endpointInfo2 ,
2201+ wantErr : false ,
2202+ },
2203+ }
2204+ ncStates := []ncState {
2205+ {
2206+ ncID : testNCID ,
2207+ ips : []string {
2208+ testIP1 ,
2209+ },
2210+ },
2211+ }
2212+
2213+ ipconfigs := make (map [string ]cns.IPConfigurationStatus , 0 )
2214+ for i := range ncStates {
2215+ state := NewPodState (ncStates [i ].ips [0 ], ipIDs [i ][0 ], ncStates [i ].ncID , types .Available , 0 )
2216+ ipconfigs [state .ID ] = state
2217+ err := UpdatePodIPConfigState (t , svc , ipconfigs , ncStates [i ].ncID )
2218+ if err != nil {
2219+ t .Fatalf ("Expected to not fail update service with config: %+v" , err )
2220+ }
2221+ }
2222+ t .Log (ipconfigs )
2223+ b , _ := testPod1Info .OrchestratorContext ()
2224+ req .OrchestratorContext = b
2225+ req .Ifname = "eth0"
2226+ podIPInfo , err := requestIPConfigsHelper (svc , req )
2227+ if err != nil {
2228+ t .Fatalf ("Expected to not fail getting pod ip info: %+v" , err )
2229+ }
2230+
2231+ ipInfo := & IPInfo {}
2232+ for i := range podIPInfo {
2233+ ip , ipnet , errIP := net .ParseCIDR (podIPInfo [i ].PodIPConfig .IPAddress + "/" + strconv .FormatUint (uint64 (podIPInfo [i ].PodIPConfig .PrefixLength ), 10 ))
2234+ if errIP != nil {
2235+ t .Fatalf ("failed to parse pod ip address: %+v" , errIP )
2236+ }
2237+ ipconfig := net.IPNet {IP : ip , Mask : ipnet .Mask }
2238+ if ip .To4 () == nil { // is an ipv6 address
2239+ ipInfo .IPv6 = append (ipInfo .IPv6 , ipconfig )
2240+ } else {
2241+ ipInfo .IPv4 = append (ipInfo .IPv4 , ipconfig )
2242+ }
2243+ }
2244+
2245+ // add goalState
2246+ err = svc .updateEndpointState (req , testPod1Info , podIPInfo )
2247+ if err != nil {
2248+ t .Fatalf ("Expected to not fail updating endpoint state: %+v" , err )
2249+ }
2250+ // update State
2251+ for _ , tt := range tests {
2252+ tt := tt
2253+ t .Run (tt .name , func (t * testing.T ) {
2254+ err := svc .UpdateEndpointHelper (tt .endpointID , tt .req )
2255+ if tt .wantErr {
2256+ assert .Error (t , err )
2257+ return
2258+ }
2259+ got , err := svc .GetEndpointHelper (tt .endpointID )
2260+ if tt .wantErr {
2261+ assert .Error (t , err )
2262+ return
2263+ }
2264+ require .NoError (t , err )
2265+ assert .Equal (t , tt .want , got )
2266+ })
2267+ }
2268+ }
0 commit comments