Improve UTs 23/5123/2 v0.6.4
authorMohamed Abukar <abukar.mohamed@nokia.com>
Thu, 19 Nov 2020 12:38:22 +0000 (14:38 +0200)
committerMohamed Abukar <abukar.mohamed@nokia.com>
Thu, 19 Nov 2020 14:34:43 +0000 (16:34 +0200)
Change-Id: I32c55b476aef34a82267a01bc53219a2f971180e
Signed-off-by: Mohamed Abukar <abukar.mohamed@nokia.com>
README.md
config/config-file.json
pkg/xapp/asn.go [deleted file]
pkg/xapp/config.go
pkg/xapp/metrics_test.go [changed mode: 0644->0755]
pkg/xapp/rmrendpointlist_test.go [changed mode: 0644->0755]
pkg/xapp/xapp_test.go

index 756ed2c..3d74dde 100755 (executable)
--- a/README.md
+++ b/README.md
@@ -1,7 +1,7 @@
 # XAPP-FRAME
 
 ## Introduction
-**xapp-frame** is a simple framework for rapid development of RIC xapps, and supports various services essential for RIC xapps such as RESTful APIs, RMR (RIC Message Routing), database backend services and watching and populating config-map changes in K8S environment.
+**xapp-frame** is a simple framework for rapid development of RIC xapps, and supports various services essential for RIC xapps such as RESTful APIs, RMR (RIC Message Routing), logging, performance metrics and database backend services. It also provides configuration interface for reading, watching and populating config-map changes in K8S environment.
 
 ## Architecture
 
@@ -59,7 +59,7 @@ func main() {
 
 #### To run the generated executable binary locally, run the following command:
 
-    RMR_SEED_RT=config/uta_rtg.rt ./example_xapp -f config/config-file.json
+    RMR_SEED_RT=examples/config/uta_rtg.rt ./example_xapp -f examples/config/config-file.json
 
 Congratulations! You've just built your first **xapp** application.
 
index cacdde9..437b526 100755 (executable)
@@ -56,6 +56,9 @@
                 "policies": [801111, 902222],
                 "description": "rmr data port for ueec"
             }
