X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=pkg%2Fresthooks%2Fresthooks_test.go;h=a40f7d7dd74fe2015f63e3cec2339a4883059433;hb=refs%2Fchanges%2F32%2F11232%2F1;hp=b18b1648ccb85f7b243b3bb337d12e6748b77f78;hpb=f8dc81bc7dd198da9fcb17bef2e164dc71c39e49;p=ric-plt%2Fricdms.git diff --git a/pkg/resthooks/resthooks_test.go b/pkg/resthooks/resthooks_test.go index b18b164..a40f7d7 100644 --- a/pkg/resthooks/resthooks_test.go +++ b/pkg/resthooks/resthooks_test.go @@ -1,22 +1,23 @@ -//================================================================================== -// Copyright (c) 2022 Samsung +// ================================================================================== // -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at +// Copyright (c) 2022 Samsung // -// http://www.apache.org/licenses/LICENSE-2.0 +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at // -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +// http://www.apache.org/licenses/LICENSE-2.0 // -// This source code is part of the near-RT RIC (RAN Intelligent Controller) -// platform project (RICP). -//================================================================================== +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. // +// This source code is part of the near-RT RIC (RAN Intelligent Controller) +// platform project (RICP). +// +// ================================================================================== package resthooks import ( @@ -28,18 +29,21 @@ import ( "net/http" "net/http/httptest" "os" + "path/filepath" "strings" "testing" ch "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/charts" + "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/deploy" "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/health" "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/models" "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/onboard" "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi/operations/charts" + d "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi/operations/deploy" + "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi/operations/experiment" h "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi/operations/health" "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/ricdms" "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/mock" ) var rh *Resthook @@ -52,9 +56,10 @@ func TestMain(m *testing.M) { } ricdms.Init() rh = &Resthook{ - HealthChecker: HealthCheckerMock{}, + HealthChecker: health.NewHealthChecker(), Onboarder: onboard.NewOnboarder(), ChartMgr: ch.NewChartmgr(), + DeployMgr: deploy.NewDeploymentManager(), } code := m.Run() os.Exit(code) @@ -74,6 +79,54 @@ func TestHealth(t *testing.T) { } } +func TestHealthxAppFail(t *testing.T) { + svr := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(501) + })) + + svr.Listener.Close() + svr.Listener, _ = net.Listen("tcp", ricdms.Config.MockServer) + + svr.Start() + defer svr.Close() + + resp := rh.GetxAppHealth("test", "test") + switch resp.(type) { + case *h.GetHealthCheckOK: + assert.Fail(t, "Health check should not be okay: %v", resp) + + case *h.GetHealthCheckInternalServerError: + break + + default: + assert.Fail(t, "Unknown type of resp : %v", resp) + } +} + +func TestHealthxApp(t *testing.T) { + svr := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + })) + + svr.Listener.Close() + svr.Listener, _ = net.Listen("tcp", ricdms.Config.MockServer) + + svr.Start() + defer svr.Close() + + resp := rh.GetxAppHealth("test", "test") + switch resp.(type) { + case *h.GetHealthCheckOK: + assert.Equal(t, successStatus, resp.(*h.GetHealthCheckOK).Payload) + + case *h.GetHealthCheckInternalServerError: + assert.Fail(t, "Internal Server generated: %v", resp) + + default: + assert.Fail(t, "Unknown type of resp : %v", resp) + } +} + func TestOnboard(t *testing.T) { xApp := &models.Descriptor{ Config: "SAMPLE_CONFIGURATION", @@ -122,8 +175,9 @@ func TestGetCharts(t *testing.T) { resp := rh.GetCharts() assert.NotEqual(t, nil, resp) - successResp := resp.(*charts.GetChartsListOK) - assert.Equal(t, "SAMPLE_RESPONSE", successResp.Payload) + if _, ok := resp.(*charts.GetChartsListOK); !ok { + assert.Fail(t, "response type did not match : %t", resp) + } } func TestDownloadChart(t *testing.T) { @@ -144,10 +198,95 @@ func TestDownloadChart(t *testing.T) { assert.IsType(t, &charts.DownloadHelmChartOK{}, resp, "response did not match type") } -type HealthCheckerMock struct { - mock.Mock +func TestGetChartsByName(t *testing.T) { + svr := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + ricdms.Logger.Debug("request received by mock to get chart by name") + path, _ := filepath.Abs("./mocks/resp-get-charts-by-name.json") + file, err := os.Open(path) + + if err != nil { + ricdms.Logger.Error("error in reading file: %v", err) + } + + jsonData, err := io.ReadAll(file) + if err != nil { + ricdms.Logger.Error("Error in rading file: %v", err) + } + w.Write(jsonData) + })) + + svr.Listener.Close() + svr.Listener, _ = net.Listen("tcp", ricdms.Config.MockServer) + + svr.Start() + defer svr.Close() + + resp := rh.GetChartsByName("TEST_NAME") + ricdms.Logger.Debug("resp Data: %s", resp.(*charts.GetChartOK).Payload...) + assert.IsType(t, &charts.GetChartOK{}, resp, "response did not match type") +} + +func TestGetChartsByNameAndVersion(t *testing.T) { + svr := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + ricdms.Logger.Debug("request received by mock to get chart by name and version") + path, _ := filepath.Abs("./mocks/resp-get-charts-by-name-and-ver.json") + file, err := os.Open(path) + + if err != nil { + ricdms.Logger.Error("error in reading file: %v", err) + } + + jsonData, err := io.ReadAll(file) + if err != nil { + ricdms.Logger.Error("Error in rading file: %v", err) + } + w.Write(jsonData) + })) + + svr.Listener.Close() + svr.Listener, _ = net.Listen("tcp", ricdms.Config.MockServer) + + svr.Start() + defer svr.Close() + + resp := rh.GetChartByNameAndVersion("Test", "1.0.0") + ricdms.Logger.Debug("resp data: %s", resp.(*charts.GetChartsFetcherOK).Payload) + assert.IsType(t, &charts.GetChartsFetcherOK{}, resp, "response did not match type") +} + +func TestDownloadAndInstall(t *testing.T) { + response := rh.DownloadAndInstallChart("sample app", "1.0.0", "test") + if _, ok := response.(*d.PostDeployInternalServerError); !ok { + assert.Fail(t, "response type did not match (actual) %T", response) + } + +} + +func TestUninstallxApp(t *testing.T) { + response := rh.UninstallChart("test", "test", "test") + if _, ok := response.(*d.DeleteDeployInternalServerError); !ok { + assert.Fail(t, "response type did not match actual: %T", response) + } } -func (h HealthCheckerMock) GetStatus() *models.Status { - return successStatus +func TestCustomOnaboardEmptyPkg(t *testing.T) { + svr := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + ricdms.Logger.Debug("request received by mock to get chart by name and version") + })) + + svr.Listener.Close() + svr.Listener, _ = net.Listen("tcp", ricdms.Config.MockServer) + + svr.Start() + defer svr.Close() + + resp := rh.Onboarder.CustomOnboard(nil) + ricdms.Logger.Debug("resp type:%T", resp) + + switch resp.(type) { + case *experiment.PostCustomOnboardInternalServerError: + break + default: + assert.Failf(t, "case 1 response type did not match", "[actual: %T]", resp) + } }