Skip to content

Commit a0d5808

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

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed

cns/restserver/ipam_test.go

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ type ncState struct {
7272
ncID string
7373
ips []string
7474
}
75+
type endpointState struct {
76+
nc ncState
77+
ipInfo map[string]*IPInfo
78+
}
7579

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

0 commit comments

Comments
 (0)