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"
32 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
35 type testingRmrControl struct {
37 syncChan chan struct{}
40 func (tc *testingRmrControl) ReadyCB(data interface{}) {
41 xapp.Logger.Info("testingRmrControl(%s) ReadyCB", tc.desc)
42 tc.syncChan <- struct{}{}
46 func (tc *testingRmrControl) WaitCB() {
50 func (tc *testingRmrControl) init(desc string, rtfile string, port string) {
51 os.Setenv("RMR_SEED_RT", rtfile)
52 os.Setenv("RMR_SRC_ID", "localhost:"+port)
53 xapp.Logger.Info("Using rt file %s", os.Getenv("RMR_SEED_RT"))
54 xapp.Logger.Info("Using src id %s", os.Getenv("RMR_SRC_ID"))
55 tc.desc = strings.ToUpper(desc)
56 tc.syncChan = make(chan struct{})
59 //-----------------------------------------------------------------------------
61 //-----------------------------------------------------------------------------
62 type testingRmrStubControl struct {
64 rmrConChan chan *RMRParams
65 rmrClientTest *xapp.RMRClient
70 func (tc *testingRmrStubControl) GetMsgCnt() uint64 {
74 func (tc *testingRmrStubControl) IncMsgCnt() {
78 func (tc *testingRmrStubControl) DecMsgCnt() {
84 func (tc *testingRmrStubControl) TestMsgCnt(t *testing.T) {
85 if tc.GetMsgCnt() > 0 {
86 testError(t, "(%s) message count expected 0 but is %d", tc.desc, tc.GetMsgCnt())
90 func (tc *testingRmrStubControl) RmrSend(params *RMRParams) (err error) {
92 //NOTE: Do this way until xapp-frame sending is improved
94 xapp.Logger.Info("(%s) RmrSend %s", tc.desc, params.String())
97 for ; i <= 10 && status == false; i++ {
98 status = tc.rmrClientTest.SendMsg(params.RMRParams)
100 xapp.Logger.Info("(%s) RmrSend failed. Retry count %v, %s", tc.desc, i, params.String())
101 time.Sleep(500 * time.Millisecond)
105 err = fmt.Errorf("(%s) RmrSend failed. Retry count %v, %s", tc.desc, i, params.String())
106 xapp.Rmr.Free(params.Mbuf)
111 func (tc *testingRmrStubControl) init(desc string, rtfile string, port string, stat string, consumer xapp.MessageConsumer) {
113 tc.testingRmrControl.init(desc, rtfile, port)
114 tc.rmrConChan = make(chan *RMRParams)
115 tc.rmrClientTest = xapp.NewRMRClientWithParams("tcp:"+port, 4096, 1, stat)
116 tc.rmrClientTest.SetReadyCB(tc.ReadyCB, nil)
117 go tc.rmrClientTest.Start(consumer)
119 allRmrStubs = append(allRmrStubs, tc)
122 var allRmrStubs []*testingRmrStubControl
124 //-----------------------------------------------------------------------------
126 //-----------------------------------------------------------------------------
128 func testError(t *testing.T, pattern string, args ...interface{}) {
129 xapp.Logger.Error(fmt.Sprintf(pattern, args...))
130 t.Errorf(fmt.Sprintf(pattern, args...))
133 func testLog(t *testing.T, pattern string, args ...interface{}) {
134 xapp.Logger.Info(fmt.Sprintf(pattern, args...))
135 t.Logf(fmt.Sprintf(pattern, args...))
138 func testCreateTmpFile(str string) (string, error) {
139 file, err := ioutil.TempFile("/tmp", "*.rt")
143 _, err = file.WriteString(str)
148 return file.Name(), nil
151 //-----------------------------------------------------------------------------
153 //-----------------------------------------------------------------------------
155 var xappConn1 *testingXappStub
156 var xappConn2 *testingXappStub
157 var e2termConn *testingE2termStub
158 var rtmgrHttp *testingHttpRtmgrStub
159 var mainCtrl *testingSubmgrControl
161 func ut_test_init() {
162 xapp.Logger.Info("ut_test_init")
164 //---------------------------------
166 //---------------------------------
167 rtmgrHttp = createNewHttpRtmgrStub("RTMGRSTUB", "8989")
170 //---------------------------------
172 //---------------------------------
175 //Cfg creation won't work like this as xapp-frame reads it during init.
186 "protPort": "tcp:14560",
189 "txMessages": ["RIC_SUB_REQ", "RIC_SUB_DEL_REQ"],
190 "rxMessages": ["RIC_SUB_RESP", "RIC_SUB_FAILURE", "RIC_SUB_DEL_RESP", "RIC_SUB_DEL_FAILURE", "RIC_INDICATION"]
195 "namespaces": ["sdl", "rnib"]
198 "HostAddr" : "localhost",
204 cfgfilename,_ := testCreateTmpFile(cfgstr)
205 defer os.Remove(cfgfilename)
206 os.Setenv("CFG_FILE", cfgfilename)
208 xapp.Logger.Info("Using cfg file %s", os.Getenv("CFG_FILE"))
210 //---------------------------------
211 // Static routetable for rmr
213 // NOTE: Routing table is configured so, that responses
214 // are duplicated to xapp1 and xapp2 instances.
215 // If XID is not matching xapp stub will just
216 // drop message. (Messages 12011, 12012, 12021, 12022)
218 // NOTE2: 55555 message type is for stub rmr connectivity probing
220 // NOTE3: Ports per entity:
223 // -------------------
229 //---------------------------------
231 allrt := `newrt|start
232 mse|12010|-1|localhost:14560
233 mse|12010,localhost:14560|-1|localhost:15560
234 mse|12011,localhost:15560|-1|localhost:14560
235 mse|12012,localhost:15560|-1|localhost:14560
236 mse|12011,localhost:14560|-1|localhost:13660;localhost:13560
237 mse|12012,localhost:14560|-1|localhost:13660;localhost:13560
238 mse|12020|-1|localhost:14560
239 mse|12020,localhost:14560|-1|localhost:15560
240 mse|12021,localhost:15560|-1|localhost:14560
241 mse|12022,localhost:15560|-1|localhost:14560
242 mse|12021,localhost:14560|-1|localhost:13660;localhost:13560
243 mse|12022,localhost:14560|-1|localhost:13660;localhost:13560
244 mse|55555|-1|localhost:13660;localhost:13560,localhost:15560
248 //---------------------------------
250 //---------------------------------
251 xapp.Logger.Info("### submgr ctrl run ###")
253 subrtfilename, _ := testCreateTmpFile(subsrt)
254 defer os.Remove(subrtfilename)
255 mainCtrl = createSubmgrControl("main", subrtfilename, "14560")
257 //---------------------------------
259 //---------------------------------
260 xapp.Logger.Info("### xapp1 stub run ###")
262 xapprtfilename1, _ := testCreateTmpFile(xapprt1)
263 defer os.Remove(xapprtfilename1)
264 xappConn1 = createNewXappStub("xappstub1", xapprtfilename1, "13560", "RMRXAPP1STUB")
266 //---------------------------------
268 //---------------------------------
269 xapp.Logger.Info("### xapp2 stub run ###")
271 xapprtfilename2, _ := testCreateTmpFile(xapprt2)
272 defer os.Remove(xapprtfilename2)
273 xappConn2 = createNewXappStub("xappstub2", xapprtfilename2, "13660", "RMRXAPP2STUB")
275 //---------------------------------
277 //---------------------------------
278 xapp.Logger.Info("### e2term stub run ###")
280 e2termrtfilename, _ := testCreateTmpFile(e2termrt)
281 defer os.Remove(e2termrtfilename)
282 e2termConn = createNewE2termStub("e2termstub", e2termrtfilename, "15560", "RMRE2TERMSTUB")
284 //---------------------------------
285 // Testing message sending
286 //---------------------------------
287 var dummyBuf []byte = make([]byte, 100)
289 params := &RMRParams{&xapp.RMRParams{}}
292 params.Payload = dummyBuf
293 params.PayloadLen = 100
294 params.Meid = &xapp.RMRMeid{RanName: "NONEXISTINGRAN"}
295 params.Xid = "THISISTESTFORSTUBS"
300 for ; i <= 10 && status == false; i++ {
301 xapp.Rmr.Send(params.RMRParams, false)
304 for _, val := range allRmrStubs {
305 if val.active == false {
313 xapp.Logger.Info("Sleep 0.5 secs and try routes again")
314 time.Sleep(500 * time.Millisecond)
318 xapp.Logger.Error("Could not initialize routes")
323 //-----------------------------------------------------------------------------
325 //-----------------------------------------------------------------------------
326 func TestMain(m *testing.M) {
327 xapp.Logger.Info("TestMain start")