Generalized unittest stubs so frame can be used also in other apps
Change-Id: I1d2acc8ee50184d61c4eb24fb5c76e0927203c99
Signed-off-by: Juha Hyttinen <juha.hyttinen@nokia.com>
diff --git a/pkg/teststub/controlRmr.go b/pkg/teststub/controlRmr.go
new file mode 100644
index 0000000..bcea500
--- /dev/null
+++ b/pkg/teststub/controlRmr.go
@@ -0,0 +1,62 @@
+/*
+==================================================================================
+ Copyright (c) 2019 AT&T Intellectual Property.
+ Copyright (c) 2019 Nokia
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================================
+*/
+package teststub
+
+import (
+ "fmt"
+ "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
+ "os"
+ "testing"
+)
+
+//-----------------------------------------------------------------------------
+//
+//-----------------------------------------------------------------------------
+type RmrControl struct {
+ TestWrapper
+ syncChan chan struct{}
+}
+
+func (tc *RmrControl) ReadyCB(data interface{}) {
+ tc.syncChan <- struct{}{}
+ return
+}
+
+func (tc *RmrControl) WaitCB() {
+ <-tc.syncChan
+}
+
+func (tc *RmrControl) Init(desc string, rtfile string, port string) {
+ tc.TestWrapper.Init(desc)
+ 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.syncChan = make(chan struct{})
+}
+
+func (tc *RmrControl) TestError(t *testing.T, pattern string, args ...interface{}) {
+ tc.Logger.Error(fmt.Sprintf(pattern, args...))
+ t.Errorf(fmt.Sprintf(pattern, args...))
+}
+
+func (tc *RmrControl) TestLog(t *testing.T, pattern string, args ...interface{}) {
+ tc.Logger.Info(fmt.Sprintf(pattern, args...))
+ t.Logf(fmt.Sprintf(pattern, args...))
+}
diff --git a/pkg/teststub/controlRmrStub.go b/pkg/teststub/controlRmrStub.go
new file mode 100644
index 0000000..a8c861f
--- /dev/null
+++ b/pkg/teststub/controlRmrStub.go
@@ -0,0 +1,175 @@
+/*
+==================================================================================
+ Copyright (c) 2019 AT&T Intellectual Property.
+ Copyright (c) 2019 Nokia
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================================
+*/
+package teststub
+
+import (
+ "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/xapptweaks"
+ "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
+ "strings"
+ "testing"
+ "time"
+)
+
+//-----------------------------------------------------------------------------
+//
+//-----------------------------------------------------------------------------
+type RmrStubControl struct {
+ RmrControl
+ xapptweaks.RmrWrapper
+ RecvChan chan *xapptweaks.RMRParams
+ Active bool
+ InitMsg int
+ CheckXid bool
+}
+
+func (tc *RmrStubControl) SetActive() {
+ tc.Active = true
+}
+
+func (tc *RmrStubControl) IsActive() bool {
+ return tc.Active
+}
+
+func (tc *RmrStubControl) SetCheckXid(val bool) {
+ tc.CheckXid = val
+}
+
+func (tc *RmrStubControl) IsCheckXid() bool {
+ return tc.CheckXid
+}
+
+func (tc *RmrStubControl) IsChanEmpty() bool {
+ if len(tc.RecvChan) > 0 {
+ return false
+ }
+ return true
+}
+
+func (tc *RmrStubControl) TestMsgChanEmpty(t *testing.T) {
+ if tc.IsChanEmpty() == false {
+ tc.TestError(t, "message channel not empty")
+ }
+}
+
+func (tc *RmrStubControl) Init(desc string, rtfile string, port string, stat string, initMsg int) {
+ tc.InitMsg = initMsg
+ tc.Active = false
+ tc.RecvChan = make(chan *xapptweaks.RMRParams)
+ tc.RmrControl.Init(desc, rtfile, port)
+ tc.RmrWrapper.Init()
+
+ tc.Rmr = xapp.NewRMRClientWithParams("tcp:"+port, 4096, 1, stat)
+ tc.Rmr.SetReadyCB(tc.ReadyCB, nil)
+ go tc.Rmr.Start(tc)
+
+ tc.WaitCB()
+ allRmrStubs = append(allRmrStubs, tc)
+}
+
+func (tc *RmrStubControl) Consume(params *xapp.RMRParams) (err error) {
+ defer tc.Rmr.Free(params.Mbuf)
+ msg := xapptweaks.NewParams(params)
+ tc.CntRecvMsg++
+
+ if msg.Mtype == tc.InitMsg {
+ tc.Logger.Info("Testing message ignore %s", msg.String())
+ tc.SetActive()
+ return
+ }
+
+ if tc.IsCheckXid() == true && strings.Contains(msg.Xid, tc.GetDesc()) == false {
+ tc.Logger.Info("Ignore %s", msg.String())
+ return
+ }
+
+ tc.Logger.Info("Consume %s", msg.String())
+ tc.PushMsg(msg)
+ return
+}
+
+func (tc *RmrStubControl) PushMsg(msg *xapptweaks.RMRParams) {
+ tc.Logger.Debug("RmrStubControl PushMsg ... msg(%d) waiting", msg.Mtype)
+ tc.RecvChan <- msg
+ tc.Logger.Debug("RmrStubControl PushMsg ... done")
+}
+
+func (tc *RmrStubControl) WaitMsg(secs time.Duration) *xapptweaks.RMRParams {
+ tc.Logger.Debug("RmrStubControl WaitMsg ... waiting")
+ if secs == 0 {
+ msg := <-tc.RecvChan
+ tc.Logger.Debug("RmrStubControl WaitMsg ... msg(%d) done", msg.Mtype)
+ return msg
+ }
+ select {
+ case msg := <-tc.RecvChan:
+ tc.Logger.Debug("RmrStubControl WaitMsg ... msg(%d) done", msg.Mtype)
+ return msg
+ case <-time.After(secs * time.Second):
+ tc.Logger.Debug("RmrStubControl WaitMsg ... timeout")
+ return nil
+ }
+ tc.Logger.Debug("RmrStubControl WaitMsg ... error")
+ return nil
+}
+
+var allRmrStubs []*RmrStubControl
+
+//-----------------------------------------------------------------------------
+//
+//-----------------------------------------------------------------------------
+
+func RmrStubControlWaitAlive(seconds int, mtype int, rmr xapptweaks.XAppWrapperIf) bool {
+
+ var dummyBuf []byte = make([]byte, 100)
+
+ params := xapptweaks.NewParams(nil)
+ params.Mtype = mtype
+ params.SubId = -1
+ params.Payload = dummyBuf
+ params.PayloadLen = 100
+ params.Meid = &xapp.RMRMeid{RanName: "TESTPING"}
+ params.Xid = "TESTPING"
+ params.Mbuf = nil
+
+ status := false
+ i := 1
+ for ; i <= seconds*2 && status == false; i++ {
+
+ rmr.GetLogger().Info("SEND TESTPING: %s", params.String())
+ rmr.RmrSend(params)
+
+ status = true
+ for _, val := range allRmrStubs {
+ if val.IsActive() == false {
+ status = false
+ break
+ }
+ }
+ if status == true {
+ break
+ }
+ rmr.GetLogger().Info("Sleep 0.5 secs and try routes again")
+ time.Sleep(500 * time.Millisecond)
+ }
+
+ if status == false {
+ rmr.GetLogger().Error("Could not initialize routes")
+ }
+ return status
+}
diff --git a/pkg/teststub/rmrroutetable.go b/pkg/teststub/rmrroutetable.go
new file mode 100644
index 0000000..9ea2657
--- /dev/null
+++ b/pkg/teststub/rmrroutetable.go
@@ -0,0 +1,55 @@
+/*
+==================================================================================
+ Copyright (c) 2019 AT&T Intellectual Property.
+ Copyright (c) 2019 Nokia
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================================
+*/
+
+package teststub
+
+import (
+ "strconv"
+)
+
+//-----------------------------------------------------------------------------
+//
+//-----------------------------------------------------------------------------
+
+type RmrRouteTable struct {
+ lines []string
+}
+
+func (rrt *RmrRouteTable) AddEntry(mtype int, src string, subid int, trg string) {
+
+ line := "mse|"
+ line += strconv.FormatInt(int64(mtype), 10)
+ if len(src) > 0 {
+ line += "," + src
+ }
+ line += "|"
+ line += strconv.FormatInt(int64(subid), 10)
+ line += "|"
+ line += trg
+ rrt.lines = append(rrt.lines, line)
+}
+
+func (rrt *RmrRouteTable) GetRt() string {
+ allrt := "newrt|start\n"
+ for _, val := range rrt.lines {
+ allrt += val + "\n"
+ }
+ allrt += "newrt|end\n"
+ return allrt
+}
diff --git a/pkg/teststub/teststub.go b/pkg/teststub/teststub.go
new file mode 100644
index 0000000..963918a
--- /dev/null
+++ b/pkg/teststub/teststub.go
@@ -0,0 +1,39 @@
+/*
+==================================================================================
+ Copyright (c) 2019 AT&T Intellectual Property.
+ Copyright (c) 2019 Nokia
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================================
+*/
+package teststub
+
+import (
+ "io/ioutil"
+)
+
+//-----------------------------------------------------------------------------
+//
+//-----------------------------------------------------------------------------
+func CreateTmpFile(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
+}
diff --git a/pkg/teststub/testwrapper.go b/pkg/teststub/testwrapper.go
new file mode 100644
index 0000000..f27392d
--- /dev/null
+++ b/pkg/teststub/testwrapper.go
@@ -0,0 +1,55 @@
+/*
+==================================================================================
+ Copyright (c) 2019 AT&T Intellectual Property.
+ Copyright (c) 2019 Nokia
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================================
+*/
+package teststub
+
+import (
+ "fmt"
+ "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/xapptweaks"
+ "testing"
+)
+
+type TestWrapper struct {
+ xapptweaks.LogWrapper
+}
+
+func (tw *TestWrapper) Init(desc string) {
+ tw.LogWrapper.Init(desc)
+}
+
+//-----------------------------------------------------------------------------
+//
+//-----------------------------------------------------------------------------
+func (tw *TestWrapper) TestError(t *testing.T, pattern string, args ...interface{}) {
+ tw.Logger.Error(fmt.Sprintf(pattern, args...))
+ t.Errorf(fmt.Sprintf(pattern, args...))
+}
+
+//-----------------------------------------------------------------------------
+//
+//-----------------------------------------------------------------------------
+func (tw *TestWrapper) TestLog(t *testing.T, pattern string, args ...interface{}) {
+ tw.Logger.Info(fmt.Sprintf(pattern, args...))
+ t.Logf(fmt.Sprintf(pattern, args...))
+}
+
+func NewTestWrapper(desc string) *TestWrapper {
+ tent := &TestWrapper{}
+ tent.Init(desc)
+ return tent
+}