From f9668f910dcfcb7f276707c6e103e24460493126 Mon Sep 17 00:00:00 2001 From: paulyufan2 Date: Thu, 11 Apr 2024 15:44:36 -0400 Subject: [PATCH] tmp fix for testing --- cns/restserver/api_test.go | 22 ++++++++++++++++++++++ cns/restserver/restserver.go | 8 ++++---- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/cns/restserver/api_test.go b/cns/restserver/api_test.go index 8cfd648b90..5afc2a59ac 100644 --- a/cns/restserver/api_test.go +++ b/cns/restserver/api_test.go @@ -1666,6 +1666,27 @@ func setEnv(t *testing.T) *httptest.ResponseRecorder { return w } +// Since UT can only run with localhost, to mock all api test cases, add nodeListener service handlers +func addHandlersToListener(service *HTTPRestService) { + // Add handlers for UT + listener := service.Listener + + listener.AddHandler(cns.GetNetworkContainerByOrchestratorContext, service.GetNetworkContainerByOrchestratorContext) + listener.AddHandler(cns.GetAllNetworkContainers, service.GetAllNetworkContainers) + listener.AddHandler(cns.CreateHostNCApipaEndpointPath, service.CreateHostNCApipaEndpoint) + listener.AddHandler(cns.DeleteHostNCApipaEndpointPath, service.DeleteHostNCApipaEndpoint) + listener.AddHandler(cns.RequestIPConfig, NewHandlerFuncWithHistogram(service.RequestIPConfigHandler, HTTPRequestLatency)) + listener.AddHandler(cns.RequestIPConfigs, NewHandlerFuncWithHistogram(service.RequestIPConfigsHandler, HTTPRequestLatency)) + listener.AddHandler(cns.ReleaseIPConfig, NewHandlerFuncWithHistogram(service.ReleaseIPConfigHandler, HTTPRequestLatency)) + listener.AddHandler(cns.ReleaseIPConfigs, NewHandlerFuncWithHistogram(service.ReleaseIPConfigsHandler, HTTPRequestLatency)) + // listener.AddHandler(cns.PathDebugIPAddresses, service.HandleDebugIPAddresses) + // listener.AddHandler(cns.PathDebugPodContext, service.HandleDebugPodContext) + // listener.AddHandler(cns.PathDebugRestData, service.HandleDebugRestData) + listener.AddHandler(cns.V2Prefix+cns.GetNetworkContainerByOrchestratorContext, service.GetNetworkContainerByOrchestratorContext) + listener.AddHandler(cns.V2Prefix+cns.GetAllNetworkContainers, service.GetAllNetworkContainers) + listener.AddHandler(cns.V2Prefix+cns.CreateHostNCApipaEndpointPath, service.CreateHostNCApipaEndpoint) +} + func startService() error { // Create the service. config := common.ServiceConfig{} @@ -1730,6 +1751,7 @@ func startService() error { } service := service.(*HTTPRestService) + addHandlersToListener(service) err = service.Start(&config) if err != nil { diff --git a/cns/restserver/restserver.go b/cns/restserver/restserver.go index 4828ce367d..004d75a39e 100644 --- a/cns/restserver/restserver.go +++ b/cns/restserver/restserver.go @@ -252,8 +252,8 @@ func (service *HTTPRestService) Init(config *common.ServiceConfig) error { listener.AddHandler(cns.DeleteNetworkContainer, service.deleteNetworkContainer) listener.AddHandler(cns.GetInterfaceForContainer, service.getInterfaceForContainer) listener.AddHandler(cns.SetOrchestratorType, service.setOrchestratorType) - listener.AddHandler(cns.GetNetworkContainerByOrchestratorContext, service.GetNetworkContainerByOrchestratorContext) - listener.AddHandler(cns.GetAllNetworkContainers, service.GetAllNetworkContainers) + // listener.AddHandler(cns.GetNetworkContainerByOrchestratorContext, service.GetNetworkContainerByOrchestratorContext) + // listener.AddHandler(cns.GetAllNetworkContainers, service.GetAllNetworkContainers) listener.AddHandler(cns.AttachContainerToNetwork, service.attachNetworkContainerToNetwork) listener.AddHandler(cns.DetachContainerFromNetwork, service.detachNetworkContainerFromNetwork) listener.AddHandler(cns.CreateHnsNetworkPath, service.createHnsNetwork) @@ -289,8 +289,8 @@ func (service *HTTPRestService) Init(config *common.ServiceConfig) error { listener.AddHandler(cns.V2Prefix+cns.SetOrchestratorType, service.setOrchestratorType) // listener.AddHandler(cns.V2Prefix+cns.GetNetworkContainerByOrchestratorContext, service.GetNetworkContainerByOrchestratorContext) // listener.AddHandler(cns.V2Prefix+cns.GetAllNetworkContainers, service.GetAllNetworkContainers) - // listener.AddHandler(cns.V2Prefix+cns.AttachContainerToNetwork, service.attachNetworkContainerToNetwork) - // listener.AddHandler(cns.V2Prefix+cns.DetachContainerFromNetwork, service.detachNetworkContainerFromNetwork) + listener.AddHandler(cns.V2Prefix+cns.AttachContainerToNetwork, service.attachNetworkContainerToNetwork) + listener.AddHandler(cns.V2Prefix+cns.DetachContainerFromNetwork, service.detachNetworkContainerFromNetwork) // listener.AddHandler(cns.V2Prefix+cns.CreateHnsNetworkPath, service.createHnsNetwork) // listener.AddHandler(cns.V2Prefix+cns.DeleteHnsNetworkPath, service.deleteHnsNetwork) listener.AddHandler(cns.V2Prefix+cns.NumberOfCPUCoresPath, service.getNumberOfCPUCores)