1 //==================================================================================
2 // Copyright (c) 2022 Samsung
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
16 // This source code is part of the near-RT RIC (RAN Intelligent Controller)
17 // platform project (RICP).
18 //==================================================================================
35 ch "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/charts"
36 "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/deploy"
37 "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/health"
38 "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/models"
39 "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/onboard"
40 "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi/operations/charts"
41 d "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi/operations/deploy"
42 h "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi/operations/health"
43 "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/ricdms"
44 "github.com/stretchr/testify/assert"
48 var successStatus *models.Status
50 func TestMain(m *testing.M) {
52 successStatus = &models.Status{
53 Status: &health.HEALTHY,
57 HealthChecker: health.NewHealthChecker(),
58 Onboarder: onboard.NewOnboarder(),
59 ChartMgr: ch.NewChartmgr(),
60 DeployMgr: deploy.NewDeploymentManager(),
66 func TestHealth(t *testing.T) {
67 resp := rh.GetDMSHealth()
69 case *h.GetHealthCheckOK:
70 assert.Equal(t, successStatus, resp.(*h.GetHealthCheckOK).Payload)
72 case *h.GetHealthCheckInternalServerError:
73 assert.Fail(t, "Internal Server generated: %v", resp)
76 assert.Fail(t, "Unknown type of resp : %v", resp)
80 func TestHealthxAppFail(t *testing.T) {
81 svr := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
86 svr.Listener, _ = net.Listen("tcp", ricdms.Config.MockServer)
91 resp := rh.GetxAppHealth("test", "test")
93 case *h.GetHealthCheckOK:
94 assert.Fail(t, "Health check should not be okay: %v", resp)
96 case *h.GetHealthCheckInternalServerError:
100 assert.Fail(t, "Unknown type of resp : %v", resp)
104 func TestHealthxApp(t *testing.T) {
105 svr := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
106 w.WriteHeader(http.StatusOK)
110 svr.Listener, _ = net.Listen("tcp", ricdms.Config.MockServer)
115 resp := rh.GetxAppHealth("test", "test")
117 case *h.GetHealthCheckOK:
118 assert.Equal(t, successStatus, resp.(*h.GetHealthCheckOK).Payload)
120 case *h.GetHealthCheckInternalServerError:
121 assert.Fail(t, "Internal Server generated: %v", resp)
124 assert.Fail(t, "Unknown type of resp : %v", resp)
128 func TestOnboard(t *testing.T) {
129 xApp := &models.Descriptor{
130 Config: "SAMPLE_CONFIGURATION",
131 Schema: "SAMPLE_SCHEMA",
134 svr := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
135 var d map[string]interface{}
136 reqBytes, _ := ioutil.ReadAll(r.Body)
139 err := json.Unmarshal(reqBytes, &d)
141 ricdms.Logger.Debug("after unmarshal : %+v body: %+v", d, string(reqBytes))
144 assert.Fail(t, "Not able to parse the request body")
147 assert.Equal(t, xApp.Config, d["config-file.json"])
148 assert.Equal(t, xApp.Schema, d["controls-schema.json"])
149 fmt.Fprintf(w, "SAMPLE_RESPONSE")
152 svr.Listener, _ = net.Listen("tcp", ricdms.Config.MockServer)
157 resp := rh.OnBoard(xApp)
158 assert.NotEqual(t, nil, resp)
161 func TestGetCharts(t *testing.T) {
163 svr := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
164 ricdms.Logger.Debug("Mock server running")
165 fmt.Fprintf(w, "SAMPLE_RESPONSE")
168 svr.Listener, _ = net.Listen("tcp", ricdms.Config.MockServer)
173 resp := rh.GetCharts()
174 assert.NotEqual(t, nil, resp)
176 if _, ok := resp.(*charts.GetChartsListOK); !ok {
177 assert.Fail(t, "response type did not match : %t", resp)
181 func TestDownloadChart(t *testing.T) {
182 svr := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
183 ricdms.Logger.Debug("request received by mock to download chart")
184 reader := strings.NewReader("SAMPLE_RESPONSE")
185 data, _ := io.ReadAll(reader)
186 ricdms.Logger.Debug("writing : %+v", data)
190 svr.Listener, _ = net.Listen("tcp", ricdms.Config.MockServer)
195 resp := rh.DownloadChart("CHART_NAME", "VERSION")
196 assert.IsType(t, &charts.DownloadHelmChartOK{}, resp, "response did not match type")
199 func TestGetChartsByName(t *testing.T) {
200 svr := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
201 ricdms.Logger.Debug("request received by mock to get chart by name")
202 path, _ := filepath.Abs("./mocks/resp-get-charts-by-name.json")
203 file, err := os.Open(path)
206 ricdms.Logger.Error("error in reading file: %v", err)
209 jsonData, err := io.ReadAll(file)
211 ricdms.Logger.Error("Error in rading file: %v", err)
217 svr.Listener, _ = net.Listen("tcp", ricdms.Config.MockServer)
222 resp := rh.GetChartsByName("TEST_NAME")
223 ricdms.Logger.Debug("resp Data: %s", resp.(*charts.GetChartOK).Payload...)
224 assert.IsType(t, &charts.GetChartOK{}, resp, "response did not match type")
227 func TestGetChartsByNameAndVersion(t *testing.T) {
228 svr := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
229 ricdms.Logger.Debug("request received by mock to get chart by name and version")
230 path, _ := filepath.Abs("./mocks/resp-get-charts-by-name-and-ver.json")
231 file, err := os.Open(path)
234 ricdms.Logger.Error("error in reading file: %v", err)
237 jsonData, err := io.ReadAll(file)
239 ricdms.Logger.Error("Error in rading file: %v", err)
245 svr.Listener, _ = net.Listen("tcp", ricdms.Config.MockServer)
250 resp := rh.GetChartByNameAndVersion("Test", "1.0.0")
251 ricdms.Logger.Debug("resp data: %s", resp.(*charts.GetChartsFetcherOK).Payload)
252 assert.IsType(t, &charts.GetChartsFetcherOK{}, resp, "response did not match type")
255 func TestDownloadAndInstall(t *testing.T) {
256 response := rh.DownloadAndInstallChart("sample app", "1.0.0", "test")
257 if _, ok := response.(*d.PostDeployInternalServerError); !ok {
258 assert.Fail(t, "response type did not match (actual) %T", response)
263 func TestUninstallxApp(t *testing.T) {
264 response := rh.UninstallChart("test", "test", "test")
265 if _, ok := response.(*d.DeleteDeployInternalServerError); !ok {
266 assert.Fail(t, "response type did not match actual: %T", response)