Skip to content

feat: add unit tests for CNS endpoint handler APIs#4344

Open
behzad-mir wants to merge 1 commit intomasterfrom
ut-cns-endpoint-tests
Open

feat: add unit tests for CNS endpoint handler APIs#4344
behzad-mir wants to merge 1 commit intomasterfrom
ut-cns-endpoint-tests

Conversation

@behzad-mir
Copy link
Copy Markdown
Contributor

@behzad-mir behzad-mir commented Apr 10, 2026

Adds comprehensive unit tests for:

  • GetEndpointState API
  • GetEndpoint API
  • EndpointStateHandler

Also adds JSON tags to EndpointInfo, IPInfo, and Response structs for proper serialization in tests and API responses.

CNS Endpoint Handler Tests

File: cns/restserver/endpoint_handler_test.go (NEW)

Test Function Description
TestGetEndpointHelper Get existing/non-existent endpoints
TestGetEndpointHelper_LegacyFormat Legacy Windows CNI 1.4.x format lookup
TestUpdateEndpointHelper Update HNS ID, ACI, SwiftV2 secondary NIC
TestDeleteEndpointStateHelper Delete existing/non-existent endpoints
TestEndpointHandlerAPI_GetMethod HTTP GET handler
TestEndpointHandlerAPI_PatchMethod HTTP PATCH handler
TestEndpointHandlerAPI_DeleteMethod HTTP DELETE handler
TestEndpointHandlerAPI_OptManageEndpointStateDisabled Feature flag validation
TestStatelessCNI_EndToEnd_Flow Full lifecycle: create → update → get → delete
TestStatelessCNI_SwiftV2_MultiNIC Multi-NIC with InfraNIC + FrontendNIC

CNS Util Tests

File: cns/restserver/util_test.go

Test Function Description
TestRestoreState EndpointState restoration on CNS restart

Verification

# Run CNS endpoint handler tests
go test -v -run 'TestGetEndpointHelper|TestUpdateEndpointHelper|TestDeleteEndpointStateHelper|TestEndpointHandlerAPI|TestStatelessCNI_EndToEnd|TestStatelessCNI_SwiftV2|TestRestoreState' ./cns/restserver/

Requirements:

Notes:

@behzad-mir behzad-mir marked this pull request as ready for review April 10, 2026 18:38
@behzad-mir behzad-mir requested a review from a team as a code owner April 10, 2026 18:38
@behzad-mir behzad-mir requested review from Copilot and nddq April 10, 2026 18:38
@behzad-mir behzad-mir added the cni Related to CNI. label Apr 10, 2026
@behzad-mir behzad-mir requested a review from QxBytes April 10, 2026 18:38
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds unit test coverage for CNS endpoint handler APIs (GetEndpoint/GetEndpointState/Update/Delete flows) and updates CNS REST server structs to include JSON tags so responses/state can be serialized/deserialized consistently.

Changes:

  • Added a new endpoint_handler_test.go with unit tests covering helper functions and HTTP handler behavior for endpoint state management.
  • Added JSON tags to EndpointInfo, IPInfo, and Response in cns/restserver/restserver.go.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
cns/restserver/restserver.go Adds JSON tags to endpoint/state response structs to support consistent JSON encoding/decoding.
cns/restserver/endpoint_handler_test.go Introduces new unit tests for endpoint helper methods and the EndpointHandlerAPI HTTP dispatch behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread cns/restserver/restserver.go
Comment thread cns/restserver/endpoint_handler_test.go
Comment thread cns/restserver/endpoint_handler_test.go
Comment thread cns/restserver/endpoint_handler_test.go
@behzad-mir behzad-mir force-pushed the ut-cns-endpoint-tests branch from 90f08ab to 7aabc7d Compare April 10, 2026 18:50
Adds comprehensive unit tests for:
- GetEndpointState API
- GetEndpoint API
- EndpointStateHandler

