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 xapp.Logger.Debug("Replacing real db with test db")
60 mainCtrl.c.e2SubsDb = CreateMock() // This overrides real E2 Subscription database for testing
61 mainCtrl.c.restSubsDb = CreateRestSubsDbMock() // This overrides real REST Subscription database for testing
62 xapp.SetReadyCB(mainCtrl.ReadyCB, nil)
63 go xapp.RunWithParams(mainCtrl.c, false)
65 mainCtrl.c.ReadyCB(nil)
69 func (mc *testingSubmgrControl) SimulateRestart(t *testing.T) {
70 mc.TestLog(t, "Simulating submgr restart")
71 mainCtrl.c.registry.subIds = nil
72 // Initialize subIds slice and subscription map
73 mainCtrl.c.registry.Initialize()
74 restDuplicateCtrl.Init()
75 // Read subIds and subscriptions from database
76 subIds, register, err := mainCtrl.c.ReadAllSubscriptionsFromSdl()
78 mc.TestError(t, "%v", err)
80 mainCtrl.c.registry.register = nil
81 mainCtrl.c.registry.subIds = subIds
82 mainCtrl.c.registry.register = register
84 mc.TestLog(t, "register:")
85 for subId, subs := range 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 mc.TestLog(t, "mainCtrl.c.registry.register:")
92 for subId, subs := range mainCtrl.c.registry.register {
93 mc.TestLog(t, " subId=%v", subId)
94 mc.TestLog(t, " subs.SubRespRcvd=%v", subs.SubRespRcvd)
95 mc.TestLog(t, " subs=%v\n", subs)
98 go mainCtrl.c.HandleUncompletedSubscriptions(mainCtrl.c.registry.register)
101 func (mc *testingSubmgrControl) MakeTransactionNil(t *testing.T, subId uint32) {
103 mc.TestLog(t, "Makin transaction nil for SubId=%v", subId)
104 subs := mainCtrl.c.registry.GetSubscription(subId)
108 func (mc *testingSubmgrControl) SetResetTestFlag(t *testing.T, status bool) {
109 mc.TestLog(t, "ResetTestFlag set to %v", status)
110 mainCtrl.c.ResetTestFlag = status
113 func (mc *testingSubmgrControl) removeExistingSubscriptions(t *testing.T) {
115 mc.TestLog(t, "Removing existing subscriptions")
116 mainCtrl.c.RemoveAllSubscriptionsFromSdl()
117 mainCtrl.c.registry.subIds = nil
118 // Initialize subIds slice and subscription map
119 mainCtrl.c.registry.Initialize()
122 func PringSubscriptionQueryResult(resp models.SubscriptionList) {
123 for _, item := range resp {
124 fmt.Printf("item.SubscriptionID=%v\n", item.SubscriptionID)
125 fmt.Printf("item.Meid=%v\n", item.Meid)
126 fmt.Printf("item.ClientEndpoint=%v\n", item.ClientEndpoint)
130 func (mc *testingSubmgrControl) wait_registry_empty(t *testing.T, secs int) bool {
133 for ; i <= secs*10; i++ {
134 cnt = len(mc.c.registry.register)
138 time.Sleep(100 * time.Millisecond)
140 mc.TestError(t, "(submgr) no registry empty within %d secs: %d", secs, cnt)
144 func (mc *testingSubmgrControl) get_registry_next_subid(t *testing.T) uint32 {
145 mc.c.registry.mutex.Lock()
146 defer mc.c.registry.mutex.Unlock()
147 return mc.c.registry.subIds[0]
150 func (mc *testingSubmgrControl) wait_registry_next_subid_change(t *testing.T, origSubId uint32, secs int) (uint32, bool) {
152 for ; i <= secs*10; i++ {
153 mc.c.registry.mutex.Lock()
154 currSubId := mc.c.registry.subIds[0]
155 mc.c.registry.mutex.Unlock()
156 if currSubId != origSubId {
157 return currSubId, true
159 time.Sleep(100 * time.Millisecond)
161 mc.TestError(t, "(submgr) no subId change within %d secs", secs)
165 func (mc *testingSubmgrControl) wait_subs_clean(t *testing.T, e2SubsId uint32, secs int) bool {
166 var subs *Subscription
168 for ; i <= secs*10; i++ {
169 subs = mc.c.registry.GetSubscription(e2SubsId)
173 time.Sleep(100 * time.Millisecond)
176 mc.TestError(t, "(submgr) no clean within %d secs: %s", secs, subs.String())
178 mc.TestError(t, "(submgr) no clean within %d secs: subs(N/A)", secs)
183 func (mc *testingSubmgrControl) wait_multi_subs_clean(t *testing.T, e2SubsIds []uint32, secs int) bool {
185 purgedSubscriptions := 0
187 for i := 1; i <= secs*10; i++ {
188 purgedSubscriptions = 0
189 for k := 0; k <= len(e2SubsIds); i++ {
190 subs := mc.c.registry.GetSubscription(e2SubsIds[k])
192 mc.TestLog(t, "(submgr) subscriber purged for esSubsId %v", e2SubsIds[k])
193 purgedSubscriptions += 1
194 if purgedSubscriptions == len(e2SubsIds) {
199 mc.TestLog(t, "(submgr) subscriptions pending purging %v/%v after %d msecs", purgedSubscriptions, len(e2SubsIds), i+500)
200 time.Sleep(100 * time.Millisecond)
203 mc.TestError(t, "(submgr) no clean within %d secs: subs(N/A) - %v/%v subscriptions found still", secs, purgedSubscriptions, len(e2SubsIds))
208 func (mc *testingSubmgrControl) wait_subs_trans_clean(t *testing.T, e2SubsId uint32, secs int) bool {
209 var trans TransactionIf
211 for ; i <= secs*10; i++ {
212 subs := mc.c.registry.GetSubscription(e2SubsId)
216 trans = subs.GetTransaction()
220 time.Sleep(100 * time.Millisecond)
223 mc.TestError(t, "(submgr) no clean within %d secs: %s", secs, trans.String())
225 mc.TestError(t, "(submgr) no clean within %d secs: trans(N/A)", secs)
230 func (mc *testingSubmgrControl) get_subs_entrypoint_cnt(t *testing.T, origSubId uint32) int {
231 subs := mc.c.registry.GetSubscription(origSubId)
233 mc.TestError(t, "(submgr) no subs %d exists during entrypoint cnt get", origSubId)
236 return subs.EpList.Size()
239 func (mc *testingSubmgrControl) wait_subs_entrypoint_cnt_change(t *testing.T, origSubId uint32, orig int, secs int) (int, bool) {
241 subs := mc.c.registry.GetSubscription(origSubId)
243 mc.TestError(t, "(submgr) no subs %d exists during entrypoint cnt wait", origSubId)
248 for ; i <= secs*10; i++ {
249 curr := subs.EpList.Size()
253 time.Sleep(100 * time.Millisecond)
255 mc.TestError(t, "(submgr) no subs %d entrypoint cnt change within %d secs", origSubId, secs)
260 // Counter check for received message. Note might not be yet handled
262 func (mc *testingSubmgrControl) get_msgcounter(t *testing.T) uint64 {
263 return mc.c.CntRecvMsg
266 func (mc *testingSubmgrControl) wait_msgcounter_change(t *testing.T, orig uint64, secs int) (uint64, bool) {
268 for ; i <= secs*10; i++ {
269 curr := mc.c.CntRecvMsg
273 time.Sleep(100 * time.Millisecond)
275 mc.TestError(t, "(submgr) no msg counter change within %d secs", secs)
279 func (mc *testingSubmgrControl) GetMetrics(t *testing.T) (string, error) {
280 req, err := http.NewRequest("GET", "http://localhost:8080/ric/v1/metrics", nil)
282 return "", fmt.Errorf("Error reading request. %v", err)
284 client := &http.Client{Timeout: time.Second * 10}
285 resp, err := client.Do(req)
287 return "", fmt.Errorf("Error reading response. %v", err)
289 defer resp.Body.Close()
291 respBody, err := ioutil.ReadAll(resp.Body)
293 return "", fmt.Errorf("Error reading body. %v", err)
295 return string(respBody[:]), nil
298 func (mc *testingSubmgrControl) CounterValuesToBeVeriefied(t *testing.T, countersToBeAdded CountersToBeAdded) {
300 if len(toBeAddedCountersMap) == 0 {
301 toBeAddedCountersMap = make(map[string]Counter)
303 for _, counter := range countersToBeAdded {
304 toBeAddedCountersMap[counter.Name] = counter
306 mc.GetCounterValuesBefore(t)
309 func (mc *testingSubmgrControl) GetCounterValuesBefore(t *testing.T) {
310 countersBeforeMap = make(map[string]Counter)
311 countersBeforeMap = mc.GetCurrentCounterValues(t, toBeAddedCountersMap)
314 func (mc *testingSubmgrControl) VerifyCounterValues(t *testing.T) {
315 currentCountersMap := mc.GetCurrentCounterValues(t, toBeAddedCountersMap)
316 for _, toBeAddedCounter := range toBeAddedCountersMap {
317 if currentCounter, ok := currentCountersMap[toBeAddedCounter.Name]; ok == true {
318 if beforeCounter, ok := countersBeforeMap[toBeAddedCounter.Name]; ok == true {
319 if currentCounter.Value != beforeCounter.Value+toBeAddedCounter.Value {
320 mc.TestError(t, "Error in expected counter value: counterName %v, current value %v, expected value %v",
321 currentCounter.Name, currentCounter.Value, beforeCounter.Value+toBeAddedCounter.Value)
323 //fmt.Printf("beforeCounter.Value=%v, toBeAddedCounter.Value=%v, \n",beforeCounter.Value, toBeAddedCounter.Value)
326 mc.TestError(t, "Counter %v not in countersBeforeMap", toBeAddedCounter.Name)
329 mc.TestError(t, "Counter %v not in currentCountersMap", toBeAddedCounter.Name)
334 //fmt.Printf("toBeAddedCountersMap=%v\n",toBeAddedCountersMap)
335 toBeAddedCountersMap = make(map[string]Counter)
338 func (mc *testingSubmgrControl) GetCurrentCounterValues(t *testing.T, chekedCountersMap map[string]Counter) map[string]Counter {
339 countersString, err := mc.GetMetrics(t)
341 mc.TestError(t, "Error GetMetrics() failed %v", err)
345 retCounterMap := make(map[string]Counter)
346 stringsTable := strings.Split(countersString, "\n")
347 for _, counter := range chekedCountersMap {
348 for _, counterString := range stringsTable {
349 if !strings.Contains(counterString, "#") && strings.Contains(counterString, counter.Name) {
350 counterString := strings.Split(counterString, " ")
351 if strings.Contains(counterString[0], counter.Name) {
352 val, err := strconv.ParseUint(counterString[1], 10, 64)
354 mc.TestError(t, "Error: strconv.ParseUint failed %v", err)
357 //fmt.Printf("counter=%v\n", counter)
358 retCounterMap[counter.Name] = counter
364 if len(retCounterMap) != len(chekedCountersMap) {
365 mc.TestError(t, "Error: len(retCounterMap) != len(chekedCountersMap)")
371 func (mc *testingSubmgrControl) sendGetRequest(t *testing.T, addr string, path string) {
373 mc.TestLog(t, "GET http://"+addr+"%v", path)
374 req, err := http.NewRequest("GET", "http://"+addr+path, nil)
376 mc.TestError(t, "Error reading request. %v", err)
379 req.Header.Set("Cache-Control", "no-cache")
380 client := &http.Client{Timeout: time.Second * 2}
381 resp, err := client.Do(req)
383 mc.TestError(t, "Error reading response. %v", err)
386 defer resp.Body.Close()
388 mc.TestLog(t, "Response status: %v", resp.Status)
389 mc.TestLog(t, "Response Headers: %v", resp.Header)
390 if !strings.Contains(resp.Status, "200 OK") {
391 mc.TestError(t, "Wrong response status")
395 respBody, err := ioutil.ReadAll(resp.Body)
397 mc.TestError(t, "Error reading body. %v", err)
400 mc.TestLog(t, "%s", respBody)
404 func (mc *testingSubmgrControl) sendPostRequest(t *testing.T, addr string, path string) {
406 mc.TestLog(t, "POST http://"+addr+"%v", path)
407 req, err := http.NewRequest("POST", "http://"+addr+path, nil)
409 mc.TestError(t, "Error reading request. %v", err)
412 client := &http.Client{Timeout: time.Second * 2}
413 resp, err := client.Do(req)
415 mc.TestError(t, "Error reading response. %v", err)
418 defer resp.Body.Close()
420 mc.TestLog(t, "Response status: %v", resp.Status)
421 mc.TestLog(t, "Response Headers: %v", resp.Header)
422 if !strings.Contains(resp.Status, "200 OK") {
423 mc.TestError(t, "Wrong response status")
427 respBody, err := ioutil.ReadAll(resp.Body)
429 mc.TestError(t, "Error reading body. %v", err)
432 mc.TestLog(t, "%s", respBody)