Skip to content

Commit 8b76b55

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

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

cns/restserver/ipam_test.go

Lines changed: 76 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,75 @@ func createAndSaveMockNCRequest(t *testing.T, svc *HTTPRestService, ncID string,
21542158
require.Equal(t, types.Success, returnCode)
21552159
require.Empty(t, returnMessage)
21562160
}
2161+
2162+
func TestStatelessCNIStateFile(t *testing.T) {
2163+
goodStore := store.NewMockStore("")
2164+
goodEndpointState := make(map[string]*EndpointInfo)
2165+
endpointInfo1ContainerID := "0a4917617e15d24dc495e407d8eb5c88e4406e58fa209e4eb75a2c2fb7045eea"
2166+
endpointInfo1 := &EndpointInfo{PodName: "pod1", PodNamespace: "default", IfnameToIPMap: make(map[string]*IPInfo)}
2167+
endpointInfo1.IfnameToIPMap["eth0"] = &IPInfo{IPv4: []net.IPNet{{IP: net.IPv4(10, 241, 0, 65), Mask: net.IPv4Mask(255, 255, 255, 0)}}}
2168+
2169+
endpointInfo2ContainerID := "1b4917617e15d24dc495e407d8eb5c88e4406e58fa209e4eb75a2c2fb7045eea"
2170+
endpointInfo2 := &EndpointInfo{PodName: "pod2", PodNamespace: "default", IfnameToIPMap: make(map[string]*IPInfo)}
2171+
endpointInfo2.IfnameToIPMap["eth2"] = &IPInfo{IPv4: nil, NICType: cns.DelegatedVMNIC, HnsEndpointID: "5c15cccc-830a-4dff-81f3-4b1e55cb7dcb",
2172+
HnsNetworkID: "5c0712cd-824c-4898-b1c0-2fcb16ede4fb", MacAddress: "7c:1e:52:06:d3:4b"}
2173+
2174+
goodEndpointState[endpointInfo1ContainerID] = endpointInfo1
2175+
err := goodStore.Write(EndpointStoreKey, goodEndpointState)
2176+
if err != nil {
2177+
t.Fatalf("Error writing to store: %v", err)
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: endpointInfo1.IfnameToIPMap,
2191+
store: goodStore,
2192+
want: endpointInfo1,
2193+
wantErr: false,
2194+
},
2195+
{
2196+
name: "good with ACI endpoint",
2197+
endpointID: endpointInfo2ContainerID,
2198+
req: endpointInfo2.IfnameToIPMap,
2199+
store: goodStore,
2200+
want: endpointInfo1,
2201+
wantErr: false,
2202+
},
2203+
{
2204+
name: "endpoint never existed",
2205+
endpointID: "0a4917617e15d24dc495e407d8eb5c88e4406e58fa209e4eb75a2c2fb7045ee3",
2206+
req: endpointInfo2.IfnameToIPMap,
2207+
store: goodStore,
2208+
want: nil,
2209+
wantErr: true,
2210+
},
2211+
{
2212+
name: "empty store",
2213+
store: store.NewMockStore(""),
2214+
want: nil,
2215+
wantErr: true,
2216+
},
2217+
}
2218+
2219+
for _, tt := range tests {
2220+
tt := tt
2221+
t.Run(tt.name, func(t *testing.T) {
2222+
err := svc.UpdateEndpointHelper(tt.endpointID, tt.req)
2223+
got, err := svc.GetEndpointHelper(tt.endpointID)
2224+
if tt.wantErr {
2225+
assert.Error(t, err)
2226+
return
2227+
}
2228+
assert.NoError(t, err)
2229+
assert.Equal(t, tt.want, got)
2230+
})
2231+
}
2232+
}

0 commit comments

Comments
 (0)