X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=servicemanager%2Finternal%2Fdiscoverservice%2Fdiscoverservice_test.go;h=ab6ae857b9b90b47fc4a263b1227c30351c6436a;hb=fdb05902d0e3234446ac0cfd8ffed0743cfd20e5;hp=24246586add4fecb3da34ac03d18d3c9cd5ce4c1;hpb=cbac30ac2136aebbc76a69f197a782ab94d083e8;p=nonrtric%2Fplt%2Fsme.git diff --git a/servicemanager/internal/discoverservice/discoverservice_test.go b/servicemanager/internal/discoverservice/discoverservice_test.go index 2424658..ab6ae85 100644 --- a/servicemanager/internal/discoverservice/discoverservice_test.go +++ b/servicemanager/internal/discoverservice/discoverservice_test.go @@ -23,6 +23,8 @@ package discoverservice import ( "fmt" "net/http" + "net/http/httptest" + "net/url" "os" "reflect" "sort" @@ -46,13 +48,24 @@ import ( "oransc.org/nonrtric/servicemanager/internal/envreader" "oransc.org/nonrtric/servicemanager/internal/kongclear" - "oransc.org/nonrtric/servicemanager/internal/providermanagement" + "oransc.org/nonrtric/servicemanager/internal/providermanagement" provapi "oransc.org/nonrtric/servicemanager/internal/providermanagementapi" "oransc.org/nonrtric/servicemanager/internal/publishservice" + + "oransc.org/nonrtric/capifcore" + mockKong "oransc.org/nonrtric/servicemanager/mockkong" ) -var requestHandler *echo.Echo +var ( + eServiceManager *echo.Echo + eCapifWeb *echo.Echo + eKong *echo.Echo + mockConfigReader *envreader.MockConfigReader + serviceManagerServer *httptest.Server + capifServer *httptest.Server + mockKongServer *httptest.Server +) func TestMain(m *testing.M) { err := setupTest() @@ -68,22 +81,70 @@ func TestMain(m *testing.M) { } func setupTest() error { - myEnv, myPorts, err := envreader.ReadDotEnv() + // Start the mock Kong server + eKong = echo.New() + mockKong.RegisterHandlers(eKong) + mockKongServer = httptest.NewServer(eKong) + + // Parse the server URL + parsedMockKongURL, err := url.Parse(mockKongServer.URL) if err != nil { - log.Fatal("error loading environment file on setupTest") + log.Fatalf("error parsing mock Kong URL: %v", err) return err } - requestHandler, err = getEcho(myEnv, myPorts) + // Extract the host and port + mockKongHost := parsedMockKongURL.Hostname() + mockKongControlPlanePort := parsedMockKongURL.Port() + + eCapifWeb = echo.New() + capifcore.RegisterHandlers(eCapifWeb, nil, nil) + capifServer = httptest.NewServer(eCapifWeb) + + // Parse the server URL + parsedCapifURL, err := url.Parse(capifServer.URL) if err != nil { - log.Fatal("getEcho fatal error on setupTest") + log.Fatalf("error parsing mock Kong URL: %v", err) return err } - err = teardown() + + // Extract the host and port + capifHost := parsedCapifURL.Hostname() + capifPort := parsedCapifURL.Port() + + // Set up the mock config reader with the desired configuration for testing + mockConfigReader = &envreader.MockConfigReader{ + MockedConfig: map[string]string{ + "KONG_DOMAIN": "kong", + "KONG_PROTOCOL": "http", + "KONG_CONTROL_PLANE_IPV4": mockKongHost, + "KONG_CONTROL_PLANE_PORT": mockKongControlPlanePort, + "KONG_DATA_PLANE_IPV4": "10.101.1.101", + "KONG_DATA_PLANE_PORT": "32080", + "CAPIF_PROTOCOL": "http", + "CAPIF_IPV4": capifHost, + "CAPIF_PORT": capifPort, + "LOG_LEVEL": "Info", + "SERVICE_MANAGER_PORT": "8095", + "TEST_SERVICE_IPV4": "10.101.1.101", + "TEST_SERVICE_PORT": "30951", + }, + } + + myEnv, myPorts, err := mockConfigReader.ReadDotEnv() + if err != nil { + log.Fatal("error loading environment file on setupTest") + return err + } + + eServiceManager = echo.New() + err = registerHandlers(eServiceManager, myEnv, myPorts) if err != nil { - log.Fatal("getEcho fatal error on teardown") + log.Fatal("registerHandlers fatal error on setupTest") return err } + serviceManagerServer = httptest.NewServer(eServiceManager) + capifCleanUp() return err } @@ -118,16 +179,14 @@ func getProvider() provapi.APIProviderEnrolmentDetails { } } -func teardown() error { - log.Trace("entering teardown") - - t := new(testing.T) // Create a new testing.T instance for teardown +func capifCleanUp() { + t := new(testing.T) // Create a new testing.T instance for capifCleanUp // Delete the invoker invokerInfo := "invoker a" invokerId := "api_invoker_id_" + strings.Replace(invokerInfo, " ", "_", 1) - result := testutil.NewRequest().Delete("/api-invoker-management/v1/onboardedInvokers/"+invokerId).Go(t, requestHandler) + result := testutil.NewRequest().Delete("/api-invoker-management/v1/onboardedInvokers/"+invokerId).Go(t, eServiceManager) assert.Equal(t, http.StatusNoContent, result.Code()) // Delete the original published service @@ -135,7 +194,7 @@ func teardown() error { apiName := "apiName" apiId := "api_id_" + apiName - result = testutil.NewRequest().Delete("/published-apis/v1/"+apfId+"/service-apis/"+apiId).Go(t, requestHandler) + result = testutil.NewRequest().Delete("/published-apis/v1/"+apfId+"/service-apis/"+apiId).Go(t, eServiceManager) assert.Equal(t, http.StatusNoContent, result.Code()) // Delete the first published service @@ -143,22 +202,26 @@ func teardown() error { apiName = "apiName1" apiId = "api_id_" + apiName - result = testutil.NewRequest().Delete("/published-apis/v1/"+apfId+"/service-apis/"+apiId).Go(t, requestHandler) + result = testutil.NewRequest().Delete("/published-apis/v1/"+apfId+"/service-apis/"+apiId).Go(t, eServiceManager) assert.Equal(t, http.StatusNoContent, result.Code()) // Delete the second published service apiName = "apiName2" apiId = "api_id_" + apiName - result = testutil.NewRequest().Delete("/published-apis/v1/"+apfId+"/service-apis/"+apiId).Go(t, requestHandler) + result = testutil.NewRequest().Delete("/published-apis/v1/"+apfId+"/service-apis/"+apiId).Go(t, eServiceManager) assert.Equal(t, http.StatusNoContent, result.Code()) // Delete the provider domainID := "domain_id_Kong" - result = testutil.NewRequest().Delete("/api-provider-management/v1/registrations/"+domainID).Go(t, requestHandler) + result = testutil.NewRequest().Delete("/api-provider-management/v1/registrations/"+domainID).Go(t, eServiceManager) assert.Equal(t, http.StatusNoContent, result.Code()) +} - myEnv, myPorts, err := envreader.ReadDotEnv() +func teardown() error { + log.Trace("entering teardown") + + myEnv, myPorts, err := mockConfigReader.ReadDotEnv() if err != nil { log.Fatal("error loading environment file") return err @@ -168,15 +231,20 @@ func teardown() error { if err != nil { log.Fatal("error clearing Kong on teardown") } + + mockKongServer.Close() + capifServer.Close() + serviceManagerServer.Close() + return err } func TestRegisterValidProvider(t *testing.T) { - teardown() + capifCleanUp() newProvider := getProvider() // Register a valid provider - result := testutil.NewRequest().Post("/api-provider-management/v1/registrations").WithJsonBody(newProvider).Go(t, requestHandler) + result := testutil.NewRequest().Post("/api-provider-management/v1/registrations").WithJsonBody(newProvider).Go(t, eServiceManager) assert.Equal(t, http.StatusCreated, result.Code()) var resultProvider provapi.APIProviderEnrolmentDetails @@ -189,7 +257,7 @@ func TestPublishUnpublishService(t *testing.T) { apiName := "apiName1" apiId := "api_id_" + apiName - myEnv, myPorts, err := envreader.ReadDotEnv() + myEnv, myPorts, err := mockConfigReader.ReadDotEnv() assert.Nil(t, err, "error reading env file") testServiceIpv4 := common29122.Ipv4Addr(myEnv["TEST_SERVICE_IPV4"]) @@ -199,7 +267,7 @@ func TestPublishUnpublishService(t *testing.T) { assert.NotZero(t, testServicePort, "TEST_SERVICE_PORT is required in .env file for unit testing") // Check no services published - result := testutil.NewRequest().Get("/published-apis/v1/"+apfId+"/service-apis").Go(t, requestHandler) + result := testutil.NewRequest().Get("/published-apis/v1/"+apfId+"/service-apis").Go(t, eServiceManager) assert.Equal(t, http.StatusOK, result.Code()) // Parse JSON from the response body @@ -209,7 +277,7 @@ func TestPublishUnpublishService(t *testing.T) { // Check if the parsed array is empty assert.Zero(t, len(resultServices)) - assert.True (t, len(resultServices) == 0) + assert.True(t, len(resultServices) == 0) aefId := "AEF_id_rApp_Kong_as_AEF" namespace := "namespace" @@ -226,7 +294,7 @@ func TestPublishUnpublishService(t *testing.T) { newServiceDescription := getServiceAPIDescription(aefId, apiName, apiCategory, apiVersion, &protocolHTTP11, &dataFormatJSON, description, testServiceIpv4, testServicePort, publishapi.CommunicationTypeREQUESTRESPONSE) // Publish a service for provider - result = testutil.NewRequest().Post("/published-apis/v1/"+apfId+"/service-apis").WithJsonBody(newServiceDescription).Go(t, requestHandler) + result = testutil.NewRequest().Post("/published-apis/v1/"+apfId+"/service-apis").WithJsonBody(newServiceDescription).Go(t, eServiceManager) assert.Equal(t, http.StatusCreated, result.Code()) if result.Code() != http.StatusCreated { @@ -241,7 +309,7 @@ func TestPublishUnpublishService(t *testing.T) { assert.Equal(t, "http://example.com/published-apis/v1/"+apfId+"/service-apis/"+*resultService.ApiId, result.Recorder.Header().Get(echo.HeaderLocation)) // Check that the service is published for the provider - result = testutil.NewRequest().Get("/published-apis/v1/"+apfId+"/service-apis/"+apiId).Go(t, requestHandler) + result = testutil.NewRequest().Get("/published-apis/v1/"+apfId+"/service-apis/"+apiId).Go(t, eServiceManager) assert.Equal(t, http.StatusOK, result.Code()) err = result.UnmarshalJsonToObject(&resultService) @@ -254,17 +322,17 @@ func TestPublishUnpublishService(t *testing.T) { resultServiceIpv4 := *interfaceDescription.Ipv4Addr resultServicePort := *interfaceDescription.Port - kongIPv4 := common29122.Ipv4Addr(myEnv["KONG_IPV4"]) + kongDataPlaneIPv4 := common29122.Ipv4Addr(myEnv["KONG_DATA_PLANE_IPV4"]) kongDataPlanePort := common29122.Port(myPorts["KONG_DATA_PLANE_PORT"]) - assert.NotEmpty(t, kongIPv4, "KONG_IPV4 is required in .env file for unit testing") + assert.NotEmpty(t, kongDataPlaneIPv4, "KONG_DATA_PLANE_IPV4 is required in .env file for unit testing") assert.NotZero(t, kongDataPlanePort, "KONG_DATA_PLANE_PORT is required in .env file for unit testing") - assert.Equal(t, kongIPv4, resultServiceIpv4) + assert.Equal(t, kongDataPlaneIPv4, resultServiceIpv4) assert.Equal(t, kongDataPlanePort, resultServicePort) // Check one service published - result = testutil.NewRequest().Get("/published-apis/v1/"+apfId+"/service-apis").Go(t, requestHandler) + result = testutil.NewRequest().Get("/published-apis/v1/"+apfId+"/service-apis").Go(t, eServiceManager) assert.Equal(t, http.StatusOK, result.Code()) // Parse JSON from the response body @@ -272,7 +340,7 @@ func TestPublishUnpublishService(t *testing.T) { assert.NoError(t, err, "error unmarshaling response") // Check if the parsed array has one item - assert.True (t, len(resultServices) == 1) + assert.True(t, len(resultServices) == 1) // Publish a second service for provider apiName2 := "apiName2" @@ -284,7 +352,7 @@ func TestPublishUnpublishService(t *testing.T) { newServiceDescription2 := getServiceAPIDescription(aefId, apiName2, apiCategory, apiVersion, &protocolHTTP1, &dataFormatOther, description, testServiceIpv4, testServicePort, publishapi.CommunicationTypeSUBSCRIBENOTIFY) - result = testutil.NewRequest().Post("/published-apis/v1/"+apfId+"/service-apis").WithJsonBody(newServiceDescription2).Go(t, requestHandler) + result = testutil.NewRequest().Post("/published-apis/v1/"+apfId+"/service-apis").WithJsonBody(newServiceDescription2).Go(t, eServiceManager) assert.Equal(t, http.StatusCreated, result.Code()) if result.Code() != http.StatusCreated { @@ -297,7 +365,7 @@ func TestPublishUnpublishService(t *testing.T) { assert.Equal(t, apiId2, *resultService.ApiId) // Check no services published - result = testutil.NewRequest().Get("/published-apis/v1/"+apfId+"/service-apis").Go(t, requestHandler) + result = testutil.NewRequest().Get("/published-apis/v1/"+apfId+"/service-apis").Go(t, eServiceManager) assert.Equal(t, http.StatusOK, result.Code()) // Parse JSON from the response body @@ -305,7 +373,7 @@ func TestPublishUnpublishService(t *testing.T) { assert.NoError(t, err, "error unmarshaling response") // Check if the parsed array has two items - assert.True (t, len(resultServices) == 2) + assert.True(t, len(resultServices) == 2) } func TestOnboardInvoker(t *testing.T) { @@ -313,7 +381,7 @@ func TestOnboardInvoker(t *testing.T) { newInvoker := getInvoker(invokerInfo) // Onboard a valid invoker - result := testutil.NewRequest().Post("/api-invoker-management/v1/onboardedInvokers").WithJsonBody(newInvoker).Go(t, requestHandler) + result := testutil.NewRequest().Post("/api-invoker-management/v1/onboardedInvokers").WithJsonBody(newInvoker).Go(t, eServiceManager) assert.Equal(t, http.StatusCreated, result.Code()) var resultInvoker invokermanagementapi.APIInvokerEnrolmentDetails @@ -336,7 +404,7 @@ func TestGetAllServiceAPIs(t *testing.T) { apiName2 := "apiName2" // Get all APIs, without any filter - result := testutil.NewRequest().Get("/service-apis/v1/allServiceAPIs?api-invoker-id="+invokerId).Go(t, requestHandler) + result := testutil.NewRequest().Get("/service-apis/v1/allServiceAPIs?api-invoker-id="+invokerId).Go(t, eServiceManager) assert.Equal(t, http.StatusOK, result.Code()) var resultDiscovery discoverserviceapi.DiscoveredAPIs @@ -364,7 +432,7 @@ func TestGetAllServiceAPIsWhenMissingProvider(t *testing.T) { invokerId := "unregistered" // Get all APIs, without any filter - result := testutil.NewRequest().Get("/service-apis/v1/allServiceAPIs?api-invoker-id="+invokerId).Go(t, requestHandler) + result := testutil.NewRequest().Get("/service-apis/v1/allServiceAPIs?api-invoker-id="+invokerId).Go(t, eServiceManager) assert.Equal(t, http.StatusNotFound, result.Code()) var problemDetails common29122.ProblemDetails @@ -383,7 +451,7 @@ func TestFilterApiName(t *testing.T) { invokerId := "api_invoker_id_" + strings.Replace(invokerInfo, " ", "_", 1) // Get APIs with filter - result := testutil.NewRequest().Get("/service-apis/v1/allServiceAPIs?api-invoker-id="+invokerId+"&api-name="+apiName).Go(t, requestHandler) + result := testutil.NewRequest().Get("/service-apis/v1/allServiceAPIs?api-invoker-id="+invokerId+"&api-name="+apiName).Go(t, eServiceManager) assert.Equal(t, http.StatusOK, result.Code()) var resultDiscovery discoverserviceapi.DiscoveredAPIs @@ -408,7 +476,7 @@ func TestFilterAefId(t *testing.T) { aefId := "AEF_id_rApp_Kong_as_AEF" // Get APIs with filter - result := testutil.NewRequest().Get("/service-apis/v1/allServiceAPIs?api-invoker-id="+invokerId+"&aef-id="+aefId).Go(t, requestHandler) + result := testutil.NewRequest().Get("/service-apis/v1/allServiceAPIs?api-invoker-id="+invokerId+"&aef-id="+aefId).Go(t, eServiceManager) assert.Equal(t, http.StatusOK, result.Code()) var resultDiscovery discoverserviceapi.DiscoveredAPIs @@ -438,7 +506,7 @@ func TestFilterVersion(t *testing.T) { apiVersion := "v1" // Get APIs with filter - result := testutil.NewRequest().Get("/service-apis/v1/allServiceAPIs?api-invoker-id="+invokerId+"&api-version="+apiVersion).Go(t, requestHandler) + result := testutil.NewRequest().Get("/service-apis/v1/allServiceAPIs?api-invoker-id="+invokerId+"&api-version="+apiVersion).Go(t, eServiceManager) assert.Equal(t, http.StatusOK, result.Code()) var resultDiscovery discoverserviceapi.DiscoveredAPIs @@ -463,7 +531,7 @@ func TestFilterCommType(t *testing.T) { commType := publishapi.CommunicationTypeREQUESTRESPONSE // Get APIs with filter - result := testutil.NewRequest().Get("/service-apis/v1/allServiceAPIs?api-invoker-id="+invokerId+"&comm-type="+string(commType)).Go(t, requestHandler) + result := testutil.NewRequest().Get("/service-apis/v1/allServiceAPIs?api-invoker-id="+invokerId+"&comm-type="+string(commType)).Go(t, eServiceManager) assert.Equal(t, http.StatusOK, result.Code()) var resultDiscovery discoverserviceapi.DiscoveredAPIs @@ -488,7 +556,7 @@ func TestFilterVersionAndCommType(t *testing.T) { commType := publishapi.CommunicationTypeREQUESTRESPONSE // Get APIs with filter - result := testutil.NewRequest().Get("/service-apis/v1/allServiceAPIs?api-invoker-id="+invokerId+"&api-version="+apiVersion+"&comm-type="+string(commType)).Go(t, requestHandler) + result := testutil.NewRequest().Get("/service-apis/v1/allServiceAPIs?api-invoker-id="+invokerId+"&api-version="+apiVersion+"&comm-type="+string(commType)).Go(t, eServiceManager) assert.Equal(t, http.StatusOK, result.Code()) var resultDiscovery discoverserviceapi.DiscoveredAPIs @@ -512,7 +580,7 @@ func TestFilterAPICategory(t *testing.T) { apiCategory := "apiCategory" // Get APIs with filter - result := testutil.NewRequest().Get("/service-apis/v1/allServiceAPIs?api-invoker-id="+invokerId+"&api-cat="+apiCategory).Go(t, requestHandler) + result := testutil.NewRequest().Get("/service-apis/v1/allServiceAPIs?api-invoker-id="+invokerId+"&api-cat="+apiCategory).Go(t, eServiceManager) assert.Equal(t, http.StatusOK, result.Code()) var resultDiscovery discoverserviceapi.DiscoveredAPIs @@ -536,7 +604,7 @@ func TestFilterProtocol(t *testing.T) { var protocolHTTP11 = publishapi.ProtocolHTTP11 // Get APIs with filter - result := testutil.NewRequest().Get("/service-apis/v1/allServiceAPIs?api-invoker-id="+invokerId+"&protocol="+string(protocolHTTP11)).Go(t, requestHandler) + result := testutil.NewRequest().Get("/service-apis/v1/allServiceAPIs?api-invoker-id="+invokerId+"&protocol="+string(protocolHTTP11)).Go(t, eServiceManager) assert.Equal(t, http.StatusOK, result.Code()) var resultDiscovery discoverserviceapi.DiscoveredAPIs @@ -561,7 +629,7 @@ func TestFilterDataFormat(t *testing.T) { var dataFormatJSON = publishapi.DataFormatJSON // Get APIs with filter - result := testutil.NewRequest().Get("/service-apis/v1/allServiceAPIs?api-invoker-id="+invokerId+"&data-format="+string(dataFormatJSON)).Go(t, requestHandler) + result := testutil.NewRequest().Get("/service-apis/v1/allServiceAPIs?api-invoker-id="+invokerId+"&data-format="+string(dataFormatJSON)).Go(t, eServiceManager) assert.Equal(t, http.StatusOK, result.Code()) var resultDiscovery discoverserviceapi.DiscoveredAPIs @@ -576,26 +644,25 @@ func TestFilterDataFormat(t *testing.T) { assert.Equal(t, apiName, (*resultDiscovery.ServiceAPIDescriptions)[0].ApiName) } } - teardown() + capifCleanUp() } -func getEcho(myEnv map[string]string, myPorts map[string]int) (*echo.Echo, error) { +func registerHandlers(e *echo.Echo, myEnv map[string]string, myPorts map[string]int) (err error) { capifProtocol := myEnv["CAPIF_PROTOCOL"] capifIPv4 := common29122.Ipv4Addr(myEnv["CAPIF_IPV4"]) capifPort := common29122.Port(myPorts["CAPIF_PORT"]) kongDomain := myEnv["KONG_DOMAIN"] kongProtocol := myEnv["KONG_PROTOCOL"] - kongIPv4 := common29122.Ipv4Addr(myEnv["KONG_IPV4"]) - kongDataPlanePort := common29122.Port(myPorts["KONG_DATA_PLANE_PORT"]) + kongControlPlaneIPv4 := common29122.Ipv4Addr(myEnv["KONG_CONTROL_PLANE_IPV4"]) kongControlPlanePort := common29122.Port(myPorts["KONG_CONTROL_PLANE_PORT"]) - - e := echo.New() + kongDataPlaneIPv4 := common29122.Ipv4Addr(myEnv["KONG_DATA_PLANE_IPV4"]) + kongDataPlanePort := common29122.Port(myPorts["KONG_DATA_PLANE_PORT"]) // Register ProviderManagement providerManagerSwagger, err := provapi.GetSwagger() if err != nil { log.Fatalf("error loading ProviderManagement swagger spec\n: %s", err) - return nil, err + return err } providerManagerSwagger.Servers = nil providerManager := providermanagement.NewProviderManager(capifProtocol, capifIPv4, capifPort) @@ -610,12 +677,16 @@ func getEcho(myEnv map[string]string, myPorts map[string]int) (*echo.Echo, error publishServiceSwagger, err := publishapi.GetSwagger() if err != nil { fmt.Fprintf(os.Stderr, "Error loading PublishService swagger spec\n: %s", err) - return nil, err + return err } publishServiceSwagger.Servers = nil - ps := publishservice.NewPublishService(kongDomain, kongProtocol, kongIPv4, kongDataPlanePort, kongControlPlanePort, capifProtocol, capifIPv4, capifPort) + ps := publishservice.NewPublishService( + kongDomain, kongProtocol, + kongControlPlaneIPv4, kongControlPlanePort, + kongDataPlaneIPv4, kongDataPlanePort, + capifProtocol, capifIPv4, capifPort) group = e.Group("/published-apis/v1") group.Use(echomiddleware.Logger()) @@ -626,7 +697,7 @@ func getEcho(myEnv map[string]string, myPorts map[string]int) (*echo.Echo, error invokerServiceSwagger, err := invokermanagementapi.GetSwagger() if err != nil { fmt.Fprintf(os.Stderr, "Error loading InvokerManagement swagger spec\n: %s", err) - return nil, err + return err } invokerServiceSwagger.Servers = nil @@ -654,7 +725,7 @@ func getEcho(myEnv map[string]string, myPorts map[string]int) (*echo.Echo, error group.Use(middleware.OapiRequestValidator(discoverySeviceSwagger)) discoverserviceapi.RegisterHandlersWithBaseURL(e, ds, "service-apis/v1") - return e, err + return err } func getServiceAPIDescription(aefId string, apiName string, apiCategory string, apiVersion string, protocol *publishapi.Protocol, dataFormat *publishapi.DataFormat, description string, testServiceIpv4 common29122.Ipv4Addr, testServicePort common29122.Port, commType publishapi.CommunicationType) publishapi.ServiceAPIDescription {