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 XappDescriptor = `{
59 "o-ran-sc-ric-xapp-desc-v1:ric": {
64 "release-name": "ueec-xapp",
66 "namespace": "ricxapp"
77 func TestMain(M *testing.M) {
80 n = NewNbi(sbi.NewSBIClient("localhost:8080", "localhost:9093", 5))
82 time.Sleep(time.Duration(1) * time.Second)
87 func TestModifyConfigmap(t *testing.T) {
88 ts := CreateHTTPServer(t, "PUT", "/ric/v1/config", 8080, http.StatusOK, apimodel.ConfigValidationErrors{})
92 err := json.Unmarshal([]byte(XappConfig), &f)
93 assert.Equal(t, true, err == nil)
95 err = n.ManageConfigmaps("o-ran-sc-ric-ueec-config-v1", XappConfig, 1)
96 assert.Equal(t, true, err == nil)
99 func TestXappDescModuleChangeCB(t *testing.T) {
100 ok := n.testModuleChangeCB("o-ran-sc-ric-xapp-desc-v1")
104 func TestUeecConfigModuleChangeCB(t *testing.T) {
105 ok := n.testModuleChangeCB("o-ran-sc-ric-ueec-config-v1")
109 func TestUeecConfigDoneModuleChangeCB(t *testing.T) {
110 ok := n.testModuleChangeCBDone("o-ran-sc-ric-ueec-config-v1")
114 func TestXappDescGnbStateCB(t *testing.T) {
115 ok := n.testGnbStateCB("o-ran-sc-ric-xapp-desc-v1")
119 func TestAlarmGnbStateCB(t *testing.T) {
120 ok := n.testGnbStateCB("o-ran-sc-ric-alarm-v1")
124 func TestGnbStateCB(t *testing.T) {
125 var rnibOk xapp.RNIBIRNibError
126 var gNbIDs []*xapp.RNIBNbIdentity
127 gNbID := xapp.RNIBNbIdentity{
128 InventoryName: "test-gnb",
130 gNbIDs = append(gNbIDs, &gNbID)
131 nodeInfo := xapp.RNIBNodebInfo{}
133 rnibM.On("GetListGnbIds").Return(gNbIDs, rnibOk).Once()
134 rnibM.On("GetNodeb", mock.Anything).Return(&nodeInfo, rnibOk).Once()
135 ok := n.testGnbStateCB("")
139 func TestGnbStateCBWhenRnibGetListGnbIdsFails(t *testing.T) {
140 var rnibErr xapp.RNIBIRNibError = errors.New("Some RNIB Error")
142 rnibM.On("GetListGnbIds").Return(nil, rnibErr).Once()
143 ok := n.testGnbStateCB("")
147 func TestGnbStateCBWhenRnibGetNodebFails(t *testing.T) {
148 var rnibOk xapp.RNIBIRNibError
149 var rnibErr xapp.RNIBIRNibError = errors.New("Some RNIB Error")
150 var gNbIDs []*xapp.RNIBNbIdentity
151 gNbID := xapp.RNIBNbIdentity{
152 InventoryName: "test-gnb",
154 gNbIDs = append(gNbIDs, &gNbID)
156 rnibM.On("GetListGnbIds").Return(gNbIDs, rnibOk).Once()
157 rnibM.On("GetNodeb", mock.Anything).Return(nil, rnibErr).Once()
158 ok := n.testGnbStateCB("")
162 func TestDeployXApp(t *testing.T) {
163 ts := CreateHTTPServer(t, "POST", "/ric/v1/xapps", 8080, http.StatusCreated, apimodel.Xapp{})
167 err := json.Unmarshal([]byte(XappDescriptor), &f)
168 assert.Equal(t, true, err == nil)
170 err = n.ManageXapps("o-ran-sc-ric-xapp-desc-v1", XappDescriptor, 0)
171 assert.Equal(t, true, err == nil)
174 func TestUnDeployXApp(t *testing.T) {
175 ts := CreateHTTPServer(t, "DELETE", "/ric/v1/xapps/ueec-xapp", 8080, http.StatusNoContent, apimodel.Xapp{})
179 err := json.Unmarshal([]byte(XappDescriptor), &f)
180 assert.Equal(t, true, err == nil)
182 err = n.ManageXapps("o-ran-sc-ric-xapp-desc-v1", XappDescriptor, 2)
183 assert.Equal(t, true, err == nil)
186 func TestGetDeployedXapps(t *testing.T) {
187 ts := CreateHTTPServer(t, "GET", "/ric/v1/xapps", 8080, http.StatusOK, apimodel.AllDeployedXapps{})
190 err := sbiClient.GetDeployedXapps()
191 assert.Equal(t, true, err == nil)
194 func TestErrorCases(t *testing.T) {
196 err := n.ManageXapps("o-ran-sc-ric-xapp-desc-v1", "", 2)
197 assert.Equal(t, true, err == nil)
200 err = n.ManageXapps("", "{}", 2)
201 assert.Equal(t, true, err == nil)
204 err = n.ManageXapps("o-ran-sc-ric-ueec-config-v1", "{}", 2)
205 assert.Equal(t, true, err == nil)
208 err = n.ManageXapps("o-ran-sc-ric-ueec-config-v1", XappDescriptor, 1)
209 assert.Equal(t, true, err == nil)
212 err = n.ManageConfigmaps("o-ran-sc-ric-ueec-config-v1", "", 1)
213 assert.Equal(t, true, err == nil)
216 err = n.ManageConfigmaps("o-ran-sc-ric-ueec-config-v1", "{}", 0)
217 assert.Equal(t, true, err != nil)
220 func TestConnStatus2Str(t *testing.T) {
221 assert.Equal(t, n.ConnStatus2Str(0), "not-specified")
222 assert.Equal(t, n.ConnStatus2Str(1), "connected")
223 assert.Equal(t, n.ConnStatus2Str(2), "disconnected")
224 assert.Equal(t, n.ConnStatus2Str(3), "setup-failed")
225 assert.Equal(t, n.ConnStatus2Str(4), "connecting")
226 assert.Equal(t, n.ConnStatus2Str(5), "shutting-down")
227 assert.Equal(t, n.ConnStatus2Str(6), "shutdown")
228 assert.Equal(t, n.ConnStatus2Str(1234), "not-specified")
231 func TestE2APProt2Str(t *testing.T) {
232 assert.Equal(t, n.E2APProt2Str(0), "not-specified")
233 assert.Equal(t, n.E2APProt2Str(1), "x2-setup-request")
234 assert.Equal(t, n.E2APProt2Str(2), "endc-x2-setup-request")
235 assert.Equal(t, n.E2APProt2Str(1111), "not-specified")
238 func TestNodeType2Str(t *testing.T) {
239 assert.Equal(t, n.NodeType2Str(0), "not-specified")
240 assert.Equal(t, n.NodeType2Str(1), "enb")
241 assert.Equal(t, n.NodeType2Str(2), "gnb")
242 assert.Equal(t, n.NodeType2Str(1111), "not-specified")
245 func TestTeardown(t *testing.T) {
249 func CreateHTTPServer(t *testing.T, method, url string, port, status int, respData interface{}) *httptest.Server {
250 l, err := net.Listen("tcp", fmt.Sprintf("localhost:%d", port))
252 t.Error("Failed to create listener: " + err.Error())
254 ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
255 assert.Equal(t, r.Method, method)
256 assert.Equal(t, r.URL.String(), url)
257 w.Header().Add("Content-Type", "application/json")
258 w.WriteHeader(status)
259 b, _ := json.Marshal(respData)
270 func DescMatcher(result, expected *apimodel.XappDescriptor) bool {
271 if *result.XappName == *expected.XappName && result.HelmVersion == expected.HelmVersion &&
272 result.Namespace == expected.Namespace && result.ReleaseName == expected.ReleaseName {
278 func ConfigMatcher(result, expected *apimodel.XAppConfig) bool {
279 if *result.Metadata.XappName == *expected.Metadata.XappName &&
280 *result.Metadata.Namespace == *expected.Metadata.Namespace {
286 type rnibMock struct {
290 func (m *rnibMock) GetListGnbIds() ([]*xapp.RNIBNbIdentity, xapp.RNIBIRNibError) {
293 return nil, a.Error(1)
295 return a.Get(0).([]*xapp.RNIBNbIdentity), a.Error(1)
298 func (m *rnibMock) GetNodeb(invName string) (*xapp.RNIBNodebInfo, xapp.RNIBIRNibError) {
299 a := m.Called(invName)
301 return nil, a.Error(1)
303 return a.Get(0).(*xapp.RNIBNodebInfo), a.Error(1)