2 ==================================================================================
3 Copyright (c) 2019 AT&T Intellectual Property.
4 Copyright (c) 2019 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 ==================================================================================
25 "github.com/stretchr/testify/assert"
34 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
35 apimodel "gerrit.oran-osc.org/r/ric-plt/o1mediator/pkg/appmgrmodel"
36 "gerrit.oran-osc.org/r/ric-plt/o1mediator/pkg/sbi"
37 "github.com/stretchr/testify/mock"
41 "o-ran-sc-ric-ueec-config-v1:ric": {
44 "namespace": "ricxapp",
58 var XappConfigErr = `{
62 var XappDescriptor = `{
63 "o-ran-sc-ric-xapp-desc-v1:ric": {
68 "release-name": "ueec-xapp",
70 "namespace": "ricxapp"
81 func TestMain(M *testing.M) {
84 n = NewNbi(sbi.NewSBIClient("localhost:8080", "localhost:9093", 5))
86 time.Sleep(time.Duration(1) * time.Second)
91 func TestModifyConfigmap(t *testing.T) {
92 ts := CreateHTTPServer(t, "PUT", "/ric/v1/config", 8080, http.StatusOK, apimodel.ConfigValidationErrors{})
96 err := json.Unmarshal([]byte(XappConfig), &f)
97 assert.Equal(t, true, err == nil)
99 err = n.ManageConfigmaps("o-ran-sc-ric-ueec-config-v1", XappConfig, 1)
100 assert.Equal(t, true, err == nil)
103 func TestXappDescModuleChangeCB(t *testing.T) {
104 ok := n.testModuleChangeCB("o-ran-sc-ric-xapp-desc-v1")
108 func TestUeecConfigModuleChangeCB(t *testing.T) {
109 ok := n.testModuleChangeCB("o-ran-sc-ric-ueec-config-v1")
113 func TestUeecConfigDoneModuleChangeCB(t *testing.T) {
114 ok := n.testModuleChangeCBDone("o-ran-sc-ric-ueec-config-v1")
118 func TestXappDescGnbStateCB(t *testing.T) {
119 ok := n.testGnbStateCB("o-ran-sc-ric-xapp-desc-v1")
123 func TestAlarmGnbStateCB(t *testing.T) {
124 ok := n.testGnbStateCB("o-ran-sc-ric-alarm-v1")
128 func TestGnbStateCB(t *testing.T) {
129 var rnibOk xapp.RNIBIRNibError
130 var gNbIDs []*xapp.RNIBNbIdentity
131 gNbID := xapp.RNIBNbIdentity{
132 InventoryName: "test-gnb",
134 gNbIDs = append(gNbIDs, &gNbID)
135 nodeInfo := xapp.RNIBNodebInfo{}
137 rnibM.On("GetListGnbIds").Return(gNbIDs, rnibOk).Once()
138 rnibM.On("GetNodeb", mock.Anything).Return(&nodeInfo, rnibOk).Once()
139 ok := n.testGnbStateCB("")
143 func TestGnbStateCBWhenRnibGetListGnbIdsFails(t *testing.T) {
144 var rnibErr xapp.RNIBIRNibError = errors.New("Some RNIB Error")
146 rnibM.On("GetListGnbIds").Return(nil, rnibErr).Once()
147 ok := n.testGnbStateCB("")
151 func TestGnbStateCBWhenRnibGetNodebFails(t *testing.T) {
152 var rnibOk xapp.RNIBIRNibError
153 var rnibErr xapp.RNIBIRNibError = errors.New("Some RNIB Error")
154 var gNbIDs []*xapp.RNIBNbIdentity
155 gNbID := xapp.RNIBNbIdentity{
156 InventoryName: "test-gnb",
158 gNbIDs = append(gNbIDs, &gNbID)
160 rnibM.On("GetListGnbIds").Return(gNbIDs, rnibOk).Once()
161 rnibM.On("GetNodeb", mock.Anything).Return(nil, rnibErr).Once()
162 ok := n.testGnbStateCB("")
166 func TestDeployXApp(t *testing.T) {
167 ts := CreateHTTPServer(t, "POST", "/ric/v1/xapps", 8080, http.StatusCreated, apimodel.Xapp{})
171 err := json.Unmarshal([]byte(XappDescriptor), &f)
172 assert.Equal(t, true, err == nil)
174 err = n.ManageXapps("o-ran-sc-ric-xapp-desc-v1", XappDescriptor, 0)
175 assert.Equal(t, true, err == nil)
178 func TestUnDeployXApp(t *testing.T) {
179 ts := CreateHTTPServer(t, "DELETE", "/ric/v1/xapps/ueec-xapp", 8080, http.StatusNoContent, apimodel.Xapp{})
183 err := json.Unmarshal([]byte(XappDescriptor), &f)
184 assert.Equal(t, true, err == nil)
186 err = n.ManageXapps("o-ran-sc-ric-xapp-desc-v1", XappDescriptor, 2)
187 assert.Equal(t, true, err == nil)
190 func TestGetDeployedXapps(t *testing.T) {
191 ts := CreateHTTPServer(t, "GET", "/ric/v1/xapps", 8080, http.StatusOK, apimodel.AllDeployedXapps{})
194 err := sbiClient.GetDeployedXapps()
195 assert.Equal(t, true, err == nil)
198 func TestErrorCases(t *testing.T) {
200 err := n.ManageXapps("o-ran-sc-ric-xapp-desc-v1", "", 2)
201 assert.Equal(t, true, err == nil)
204 err = n.ManageXapps("", "{}", 2)
205 assert.Equal(t, true, err == nil)
208 err = n.ManageXapps("o-ran-sc-ric-ueec-config-v1", "{}", 2)
209 assert.Equal(t, true, err == nil)
212 err = n.ManageXapps("o-ran-sc-ric-ueec-config-v1", XappDescriptor, 1)
213 assert.Equal(t, true, err == nil)
216 err = n.ManageConfigmaps("o-ran-sc-ric-ueec-config-v1", "", 1)
217 assert.Equal(t, true, err == nil)
220 err = n.ManageConfigmaps("o-ran-sc-ric-ueec-config-v1", "{}", 0)
221 assert.Equal(t, true, err != nil)
224 err = n.ManageConfigmaps("o-ran-sc-ric-ueec-config-v1", XappConfigErr, 1)
225 assert.Equal(t, true, err != nil)
228 err = n.ManageXapps("o-ran-sc-ric-ueec-config-v1", XappDescriptor, 5)
229 assert.Equal(t, true, err == nil)
232 err = n.ManageXapps("o-ran-sc-ric-ueec-config-v1", XappConfigErr, 1)
233 assert.Equal(t, true, err != nil)
236 func TestConnStatus2Str(t *testing.T) {
237 assert.Equal(t, n.ConnStatus2Str(0), "not-specified")
238 assert.Equal(t, n.ConnStatus2Str(1), "connected")
239 assert.Equal(t, n.ConnStatus2Str(2), "disconnected")
240 assert.Equal(t, n.ConnStatus2Str(3), "setup-failed")
241 assert.Equal(t, n.ConnStatus2Str(4), "connecting")
242 assert.Equal(t, n.ConnStatus2Str(5), "shutting-down")
243 assert.Equal(t, n.ConnStatus2Str(6), "shutdown")
244 assert.Equal(t, n.ConnStatus2Str(1234), "not-specified")
247 func TestE2APProt2Str(t *testing.T) {
248 assert.Equal(t, n.E2APProt2Str(0), "not-specified")
249 assert.Equal(t, n.E2APProt2Str(1), "x2-setup-request")
250 assert.Equal(t, n.E2APProt2Str(2), "endc-x2-setup-request")
251 assert.Equal(t, n.E2APProt2Str(1111), "not-specified")
254 func TestNodeType2Str(t *testing.T) {
255 assert.Equal(t, n.NodeType2Str(0), "not-specified")
256 assert.Equal(t, n.NodeType2Str(1), "enb")
257 assert.Equal(t, n.NodeType2Str(2), "gnb")
258 assert.Equal(t, n.NodeType2Str(1111), "not-specified")
261 func TestTeardown(t *testing.T) {
265 func CreateHTTPServer(t *testing.T, method, url string, port, status int, respData interface{}) *httptest.Server {
266 l, err := net.Listen("tcp", fmt.Sprintf("localhost:%d", port))
268 t.Error("Failed to create listener: " + err.Error())
270 ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
271 assert.Equal(t, r.Method, method)
272 assert.Equal(t, r.URL.String(), url)
273 w.Header().Add("Content-Type", "application/json")
274 w.WriteHeader(status)
275 b, _ := json.Marshal(respData)
286 func DescMatcher(result, expected *apimodel.XappDescriptor) bool {
287 if *result.XappName == *expected.XappName && result.HelmVersion == expected.HelmVersion &&
288 result.Namespace == expected.Namespace && result.ReleaseName == expected.ReleaseName {
294 func ConfigMatcher(result, expected *apimodel.XAppConfig) bool {
295 if *result.Metadata.XappName == *expected.Metadata.XappName &&
296 *result.Metadata.Namespace == *expected.Metadata.Namespace {
302 type rnibMock struct {
306 func (m *rnibMock) GetListGnbIds() ([]*xapp.RNIBNbIdentity, xapp.RNIBIRNibError) {
309 return nil, a.Error(1)
311 return a.Get(0).([]*xapp.RNIBNbIdentity), a.Error(1)
314 func (m *rnibMock) GetNodeb(invName string) (*xapp.RNIBNodebInfo, xapp.RNIBIRNibError) {
315 a := m.Called(invName)
317 return nil, a.Error(1)
319 return a.Get(0).(*xapp.RNIBNodebInfo), a.Error(1)