Implement health check for xApp
[ric-plt/ricdms.git] / pkg / resthooks / resthooks_test.go
1 //==================================================================================
2 //  Copyright (c) 2022 Samsung
3 //
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
7 //
8 //       http://www.apache.org/licenses/LICENSE-2.0
9 //
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.
15 //
16 //   This source code is part of the near-RT RIC (RAN Intelligent Controller)
17 //   platform project (RICP).
18 //==================================================================================
19 //
20 package resthooks
21
22 import (
23         "encoding/json"
24         "fmt"
25         "io"
26         "io/ioutil"
27         "net"
28         "net/http"
29         "net/http/httptest"
30         "os"
31         "path/filepath"
32         "strings"
33         "testing"
34
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 )
46
47 var rh *Resthook
48 var successStatus *models.Status
49
50 func TestMain(m *testing.M) {
51
52         successStatus = &models.Status{
53                 Status: &health.HEALTHY,
54         }
55         ricdms.Init()
56         rh = &Resthook{
57                 HealthChecker: health.NewHealthChecker(),
58                 Onboarder:     onboard.NewOnboarder(),
59                 ChartMgr:      ch.NewChartmgr(),
60                 DeployMgr:     deploy.NewDeploymentManager(),
61         }
62         code := m.Run()
63         os.Exit(code)
64 }
65
66 func TestHealth(t *testing.T) {
67         resp := rh.GetDMSHealth()
68         switch resp.(type) {
69         case *h.GetHealthCheckOK:
70                 assert.Equal(t, successStatus, resp.(*h.GetHealthCheckOK).Payload)
71
72         case *h.GetHealthCheckInternalServerError:
73                 assert.Fail(t, "Internal Server generated: %v", resp)
74
75         default:
76                 assert.Fail(t, "Unknown type of resp : %v", resp)
77         }
78 }
79
80 func TestHealthxAppFail(t *testing.T) {
81         svr := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
82                 w.WriteHeader(501)
83         }))
84
85         svr.Listener.Close()
86         svr.Listener, _ = net.Listen("tcp", ricdms.Config.MockServer)
87
88         svr.Start()
89         defer svr.Close()
90
91         resp := rh.GetxAppHealth("test", "test")
92         switch resp.(type) {
93         case *h.GetHealthCheckOK:
94                 assert.Fail(t, "Health check should not be okay: %v", resp)
95
96         case *h.GetHealthCheckInternalServerError:
97                 break
98
99         default:
100                 assert.Fail(t, "Unknown type of resp : %v", resp)
101         }
102 }
103
104 func TestHealthxApp(t *testing.T) {
105         svr := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
106                 w.WriteHeader(http.StatusOK)
107         }))
108
109         svr.Listener.Close()
110         svr.Listener, _ = net.Listen("tcp", ricdms.Config.MockServer)
111
112         svr.Start()
113         defer svr.Close()
114
115         resp := rh.GetxAppHealth("test", "test")
116         switch resp.(type) {
117         case *h.GetHealthCheckOK:
118                 assert.Equal(t, successStatus, resp.(*h.GetHealthCheckOK).Payload)
119
120         case *h.GetHealthCheckInternalServerError:
121                 assert.Fail(t, "Internal Server generated: %v", resp)
122
123         default:
124                 assert.Fail(t, "Unknown type of resp : %v", resp)
125         }
126 }
127
128 func TestOnboard(t *testing.T) {
129         xApp := &models.Descriptor{
130                 Config: "SAMPLE_CONFIGURATION",
131                 Schema: "SAMPLE_SCHEMA",
132         }
133
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)
137                 defer r.Body.Close()
138
139                 err := json.Unmarshal(reqBytes, &d)
140
141                 ricdms.Logger.Debug("after unmarshal : %+v body: %+v", d, string(reqBytes))
142
143                 if err != nil {
144                         assert.Fail(t, "Not able to parse the request body")
145                 }
146
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")
150         }))
151         svr.Listener.Close()
152         svr.Listener, _ = net.Listen("tcp", ricdms.Config.MockServer)
153
154         svr.Start()
155         defer svr.Close()
156
157         resp := rh.OnBoard(xApp)
158         assert.NotEqual(t, nil, resp)
159 }
160
161 func TestGetCharts(t *testing.T) {
162
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")
166         }))
167         svr.Listener.Close()
168         svr.Listener, _ = net.Listen("tcp", ricdms.Config.MockServer)
169
170         svr.Start()
171         defer svr.Close()
172
173         resp := rh.GetCharts()
174         assert.NotEqual(t, nil, resp)
175
176         if _, ok := resp.(*charts.GetChartsListOK); !ok {
177                 assert.Fail(t, "response type did not match : %t", resp)
178         }
179 }
180
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)
187                 w.Write(data)
188         }))
189         svr.Listener.Close()
190         svr.Listener, _ = net.Listen("tcp", ricdms.Config.MockServer)
191
192         svr.Start()
193         defer svr.Close()
194
195         resp := rh.DownloadChart("CHART_NAME", "VERSION")
196         assert.IsType(t, &charts.DownloadHelmChartOK{}, resp, "response did not match type")
197 }
198
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)
204
205                 if err != nil {
206                         ricdms.Logger.Error("error in reading file: %v", err)
207                 }
208
209                 jsonData, err := io.ReadAll(file)
210                 if err != nil {
211                         ricdms.Logger.Error("Error in rading file: %v", err)
212                 }
213                 w.Write(jsonData)
214         }))
215
216         svr.Listener.Close()
217         svr.Listener, _ = net.Listen("tcp", ricdms.Config.MockServer)
218
219         svr.Start()
220         defer svr.Close()
221
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")
225 }
226
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)
232
233                 if err != nil {
234                         ricdms.Logger.Error("error in reading file: %v", err)
235                 }
236
237                 jsonData, err := io.ReadAll(file)
238                 if err != nil {
239                         ricdms.Logger.Error("Error in rading file: %v", err)
240                 }
241                 w.Write(jsonData)
242         }))
243
244         svr.Listener.Close()
245         svr.Listener, _ = net.Listen("tcp", ricdms.Config.MockServer)
246
247         svr.Start()
248         defer svr.Close()
249
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")
253 }
254
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)
259         }
260
261 }
262
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)
267         }
268 }