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 ==================================================================================
24 "github.com/stretchr/testify/assert"
32 "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/teststub"
33 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/models"
34 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
37 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
40 type testingSubmgrControl struct {
50 type CountersToBeAdded []Counter
52 var allCountersMap map[string]Counter
53 var allCountersBeforeMap map[string]Counter
54 var toBeAddedCountersBeforeMap map[string]Counter
55 var toBeAddedCountersMap map[string]Counter
57 func createSubmgrControl(srcId teststub.RmrSrcId, rtgSvc teststub.RmrRtgSvc) *testingSubmgrControl {
58 mainCtrl = &testingSubmgrControl{}
59 mainCtrl.RmrControl.Init("SUBMGRCTL", srcId, rtgSvc)
60 mainCtrl.c = NewControl()
61 mainCtrl.c.UTTesting = true
62 mainCtrl.c.LoggerLevel = 4
63 mainCtrl.c.e2ap.SetASN1DebugPrintStatus(mainCtrl.c.LoggerLevel)
64 xapp.Logger.Debug("Test: LoggerLevel %v", mainCtrl.c.LoggerLevel)
65 xapp.Logger.Debug("Replacing real db with test db")
66 mainCtrl.c.e2SubsDb = CreateMock() // This overrides real E2 Subscription database for testing
67 mainCtrl.c.restSubsDb = CreateRestSubsDbMock() // This overrides real REST Subscription database for testing
68 mainCtrl.c.e2IfStateDb = CreateXappRnibIfMock() // This overrides real RNIB database for testing
69 xapp.SetReadyCB(mainCtrl.ReadyCB, nil)
70 go xapp.RunWithParams(mainCtrl.c, false)
72 mainCtrl.c.ReadyCB(nil)
76 func (mc *testingSubmgrControl) SimulateRestart(t *testing.T) {
77 mc.TestLog(t, "Simulating submgr restart")
78 mainCtrl.c.registry.subIds = nil
79 // Initialize subIds slice and subscription map
80 mainCtrl.c.registry.Initialize()
81 mainCtrl.c.restDuplicateCtrl.Init()
82 // Read subIds and subscriptions from database
83 subIds, register, err := mainCtrl.c.ReadAllSubscriptionsFromSdl()
85 mc.TestError(t, "%v", err)
87 mainCtrl.c.registry.subIds = subIds
88 mainCtrl.c.registry.register = register
89 mc.TestLog(t, "mainCtrl.c.registry.register:")
90 for subId, subs := range mainCtrl.c.registry.register {
91 mc.TestLog(t, " subId=%v", subId)
92 mc.TestLog(t, " subs.SubRespRcvd=%v", subs.SubRespRcvd)
93 mc.TestLog(t, " subs=%v\n", subs)
96 restSubscriptions, err := mainCtrl.c.ReadAllRESTSubscriptionsFromSdl()
98 mc.TestError(t, "%v", err)
100 mainCtrl.c.registry.restSubscriptions = restSubscriptions
101 mc.TestLog(t, "mainCtrl.c.registry.restSubscriptions:")
102 for restSubId, restSubs := range mainCtrl.c.registry.restSubscriptions {
103 mc.TestLog(t, " restSubId=%v", restSubId)
104 mc.TestLog(t, " restSubs=%v\n", restSubs)
108 go mainCtrl.c.HandleUncompletedSubscriptions(mainCtrl.c.registry.register)
111 func (mc *testingSubmgrControl) MakeTransactionNil(t *testing.T, subId uint32) {
113 mc.TestLog(t, "Makin transaction nil for SubId=%v", subId)
114 subs := mainCtrl.c.registry.GetSubscription(subId)
118 func (mc *testingSubmgrControl) SetResetTestFlag(t *testing.T, status bool) {
119 mc.TestLog(t, "ResetTestFlag set to %v", status)
120 mainCtrl.c.ResetTestFlag = status
123 func (mc *testingSubmgrControl) removeExistingSubscriptions(t *testing.T) {
125 mc.TestLog(t, "Removing existing subscriptions")
126 mainCtrl.c.RemoveAllSubscriptionsFromSdl()
127 mainCtrl.c.registry.subIds = nil
128 // Initialize subIds slice and subscription map
129 mainCtrl.c.registry.Initialize()
132 func PringSubscriptionQueryResult(resp models.SubscriptionList) {
133 for _, item := range resp {
134 fmt.Printf("item.SubscriptionID=%v\n", item.SubscriptionID)
135 fmt.Printf("item.Meid=%v\n", item.Meid)
136 fmt.Printf("item.ClientEndpoint=%v\n", item.ClientEndpoint)
140 func (mc *testingSubmgrControl) wait_registry_empty(t *testing.T, secs int) bool {
143 for ; i <= secs*10; i++ {
144 cnt = len(mc.c.registry.register)
148 time.Sleep(100 * time.Millisecond)
150 mc.TestError(t, "(submgr) no registry empty within %d secs: %d, register: %v", secs, cnt, mc.c.registry.register)
154 func (mc *testingSubmgrControl) get_registry_next_subid(t *testing.T) uint32 {
155 mc.c.registry.mutex.Lock()
156 defer mc.c.registry.mutex.Unlock()
157 return mc.c.registry.subIds[0]
160 func (mc *testingSubmgrControl) wait_registry_next_subid_change(t *testing.T, origSubId uint32, secs int) (uint32, bool) {
162 for ; i <= secs*10; i++ {
163 mc.c.registry.mutex.Lock()
164 currSubId := mc.c.registry.subIds[0]
165 mc.c.registry.mutex.Unlock()
166 if currSubId != origSubId {
167 return currSubId, true
169 time.Sleep(100 * time.Millisecond)
171 mc.TestError(t, "(submgr) no subId change within %d secs", secs)
175 func (mc *testingSubmgrControl) wait_subs_clean(t *testing.T, e2SubsId uint32, secs int) bool {
176 var subs *Subscription
178 for ; i <= secs*10; i++ {
179 subs = mc.c.registry.GetSubscription(e2SubsId)
183 time.Sleep(100 * time.Millisecond)
186 mc.TestError(t, "(submgr) no clean within %d secs: %s", secs, subs.String())
188 mc.TestError(t, "(submgr) no clean within %d secs: subs(N/A)", secs)
193 func (mc *testingSubmgrControl) wait_multi_subs_clean(t *testing.T, e2SubsIds []uint32, secs int) bool {
195 purgedSubscriptions := 0
197 for i := 1; i <= secs*10; i++ {
198 purgedSubscriptions = 0
199 for k := 0; k <= len(e2SubsIds); i++ {
200 subs := mc.c.registry.GetSubscription(e2SubsIds[k])
202 mc.TestLog(t, "(submgr) subscriber purged for esSubsId %v", e2SubsIds[k])
203 purgedSubscriptions += 1
204 if purgedSubscriptions == len(e2SubsIds) {
209 mc.TestLog(t, "(submgr) subscriptions pending purging %v/%v after %d msecs", purgedSubscriptions, len(e2SubsIds), i+500)
210 time.Sleep(100 * time.Millisecond)
213 mc.TestError(t, "(submgr) no clean within %d secs: subs(N/A) - %v/%v subscriptions found still", secs, purgedSubscriptions, len(e2SubsIds))
218 func (mc *testingSubmgrControl) wait_subs_trans_clean(t *testing.T, e2SubsId uint32, secs int) bool {
219 var trans TransactionIf
221 for ; i <= secs*10; i++ {
222 subs := mc.c.registry.GetSubscription(e2SubsId)
226 trans = subs.GetTransaction()
230 time.Sleep(100 * time.Millisecond)
233 mc.TestError(t, "(submgr) no clean within %d secs: %s", secs, trans.String())
235 mc.TestError(t, "(submgr) no clean within %d secs: trans(N/A)", secs)
240 func (mc *testingSubmgrControl) get_subs_entrypoint_cnt(t *testing.T, origSubId uint32) int {
241 subs := mc.c.registry.GetSubscription(origSubId)
243 mc.TestError(t, "(submgr) no subs %d exists during entrypoint cnt get", origSubId)
246 return subs.EpList.Size()
249 func (mc *testingSubmgrControl) wait_subs_entrypoint_cnt_change(t *testing.T, origSubId uint32, orig int, secs int) (int, bool) {
251 subs := mc.c.registry.GetSubscription(origSubId)
253 mc.TestError(t, "(submgr) no subs %d exists during entrypoint cnt wait", origSubId)
258 for ; i <= secs*10; i++ {
259 curr := subs.EpList.Size()
263 time.Sleep(100 * time.Millisecond)
265 mc.TestError(t, "(submgr) no subs %d entrypoint cnt change within %d secs", origSubId, secs)
270 // Counter check for received message. Note might not be yet handled
272 func (mc *testingSubmgrControl) get_msgcounter(t *testing.T) uint64 {
273 return mc.c.CntRecvMsg
276 func (mc *testingSubmgrControl) wait_msgcounter_change(t *testing.T, orig uint64, secs int) (uint64, bool) {
278 for ; i <= secs*10; i++ {
279 curr := mc.c.CntRecvMsg
283 time.Sleep(100 * time.Millisecond)
285 mc.TestError(t, "(submgr) no msg counter change within %d secs", secs)
289 func (mc *testingSubmgrControl) VerifyAllClean(t *testing.T) {
290 // Verify that all resources are freed
291 assert.Equal(t, 0, len(mainCtrl.c.registry.register))
292 assert.Equal(t, 0, len(mainCtrl.c.registry.restSubscriptions))
293 verifyRESTKeyCount(t, 0)
294 verifyE2KeyCount(t, 0)
297 func (mc *testingSubmgrControl) GetMetrics(t *testing.T) (string, error) {
298 req, err := http.NewRequest("GET", "http://localhost:8080/ric/v1/metrics", nil)
300 return "", fmt.Errorf("Error reading request. %v", err)
302 client := &http.Client{Timeout: time.Second * 10}
303 resp, err := client.Do(req)
305 return "", fmt.Errorf("Error reading response. %v", err)
307 defer resp.Body.Close()
309 respBody, err := ioutil.ReadAll(resp.Body)
311 return "", fmt.Errorf("Error reading body. %v", err)
313 return string(respBody[:]), nil
316 func (mc *testingSubmgrControl) InitAllCounterMap() {
317 counterOpts := GetMetricsOpts()
319 allCountersMap = make(map[string]Counter)
320 for _, counterOpt := range counterOpts {
321 //fmt.Printf("counterOpt.Name: '%v'\n", counterOpt.Name)
322 counter := Counter{counterOpt.Name, 0}
323 allCountersMap[counterOpt.Name] = counter
327 func (mc *testingSubmgrControl) CounterValuesToBeVeriefied(t *testing.T, countersToBeAdded CountersToBeAdded) {
329 if len(toBeAddedCountersMap) == 0 {
330 toBeAddedCountersMap = make(map[string]Counter)
332 for _, counter := range countersToBeAdded {
333 toBeAddedCountersMap[counter.Name] = counter
335 mc.GetCounterValuesBefore(t)
338 func (mc *testingSubmgrControl) GetCounterValuesBefore(t *testing.T) {
339 toBeAddedCountersBeforeMap = make(map[string]Counter)
340 toBeAddedCountersBeforeMap = mc.GetCurrentCounterValues(t, toBeAddedCountersMap)
341 allCountersBeforeMap = make(map[string]Counter)
342 allCountersBeforeMap = mc.GetCurrentCounterValues(t, allCountersMap)
345 func (mc *testingSubmgrControl) VerifyCounterValues(t *testing.T) {
347 // Check that expected counters are added ok
348 // Get current values of counters exected to be added
349 currentCountersMap := mc.GetCurrentCounterValues(t, toBeAddedCountersMap)
350 for _, toBeAddedCounter := range toBeAddedCountersMap {
351 if currentCounter, ok := currentCountersMap[toBeAddedCounter.Name]; ok == true {
352 if beforeCounter, ok := toBeAddedCountersBeforeMap[toBeAddedCounter.Name]; ok == true {
353 if currentCounter.Value != beforeCounter.Value+toBeAddedCounter.Value {
354 mc.TestError(t, "Error in expected counter value: counterName %v, current value %v, expected value %v",
355 currentCounter.Name, currentCounter.Value, beforeCounter.Value+toBeAddedCounter.Value)
358 mc.TestError(t, "Counter %v not in toBeAddedCountersBeforeMap", toBeAddedCounter.Name)
361 mc.TestError(t, "Counter %v not in currentCountersMap", toBeAddedCounter.Name)
365 // Check that not any unexpected counter are added (this is not working correctly!)
366 // Get current values of all counters
367 currentCountersMap = mc.GetCurrentCounterValues(t, allCountersMap)
368 for _, currentCounter := range currentCountersMap {
369 if _, ok := toBeAddedCountersMap[currentCounter.Name]; ok == false {
370 if beforeCounter, ok := allCountersBeforeMap[currentCounter.Name]; ok == true {
371 if currentCounter.Value != beforeCounter.Value {
372 mc.TestError(t, "Error: unexpected counter added: counterName %v, current value %v, expected value %v",
373 currentCounter.Name, currentCounter.Value, beforeCounter.Value)
380 toBeAddedCountersMap = make(map[string]Counter)
381 allCountersBeforeMap = make(map[string]Counter)
384 func (mc *testingSubmgrControl) GetCurrentCounterValues(t *testing.T, chekedCountersMap map[string]Counter) map[string]Counter {
385 countersString, err := mc.GetMetrics(t)
387 mc.TestError(t, "Error GetMetrics() failed %v", err)
391 retCounterMap := make(map[string]Counter)
392 stringsTable := strings.Split(countersString, "\n")
393 for _, counter := range chekedCountersMap {
394 for _, counterString := range stringsTable {
395 if !strings.Contains(counterString, "#") && strings.Contains(counterString, counter.Name) {
396 counterString := strings.Split(counterString, " ")
397 if strings.Contains(counterString[0], counter.Name) {
398 val, err := strconv.ParseUint(counterString[1], 10, 64)
400 mc.TestError(t, "Error: strconv.ParseUint failed %v", err)
403 //fmt.Printf("counter=%v\n", counter)
404 retCounterMap[counter.Name] = counter
410 if len(retCounterMap) != len(chekedCountersMap) {
411 mc.TestError(t, "Error: len(retCounterMap) != len(chekedCountersMap)")
416 func (mc *testingSubmgrControl) sendGetRequest(t *testing.T, addr string, path string) {
418 mc.TestLog(t, "GET http://"+addr+"%v", path)
419 req, err := http.NewRequest("GET", "http://"+addr+path, nil)
421 mc.TestError(t, "Error reading request. %v", err)
424 req.Header.Set("Cache-Control", "no-cache")
425 client := &http.Client{Timeout: time.Second * 2}
426 resp, err := client.Do(req)
428 mc.TestError(t, "Error reading response. %v", err)
431 defer resp.Body.Close()
433 mc.TestLog(t, "Response status: %v", resp.Status)
434 mc.TestLog(t, "Response Headers: %v", resp.Header)
435 if !strings.Contains(resp.Status, "200 OK") {
436 mc.TestError(t, "Wrong response status")
440 respBody, err := ioutil.ReadAll(resp.Body)
442 mc.TestError(t, "Error reading body. %v", err)
445 mc.TestLog(t, "%s", respBody)
449 func (mc *testingSubmgrControl) sendPostRequest(t *testing.T, addr string, path string) {
451 mc.TestLog(t, "POST http://"+addr+"%v", path)
452 req, err := http.NewRequest("POST", "http://"+addr+path, nil)
454 mc.TestError(t, "Error reading request. %v", err)
457 client := &http.Client{Timeout: time.Second * 2}
458 resp, err := client.Do(req)
460 mc.TestError(t, "Error reading response. %v", err)
463 defer resp.Body.Close()
465 mc.TestLog(t, "Response status: %v", resp.Status)
466 mc.TestLog(t, "Response Headers: %v", resp.Header)
467 if !strings.Contains(resp.Status, "200 OK") {
468 mc.TestError(t, "Wrong response status")
472 respBody, err := ioutil.ReadAll(resp.Body)
474 mc.TestError(t, "Error reading body. %v", err)
477 mc.TestLog(t, "%s", respBody)
481 //-----------------------------------------------------------------------------
483 //-----------------------------------------------------------------------------
484 func (mc *testingSubmgrControl) SetE2State(t *testing.T, ranNameState string) {
486 if err := mc.c.e2IfStateDb.XappRnibStoreAndPublish("RAN_CONNECTION_STATUS_CHANGE", ranNameState, "key1", "data1"); err != nil {
487 t.Errorf("XappRnibStoreAndPublish failed: %v", err)