Skip to content

Commit

Permalink
fix:adding unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
behzad-mir committed Nov 8, 2024
1 parent ed7a7b2 commit 8b76b55
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions cns/restserver/ipam_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ type ncState struct {
ncID string
ips []string
}
type endpointState struct {

Check failure on line 75 in cns/restserver/ipam_test.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, ubuntu-latest)

type `endpointState` is unused (unused)

Check failure on line 75 in cns/restserver/ipam_test.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, windows-latest)

type `endpointState` is unused (unused)

Check failure on line 75 in cns/restserver/ipam_test.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, ubuntu-latest)

type `endpointState` is unused (unused)

Check failure on line 75 in cns/restserver/ipam_test.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, windows-latest)

type `endpointState` is unused (unused)
nc ncState
ipInfo map[string]*IPInfo
}

func getTestService(orchestratorType string) *HTTPRestService {
var config common.ServiceConfig
Expand Down Expand Up @@ -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)

Check failure on line 2222 in cns/restserver/ipam_test.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, ubuntu-latest)

ineffectual assignment to err (ineffassign)

Check failure on line 2222 in cns/restserver/ipam_test.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, windows-latest)

ineffectual assignment to err (ineffassign)

Check failure on line 2222 in cns/restserver/ipam_test.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, ubuntu-latest)

ineffectual assignment to err (ineffassign)

Check failure on line 2222 in cns/restserver/ipam_test.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, windows-latest)

ineffectual assignment to err (ineffassign)
got, err := svc.GetEndpointHelper(tt.endpointID)
if tt.wantErr {
assert.Error(t, err)
return
}
assert.NoError(t, err)

Check failure on line 2228 in cns/restserver/ipam_test.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, ubuntu-latest)

require-error: for error assertions use require (testifylint)

Check failure on line 2228 in cns/restserver/ipam_test.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, windows-latest)

require-error: for error assertions use require (testifylint)

Check failure on line 2228 in cns/restserver/ipam_test.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, ubuntu-latest)

require-error: for error assertions use require (testifylint)

Check failure on line 2228 in cns/restserver/ipam_test.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, windows-latest)

require-error: for error assertions use require (testifylint)
assert.Equal(t, tt.want, got)
})
}
}

0 comments on commit 8b76b55

Please sign in to comment.