+        ],
+        "mtypes": [
+            {"Name": "abdc", "Id": 1234}
         ]
     },
     "controls": {
diff --git a/pkg/xapp/asn.go b/pkg/xapp/asn.go
deleted file mode 100644 (file)
index 9c50491..0000000
+++ /dev/null
@@ -1,35 +0,0 @@
-/*
-==================================================================================
-  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 xapp
-
-type ASN struct {
-}
-
-func (*ASN) Encode(mtype int, len int, pdu []byte) (b []byte) {
-       Logger.Info("Encode: mtype=%d len=%d ... not implemented yet!", mtype, len)
-
-       return
-}
-
-func (*ASN) Decode(mtype int, len int, pdu []byte) (b []byte) {
-       Logger.Info("Decode: mtype=%d len=%d ... not implemented yet", mtype, len)
-
-       return
-}
index 8ab7941..ff22d71 100755 (executable)
@@ -65,7 +65,7 @@ func LoadConfig() (l *Log) {
 
        updateMTypes := func() {
                var mtypes []mtype
-               viper.UnmarshalKey("rmr.mtypes", &mtypes)
+               viper.UnmarshalKey("messaging.mtypes", &mtypes)
 
                if len(mtypes) > 0 {
                        l.Info("Config mtypes before RICMessageTypes:%d RicMessageTypeToName:%d", len(RICMessageTypes), len(RicMessageTypeToName))
old mode 100644 (file)
new mode 100755 (executable)
index 6d46988..e600743
@@ -25,6 +25,7 @@ import (
 
 var mCVect map[string]CounterVec
 var mGVect map[string]GaugeVec
+var mGGroup map[string]Gauge
 
 func TestMetricSetup(t *testing.T) {
        mCVect = Metric.RegisterCounterVecGroup(
@@ -40,7 +41,12 @@ func TestMetricSetup(t *testing.T) {
                },
                []string{"name", "event"},
                "SUBSYSTEM")
-
+       
+       mGGroup = Metric.RegisterGaugeGroup(
+               []CounterOpts{
+                       {Name: "counter3", Help: "counter3"},
+               },
+               "SUBSYSTEM2")
 }
 
 func TestMetricCounter(t *testing.T) {
@@ -138,7 +144,9 @@ func TestMetricCounterVectorPrefix(t *testing.T) {
        if m_grp.CIs("event2_counter1") == false {
                t.Errorf("m_grp event2_counter1 not exists")
        }
-       m_grp.CInc("event2_counter1")
+
+       m_grp.CAdd("event2_counter1", 1)
+       m_grp.CGet("event2_counter1")
 }
 
 func TestMetricGaugeVectorPrefix(t *testing.T) {
@@ -174,6 +182,10 @@ func TestMetricGaugeVectorPrefix(t *testing.T) {
                t.Errorf("m_grp event2_counter2 not exists")
        }
        m_grp.GInc("event2_counter2")
+
+       m_grp.GGet("event2_counter2")
+       m_grp.GDec("event2_counter2")
+       m_grp.GSet("event2_counter2", 1)
 }
 
 func TestMetricGroupCache(t *testing.T) {
@@ -241,4 +253,10 @@ func TestMetricGroupCache(t *testing.T) {
        }
        m_grp.GInc("event2_counter2")
 
+       m_grp.CAdd("event2_counter1", 1)
+       m_grp.CGet("event2_counter1")
+       m_grp.GGet("event2_counter2")
+       m_grp.GDec("event2_counter2")
+       m_grp.GSet("event2_counter2", 1)
 }
+
old mode 100644 (file)
new mode 100755 (executable)
index cbef96e..fe073e9
@@ -26,7 +26,7 @@ import (
 func TestRmrEndpointList(t *testing.T) {
        Logger.Info("CASE: TestRmrEndpointList")
 
-       epl := &RmrEndpointList{}
+       epl := NewRmrEndpointList()
 
        // Simple add / has / delete
        if epl.AddEndpoint(NewRmrEndpoint("127.0.0.1:8080")) == false {
@@ -41,6 +41,9 @@ func TestRmrEndpointList(t *testing.T) {
        if epl.HasEndpoint(NewRmrEndpoint("127.0.0.1:8081")) == false {
                t.Errorf("RmrEndpointList: 8081 has failed")
        }
+
+       Logger.Info("%+v -- %+v -- %d", epl.String(), epl.StringList(), epl.Size())
+
        if epl.DelEndpoint(NewRmrEndpoint("127.0.0.1:8081")) == false {
                t.Errorf("RmrEndpointList: 8081 del failed")
        }
index baafa16..4361491 100755 (executable)
@@ -24,10 +24,12 @@ import (
        "github.com/spf13/viper"
        "net/http"
        "net/http/httptest"
+       "github.com/stretchr/testify/assert"
        "os"
        "strings"
        "testing"
        "time"
+       "bytes"
 )
 
 //var _ = func() bool {
@@ -53,7 +55,7 @@ func TestMain(m *testing.M) {
 func TestGetHealthCheckRetursServiceUnavailableError(t *testing.T) {
        Logger.Info("CASE: TestGetHealthCheckRetursServiceUnavailableError")
        req, _ := http.NewRequest("GET", "/ric/v1/health/ready", nil)
-       /*response :=*/ executeRequest(req)
+       /*response :=*/ executeRequest(req, nil)
 
        //checkResponseCode(t, http.StatusServiceUnavailable, response.Code)
 }
@@ -65,7 +67,7 @@ func TestGetHealthCheckReturnsSuccess(t *testing.T) {
        }
 
        req, _ := http.NewRequest("GET", "/ric/v1/health/ready", nil)
-       response := executeRequest(req)
+       response := executeRequest(req, nil)
 
        checkResponseCode(t, http.StatusOK, response.Code)
 }
@@ -78,7 +80,7 @@ func TestInjectQuerySinglePath(t *testing.T) {
        Resource.InjectQueryRoute("/ric/v1/user", handler, "GET", "foo", "bar")
 
        req, _ := http.NewRequest("GET", "/ric/v1/user?foo=bar", nil)
-       response := executeRequest(req)
+       response := executeRequest(req, nil)
        checkResponseCode(t, http.StatusOK, response.Code)
 }
 
