2 ==================================================================================
3 Copyright (c) 2020 AT&T Intellectual Property.
4 Copyright (c) 2020 Nokia
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 ==================================================================================
26 "github.com/go-openapi/strfmt"
34 apimodel "gerrit.oran-osc.org/r/ric-plt/o1mediator/pkg/appmgrmodel"
35 "gerrit.oran-osc.org/r/ric-plt/o1mediator/pkg/sbi"
36 "github.com/prometheus/alertmanager/api/v2/models"
37 "github.com/stretchr/testify/assert"
44 var release = "ueec-xapp"
56 NAME READY STATUS RESTARTS AGE
57 ricxapp-ueec-7bfdd587db-2jl9j 1/1 Running 53 29d
58 ricxapp-anr-6748846478-8hmtz 1-1 Running 1 29d
59 ricxapp-dualco-7f76f65c99-5p6c6 0/1 Running 1 29d
63 func TestMain(M *testing.M) {
64 s = sbi.NewSBIClient("localhost:8080", "localhost:9093", 5)
68 func TestBuildXappDescriptor(t *testing.T) {
69 var expDesc apimodel.XappDescriptor = apimodel.XappDescriptor{
75 assert.Equal(t, expDesc, *getTestXappDescriptor())
78 func TestDeployXapp(t *testing.T) {
79 ts := createHTTPServer(t, "POST", "/ric/v1/xapps", 8080, http.StatusCreated, apimodel.Xapp{})
81 err := s.DeployXapp(getTestXappDescriptor())
85 func TestDeployXappReturnsErrorIfHttpErrorResponse(t *testing.T) {
86 ts := createHTTPServer(t, "POST", "/ric/v1/xapps", 8080, http.StatusInternalServerError, nil)
88 err := s.DeployXapp(getTestXappDescriptor())
92 func TestUndeployXapp(t *testing.T) {
93 ts := createHTTPServer(t, "DELETE", "/ric/v1/xapps/ueec-xapp", 8080, http.StatusNoContent, nil)
95 err := s.UndeployXapp(getTestXappDescriptor())
99 func TestUndeployXappReturnsErrorIfHttpErrorResponse(t *testing.T) {
100 ts := createHTTPServer(t, "DELETE", "/ric/v1/xapps/ueec-xapp", 8080, http.StatusInternalServerError, nil)
102 err := s.UndeployXapp(getTestXappDescriptor())
103 assert.NotNil(t, err)
106 func TestGetDeployedXapps(t *testing.T) {
107 ts := createHTTPServer(t, "GET", "/ric/v1/xapps", 8080, http.StatusOK, apimodel.AllDeployedXapps{})
109 err := s.GetDeployedXapps()
113 func TestGetDeployedXappsReturnsErrorIfHttpErrorResponse(t *testing.T) {
114 ts := createHTTPServer(t, "GET", "/ric/v1/xapps", 8080, http.StatusInternalServerError, apimodel.AllDeployedXapps{})
116 err := s.GetDeployedXapps()
117 assert.NotNil(t, err)
120 func TestBuildXappConfig(t *testing.T) {
121 expResp := &apimodel.XAppConfig{
122 Metadata: &apimodel.ConfigMetadata{
128 resp := s.BuildXappConfig(xappName, ns, cfgData)
129 assert.Equal(t, expResp, resp)
132 func TestModifyXappConfig(t *testing.T) {
133 ts := createHTTPServer(t, "PUT", "/ric/v1/config", 8080, http.StatusOK, apimodel.ConfigValidationErrors{})
135 xappCfg := s.BuildXappConfig(xappName, ns, cfgData)
136 err := s.ModifyXappConfig(xappCfg)
140 func TestModifyXappConfigReturnsErrorIfHttpErrorResponse(t *testing.T) {
141 ts := createHTTPServer(t, "PUT", "/ric/v1/config", 8080, http.StatusInternalServerError, nil)
143 xappCfg := s.BuildXappConfig(xappName, ns, cfgData)
144 err := s.ModifyXappConfig(xappCfg)
145 assert.NotNil(t, err)
148 func TestGetAllPodStatus(t *testing.T) {
149 oldCmdExec := sbi.CommandExec
150 defer func() { sbi.CommandExec = oldCmdExec }()
151 sbi.CommandExec = func(args string) (out string, err error) {
152 assert.Equal(t, "/usr/local/bin/kubectl get pod -n ricxapp", args)
153 return kpodOutput, nil
156 expPodList := []sbi.PodStatus{
164 Health: "unavailable",
174 podList, err := s.GetAllPodStatus("ricxapp")
176 assert.Equal(t, podList, expPodList)
179 func TestGetAllPodStatusReturnsErrorIfKubectlCommandFails(t *testing.T) {
180 oldCmdExec := sbi.CommandExec
181 defer func() { sbi.CommandExec = oldCmdExec }()
182 sbi.CommandExec = func(args string) (out string, err error) {
183 assert.Equal(t, "/usr/local/bin/kubectl get pod -n ricxapp", args)
184 return "", errors.New("Some kubectl error")
187 expPodList := make([]sbi.PodStatus, 0)
189 podList, err := s.GetAllPodStatus("ricxapp")
190 assert.NotNil(t, err)
191 assert.Equal(t, podList, expPodList)
194 func TestGetHealthState(t *testing.T) {
195 assert.Equal(t, "healthy", s.GetHealthState("1/1"))
198 func TestGetHealthStateReturnsUnavailableIfReadyStatusUnkown(t *testing.T) {
199 assert.Equal(t, "unavailable", s.GetHealthState(""))
202 func TestGetHealthStateReturnsUnhealthyIfReadyStatusNotUp(t *testing.T) {
203 assert.Equal(t, "unhealthy", s.GetHealthState("0/1"))
206 func TestGetAlerts(t *testing.T) {
207 tim := strfmt.DateTime(time.Now())
208 fingerprint := "34c8f717936f063f"
210 alerts := []models.GettableAlert{
211 models.GettableAlert{
213 Labels: models.LabelSet{
215 "alertname": "E2 CONNECTIVITY LOST TO G-NODEB",
217 "service": "RIC:UEEC",
218 "system_name": "RIC",
221 Annotations: models.LabelSet{
223 "additional_info": "ethernet",
224 "description": "eth12",
225 "instructions": "Not defined",
226 "summary": "Communication error",
231 Fingerprint: &fingerprint,
235 url := "/api/v2/alerts?active=true&inhibited=true&silenced=true&unprocessed=true"
236 ts := createHTTPServer(t, "GET", url, 9093, http.StatusOK, alerts)
239 resp, err := s.GetAlerts()
242 assert.Equal(t, true, resp != nil)
243 assert.NotNil(t, resp)
245 for _, alert := range resp.Payload {
246 assert.Equal(t, alert.Annotations, alerts[0].Annotations)
247 assert.Equal(t, alert.Alert, alerts[0].Alert)
248 assert.Equal(t, alert.Fingerprint, alerts[0].Fingerprint)
252 func TestGetAlertsReturnErrorIfHttpErrorResponse(t *testing.T) {
253 url := "/api/v2/alerts?active=true&inhibited=true&silenced=true&unprocessed=true"
254 ts := createHTTPServer(t, "GET", url, 9093, http.StatusInternalServerError, nil)
257 resp, err := s.GetAlerts()
258 assert.NotNil(t, err)
262 func TestCommandExec(t *testing.T) {
263 resp, err := sbi.CommandExec("date")
264 assert.NotEqual(t, "", resp)
268 func TestCommandExecReturnErrorIfCmdExecutionFails(t *testing.T) {
269 resp, err := sbi.CommandExec("some-not-existing-command")
270 assert.Equal(t, "", resp)
271 assert.NotNil(t, err)
274 func getTestXappDescriptor() *apimodel.XappDescriptor {
275 return s.BuildXappDescriptor(xappName, ns, release, helmVer)
278 func createHTTPServer(t *testing.T, method, url string, port, status int, respData interface{}) *httptest.Server {
279 l, err := net.Listen("tcp", fmt.Sprintf("localhost:%d", port))
281 t.Error("Failed to create listener: " + err.Error())
283 ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
284 assert.Equal(t, r.Method, method)
285 assert.Equal(t, r.URL.String(), url)
286 w.Header().Add("Content-Type", "application/json")
287 w.WriteHeader(status)
288 b, _ := json.Marshal(respData)