Also adds JSON tags to EndpointInfo, IPInfo, and Response structs
for proper serialization in tests and API responses.
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +123 to +125
PodName string `json:"PodName,omitempty"`
PodNamespace string `json:"PodNamespace,omitempty"`
IfnameToIPMap map[string]*IPInfo `json:"IfnameToIPMap,omitempty"` // key : interface name, value : IPInfo
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding omitempty on PodName, PodNamespace, and IfnameToIPMap changes the JSON output for GetEndpointResponse and the on-disk endpoint state (empty values will now be omitted rather than present as empty string/empty object). If this is intended to be backward-compatible for existing clients/state files, consider removing omitempty (or only applying it where the API contract explicitly allows missing fields).

Suggested change
PodName string `json:"PodName,omitempty"`
PodNamespace string `json:"PodNamespace,omitempty"`
IfnameToIPMap map[string]*IPInfo `json:"IfnameToIPMap,omitempty"` // key : interface name, value : IPInfo
PodName string `json:"PodName"`
PodNamespace string `json:"PodNamespace"`
IfnameToIPMap map[string]*IPInfo `json:"IfnameToIPMap"` // key : interface name, value : IPInfo

Copilot uses AI. Check for mistakes.
Comment on lines +31 to +42
httpsvc, err := NewHTTPRestService(&config, &fakes.WireserverClientFake{}, &fakes.WireserverProxyFake{},
&IPtablesProvider{}, &fakes.NMAgentClientFake{}, store.NewMockStore(""), nil, nil,
fakes.NewMockIMDSClient())
require.NoError(t, err, "NewHTTPRestService should not return an error")
require.NotNil(t, httpsvc, "HTTPRestService should not be nil")

// Enable endpoint state management (required for stateless CNI)
httpsvc.Options = make(map[string]interface{})
httpsvc.Options[acn.OptManageEndpointState] = true

// Initialize endpoint state store
httpsvc.EndpointStateStore = store.NewMockStore("")
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NewHTTPRestService already initializes Options, and this helper reassigns it (and later creates a new EndpointStateStore). Consider just setting httpsvc.Options[acn.OptManageEndpointState] = true and reusing the store instance to avoid masking default initialization behavior and allocating extra mock stores.

Suggested change
httpsvc, err := NewHTTPRestService(&config, &fakes.WireserverClientFake{}, &fakes.WireserverProxyFake{},
&IPtablesProvider{}, &fakes.NMAgentClientFake{}, store.NewMockStore(""), nil, nil,
fakes.NewMockIMDSClient())
require.NoError(t, err, "NewHTTPRestService should not return an error")
require.NotNil(t, httpsvc, "HTTPRestService should not be nil")
// Enable endpoint state management (required for stateless CNI)
httpsvc.Options = make(map[string]interface{})
httpsvc.Options[acn.OptManageEndpointState] = true
// Initialize endpoint state store
httpsvc.EndpointStateStore = store.NewMockStore("")
mockStore := store.NewMockStore("")
httpsvc, err := NewHTTPRestService(&config, &fakes.WireserverClientFake{}, &fakes.WireserverProxyFake{},
&IPtablesProvider{}, &fakes.NMAgentClientFake{}, mockStore, nil, nil,
fakes.NewMockIMDSClient())
require.NoError(t, err, "NewHTTPRestService should not return an error")
require.NotNil(t, httpsvc, "HTTPRestService should not be nil")
// Enable endpoint state management (required for stateless CNI)
httpsvc.Options[acn.OptManageEndpointState] = true
// Initialize endpoint state

Copilot uses AI. Check for mistakes.
Comment on lines +225 to +229
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := service.UpdateEndpointHelper(tt.endpointID, tt.updateReq)
if tt.wantErr {
require.Error(t, err)
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This loop runs subtests against a shared service instance, so cases can depend on state mutated by earlier cases. To keep tests independent and easier to maintain, consider creating a fresh service (and persisting initial state) per t.Run, or resetting the store/state between cases.

Copilot uses AI. Check for mistakes.
Comment on lines +427 to +431
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req := httptest.NewRequestWithContext(context.Background(), http.MethodDelete, tt.path, http.NoBody)
w := httptest.NewRecorder()

Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These subtests reuse a single service instance, which couples the cases (the first DELETE mutates state/store that subsequent cases observe). Consider instantiating a new service per case (or resetting state) so each t.Run is isolated and future parallelization/reordering won’t break expectations.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cni Related to CNI.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants