Skip to content

Commit 152aeee

Browse files
committed
fix:adding unit test
1 parent ed7a7b2 commit 152aeee

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed

cns/restserver/ipam_test.go

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

0 commit comments

Comments
 (0)