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 ==================================================================================
24 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
36 type testingRmrControl struct {
39 syncChan chan struct{}
42 func (tc *testingRmrControl) Lock() {
46 func (tc *testingRmrControl) Unlock() {
50 func (tc *testingRmrControl) GetDesc() string {
54 func (tc *testingRmrControl) ReadyCB(data interface{}) {
55 xapp.Logger.Info("testingRmrControl(%s) ReadyCB", tc.GetDesc())
56 tc.syncChan <- struct{}{}
60 func (tc *testingRmrControl) WaitCB() {
64 func (tc *testingRmrControl) init(desc string, rtfile string, port string) {
65 os.Setenv("RMR_SEED_RT", rtfile)
66 os.Setenv("RMR_SRC_ID", "localhost:"+port)
67 xapp.Logger.Info("Using rt file %s", os.Getenv("RMR_SEED_RT"))
68 xapp.Logger.Info("Using src id %s", os.Getenv("RMR_SRC_ID"))
69 tc.desc = strings.ToUpper(desc)
70 tc.syncChan = make(chan struct{})
73 //-----------------------------------------------------------------------------
75 //-----------------------------------------------------------------------------
76 type testingRmrStubControl struct {
78 rmrConChan chan *RMRParams
79 rmrClientTest *xapp.RMRClient
84 func (tc *testingRmrStubControl) GetMsgCnt() uint64 {
88 func (tc *testingRmrStubControl) IncMsgCnt() {
92 func (tc *testingRmrStubControl) DecMsgCnt() {
98 func (tc *testingRmrStubControl) TestMsgCnt(t *testing.T) {
99 if tc.GetMsgCnt() > 0 {
100 testError(t, "(%s) message count expected 0 but is %d", tc.GetDesc(), tc.GetMsgCnt())
104 func (tc *testingRmrStubControl) RmrSend(params *RMRParams) (err error) {
106 //NOTE: Do this way until xapp-frame sending is improved
108 xapp.Logger.Info("(%s) RmrSend %s", tc.GetDesc(), params.String())
111 for ; i <= 10 && status == false; i++ {
112 status = tc.rmrClientTest.SendMsg(params.RMRParams)
114 xapp.Logger.Info("(%s) RmrSend failed. Retry count %v, %s", tc.GetDesc(), i, params.String())
115 time.Sleep(500 * time.Millisecond)
119 err = fmt.Errorf("(%s) RmrSend failed. Retry count %v, %s", tc.GetDesc(), i, params.String())
120 xapp.Rmr.Free(params.Mbuf)
125 func (tc *testingRmrStubControl) init(desc string, rtfile string, port string, stat string, consumer xapp.MessageConsumer) {
127 tc.testingRmrControl.init(desc, rtfile, port)
128 tc.rmrConChan = make(chan *RMRParams)
129 tc.rmrClientTest = xapp.NewRMRClientWithParams("tcp:"+port, 4096, 1, stat)
130 tc.rmrClientTest.SetReadyCB(tc.ReadyCB, nil)
131 go tc.rmrClientTest.Start(consumer)
133 allRmrStubs = append(allRmrStubs, tc)
136 var allRmrStubs []*testingRmrStubControl
138 //-----------------------------------------------------------------------------
140 //-----------------------------------------------------------------------------
142 func testError(t *testing.T, pattern string, args ...interface{}) {
143 xapp.Logger.Error(fmt.Sprintf(pattern, args...))
144 t.Errorf(fmt.Sprintf(pattern, args...))
147 func testLog(t *testing.T, pattern string, args ...interface{}) {
148 xapp.Logger.Info(fmt.Sprintf(pattern, args...))
149 t.Logf(fmt.Sprintf(pattern, args...))
152 func testCreateTmpFile(str string) (string, error) {
153 file, err := ioutil.TempFile("/tmp", "*.rt")
157 _, err = file.WriteString(str)
162 return file.Name(), nil
165 //-----------------------------------------------------------------------------
167 //-----------------------------------------------------------------------------
169 var xappConn1 *testingXappStub
170 var xappConn2 *testingXappStub
171 var e2termConn *testingE2termStub
172 var rtmgrHttp *testingHttpRtmgrStub
173 var mainCtrl *testingSubmgrControl
175 func ut_test_init() {
176 xapp.Logger.Info("ut_test_init")
178 //---------------------------------
180 //---------------------------------
181 rtmgrHttp = createNewHttpRtmgrStub("RTMGRSTUB", "8989")
184 //---------------------------------
186 //---------------------------------
189 //Cfg creation won't work like this as xapp-frame reads it during init.
200 "protPort": "tcp:14560",
203 "txMessages": ["RIC_SUB_REQ", "RIC_SUB_DEL_REQ"],
204 "rxMessages": ["RIC_SUB_RESP", "RIC_SUB_FAILURE", "RIC_SUB_DEL_RESP", "RIC_SUB_DEL_FAILURE", "RIC_INDICATION"]
209 "namespaces": ["sdl", "rnib"]
212 "HostAddr" : "localhost",
218 cfgfilename,_ := testCreateTmpFile(cfgstr)
219 defer os.Remove(cfgfilename)
220 os.Setenv("CFG_FILE", cfgfilename)
222 xapp.Logger.Info("Using cfg file %s", os.Getenv("CFG_FILE"))
224 //---------------------------------
225 // Static routetable for rmr
227 // NOTE: Routing table is configured so, that responses
228 // are duplicated to xapp1 and xapp2 instances.
229 // If XID is not matching xapp stub will just
230 // drop message. (Messages 12011, 12012, 12021, 12022)
232 // NOTE2: 55555 message type is for stub rmr connectivity probing
234 // NOTE3: Ports per entity:
237 // -------------------
243 //---------------------------------
245 allrt := `newrt|start
246 mse|12010|-1|localhost:14560
247 mse|12010,localhost:14560|-1|localhost:15560
248 mse|12011,localhost:15560|-1|localhost:14560
249 mse|12012,localhost:15560|-1|localhost:14560
250 mse|12011,localhost:14560|-1|localhost:13660;localhost:13560
251 mse|12012,localhost:14560|-1|localhost:13660;localhost:13560
252 mse|12020|-1|localhost:14560
253 mse|12020,localhost:14560|-1|localhost:15560
254 mse|12021,localhost:15560|-1|localhost:14560
255 mse|12022,localhost:15560|-1|localhost:14560
256 mse|12021,localhost:14560|-1|localhost:13660;localhost:13560
257 mse|12022,localhost:14560|-1|localhost:13660;localhost:13560
258 mse|55555|-1|localhost:13660;localhost:13560,localhost:15560
262 //---------------------------------
264 //---------------------------------
265 xapp.Logger.Info("### submgr ctrl run ###")
267 subrtfilename, _ := testCreateTmpFile(subsrt)
268 defer os.Remove(subrtfilename)
269 mainCtrl = createSubmgrControl("main", subrtfilename, "14560")
271 //---------------------------------
273 //---------------------------------
274 xapp.Logger.Info("### xapp1 stub run ###")
276 xapprtfilename1, _ := testCreateTmpFile(xapprt1)
277 defer os.Remove(xapprtfilename1)
278 xappConn1 = createNewXappStub("xappstub1", xapprtfilename1, "13560", "RMRXAPP1STUB")
280 //---------------------------------
282 //---------------------------------
283 xapp.Logger.Info("### xapp2 stub run ###")
285 xapprtfilename2, _ := testCreateTmpFile(xapprt2)
286 defer os.Remove(xapprtfilename2)
287 xappConn2 = createNewXappStub("xappstub2", xapprtfilename2, "13660", "RMRXAPP2STUB")
289 //---------------------------------
291 //---------------------------------
292 xapp.Logger.Info("### e2term stub run ###")
294 e2termrtfilename, _ := testCreateTmpFile(e2termrt)
295 defer os.Remove(e2termrtfilename)
296 e2termConn = createNewE2termStub("e2termstub", e2termrtfilename, "15560", "RMRE2TERMSTUB")
298 //---------------------------------
299 // Testing message sending
300 //---------------------------------
301 var dummyBuf []byte = make([]byte, 100)
303 params := &RMRParams{&xapp.RMRParams{}}
306 params.Payload = dummyBuf
307 params.PayloadLen = 100
308 params.Meid = &xapp.RMRMeid{RanName: "NONEXISTINGRAN"}
309 params.Xid = "THISISTESTFORSTUBS"
314 for ; i <= 10 && status == false; i++ {
315 xapp.Rmr.Send(params.RMRParams, false)
318 for _, val := range allRmrStubs {
319 if val.active == false {
327 xapp.Logger.Info("Sleep 0.5 secs and try routes again")
328 time.Sleep(500 * time.Millisecond)
332 xapp.Logger.Error("Could not initialize routes")
337 //-----------------------------------------------------------------------------
339 //-----------------------------------------------------------------------------
340 func TestMain(m *testing.M) {
341 xapp.Logger.Info("TestMain start")