@@ -90,7 +92,7 @@ func TestInjectQueryMultiplePaths(t *testing.T) {
        Resource.InjectQueryRoute("/ric/v1/user", handler, "GET", "foo", "bar", "id", "mykey")
 
        req, _ := http.NewRequest("GET", "/ric/v1/user?foo=bar&id=mykey", nil)
-       response := executeRequest(req)
+       response := executeRequest(req, nil)
        checkResponseCode(t, http.StatusOK, response.Code)
 }
 
@@ -102,7 +104,7 @@ func TestInjectQueryFailures(t *testing.T) {
        Resource.InjectQueryRoute("/ric/v1/user", handler, "GET", "foo", "bar", "id", "mykey")
 
        req, _ := http.NewRequest("GET", "/ric/v1/user?invalid=bar&no=mykey", nil)
-       response := executeRequest(req)
+       response := executeRequest(req, nil)
        checkResponseCode(t, http.StatusNotFound, response.Code)
 }
 
@@ -116,8 +118,14 @@ func TestMessagesReceivedSuccessfully(t *testing.T) {
                params.Payload = []byte{1, 2, 3, 4, 5, 6}
                params.Meid = &RMRMeid{PlmnID: "1234", EnbID: "7788", RanName: "RanName-1234"}
                params.Xid = "TestXID"
-               Rmr.SendMsg(params)
+               
+               if i % 2 == 0 {
+                       Rmr.SendMsg(params)
+               } else {
+                       Rmr.SendWithRetry(params, false, 1)
+               }
        }
+       Rmr.RegisterMetrics()
 
        // Allow time to process the messages
        time.Sleep(time.Duration(5) * time.Second)
@@ -162,6 +170,11 @@ func TestMessagesReceivedSuccessfullyUsingWh(t *testing.T) {
                params.Meid = &RMRMeid{PlmnID: "1234", EnbID: "7788", RanName: "RanName-1234"}
                params.Xid = "TestXID"
                params.Whid = int(whid)
+
+               if i == 0 {
+                       Logger.Info("%+v", params.String())
+               }
+
                Rmr.SendMsg(params)
        }
 
@@ -258,6 +271,9 @@ func TestSubscribeChannels(t *testing.T) {
        if err := Sdl.StoreAndPublish("channel1", "event", "key1", "data1"); err != nil {
                t.Errorf("Error: Publish failed: %v", err)
        }
+
+       // Misc.
+       Sdl.MStoreAndPublish([]string{"channel1"}, "event", "key1", "data1")
 }
 
 func TestGetRicMessageSuccess(t *testing.T) {
@@ -309,18 +325,131 @@ func TestIsErrorFunctions(t *testing.T) {
        }
 }
 
