package control
import (
- "fmt"
+ "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/teststub"
+ "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/teststubdummy"
+ "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/teststube2ap"
"gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
- "io/ioutil"
"os"
- "strings"
- "sync"
"testing"
"time"
)
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
-type testingRmrControl struct {
- desc string
- mutex sync.Mutex
- syncChan chan struct{}
+func CaseBegin(desc string) *teststub.TestWrapper {
+ tent := teststub.NewTestWrapper(desc)
+ tent.Info(desc)
+ return tent
}
-func (tc *testingRmrControl) Lock() {
- tc.mutex.Lock()
-}
-
-func (tc *testingRmrControl) Unlock() {
- tc.mutex.Unlock()
-}
-
-func (tc *testingRmrControl) GetDesc() string {
- return tc.desc
-}
-
-func (tc *testingRmrControl) ReadyCB(data interface{}) {
- xapp.Logger.Info("testingRmrControl(%s) ReadyCB", tc.GetDesc())
- tc.syncChan <- struct{}{}
- return
-}
-
-func (tc *testingRmrControl) WaitCB() {
- <-tc.syncChan
-}
-
-func (tc *testingRmrControl) init(desc string, rtfile string, port string) {
- os.Setenv("RMR_SEED_RT", rtfile)
- os.Setenv("RMR_SRC_ID", "localhost:"+port)
- xapp.Logger.Info("Using rt file %s", os.Getenv("RMR_SEED_RT"))
- xapp.Logger.Info("Using src id %s", os.Getenv("RMR_SRC_ID"))
- tc.desc = strings.ToUpper(desc)
- tc.syncChan = make(chan struct{})
-}
-
-//-----------------------------------------------------------------------------
-//
-//-----------------------------------------------------------------------------
-type testingRmrStubControl struct {
- testingRmrControl
- rmrConChan chan *RMRParams
- rmrClientTest *xapp.RMRClient
- active bool
- msgCnt uint64
-}
-
-func (tc *testingRmrStubControl) GetMsgCnt() uint64 {
- return tc.msgCnt
-}
-
-func (tc *testingRmrStubControl) IncMsgCnt() {
- tc.msgCnt++
-}
-
-func (tc *testingRmrStubControl) DecMsgCnt() {
- if tc.msgCnt > 0 {
- tc.msgCnt--
- }
-}
-
-func (tc *testingRmrStubControl) TestMsgCnt(t *testing.T) {
- if tc.GetMsgCnt() > 0 {
- testError(t, "(%s) message count expected 0 but is %d", tc.GetDesc(), tc.GetMsgCnt())
- }
-}
-
-func (tc *testingRmrStubControl) RmrSend(params *RMRParams) (err error) {
- //
- //NOTE: Do this way until xapp-frame sending is improved
- //
- xapp.Logger.Info("(%s) RmrSend %s", tc.GetDesc(), params.String())
- status := false
- i := 1
- for ; i <= 10 && status == false; i++ {
- status = tc.rmrClientTest.SendMsg(params.RMRParams)
- if status == false {
- xapp.Logger.Info("(%s) RmrSend failed. Retry count %v, %s", tc.GetDesc(), i, params.String())
- time.Sleep(500 * time.Millisecond)
- }
- }
- if status == false {
- err = fmt.Errorf("(%s) RmrSend failed. Retry count %v, %s", tc.GetDesc(), i, params.String())
- xapp.Rmr.Free(params.Mbuf)
- }
- return
-}
-
-func (tc *testingRmrStubControl) init(desc string, rtfile string, port string, stat string, consumer xapp.MessageConsumer) {
- tc.active = false
- tc.testingRmrControl.init(desc, rtfile, port)
- tc.rmrConChan = make(chan *RMRParams)
- tc.rmrClientTest = xapp.NewRMRClientWithParams("tcp:"+port, 4096, 1, stat)
- tc.rmrClientTest.SetReadyCB(tc.ReadyCB, nil)
- go tc.rmrClientTest.Start(consumer)
- tc.WaitCB()
- allRmrStubs = append(allRmrStubs, tc)
-}
-
-var allRmrStubs []*testingRmrStubControl
-
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
-
-func testError(t *testing.T, pattern string, args ...interface{}) {
- xapp.Logger.Error(fmt.Sprintf(pattern, args...))
- t.Errorf(fmt.Sprintf(pattern, args...))
-}
-
-func testLog(t *testing.T, pattern string, args ...interface{}) {
- xapp.Logger.Info(fmt.Sprintf(pattern, args...))
- t.Logf(fmt.Sprintf(pattern, args...))
-}
-
-func testCreateTmpFile(str string) (string, error) {
- file, err := ioutil.TempFile("/tmp", "*.rt")
- if err != nil {
- return "", err
- }
- _, err = file.WriteString(str)
- if err != nil {
- file.Close()
- return "", err
- }
- return file.Name(), nil
-}
+const teststubPortSeed int = 55555
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
-var xappConn1 *testingXappStub
-var xappConn2 *testingXappStub
-var e2termConn *testingE2termStub
+var xappConn1 *teststube2ap.E2Stub
+var xappConn2 *teststube2ap.E2Stub
+var e2termConn1 *teststube2ap.E2Stub
+var e2termConn2 *teststube2ap.E2Stub
var rtmgrHttp *testingHttpRtmgrStub
var mainCtrl *testingSubmgrControl
-func ut_test_init() {
- xapp.Logger.Info("ut_test_init")
+var dummystub *teststubdummy.RmrDummyStub
+
+func ut_test_init() func() {
+ tent := CaseBegin("ut_test_init")
//---------------------------------
//
defer os.Remove(cfgfilename)
os.Setenv("CFG_FILE", cfgfilename)
*/
- xapp.Logger.Info("Using cfg file %s", os.Getenv("CFG_FILE"))
+ tent.Info("Using cfg file %s", os.Getenv("CFG_FILE"))
//---------------------------------
// Static routetable for rmr
// If XID is not matching xapp stub will just
// drop message. (Messages 12011, 12012, 12021, 12022)
//
- // NOTE2: 55555 message type is for stub rmr connectivity probing
+ // NOTE2: teststubPortSeed message type is for stub rmr connectivity probing
//
// NOTE3: Ports per entity:
//
- // Port Entity
+ // Entity
// -------------------
- // 14560 submgr
- // 15560 e2term stub
- // 13560 xapp1 stub
- // 13660 xapp2 stub
+
+ mainsrc := teststub.RmrSrcId{xapp.RmrEndpoint{"localhost", 14560}}
+ xapp1src := teststub.RmrSrcId{xapp.RmrEndpoint{"localhost", 13560}}
+ xapp2src := teststub.RmrSrcId{xapp.RmrEndpoint{"localhost", 13660}}
+ e2term1src := teststub.RmrSrcId{xapp.RmrEndpoint{"localhost", 15560}}
+ e2term2src := teststub.RmrSrcId{xapp.RmrEndpoint{"localhost", 15660}}
+ dummysrc := teststub.RmrSrcId{xapp.RmrEndpoint{"localhost", 16560}}
+
+ //---------------------------------
+ rt := &teststub.RmrRouteTable{}
+ rt.AddRoute(12010, "", -1, mainsrc.String())
+ rt.AddRoute(12010, mainsrc.String(), -1, "%meid")
+ rt.AddRoute(12011, e2term1src.String(), -1, mainsrc.String())
+ rt.AddRoute(12012, e2term1src.String(), -1, mainsrc.String())
+ rt.AddRoute(12011, e2term2src.String(), -1, mainsrc.String())
+ rt.AddRoute(12012, e2term2src.String(), -1, mainsrc.String())
+ rt.AddRoute(12011, mainsrc.String(), -1, xapp2src.String()+";"+xapp1src.String())
+ rt.AddRoute(12012, mainsrc.String(), -1, xapp2src.String()+";"+xapp1src.String())
+ rt.AddRoute(12020, "", -1, mainsrc.String())
+ rt.AddRoute(12020, mainsrc.String(), -1, "%meid")
+ rt.AddRoute(12021, e2term1src.String(), -1, mainsrc.String())
+ rt.AddRoute(12022, e2term1src.String(), -1, mainsrc.String())
+ rt.AddRoute(12021, e2term2src.String(), -1, mainsrc.String())
+ rt.AddRoute(12022, e2term2src.String(), -1, mainsrc.String())
+ rt.AddRoute(12021, mainsrc.String(), -1, xapp2src.String()+";"+xapp1src.String())
+ rt.AddRoute(12022, mainsrc.String(), -1, xapp2src.String()+";"+xapp1src.String())
+ rt.AddRoute(teststubPortSeed, "", -1, xapp2src.String()+";"+xapp1src.String()+";"+e2term1src.String()+";"+e2term2src.String()+";"+dummysrc.String())
+
+ rt.AddMeid(e2term1src.String(), []string{"RAN_NAME_1", "RAN_NAME_2"})
+ rt.AddMeid(e2term2src.String(), []string{"RAN_NAME_11", "RAN_NAME_12"})
+
+ rt.Enable()
+ tent.Info("rttable[%s]", rt.Table())
+
+ //---------------------------------
//
//---------------------------------
+ tent.Info("### submgr ctrl run ###")
+ mainCtrl = createSubmgrControl(mainsrc, teststub.RmrRtgSvc{})
+
+ //
+ // Tweak to get around with fact that alarm package alternates RMR_SEED_RT environment variable
+ //
+ //xapp-frame inits alarms when readycb is coming from xapps rmr
+ //alarm will make new rmr instance and overrides RMR_SEED_RT and RMR_RTG_SVC
+ //env variables. Re-create rt info.
+ for i := 0; i < int(10)*2; i++ {
+ if os.Getenv("RMR_SEED_RT") == rt.FileName() {
+ tent.Info("Waiting that alarm alternates RMR_SEED_RT=%s", os.Getenv("RMR_SEED_RT"))
+ time.Sleep(500 * time.Millisecond)
+ } else {
+ tent.Info("Alarm has alternated RMR_SEED_RT=%s, so waiting 0.5 secs before restoring it", os.Getenv("RMR_SEED_RT"))
+ time.Sleep(500 * time.Millisecond)
+ rt.Enable()
+ tent.Info("rttable[%s]", rt.Table())
+ break
+ }
+ }
- allrt := `newrt|start
-mse|12010|-1|localhost:14560
-mse|12010,localhost:14560|-1|localhost:15560
-mse|12011,localhost:15560|-1|localhost:14560
-mse|12012,localhost:15560|-1|localhost:14560
-mse|12011,localhost:14560|-1|localhost:13660;localhost:13560
-mse|12012,localhost:14560|-1|localhost:13660;localhost:13560
-mse|12020|-1|localhost:14560
-mse|12020,localhost:14560|-1|localhost:15560
-mse|12021,localhost:15560|-1|localhost:14560
-mse|12022,localhost:15560|-1|localhost:14560
-mse|12021,localhost:14560|-1|localhost:13660;localhost:13560
-mse|12022,localhost:14560|-1|localhost:13660;localhost:13560
-mse|55555|-1|localhost:13660;localhost:13560,localhost:15560
-newrt|end
-`
+ if os.Getenv("RMR_SEED_RT") != rt.FileName() {
+ tent.Error("Unittest timing issue with alarm RMR_SEED_RT=%s", os.Getenv("RMR_SEED_RT"))
+ os.Exit(1)
+ }
//---------------------------------
//
//---------------------------------
- xapp.Logger.Info("### submgr ctrl run ###")
- subsrt := allrt
- subrtfilename, _ := testCreateTmpFile(subsrt)
- defer os.Remove(subrtfilename)
- mainCtrl = createSubmgrControl("main", subrtfilename, "14560")
+ tent.Info("### xapp1 stub run ###")
+ xappConn1 = teststube2ap.CreateNewE2Stub("xappstub1", xapp1src, teststub.RmrRtgSvc{}, "RMRXAPP1STUB", teststubPortSeed)
//---------------------------------
//
//---------------------------------
- xapp.Logger.Info("### xapp1 stub run ###")
- xapprt1 := allrt
- xapprtfilename1, _ := testCreateTmpFile(xapprt1)
- defer os.Remove(xapprtfilename1)
- xappConn1 = createNewXappStub("xappstub1", xapprtfilename1, "13560", "RMRXAPP1STUB")
+ tent.Info("### xapp2 stub run ###")
+ xappConn2 = teststube2ap.CreateNewE2Stub("xappstub2", xapp2src, teststub.RmrRtgSvc{}, "RMRXAPP2STUB", teststubPortSeed)
//---------------------------------
//
//---------------------------------
- xapp.Logger.Info("### xapp2 stub run ###")
- xapprt2 := allrt
- xapprtfilename2, _ := testCreateTmpFile(xapprt2)
- defer os.Remove(xapprtfilename2)
- xappConn2 = createNewXappStub("xappstub2", xapprtfilename2, "13660", "RMRXAPP2STUB")
+ tent.Info("### e2term1 stub run ###")
+ e2termConn1 = teststube2ap.CreateNewE2termStub("e2termstub1", e2term1src, teststub.RmrRtgSvc{}, "RMRE2TERMSTUB1", teststubPortSeed)
//---------------------------------
//
//---------------------------------
- xapp.Logger.Info("### e2term stub run ###")
- e2termrt := allrt
- e2termrtfilename, _ := testCreateTmpFile(e2termrt)
- defer os.Remove(e2termrtfilename)
- e2termConn = createNewE2termStub("e2termstub", e2termrtfilename, "15560", "RMRE2TERMSTUB")
+ tent.Info("### e2term2 stub run ###")
+ e2termConn2 = teststube2ap.CreateNewE2termStub("e2termstub2", e2term2src, teststub.RmrRtgSvc{}, "RMRE2TERMSTUB2", teststubPortSeed)
+
+ //---------------------------------
+ // Just to test dummy stub
+ //---------------------------------
+ tent.Info("### dummy stub run ###")
+ dummystub = teststubdummy.CreateNewRmrDummyStub("dummystub", dummysrc, teststub.RmrRtgSvc{}, "DUMMYSTUB", teststubPortSeed)
//---------------------------------
// Testing message sending
//---------------------------------
- var dummyBuf []byte = make([]byte, 100)
-
- params := &RMRParams{&xapp.RMRParams{}}
- params.Mtype = 55555
- params.SubId = -1
- params.Payload = dummyBuf
- params.PayloadLen = 100
- params.Meid = &xapp.RMRMeid{RanName: "NONEXISTINGRAN"}
- params.Xid = "THISISTESTFORSTUBS"
- params.Mbuf = nil
-
- status := false
- i := 1
- for ; i <= 10 && status == false; i++ {
- xapp.Rmr.Send(params.RMRParams, false)
-
- status = true
- for _, val := range allRmrStubs {
- if val.active == false {
- status = false
- break
- }
- }
- if status == true {
- break
- }
- xapp.Logger.Info("Sleep 0.5 secs and try routes again")
- time.Sleep(500 * time.Millisecond)
+ if teststub.RmrStubControlWaitAlive(10, teststubPortSeed, mainCtrl.c.RMRClient, tent) == false {
+ os.Exit(1)
}
- if status == false {
- xapp.Logger.Error("Could not initialize routes")
+ if os.Getenv("RMR_SEED_RT") != rt.FileName() {
+ tent.Error("Unittest timing issue with alarm RMR_SEED_RT=%s", os.Getenv("RMR_SEED_RT"))
os.Exit(1)
}
+
+ return func() { rt.Disable() }
}
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
func TestMain(m *testing.M) {
- xapp.Logger.Info("TestMain start")
- ut_test_init()
+ CaseBegin("TestMain start")
+ cleanfn := ut_test_init()
code := m.Run()
+ cleanfn()
os.Exit(code)
}