From 8b76b550e4555aec56af12f8062094dac6d99f8d Mon Sep 17 00:00:00 2001 From: AzureAhai Date: Thu, 7 Nov 2024 16:57:41 -0800 Subject: [PATCH] fix:adding unit test --- cns/restserver/ipam_test.go | 76 +++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/cns/restserver/ipam_test.go b/cns/restserver/ipam_test.go index 7220f4d413..495255c5c9 100644 --- a/cns/restserver/ipam_test.go +++ b/cns/restserver/ipam_test.go @@ -72,6 +72,10 @@ type ncState struct { ncID string ips []string } +type endpointState struct { + nc ncState + ipInfo map[string]*IPInfo +} func getTestService(orchestratorType string) *HTTPRestService { var config common.ServiceConfig @@ -2154,3 +2158,75 @@ func createAndSaveMockNCRequest(t *testing.T, svc *HTTPRestService, ncID string, require.Equal(t, types.Success, returnCode) require.Empty(t, returnMessage) } + +func TestStatelessCNIStateFile(t *testing.T) { + goodStore := store.NewMockStore("") + goodEndpointState := make(map[string]*EndpointInfo) + endpointInfo1ContainerID := "0a4917617e15d24dc495e407d8eb5c88e4406e58fa209e4eb75a2c2fb7045eea" + endpointInfo1 := &EndpointInfo{PodName: "pod1", PodNamespace: "default", IfnameToIPMap: make(map[string]*IPInfo)} + endpointInfo1.IfnameToIPMap["eth0"] = &IPInfo{IPv4: []net.IPNet{{IP: net.IPv4(10, 241, 0, 65), Mask: net.IPv4Mask(255, 255, 255, 0)}}} + + endpointInfo2ContainerID := "1b4917617e15d24dc495e407d8eb5c88e4406e58fa209e4eb75a2c2fb7045eea" + endpointInfo2 := &EndpointInfo{PodName: "pod2", PodNamespace: "default", IfnameToIPMap: make(map[string]*IPInfo)} + 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"} + + goodEndpointState[endpointInfo1ContainerID] = endpointInfo1 + err := goodStore.Write(EndpointStoreKey, goodEndpointState) + if err != nil { + t.Fatalf("Error writing to store: %v", err) + } + tests := []struct { + name string + endpointID string + req map[string]*IPInfo + store store.KeyValueStore + want *EndpointInfo + wantErr bool + }{ + { + name: "good", + endpointID: endpointInfo1ContainerID, + req: endpointInfo1.IfnameToIPMap, + store: goodStore, + want: endpointInfo1, + wantErr: false, + }, + { + name: "good with ACI endpoint", + endpointID: endpointInfo2ContainerID, + req: endpointInfo2.IfnameToIPMap, + store: goodStore, + want: endpointInfo1, + wantErr: false, + }, + { + name: "endpoint never existed", + endpointID: "0a4917617e15d24dc495e407d8eb5c88e4406e58fa209e4eb75a2c2fb7045ee3", + req: endpointInfo2.IfnameToIPMap, + store: goodStore, + want: nil, + wantErr: true, + }, + { + name: "empty store", + store: store.NewMockStore(""), + want: nil, + wantErr: true, + }, + } + + for _, tt := range tests { + tt := tt + t.Run(tt.name, func(t *testing.T) { + err := svc.UpdateEndpointHelper(tt.endpointID, tt.req) + got, err := svc.GetEndpointHelper(tt.endpointID) + if tt.wantErr { + assert.Error(t, err) + return + } + assert.NoError(t, err) + assert.Equal(t, tt.want, got) + }) + } +}