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 type CountersToBeAdded []Counter
51 var countersBeforeMap map[string]Counter
52 var toBeAddedCountersMap map[string]Counter
54 func createSubmgrControl(srcId teststub.RmrSrcId, rtgSvc teststub.RmrRtgSvc) *testingSubmgrControl {
55 mainCtrl = &testingSubmgrControl{}
56 mainCtrl.RmrControl.Init("SUBMGRCTL", srcId, rtgSvc)
57 mainCtrl.c = NewControl()
58 mainCtrl.c.UTTesting = true
59 mainCtrl.c.LoggerLevel = int(xapp.Logger.GetLevel())
60 xapp.Logger.Debug("Test: LoggerLevel %v", mainCtrl.c.LoggerLevel)
61 xapp.Logger.Debug("Replacing real db with test db")
62 mainCtrl.c.e2SubsDb = CreateMock() // This overrides real E2 Subscription database for testing
63 mainCtrl.c.restSubsDb = CreateRestSubsDbMock() // This overrides real REST Subscription database for testing
64 xapp.SetReadyCB(mainCtrl.ReadyCB, nil)
65 go xapp.RunWithParams(mainCtrl.c, false)
67 mainCtrl.c.ReadyCB(nil)
71 func (mc *testingSubmgrControl) SimulateRestart(t *testing.T) {
72 mc.TestLog(t, "Simulating submgr restart")
73 mainCtrl.c.registry.subIds = nil
74 // Initialize subIds slice and subscription map
75 mainCtrl.c.registry.Initialize()
76 restDuplicateCtrl.Init()
77 // Read subIds and subscriptions from database
78 subIds, register, err := mainCtrl.c.ReadAllSubscriptionsFromSdl()
80 mc.TestError(t, "%v", err)
82 mainCtrl.c.registry.subIds = subIds
83 mainCtrl.c.registry.register = register
84 mc.TestLog(t, "mainCtrl.c.registry.register:")
85 for subId, subs := range mainCtrl.c.registry.register {
86 mc.TestLog(t, " subId=%v", subId)
87 mc.TestLog(t, " subs.SubRespRcvd=%v", subs.SubRespRcvd)
88 mc.TestLog(t, " subs=%v\n", subs)
91 restSubscriptions, err := mainCtrl.c.ReadAllRESTSubscriptionsFromSdl()
93 mc.TestError(t, "%v", err)
95 mainCtrl.c.registry.restSubscriptions = restSubscriptions
96 mc.TestLog(t, "mainCtrl.c.registry.restSubscriptions:")
97 for restSubId, restSubs := range mainCtrl.c.registry.restSubscriptions {
98 mc.TestLog(t, " restSubId=%v", restSubId)
99 mc.TestLog(t, " restSubs=%v\n", restSubs)
103 go mainCtrl.c.HandleUncompletedSubscriptions(mainCtrl.c.registry.register)
106 func (mc *testingSubmgrControl) MakeTransactionNil(t *testing.T, subId uint32) {
108 mc.TestLog(t, "Makin transaction nil for SubId=%v", subId)
109 subs := mainCtrl.c.registry.GetSubscription(subId)
113 func (mc *testingSubmgrControl) SetResetTestFlag(t *testing.T, status bool) {
114 mc.TestLog(t, "ResetTestFlag set to %v", status)
115 mainCtrl.c.ResetTestFlag = status
118 func (mc *testingSubmgrControl) removeExistingSubscriptions(t *testing.T) {
120 mc.TestLog(t, "Removing existing subscriptions")
121 mainCtrl.c.RemoveAllSubscriptionsFromSdl()
122 mainCtrl.c.registry.subIds = nil
123 // Initialize subIds slice and subscription map
124 mainCtrl.c.registry.Initialize()
127 func PringSubscriptionQueryResult(resp models.SubscriptionList) {
128 for _, item := range resp {
129 fmt.Printf("item.SubscriptionID=%v\n", item.SubscriptionID)
130 fmt.Printf("item.Meid=%v\n", item.Meid)
131 fmt.Printf("item.ClientEndpoint=%v\n", item.ClientEndpoint)
135 func (mc *testingSubmgrControl) wait_registry_empty(t *testing.T, secs int) bool {
138 for ; i <= secs*10; i++ {
139 cnt = len(mc.c.registry.register)
143 time.Sleep(100 * time.Millisecond)
145 mc.TestError(t, "(submgr) no registry empty within %d secs: %d, register: %v", secs, cnt, mc.c.registry.register)
149 func (mc *testingSubmgrControl) get_registry_next_subid(t *testing.T) uint32 {
150 mc.c.registry.mutex.Lock()
151 defer mc.c.registry.mutex.Unlock()
152 return mc.c.registry.subIds[0]
155 func (mc *testingSubmgrControl) wait_registry_next_subid_change(t *testing.T, origSubId uint32, secs int) (uint32, bool) {
157 for ; i <= secs*10; i++ {
158 mc.c.registry.mutex.Lock()
159 currSubId := mc.c.registry.subIds[0]
160 mc.c.registry.mutex.Unlock()
161 if currSubId != origSubId {
162 return currSubId, true
164 time.Sleep(100 * time.Millisecond)
166 mc.TestError(t, "(submgr) no subId change within %d secs", secs)
170 func (mc *testingSubmgrControl) wait_subs_clean(t *testing.T, e2SubsId uint32, secs int) bool {
171 var subs *Subscription
173 for ; i <= secs*10; i++ {
174 subs = mc.c.registry.GetSubscription(e2SubsId)
178 time.Sleep(100 * time.Millisecond)
181 mc.TestError(t, "(submgr) no clean within %d secs: %s", secs, subs.String())
183 mc.TestError(t, "(submgr) no clean within %d secs: subs(N/A)", secs)
188 func (mc *testingSubmgrControl) wait_multi_subs_clean(t *testing.T, e2SubsIds []uint32, secs int) bool {
190 purgedSubscriptions := 0
192 for i := 1; i <= secs*10; i++ {
193 purgedSubscriptions = 0
194 for k := 0; k <= len(e2SubsIds); i++ {
195 subs := mc.c.registry.GetSubscription(e2SubsIds[k])
197 mc.TestLog(t, "(submgr) subscriber purged for esSubsId %v", e2SubsIds[k])
198 purgedSubscriptions += 1
199 if purgedSubscriptions == len(e2SubsIds) {
204 mc.TestLog(t, "(submgr) subscriptions pending purging %v/%v after %d msecs", purgedSubscriptions, len(e2SubsIds), i+500)
205 time.Sleep(100 * time.Millisecond)
208 mc.TestError(t, "(submgr) no clean within %d secs: subs(N/A) - %v/%v subscriptions found still", secs, purgedSubscriptions, len(e2SubsIds))
213 func (mc *testingSubmgrControl) wait_subs_trans_clean(t *testing.T, e2SubsId uint32, secs int) bool {
214 var trans TransactionIf
216 for ; i <= secs*10; i++ {
217 subs := mc.c.registry.GetSubscription(e2SubsId)
221 trans = subs.GetTransaction()
225 time.Sleep(100 * time.Millisecond)
228 mc.TestError(t, "(submgr) no clean within %d secs: %s", secs, trans.String())
230 mc.TestError(t, "(submgr) no clean within %d secs: trans(N/A)", secs)
235 func (mc *testingSubmgrControl) get_subs_entrypoint_cnt(t *testing.T, origSubId uint32) int {
236 subs := mc.c.registry.GetSubscription(origSubId)
238 mc.TestError(t, "(submgr) no subs %d exists during entrypoint cnt get", origSubId)
241 return subs.EpList.Size()
244 func (mc *testingSubmgrControl) wait_subs_entrypoint_cnt_change(t *testing.T, origSubId uint32, orig int, secs int) (int, bool) {
246 subs := mc.c.registry.GetSubscription(origSubId)
248 mc.TestError(t, "(submgr) no subs %d exists during entrypoint cnt wait", origSubId)
253 for ; i <= secs*10; i++ {
254 curr := subs.EpList.Size()
258 time.Sleep(100 * time.Millisecond)
260 mc.TestError(t, "(submgr) no subs %d entrypoint cnt change within %d secs", origSubId, secs)
265 // Counter check for received message. Note might not be yet handled
267 func (mc *testingSubmgrControl) get_msgcounter(t *testing.T) uint64 {
268 return mc.c.CntRecvMsg
271 func (mc *testingSubmgrControl) wait_msgcounter_change(t *testing.T, orig uint64, secs int) (uint64, bool) {
273 for ; i <= secs*10; i++ {
274 curr := mc.c.CntRecvMsg
278 time.Sleep(100 * time.Millisecond)
280 mc.TestError(t, "(submgr) no msg counter change within %d secs", secs)
284 func (mc *testingSubmgrControl) GetMetrics(t *testing.T) (string, error) {
285 req, err := http.NewRequest("GET", "http://localhost:8080/ric/v1/metrics", nil)
287 return "", fmt.Errorf("Error reading request. %v", err)
289 client := &http.Client{Timeout: time.Second * 10}
290 resp, err := client.Do(req)
292 return "", fmt.Errorf("Error reading response. %v", err)
294 defer resp.Body.Close()
296 respBody, err := ioutil.ReadAll(resp.Body)
298 return "", fmt.Errorf("Error reading body. %v", err)
300 return string(respBody[:]), nil
303 func (mc *testingSubmgrControl) CounterValuesToBeVeriefied(t *testing.T, countersToBeAdded CountersToBeAdded) {
305 if len(toBeAddedCountersMap) == 0 {
306 toBeAddedCountersMap = make(map[string]Counter)
308 for _, counter := range countersToBeAdded {
309 toBeAddedCountersMap[counter.Name] = counter
311 mc.GetCounterValuesBefore(t)
314 func (mc *testingSubmgrControl) GetCounterValuesBefore(t *testing.T) {
315 countersBeforeMap = make(map[string]Counter)
316 countersBeforeMap = mc.GetCurrentCounterValues(t, toBeAddedCountersMap)
319 func (mc *testingSubmgrControl) VerifyCounterValues(t *testing.T) {
321 // Check that expected counters are added ok
322 currentCountersMap := mc.GetCurrentCounterValues(t, toBeAddedCountersMap)
323 for _, toBeAddedCounter := range toBeAddedCountersMap {
324 if currentCounter, ok := currentCountersMap[toBeAddedCounter.Name]; ok == true {
325 if beforeCounter, ok := countersBeforeMap[toBeAddedCounter.Name]; ok == true {
326 if currentCounter.Value != beforeCounter.Value+toBeAddedCounter.Value {
327 mc.TestError(t, "Error in expected counter value: counterName %v, current value %v, expected value %v",
328 currentCounter.Name, currentCounter.Value, beforeCounter.Value+toBeAddedCounter.Value)
330 //fmt.Printf("beforeCounter.Value=%v, toBeAddedCounter.Value=%v, \n",beforeCounter.Value, toBeAddedCounter.Value)
333 mc.TestError(t, "Counter %v not in countersBeforeMap", toBeAddedCounter.Name)
336 mc.TestError(t, "Counter %v not in currentCountersMap", toBeAddedCounter.Name)
340 // Check that not any unexpected counter are added
341 for _, currentCounter := range currentCountersMap {
342 if _, ok := toBeAddedCountersMap[currentCounter.Name]; ok == false {
343 if beforeCounter, ok := countersBeforeMap[currentCounter.Name]; ok == true {
344 if currentCounter.Value != beforeCounter.Value {
345 mc.TestError(t, "Error: unexpected counter value added: counterName %v, current value %v, expected value %v",
346 currentCounter.Name, beforeCounter.Value, beforeCounter.Value)
348 //fmt.Printf("beforeCounter.Value=%v, toBeAddedCounter.Value=%v, \n",beforeCounter.Value, toBeAddedCounter.Value)
351 mc.TestError(t, "Counter %v not in countersBeforeMap", beforeCounter.Name)
357 //fmt.Printf("toBeAddedCountersMap=%v\n",toBeAddedCountersMap)
358 toBeAddedCountersMap = make(map[string]Counter)
361 func (mc *testingSubmgrControl) GetCurrentCounterValues(t *testing.T, chekedCountersMap map[string]Counter) map[string]Counter {
362 countersString, err := mc.GetMetrics(t)
364 mc.TestError(t, "Error GetMetrics() failed %v", err)
368 retCounterMap := make(map[string]Counter)
369 stringsTable := strings.Split(countersString, "\n")
370 for _, counter := range chekedCountersMap {
371 for _, counterString := range stringsTable {
372 if !strings.Contains(counterString, "#") && strings.Contains(counterString, counter.Name) {
373 counterString := strings.Split(counterString, " ")
374 if strings.Contains(counterString[0], counter.Name) {
375 val, err := strconv.ParseUint(counterString[1], 10, 64)
377 mc.TestError(t, "Error: strconv.ParseUint failed %v", err)
380 //fmt.Printf("counter=%v\n", counter)
381 retCounterMap[counter.Name] = counter
387 if len(retCounterMap) != len(chekedCountersMap) {
388 mc.TestError(t, "Error: len(retCounterMap) != len(chekedCountersMap)")
394 func (mc *testingSubmgrControl) sendGetRequest(t *testing.T, addr string, path string) {
396 mc.TestLog(t, "GET http://"+addr+"%v", path)
397 req, err := http.NewRequest("GET", "http://"+addr+path, nil)
399 mc.TestError(t, "Error reading request. %v", err)
402 req.Header.Set("Cache-Control", "no-cache")
403 client := &http.Client{Timeout: time.Second * 2}
404 resp, err := client.Do(req)
406 mc.TestError(t, "Error reading response. %v", err)
409 defer resp.Body.Close()
411 mc.TestLog(t, "Response status: %v", resp.Status)
412 mc.TestLog(t, "Response Headers: %v", resp.Header)
413 if !strings.Contains(resp.Status, "200 OK") {
414 mc.TestError(t, "Wrong response status")
418 respBody, err := ioutil.ReadAll(resp.Body)
420 mc.TestError(t, "Error reading body. %v", err)
423 mc.TestLog(t, "%s", respBody)
427 func (mc *testingSubmgrControl) sendPostRequest(t *testing.T, addr string, path string) {
429 mc.TestLog(t, "POST http://"+addr+"%v", path)
430 req, err := http.NewRequest("POST", "http://"+addr+path, nil)
432 mc.TestError(t, "Error reading request. %v", err)
435 client := &http.Client{Timeout: time.Second * 2}
436 resp, err := client.Do(req)
438 mc.TestError(t, "Error reading response. %v", err)
441 defer resp.Body.Close()
443 mc.TestLog(t, "Response status: %v", resp.Status)
444 mc.TestLog(t, "Response Headers: %v", resp.Header)
445 if !strings.Contains(resp.Status, "200 OK") {
446 mc.TestError(t, "Wrong response status")
450 respBody, err := ioutil.ReadAll(resp.Body)
452 mc.TestError(t, "Error reading body. %v", err)
455 mc.TestLog(t, "%s", respBody)