+func TestAddConfigChangeListener(t *testing.T) {
+       Logger.Info("CASE: AddConfigChangeListener")
+       AddConfigChangeListener(func(f string) {})
+}
+
+func TestConfigAccess(t *testing.T) {
+       Logger.Info("CASE: AddConfigChangeListener")
+       
+       assert.Equal(t,  Config.GetString("name"), "xapp")
+       assert.Equal(t,  Config.GetInt("controls.logger.level"), 3)
+       assert.Equal(t,  Config.GetUint32("controls.logger.level"), uint32(3))
+       assert.Equal(t,  Config.GetBool("controls.waitForSdl"), false)
+       Config.Get("controls")
+       Config.GetStringSlice("messaging.ports")
+       Config.GetStringMap("messaging.ports")
+}
+
+func TestPublishConfigChange(t *testing.T) {
+       Logger.Info("CASE: AddConfigChangeListener")
+       PublishConfigChange("testApp", "values")
+}
+
+func TestNewSubscriber(t *testing.T) {
+       Logger.Info("CASE: TestNewSubscriber")
+       assert.NotNil(t, NewSubscriber("", 0), "NewSubscriber failed")
+}
+
+func TestNewRMRClient(t *testing.T) {
+       c := map[string]interface{} {"protPort": "tcp:4560"}
+       viper.Set("rmr", c)
+       assert.NotNil(t, NewRMRClient(), "NewRMRClient failed")
+
+       params := &RMRParams{}
+       params.Mtype = 1234
+       params.SubId = -1
+       params.Payload = []byte{1, 2, 3, 4, 5, 6}
+       Rmr.SendWithRetry(params, false, 1)
+}
+
+func TestInjectRoutePrefix(t *testing.T) {
+       Logger.Info("CASE: TestInjectRoutePrefix")
+       assert.NotNil(t, Resource.InjectRoutePrefix("test", nil), "InjectRoutePrefix failed")
+}
+
+func TestInjectStatusCb(t *testing.T) {
+       Logger.Info("CASE: TestInjectStatusCb")
+
+       var f = func() bool {
+               return true
+       }
+       Resource.InjectStatusCb(f)
+}
+
+func TestSdlInterfaces(t *testing.T) {
+       Sdl.Read("myKey")
+       Sdl.MRead([]string{"myKey"})
+       Sdl.ReadAllKeys("myKey")
+       Sdl.Store("myKey", "Values")
+       Sdl.MStore("myKey", "Values")
+       Sdl.RegisterMetrics()
+
+       // Misc.
+       var NotificationCb = func(ch string, events ...string) {}
+       Sdl.Subscribe(NotificationCb, "channel1")
+       Sdl.MSubscribe(NotificationCb, "channel1", "channel2")
+       Sdl.MStoreAndPublish([]string{"channel1"}, "event", "key1", "data1")
+}
+
+func TestRnibInterfaces(t *testing.T) {
+       Rnib.GetNodeb("test-gnb")
+       Rnib.GetCellList("test-gnb")
+       Rnib.GetListGnbIds()
+       Rnib.GetListEnbIds()
+       Rnib.GetCountGnbList()
+       Rnib.GetCell("test-gnb", 0)
+       Rnib.GetCell("test-gnb", 0)
+       Rnib.GetCellById(0, "cell-1")
+
+       // Misc.
+       var NotificationCb = func(ch string, events ...string) {}
+       Rnib.Subscribe(NotificationCb, "channel1")
+       Rnib.StoreAndPublish("channel1", "event", "key1", "data1")
+}
+
+func TestLogger(t *testing.T) {
+       Logger.Error("CASE: TestNewSubscriber")
+       Logger.Warn("CASE: TestNewSubscriber")
+}
+
+func TestConfigHandler(t *testing.T) {
+       Logger.Error("CASE: TestConfigHandler")
+       req, _ := http.NewRequest("POST", "/ric/v1/cm/appname", bytes.NewBuffer([]byte{}))
+       handleFunc := http.HandlerFunc(configHandler)
+       executeRequest(req, handleFunc)
+}
+
+func TestMisc(t *testing.T) {
+       Logger.Info("CASE: TestMisc")
+
+       IsReady()
+       SetReadyCB(func(interface{}) {}, "")
+       XappReadyCb("")
+       SetShutdownCB(func() {})
+}
+
 func TestTeardown(t *testing.T) {
        Logger.Info("CASE: TestTeardown")
+       Sdl.Delete([]string{"myKey"})
        Sdl.Clear()
+       Sdl.IsReady()
+       Sdl.GetStat()
 }
 
 // Helper functions
-func executeRequest(req *http.Request) *httptest.ResponseRecorder {
+func executeRequest(req *http.Request, handleR http.HandlerFunc) *httptest.ResponseRecorder {
        rr := httptest.NewRecorder()
-       vars := map[string]string{"id": "1"}
-       req = mux.SetURLVars(req, vars)
-       Resource.router.ServeHTTP(rr, req)
-
+       if handleR != nil {
+               vars := map[string]string{"name": "myxapp"}
+               req = mux.SetURLVars(req, vars)
+               handleR.ServeHTTP(rr, req)
+       } else {
+               vars := map[string]string{"id": "1"}
+               req = mux.SetURLVars(req, vars)
+               Resource.router.ServeHTTP(rr, req)
+       }
        return rr
 }
 
@@ -332,7 +461,7 @@ func checkResponseCode(t *testing.T, expected, actual int) {
 
 func getMetrics(t *testing.T) string {
        req, _ := http.NewRequest("GET", "/ric/v1/metrics", nil)
-       response := executeRequest(req)
+       response := executeRequest(req, nil)
 
        return response.Body.String()
 }