Update for R4
[ric-plt/o1.git] / agent / pkg / nbi / nbi_test.go
1 /*
2 ==================================================================================
3   Copyright (c) 2019 AT&T Intellectual Property.
4   Copyright (c) 2019 Nokia
5
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
9
10        http://www.apache.org/licenses/LICENSE-2.0
11
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 ==================================================================================
18 */
19
20 package nbi
21
22 import (
23         "encoding/json"
24         "github.com/stretchr/testify/assert"
25         "net"
26         "net/http"
27         "net/http/httptest"
28         "os"
29         "testing"
30         "time"
31
32         apimodel "gerrit.oran-osc.org/r/ric-plt/o1mediator/pkg/appmgrmodel"
33         "gerrit.oran-osc.org/r/ric-plt/o1mediator/pkg/sbi"
34 )
35
36 var XappConfig = `{
37         "o-ran-sc-ric-ueec-config-v1:ric": {
38           "config": {
39                 "name": "ueec",
40                 "namespace": "ricxapp",
41                 "control": {
42                   "active": true,
43                   "interfaceId": {
44                         "globalENBId": {
45                           "plmnId": "1234",
46                           "eNBId": "55"
47                         }
48                   }
49                 }
50           }
51         }
52   }`
53
54 var XappDescriptor = `{
55         "o-ran-sc-ric-xapp-desc-v1:ric": {
56           "xapps": {
57                 "xapp": [
58                   {
59                         "name": "ueec",
60                         "release-name": "ueec-xapp",
61                         "version": "0.0.1",
62                         "namespace": "ricxapp"
63                   }
64                 ]
65           }
66         }
67   }`
68
69 var kpodOutput = `
70 NAME                               READY   STATUS    RESTARTS   AGE
71 ricxapp-ueec-7bfdd587db-2jl9j      1/1     Running   53         29d
72 ricxapp-anr-6748846478-8hmtz       1-1     Running   1          29d
73 ricxapp-dualco-7f76f65c99-5p6c6    0/1     Running   1          29d
74 `
75
76 var n *Nbi
77
78 // Test cases
79 func TestMain(M *testing.M) {
80         n = NewNbi(sbi.NewSBIClient("localhost:8080", "/ric/v1/", []string{"http"}, 5))
81         go n.Start()
82         time.Sleep(time.Duration(1) * time.Second)
83
84         os.Exit(M.Run())
85 }
86
87 func TestModifyConfigmap(t *testing.T) {
88         ts := CreateHTTPServer(t, "PUT", "/ric/v1/config", http.StatusOK, apimodel.ConfigValidationErrors{})
89         defer ts.Close()
90
91         var f interface{}
92         err := json.Unmarshal([]byte(XappConfig), &f)
93         assert.Equal(t, true, err == nil)
94
95         err = n.ManageConfigmaps("o-ran-sc-ric-ueec-config-v1", XappConfig, 1)
96         assert.Equal(t, true, err == nil)
97 }
98
99 func TestDeployXApp(t *testing.T) {
100         ts := CreateHTTPServer(t, "POST", "/ric/v1/xapps", http.StatusCreated, apimodel.Xapp{})
101         defer ts.Close()
102
103         var f interface{}
104         err := json.Unmarshal([]byte(XappDescriptor), &f)
105         assert.Equal(t, true, err == nil)
106
107         err = n.ManageXapps("o-ran-sc-ric-xapp-desc-v1", XappDescriptor, 0)
108         assert.Equal(t, true, err == nil)
109 }
110
111 func TestUnDeployXApp(t *testing.T) {
112         ts := CreateHTTPServer(t, "DELETE", "/ric/v1/xapps/ueec-xapp", http.StatusNoContent, apimodel.Xapp{})
113         defer ts.Close()
114
115         var f interface{}
116         err := json.Unmarshal([]byte(XappDescriptor), &f)
117         assert.Equal(t, true, err == nil)
118
119         err = n.ManageXapps("o-ran-sc-ric-xapp-desc-v1", XappDescriptor, 2)
120         assert.Equal(t, true, err == nil)
121 }
122
123 func TestGetDeployedXapps(t *testing.T) {
124         ts := CreateHTTPServer(t, "GET", "/ric/v1/xapps", http.StatusOK, apimodel.AllDeployedXapps{})
125         defer ts.Close()
126
127         err := sbiClient.GetDeployedXapps()
128         assert.Equal(t, true, err == nil)
129 }
130
131 func TestGetAllPodStatus(t *testing.T) {
132         sbi.CommandExec = func(args string) (out string, err error) {
133                 assert.Equal(t, "/usr/local/bin/kubectl get pod -n ricxapp", args)
134                 return kpodOutput, nil
135         }
136
137         expectedPodList := []sbi.PodStatus{
138                 sbi.PodStatus{
139                         Name:   "ueec",
140                         Health: "healthy",
141                         Status: "Running",
142                 },
143                 sbi.PodStatus{
144                         Name:   "anr",
145                         Health: "unavailable",
146                         Status: "Running",
147                 },
148                 sbi.PodStatus{
149                         Name:   "dualco",
150                         Health: "unhealthy",
151                         Status: "Running",
152                 },
153         }
154
155         podList, err := sbiClient.GetAllPodStatus("ricxapp")
156         assert.Equal(t, true, err == nil)
157         assert.Equal(t, podList, expectedPodList)
158 }
159
160 func TestErrorCases(t *testing.T) {
161         // Invalid config
162         err := n.ManageXapps("o-ran-sc-ric-xapp-desc-v1", "", 2)
163         assert.Equal(t, true, err == nil)
164
165         // Invalid module
166         err = n.ManageXapps("", "{}", 2)
167         assert.Equal(t, true, err == nil)
168
169         // Unexpected module
170         err = n.ManageXapps("o-ran-sc-ric-ueec-config-v1", "{}", 2)
171         assert.Equal(t, true, err == nil)
172
173         // Invalid operation
174         err = n.ManageXapps("o-ran-sc-ric-ueec-config-v1", XappDescriptor, 1)
175         assert.Equal(t, true, err == nil)
176
177         // Invalid config
178         err = n.ManageConfigmaps("o-ran-sc-ric-ueec-config-v1", "", 1)
179         assert.Equal(t, true, err == nil)
180
181         // Invalid operation
182         err = n.ManageConfigmaps("o-ran-sc-ric-ueec-config-v1", "{}", 0)
183         assert.Equal(t, true, err != nil)
184 }
185
186 func TestConnStatus2Str(t *testing.T) {
187         assert.Equal(t, n.ConnStatus2Str(0), "not-specified")
188         assert.Equal(t, n.ConnStatus2Str(1), "connected")
189         assert.Equal(t, n.ConnStatus2Str(2), "disconnected")
190         assert.Equal(t, n.ConnStatus2Str(3), "setup-failed")
191         assert.Equal(t, n.ConnStatus2Str(4), "connecting")
192         assert.Equal(t, n.ConnStatus2Str(5), "shutting-down")
193         assert.Equal(t, n.ConnStatus2Str(6), "shutdown")
194         assert.Equal(t, n.ConnStatus2Str(1234), "not-specified")
195 }
196
197 func TestE2APProt2Str(t *testing.T) {
198         assert.Equal(t, n.E2APProt2Str(0), "not-specified")
199         assert.Equal(t, n.E2APProt2Str(1), "x2-setup-request")
200         assert.Equal(t, n.E2APProt2Str(2), "endc-x2-setup-request")
201         assert.Equal(t, n.E2APProt2Str(1111), "not-specified")
202 }
203
204 func TestNodeType2Str(t *testing.T) {
205         assert.Equal(t, n.NodeType2Str(0), "not-specified")
206         assert.Equal(t, n.NodeType2Str(1), "enb")
207         assert.Equal(t, n.NodeType2Str(2), "gnb")
208         assert.Equal(t, n.NodeType2Str(1111), "not-specified")
209 }
210
211 func TestTeardown(t *testing.T) {
212         n.Stop()
213 }
214
215 func CreateHTTPServer(t *testing.T, method, url string, status int, respData interface{}) *httptest.Server {
216         l, err := net.Listen("tcp", "localhost:8080")
217         if err != nil {
218                 t.Error("Failed to create listener: " + err.Error())
219         }
220         ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
221                 assert.Equal(t, r.Method, method)
222                 assert.Equal(t, r.URL.String(), url)
223                 w.Header().Add("Content-Type", "application/json")
224                 w.WriteHeader(status)
225                 b, _ := json.Marshal(respData)
226                 w.Write(b)
227         }))
228         ts.Listener.Close()
229         ts.Listener = l
230
231         ts.Start()
232
233         return ts
234 }
235
236 func DescMatcher(result, expected *apimodel.XappDescriptor) bool {
237         if *result.XappName == *expected.XappName && result.HelmVersion == expected.HelmVersion &&
238                 result.Namespace == expected.Namespace && result.ReleaseName == expected.ReleaseName {
239                 return true
240         }
241         return false
242 }
243
244 func ConfigMatcher(result, expected *apimodel.XAppConfig) bool {
245         if *result.Metadata.XappName == *expected.Metadata.XappName &&
246                 *result.Metadata.Namespace == *expected.Metadata.Namespace {
247                 return true
248         }
249         return false
250 }