3812fd6e2e1c72c5a961c1396bef10626943bcc7
[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         "os"    
24         "time"
25         "encoding/json"
26         "testing"
27         "net"
28     "net/http"
29     "net/http/httptest"
30         "github.com/stretchr/testify/assert"
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
37 var XappConfig = `{
38         "o-ran-sc-ric-ueec-config-v1:ric": {
39           "config": {
40                 "name": "ueec",
41                 "namespace": "ricxapp",
42                 "control": {
43                   "active": true,
44                   "interfaceId": {
45                         "globalENBId": {
46                           "plmnId": "1234",
47                           "eNBId": "55"
48                         }
49                   }
50                 }
51           }
52         }
53   }`
54
55 var XappDescriptor = `{
56         "o-ran-sc-ric-xapp-desc-v1:ric": {
57           "xapps": {
58                 "xapp": [
59                   {
60                         "name": "ueec",
61                         "release-name": "ueec-xapp",
62                         "version": "0.0.1",
63                         "namespace": "ricxapp"
64                   }
65                 ]
66           }
67         }
68   }`
69
70 var n *Nbi
71
72 // Test cases
73 func TestMain(M *testing.M) {
74         n = NewNbi(sbi.NewSBIClient("localhost:8080", "/ric/v1/", []string{"http"}, 5))
75         go n.Start()
76         time.Sleep(time.Duration(1) * time.Second)
77
78         os.Exit(M.Run())
79 }
80
81 func TestModifyConfigmap(t *testing.T) {
82         ts := CreateHTTPServer(t, "PUT", "/ric/v1/config", http.StatusOK, apimodel.ConfigValidationErrors{})
83         defer ts.Close()
84
85         var f interface{}
86         err := json.Unmarshal([]byte(XappConfig), &f)
87         assert.Equal(t, true, err == nil)
88
89         err = n.ManageConfigmaps("o-ran-sc-ric-ueec-config-v1", XappConfig, 1)
90         assert.Equal(t, true, err == nil)
91 }
92
93 func TestDeployXApp(t *testing.T) {
94         ts := CreateHTTPServer(t, "POST", "/ric/v1/xapps", http.StatusCreated, apimodel.Xapp{})
95         defer ts.Close()
96
97         var f interface{}
98         err := json.Unmarshal([]byte(XappDescriptor), &f)
99         assert.Equal(t, true, err == nil)
100
101         err = n.ManageXapps("o-ran-sc-ric-xapp-desc-v1", XappDescriptor, 0)
102         assert.Equal(t, true, err == nil)
103 }
104
105 func TestUnDeployXApp(t *testing.T) {
106         ts := CreateHTTPServer(t, "DELETE", "/ric/v1/xapps/ueec-xapp", http.StatusNoContent, apimodel.Xapp{})
107         defer ts.Close()
108
109         var f interface{}
110         err := json.Unmarshal([]byte(XappDescriptor), &f)
111         assert.Equal(t, true, err == nil)
112
113         err = n.ManageXapps("o-ran-sc-ric-xapp-desc-v1", XappDescriptor, 2)
114         assert.Equal(t, true, err == nil)
115 }
116
117 func TestGetDeployedXapps(t *testing.T) {
118         ts := CreateHTTPServer(t, "GET", "/ric/v1/xapps", http.StatusOK, apimodel.AllDeployedXapps{})
119         defer ts.Close()
120
121         err := sbiClient.GetDeployedXapps()
122         assert.Equal(t, true, err == nil)
123 }
124
125 func TestErrorCases(t *testing.T) {
126         // Invalid config
127         err := n.ManageXapps("o-ran-sc-ric-xapp-desc-v1", "", 2)
128         assert.Equal(t, true, err == nil)
129
130         // Invalid module
131         err = n.ManageXapps("", "{}", 2)
132         assert.Equal(t, true, err == nil)
133
134         // Unexpected module
135         err = n.ManageXapps("o-ran-sc-ric-ueec-config-v1", "{}", 2)
136         assert.Equal(t, true, err == nil)
137
138         // Invalid operation
139         err = n.ManageXapps("o-ran-sc-ric-ueec-config-v1", XappDescriptor, 1)
140         assert.Equal(t, true, err == nil)
141
142         // Invalid config
143         err = n.ManageConfigmaps("o-ran-sc-ric-ueec-config-v1", "", 1)
144         assert.Equal(t, true, err == nil)
145
146         // Invalid module
147         err = n.ManageConfigmaps("", "{}", 1)
148         assert.Equal(t, true, err == nil)
149
150         // Unexpected module
151         err = n.ManageConfigmaps("o-ran-sc-ric-xapp-desc-v1", "{}", 0)
152         assert.Equal(t, true, err == nil)
153
154         // Invalid operation
155         err = n.ManageConfigmaps("o-ran-sc-ric-ueec-config-v1", "{}", 0)
156         assert.Equal(t, true, err != nil)
157 }
158
159 func TestConnStatus2Str(t *testing.T) {
160         assert.Equal(t, n.ConnStatus2Str(0), "not-specified")
161         assert.Equal(t, n.ConnStatus2Str(1), "connected")
162         assert.Equal(t, n.ConnStatus2Str(2), "disconnected")
163         assert.Equal(t, n.ConnStatus2Str(3), "setup-failed")
164         assert.Equal(t, n.ConnStatus2Str(4), "connecting")
165         assert.Equal(t, n.ConnStatus2Str(5), "shutting-down")
166         assert.Equal(t, n.ConnStatus2Str(6), "shutdown")
167         assert.Equal(t, n.ConnStatus2Str(1234), "not-specified")
168 }
169
170 func TestE2APProt2Str(t *testing.T) {
171         assert.Equal(t, n.E2APProt2Str(0), "not-specified")
172         assert.Equal(t, n.E2APProt2Str(1), "x2-setup-request")
173         assert.Equal(t, n.E2APProt2Str(2), "endc-x2-setup-request")
174         assert.Equal(t, n.E2APProt2Str(1111), "not-specified")
175 }
176
177 func TestNodeType2Str(t *testing.T) {
178         assert.Equal(t, n.NodeType2Str(0), "not-specified")
179         assert.Equal(t, n.NodeType2Str(1), "enb")
180         assert.Equal(t, n.NodeType2Str(2), "gnb")
181         assert.Equal(t, n.NodeType2Str(1111), "not-specified")
182 }
183
184 func TestTeardown(t *testing.T) {
185         n.Stop()
186 }
187
188 func CreateHTTPServer(t *testing.T, method, url string, status int, respData interface{}) *httptest.Server {
189         l, err := net.Listen("tcp", "localhost:8080")
190         if err != nil {
191                         t.Error("Failed to create listener: " + err.Error())
192         }
193         ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
194                 assert.Equal(t, r.Method, method)
195                 assert.Equal(t, r.URL.String(), url)
196                 w.Header().Add("Content-Type", "application/json")
197                 w.WriteHeader(status)
198                 b, _ := json.Marshal(respData)
199                 w.Write(b)
200         }))
201         ts.Listener.Close()
202         ts.Listener = l
203
204         ts.Start()
205
206         return ts
207 }
208
209 func DescMatcher(result, expected *apimodel.XappDescriptor) bool {
210         if *result.XappName == *expected.XappName && result.HelmVersion == expected.HelmVersion &&
211                 result.Namespace == expected.Namespace && result.ReleaseName == expected.ReleaseName {
212                 return true
213         }
214         return false
215 }
216
217 func ConfigMatcher(result, expected *apimodel.XAppConfig) bool {
218         if *result.Metadata.XappName == *expected.Metadata.XappName &&
219            *result.Metadata.Namespace == *expected.Metadata.Namespace {
220                 return true
221         }
222         return false
223 }