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 allCountersMap map[string]Counter
52 var allCountersBeforeMap map[string]Counter
53 var toBeAddedCountersBeforeMap map[string]Counter
54 var toBeAddedCountersMap map[string]Counter
56 func createSubmgrControl(srcId teststub.RmrSrcId, rtgSvc teststub.RmrRtgSvc) *testingSubmgrControl {
57 mainCtrl = &testingSubmgrControl{}
58 mainCtrl.RmrControl.Init("SUBMGRCTL", srcId, rtgSvc)
59 mainCtrl.c = NewControl()
60 mainCtrl.c.UTTesting = true
61 mainCtrl.c.LoggerLevel = 4
62 mainCtrl.c.e2ap.SetASN1DebugPrintStatus(mainCtrl.c.LoggerLevel)
63 xapp.Logger.Debug("Test: LoggerLevel %v", mainCtrl.c.LoggerLevel)
64 xapp.Logger.Debug("Replacing real db with test db")
65 mainCtrl.c.e2SubsDb = CreateMock() // This overrides real E2 Subscription database for testing
66 mainCtrl.c.restSubsDb = CreateRestSubsDbMock() // This overrides real REST Subscription database for testing
67 mainCtrl.c.e2IfStateDb = CreateXappRnibIfMock() // This overrides real RNIB database for testing
68 xapp.SetReadyCB(mainCtrl.ReadyCB, nil)
69 go xapp.RunWithParams(mainCtrl.c, false)
71 mainCtrl.c.ReadyCB(nil)
75 func (mc *testingSubmgrControl) SimulateRestart(t *testing.T) {
76 mc.TestLog(t, "Simulating submgr restart")
77 mainCtrl.c.registry.subIds = nil
78 // Initialize subIds slice and subscription map
79 mainCtrl.c.registry.Initialize()
80 mainCtrl.c.restDuplicateCtrl.Init()
81 // Read subIds and subscriptions from database
82 subIds, register, err := mainCtrl.c.ReadAllSubscriptionsFromSdl()
84 mc.TestError(t, "%v", err)
86 mainCtrl.c.registry.subIds = subIds
87 mainCtrl.c.registry.register = register
88 mc.TestLog(t, "mainCtrl.c.registry.register:")
89 for subId, subs := range mainCtrl.c.registry.register {
90 mc.TestLog(t, " subId=%v", subId)
91 mc.TestLog(t, " subs.SubRespRcvd=%v", subs.SubRespRcvd)
92 mc.TestLog(t, " subs=%v\n", subs)
95 restSubscriptions, err := mainCtrl.c.ReadAllRESTSubscriptionsFromSdl()
97 mc.TestError(t, "%v", err)
99 mainCtrl.c.registry.restSubscriptions = restSubscriptions
100 mc.TestLog(t, "mainCtrl.c.registry.restSubscriptions:")
101 for restSubId, restSubs := range mainCtrl.c.registry.restSubscriptions {
102 mc.TestLog(t, " restSubId=%v", restSubId)
103 mc.TestLog(t, " restSubs=%v\n", restSubs)
107 go mainCtrl.c.HandleUncompletedSubscriptions(mainCtrl.c.registry.register)
110 func (mc *testingSubmgrControl) MakeTransactionNil(t *testing.T, subId uint32) {
112 mc.TestLog(t, "Makin transaction nil for SubId=%v", subId)
113 subs := mainCtrl.c.registry.GetSubscription(subId)
117 func (mc *testingSubmgrControl) SetResetTestFlag(t *testing.T, status bool) {
118 mc.TestLog(t, "ResetTestFlag set to %v", status)
119 mainCtrl.c.ResetTestFlag = status
122 func (mc *testingSubmgrControl) removeExistingSubscriptions(t *testing.T) {
124 mc.TestLog(t, "Removing existing subscriptions")
125 mainCtrl.c.RemoveAllSubscriptionsFromSdl()
126 mainCtrl.c.registry.subIds = nil
127 // Initialize subIds slice and subscription map
128 mainCtrl.c.registry.Initialize()
131 func PringSubscriptionQueryResult(resp models.SubscriptionList) {
132 for _, item := range resp {
133 fmt.Printf("item.SubscriptionID=%v\n", item.SubscriptionID)
134 fmt.Printf("item.Meid=%v\n", item.Meid)
135 fmt.Printf("item.ClientEndpoint=%v\n", item.ClientEndpoint)
139 func (mc *testingSubmgrControl) wait_registry_empty(t *testing.T, secs int) bool {
142 for ; i <= secs*10; i++ {
143 cnt = len(mc.c.registry.register)
147 time.Sleep(100 * time.Millisecond)
149 mc.TestError(t, "(submgr) no registry empty within %d secs: %d, register: %v", secs, cnt, mc.c.registry.register)
153 func (mc *testingSubmgrControl) get_registry_next_subid(t *testing.T) uint32 {
154 mc.c.registry.mutex.Lock()
155 defer mc.c.registry.mutex.Unlock()
156 return mc.c.registry.subIds[0]
159 func (mc *testingSubmgrControl) wait_registry_next_subid_change(t *testing.T, origSubId uint32, secs int) (uint32, bool) {
161 for ; i <= secs*10; i++ {
162 mc.c.registry.mutex.Lock()
163 currSubId := mc.c.registry.subIds[0]
164 mc.c.registry.mutex.Unlock()
165 if currSubId != origSubId {
166 return currSubId, true
168 time.Sleep(100 * time.Millisecond)
170 mc.TestError(t, "(submgr) no subId change within %d secs", secs)
174 func (mc *testingSubmgrControl) wait_subs_clean(t *testing.T, e2SubsId uint32, secs int) bool {
175 var subs *Subscription
177 for ; i <= secs*10; i++ {
178 subs = mc.c.registry.GetSubscription(e2SubsId)
182 time.Sleep(100 * time.Millisecond)
185 mc.TestError(t, "(submgr) no clean within %d secs: %s", secs, subs.String())
187 mc.TestError(t, "(submgr) no clean within %d secs: subs(N/A)", secs)
192 func (mc *testingSubmgrControl) wait_multi_subs_clean(t *testing.T, e2SubsIds []uint32, secs int) bool {
194 purgedSubscriptions := 0
196 for i := 1; i <= secs*10; i++ {
197 purgedSubscriptions = 0
198 for k := 0; k <= len(e2SubsIds); i++ {
199 subs := mc.c.registry.GetSubscription(e2SubsIds[k])
201 mc.TestLog(t, "(submgr) subscriber purged for esSubsId %v", e2SubsIds[k])
202 purgedSubscriptions += 1
203 if purgedSubscriptions == len(e2SubsIds) {
208 mc.TestLog(t, "(submgr) subscriptions pending purging %v/%v after %d msecs", purgedSubscriptions, len(e2SubsIds), i+500)
209 time.Sleep(100 * time.Millisecond)
212 mc.TestError(t, "(submgr) no clean within %d secs: subs(N/A) - %v/%v subscriptions found still", secs, purgedSubscriptions, len(e2SubsIds))
217 func (mc *testingSubmgrControl) wait_subs_trans_clean(t *testing.T, e2SubsId uint32, secs int) bool {
218 var trans TransactionIf
220 for ; i <= secs*10; i++ {
221 subs := mc.c.registry.GetSubscription(e2SubsId)
225 trans = subs.GetTransaction()
229 time.Sleep(100 * time.Millisecond)
232 mc.TestError(t, "(submgr) no clean within %d secs: %s", secs, trans.String())
234 mc.TestError(t, "(submgr) no clean within %d secs: trans(N/A)", secs)
239 func (mc *testingSubmgrControl) get_subs_entrypoint_cnt(t *testing.T, origSubId uint32) int {
240 subs := mc.c.registry.GetSubscription(origSubId)
242 mc.TestError(t, "(submgr) no subs %d exists during entrypoint cnt get", origSubId)
245 return subs.EpList.Size()
248 func (mc *testingSubmgrControl) wait_subs_entrypoint_cnt_change(t *testing.T, origSubId uint32, orig int, secs int) (int, bool) {
250 subs := mc.c.registry.GetSubscription(origSubId)
252 mc.TestError(t, "(submgr) no subs %d exists during entrypoint cnt wait", origSubId)
257 for ; i <= secs*10; i++ {
258 curr := subs.EpList.Size()
262 time.Sleep(100 * time.Millisecond)
264 mc.TestError(t, "(submgr) no subs %d entrypoint cnt change within %d secs", origSubId, secs)
269 // Counter check for received message. Note might not be yet handled
271 func (mc *testingSubmgrControl) get_msgcounter(t *testing.T) uint64 {
272 return mc.c.CntRecvMsg
275 func (mc *testingSubmgrControl) wait_msgcounter_change(t *testing.T, orig uint64, secs int) (uint64, bool) {
277 for ; i <= secs*10; i++ {
278 curr := mc.c.CntRecvMsg
282 time.Sleep(100 * time.Millisecond)
284 mc.TestError(t, "(submgr) no msg counter change within %d secs", secs)
288 func (mc *testingSubmgrControl) GetMetrics(t *testing.T) (string, error) {
289 req, err := http.NewRequest("GET", "http://localhost:8080/ric/v1/metrics", nil)
291 return "", fmt.Errorf("Error reading request. %v", err)
293 client := &http.Client{Timeout: time.Second * 10}
294 resp, err := client.Do(req)
296 return "", fmt.Errorf("Error reading response. %v", err)
298 defer resp.Body.Close()
300 respBody, err := ioutil.ReadAll(resp.Body)
302 return "", fmt.Errorf("Error reading body. %v", err)
304 return string(respBody[:]), nil
307 func (mc *testingSubmgrControl) InitAllCounterMap() {
308 counterOpts := GetMetricsOpts()
310 allCountersMap = make(map[string]Counter)
311 for _, counterOpt := range counterOpts {
312 //fmt.Printf("counterOpt.Name: '%v'\n", counterOpt.Name)
313 counter := Counter{counterOpt.Name, 0}
314 allCountersMap[counterOpt.Name] = counter
318 func (mc *testingSubmgrControl) CounterValuesToBeVeriefied(t *testing.T, countersToBeAdded CountersToBeAdded) {
320 if len(toBeAddedCountersMap) == 0 {
321 toBeAddedCountersMap = make(map[string]Counter)
323 for _, counter := range countersToBeAdded {
324 toBeAddedCountersMap[counter.Name] = counter
326 mc.GetCounterValuesBefore(t)
329 func (mc *testingSubmgrControl) GetCounterValuesBefore(t *testing.T) {
330 toBeAddedCountersBeforeMap = make(map[string]Counter)
331 toBeAddedCountersBeforeMap = mc.GetCurrentCounterValues(t, toBeAddedCountersMap)
332 allCountersBeforeMap = make(map[string]Counter)
333 allCountersBeforeMap = mc.GetCurrentCounterValues(t, allCountersMap)
336 func (mc *testingSubmgrControl) VerifyCounterValues(t *testing.T) {
338 // Check that expected counters are added ok
339 // Get current values of counters exected to be added
340 currentCountersMap := mc.GetCurrentCounterValues(t, toBeAddedCountersMap)
341 for _, toBeAddedCounter := range toBeAddedCountersMap {
342 if currentCounter, ok := currentCountersMap[toBeAddedCounter.Name]; ok == true {
343 if beforeCounter, ok := toBeAddedCountersBeforeMap[toBeAddedCounter.Name]; ok == true {
344 if currentCounter.Value != beforeCounter.Value+toBeAddedCounter.Value {
345 mc.TestError(t, "Error in expected counter value: counterName %v, current value %v, expected value %v",
346 currentCounter.Name, currentCounter.Value, beforeCounter.Value+toBeAddedCounter.Value)
349 mc.TestError(t, "Counter %v not in toBeAddedCountersBeforeMap", toBeAddedCounter.Name)
352 mc.TestError(t, "Counter %v not in currentCountersMap", toBeAddedCounter.Name)
356 // Check that not any unexpected counter are added (this is not working correctly!)
357 // Get current values of all counters
358 currentCountersMap = mc.GetCurrentCounterValues(t, allCountersMap)
359 for _, currentCounter := range currentCountersMap {
360 if _, ok := toBeAddedCountersMap[currentCounter.Name]; ok == false {
361 if beforeCounter, ok := allCountersBeforeMap[currentCounter.Name]; ok == true {
362 if currentCounter.Value != beforeCounter.Value {
363 mc.TestError(t, "Error: unexpected counter added: counterName %v, current value %v, expected value %v",
364 currentCounter.Name, currentCounter.Value, beforeCounter.Value)
371 toBeAddedCountersMap = make(map[string]Counter)
372 allCountersBeforeMap = make(map[string]Counter)
375 func (mc *testingSubmgrControl) GetCurrentCounterValues(t *testing.T, chekedCountersMap map[string]Counter) map[string]Counter {
376 countersString, err := mc.GetMetrics(t)
378 mc.TestError(t, "Error GetMetrics() failed %v", err)
382 retCounterMap := make(map[string]Counter)
383 stringsTable := strings.Split(countersString, "\n")
384 for _, counter := range chekedCountersMap {
385 for _, counterString := range stringsTable {
386 if !strings.Contains(counterString, "#") && strings.Contains(counterString, counter.Name) {
387 counterString := strings.Split(counterString, " ")
388 if strings.Contains(counterString[0], counter.Name) {
389 val, err := strconv.ParseUint(counterString[1], 10, 64)
391 mc.TestError(t, "Error: strconv.ParseUint failed %v", err)
394 //fmt.Printf("counter=%v\n", counter)
395 retCounterMap[counter.Name] = counter
401 if len(retCounterMap) != len(chekedCountersMap) {
402 mc.TestError(t, "Error: len(retCounterMap) != len(chekedCountersMap)")
407 func (mc *testingSubmgrControl) sendGetRequest(t *testing.T, addr string, path string) {
409 mc.TestLog(t, "GET http://"+addr+"%v", path)
410 req, err := http.NewRequest("GET", "http://"+addr+path, nil)
412 mc.TestError(t, "Error reading request. %v", err)
415 req.Header.Set("Cache-Control", "no-cache")
416 client := &http.Client{Timeout: time.Second * 2}
417 resp, err := client.Do(req)
419 mc.TestError(t, "Error reading response. %v", err)
422 defer resp.Body.Close()
424 mc.TestLog(t, "Response status: %v", resp.Status)
425 mc.TestLog(t, "Response Headers: %v", resp.Header)
426 if !strings.Contains(resp.Status, "200 OK") {
427 mc.TestError(t, "Wrong response status")
431 respBody, err := ioutil.ReadAll(resp.Body)
433 mc.TestError(t, "Error reading body. %v", err)
436 mc.TestLog(t, "%s", respBody)
440 func (mc *testingSubmgrControl) sendPostRequest(t *testing.T, addr string, path string) {
442 mc.TestLog(t, "POST http://"+addr+"%v", path)
443 req, err := http.NewRequest("POST", "http://"+addr+path, nil)
445 mc.TestError(t, "Error reading request. %v", err)
448 client := &http.Client{Timeout: time.Second * 2}
449 resp, err := client.Do(req)
451 mc.TestError(t, "Error reading response. %v", err)
454 defer resp.Body.Close()
456 mc.TestLog(t, "Response status: %v", resp.Status)
457 mc.TestLog(t, "Response Headers: %v", resp.Header)
458 if !strings.Contains(resp.Status, "200 OK") {
459 mc.TestError(t, "Wrong response status")
463 respBody, err := ioutil.ReadAll(resp.Body)
465 mc.TestError(t, "Error reading body. %v", err)
468 mc.TestLog(t, "%s", respBody)
472 //-----------------------------------------------------------------------------
474 //-----------------------------------------------------------------------------
475 func (mc *testingSubmgrControl) SetE2State(t *testing.T, ranNameState string) {
477 if err := mc.c.e2IfStateDb.XappRnibStoreAndPublish("RAN_CONNECTION_STATUS_CHANGE", ranNameState, "key1", "data1"); err != nil {
478 t.Errorf("XappRnibStoreAndPublish failed: %v", err)