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 ==================================================================================
31 "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/teststub"
32 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/models"
33 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
36 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
39 type testingSubmgrControl struct {
49 var countersBeforeMap map[string]Counter
50 var toBeAddedCountersMap map[string]Counter
52 func createSubmgrControl(srcId teststub.RmrSrcId, rtgSvc teststub.RmrRtgSvc) *testingSubmgrControl {
53 mainCtrl = &testingSubmgrControl{}
54 mainCtrl.RmrControl.Init("SUBMGRCTL", srcId, rtgSvc)
55 mainCtrl.c = NewControl()
56 xapp.Logger.Debug("Replacing real db with test db")
57 mainCtrl.c.db = CreateMock() // This overrides real database for testing
58 xapp.SetReadyCB(mainCtrl.ReadyCB, nil)
59 go xapp.RunWithParams(mainCtrl.c, false)
61 mainCtrl.c.ReadyCB(nil)
65 func (mc *testingSubmgrControl) SimulateRestart(t *testing.T) {
66 mc.TestLog(t, "Simulating submgr restart")
67 mainCtrl.c.registry.subIds = nil
68 // Initialize subIds slice and subscription map
69 mainCtrl.c.registry.Initialize()
70 // Read subIds and subscriptions from database
71 subIds, register, err := mainCtrl.c.ReadAllSubscriptionsFromSdl()
73 mc.TestError(t, "%v", err)
75 mainCtrl.c.registry.register = nil
76 mainCtrl.c.registry.subIds = subIds
77 mainCtrl.c.registry.register = register
79 mc.TestLog(t, "register:")
80 for subId, subs := range register {
81 mc.TestLog(t, " subId=%v", subId)
82 mc.TestLog(t, " subs.SubRespRcvd=%v", subs.SubRespRcvd)
83 mc.TestLog(t, " subs=%v\n", subs)
86 mc.TestLog(t, "mainCtrl.c.registry.register:")
87 for subId, subs := range mainCtrl.c.registry.register {
88 mc.TestLog(t, " subId=%v", subId)
89 mc.TestLog(t, " subs.SubRespRcvd=%v", subs.SubRespRcvd)
90 mc.TestLog(t, " subs=%v\n", subs)
93 go mainCtrl.c.HandleUncompletedSubscriptions(mainCtrl.c.registry.register)
96 func (mc *testingSubmgrControl) SetResetTestFlag(t *testing.T, status bool) {
97 mc.TestLog(t, "ResetTestFlag set to %v=", status)
98 mainCtrl.c.ResetTestFlag = status
101 func (mc *testingSubmgrControl) removeExistingSubscriptions(t *testing.T) {
103 mc.TestLog(t, "Removing existing subscriptions")
104 mainCtrl.c.RemoveAllSubscriptionsFromSdl()
105 mainCtrl.c.registry.subIds = nil
106 // Initialize subIds slice and subscription map
107 mainCtrl.c.registry.Initialize()
110 func PringSubscriptionQueryResult(resp models.SubscriptionList) {
111 for _, item := range resp {
112 fmt.Printf("item.SubscriptionID=%v\n", item.SubscriptionID)
113 fmt.Printf("item.Meid=%v\n", item.Meid)
114 fmt.Printf("item.Endpoint=%v\n", item.Endpoint)
118 func (mc *testingSubmgrControl) wait_registry_empty(t *testing.T, secs int) bool {
121 for ; i <= secs*2; i++ {
122 cnt = len(mc.c.registry.register)
126 time.Sleep(500 * time.Millisecond)
128 mc.TestError(t, "(submgr) no registry empty within %d secs: %d", secs, cnt)
132 func (mc *testingSubmgrControl) get_registry_next_subid(t *testing.T) uint32 {
133 mc.c.registry.mutex.Lock()
134 defer mc.c.registry.mutex.Unlock()
135 return mc.c.registry.subIds[0]
138 func (mc *testingSubmgrControl) wait_registry_next_subid_change(t *testing.T, origSubId uint32, secs int) (uint32, bool) {
140 for ; i <= secs*2; i++ {
141 mc.c.registry.mutex.Lock()
142 currSubId := mc.c.registry.subIds[0]
143 mc.c.registry.mutex.Unlock()
144 if currSubId != origSubId {
145 return currSubId, true
147 time.Sleep(500 * time.Millisecond)
149 mc.TestError(t, "(submgr) no subId change within %d secs", secs)
153 func (mc *testingSubmgrControl) wait_subs_clean(t *testing.T, e2SubsId uint32, secs int) bool {
154 var subs *Subscription
156 for ; i <= secs*2; i++ {
157 subs = mc.c.registry.GetSubscription(e2SubsId)
161 time.Sleep(500 * time.Millisecond)
164 mc.TestError(t, "(submgr) no clean within %d secs: %s", secs, subs.String())
166 mc.TestError(t, "(submgr) no clean within %d secs: subs(N/A)", secs)
171 func (mc *testingSubmgrControl) wait_subs_trans_clean(t *testing.T, e2SubsId uint32, secs int) bool {
172 var trans TransactionIf
174 for ; i <= secs*2; i++ {
175 subs := mc.c.registry.GetSubscription(e2SubsId)
179 trans = subs.GetTransaction()
183 time.Sleep(500 * time.Millisecond)
186 mc.TestError(t, "(submgr) no clean within %d secs: %s", secs, trans.String())
188 mc.TestError(t, "(submgr) no clean within %d secs: trans(N/A)", secs)
193 func (mc *testingSubmgrControl) get_subs_entrypoint_cnt(t *testing.T, origSubId uint32) int {
194 subs := mc.c.registry.GetSubscription(origSubId)
196 mc.TestError(t, "(submgr) no subs %d exists during entrypoint cnt get", origSubId)
199 return subs.EpList.Size()
202 func (mc *testingSubmgrControl) wait_subs_entrypoint_cnt_change(t *testing.T, origSubId uint32, orig int, secs int) (int, bool) {
204 subs := mc.c.registry.GetSubscription(origSubId)
206 mc.TestError(t, "(submgr) no subs %d exists during entrypoint cnt wait", origSubId)
211 for ; i <= secs*2; i++ {
212 curr := subs.EpList.Size()
216 time.Sleep(500 * time.Millisecond)
218 mc.TestError(t, "(submgr) no subs %d entrypoint cnt change within %d secs", origSubId, secs)
223 // Counter check for received message. Note might not be yet handled
225 func (mc *testingSubmgrControl) get_msgcounter(t *testing.T) uint64 {
226 return mc.c.CntRecvMsg
229 func (mc *testingSubmgrControl) wait_msgcounter_change(t *testing.T, orig uint64, secs int) (uint64, bool) {
231 for ; i <= secs*2; i++ {
232 curr := mc.c.CntRecvMsg
236 time.Sleep(500 * time.Millisecond)
238 mc.TestError(t, "(submgr) no msg counter change within %d secs", secs)
242 func (mc *testingSubmgrControl) GetMetrics(t *testing.T) (string, error) {
243 req, err := http.NewRequest("GET", "http://localhost:8080/ric/v1/metrics", nil)
245 return "", fmt.Errorf("Error reading request. %v", err)
247 client := &http.Client{Timeout: time.Second * 10}
248 resp, err := client.Do(req)
250 return "", fmt.Errorf("Error reading response. %v", err)
252 defer resp.Body.Close()
254 respBody, err := ioutil.ReadAll(resp.Body)
256 return "", fmt.Errorf("Error reading body. %v", err)
258 return string(respBody[:]), nil
261 func (mc *testingSubmgrControl) SetTimesCounterWillBeAdded(counterName string, addedValue uint64) {
262 if len(toBeAddedCountersMap) == 0 {
263 toBeAddedCountersMap = make(map[string]Counter)
266 counter.Name = counterName
267 counter.Value = addedValue
268 toBeAddedCountersMap[counterName] = counter
271 func (mc *testingSubmgrControl) GetCounterValuesBefore(t *testing.T) {
272 countersBeforeMap = make(map[string]Counter)
273 countersBeforeMap = mc.GetCurrentCounterValues(t, toBeAddedCountersMap)
276 func (mc *testingSubmgrControl) VerifyCounterValues(t *testing.T) {
277 currentCountersMap := mc.GetCurrentCounterValues(t, toBeAddedCountersMap)
278 for _, toBeAddedCounter := range toBeAddedCountersMap {
279 if currentCounter, ok := currentCountersMap[toBeAddedCounter.Name]; ok == true {
280 if beforeCounter, ok := countersBeforeMap[toBeAddedCounter.Name]; ok == true {
281 if currentCounter.Value != beforeCounter.Value+toBeAddedCounter.Value {
282 mc.TestError(t, "Error in expected counter value: counterName %v, current value %v, expected value %v",
283 currentCounter.Name, currentCounter.Value, beforeCounter.Value+toBeAddedCounter.Value)
285 //fmt.Printf("beforeCounter.Value=%v, toBeAddedCounter.Value=%v, \n",beforeCounter.Value, toBeAddedCounter.Value)
288 mc.TestError(t, "Counter %v not in countersBeforeMap", toBeAddedCounter.Name)
291 mc.TestError(t, "Counter %v not in currentCountersMap", toBeAddedCounter.Name)
296 //fmt.Printf("toBeAddedCountersMap=%v\n",toBeAddedCountersMap)
297 toBeAddedCountersMap = make(map[string]Counter)
300 func (mc *testingSubmgrControl) GetCurrentCounterValues(t *testing.T, chekedCountersMap map[string]Counter) map[string]Counter {
301 countersString, err := mc.GetMetrics(t)
303 mc.TestError(t, "Error GetMetrics() failed %v", err)
307 retCounterMap := make(map[string]Counter)
308 stringsTable := strings.Split(countersString, "\n")
309 for _, counter := range chekedCountersMap {
310 for _, counterString := range stringsTable {
311 if !strings.Contains(counterString, "#") && strings.Contains(counterString, counter.Name) {
312 counterString := strings.Split(counterString, " ")
313 if strings.Contains(counterString[0], counter.Name) {
314 val, err := strconv.ParseUint(counterString[1], 10, 64)
316 mc.TestError(t, "Error: strconv.ParseUint failed %v", err)
319 //fmt.Printf("counter=%v\n", counter)
320 retCounterMap[counter.Name] = counter
326 if len(retCounterMap) != len(chekedCountersMap) {
327 mc.TestError(t, "Error: len(retCounterMap) != len(chekedCountersMap)")