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"
45 "github.com/stretchr/testify/mock"
49 var successStatus *models.Status
51 func TestMain(m *testing.M) {
53 successStatus = &models.Status{
54 Status: &health.HEALTHY,
58 HealthChecker: HealthCheckerMock{},
59 Onboarder: onboard.NewOnboarder(),
60 ChartMgr: ch.NewChartmgr(),
61 DeployMgr: deploy.NewDeploymentManager(),
67 func TestHealth(t *testing.T) {
68 resp := rh.GetDMSHealth()
70 case *h.GetHealthCheckOK:
71 assert.Equal(t, successStatus, resp.(*h.GetHealthCheckOK).Payload)
73 case *h.GetHealthCheckInternalServerError:
74 assert.Fail(t, "Internal Server generated: %v", resp)
77 assert.Fail(t, "Unknown type of resp : %v", resp)
81 func TestOnboard(t *testing.T) {
82 xApp := &models.Descriptor{
83 Config: "SAMPLE_CONFIGURATION",
84 Schema: "SAMPLE_SCHEMA",
87 svr := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
88 var d map[string]interface{}
89 reqBytes, _ := ioutil.ReadAll(r.Body)
92 err := json.Unmarshal(reqBytes, &d)
94 ricdms.Logger.Debug("after unmarshal : %+v body: %+v", d, string(reqBytes))
97 assert.Fail(t, "Not able to parse the request body")
100 assert.Equal(t, xApp.Config, d["config-file.json"])
101 assert.Equal(t, xApp.Schema, d["controls-schema.json"])
102 fmt.Fprintf(w, "SAMPLE_RESPONSE")
105 svr.Listener, _ = net.Listen("tcp", ricdms.Config.MockServer)
110 resp := rh.OnBoard(xApp)
111 assert.NotEqual(t, nil, resp)
114 func TestGetCharts(t *testing.T) {
116 svr := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
117 ricdms.Logger.Debug("Mock server running")
118 fmt.Fprintf(w, "SAMPLE_RESPONSE")
121 svr.Listener, _ = net.Listen("tcp", ricdms.Config.MockServer)
126 resp := rh.GetCharts()
127 assert.NotEqual(t, nil, resp)
129 successResp := resp.(*charts.GetChartsListOK)
130 assert.Equal(t, "SAMPLE_RESPONSE", successResp.Payload)
133 func TestDownloadChart(t *testing.T) {
134 svr := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
135 ricdms.Logger.Debug("request received by mock to download chart")
136 reader := strings.NewReader("SAMPLE_RESPONSE")
137 data, _ := io.ReadAll(reader)
138 ricdms.Logger.Debug("writing : %+v", data)
142 svr.Listener, _ = net.Listen("tcp", ricdms.Config.MockServer)
147 resp := rh.DownloadChart("CHART_NAME", "VERSION")
148 assert.IsType(t, &charts.DownloadHelmChartOK{}, resp, "response did not match type")
151 func TestGetChartsByName(t *testing.T) {
152 svr := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
153 ricdms.Logger.Debug("request received by mock to get chart by name")
154 path, _ := filepath.Abs("./mocks/resp-get-charts-by-name.json")
155 file, err := os.Open(path)
158 ricdms.Logger.Error("error in reading file: %v", err)
161 jsonData, err := io.ReadAll(file)
163 ricdms.Logger.Error("Error in rading file: %v", err)
169 svr.Listener, _ = net.Listen("tcp", ricdms.Config.MockServer)
174 resp := rh.GetChartsByName("TEST_NAME")
175 ricdms.Logger.Debug("resp Data: %s", resp.(*charts.GetChartOK).Payload...)
176 assert.IsType(t, &charts.GetChartOK{}, resp, "response did not match type")
179 func TestGetChartsByNameAndVersion(t *testing.T) {
180 svr := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
181 ricdms.Logger.Debug("request received by mock to get chart by name and version")
182 path, _ := filepath.Abs("./mocks/resp-get-charts-by-name-and-ver.json")
183 file, err := os.Open(path)
186 ricdms.Logger.Error("error in reading file: %v", err)
189 jsonData, err := io.ReadAll(file)
191 ricdms.Logger.Error("Error in rading file: %v", err)
197 svr.Listener, _ = net.Listen("tcp", ricdms.Config.MockServer)
202 resp := rh.GetChartByNameAndVersion("Test", "1.0.0")
203 ricdms.Logger.Debug("resp data: %s", resp.(*charts.GetChartsFetcherOK).Payload)
204 assert.IsType(t, &charts.GetChartsFetcherOK{}, resp, "response did not match type")
207 func TestDownloadAndInstall(t *testing.T) {
208 response := rh.DownloadAndInstallChart("sample app", "1.0.0", "test")
209 if _, ok := response.(*d.PostDeployInternalServerError); !ok {
210 assert.Fail(t, "response type did not match (actual) %T", response)
215 type HealthCheckerMock struct {
219 func (h HealthCheckerMock) GetStatus() *models.Status {