e514cb0faf306b87334dc6ca31e700f90afa69d0
[ric-plt/submgr.git] / pkg / control / ut_ctrl_submgr_test.go
1 /*
2 ==================================================================================
3   Copyright (c) 2019 AT&T Intellectual Property.
4   Copyright (c) 2019 Nokia
5
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
9
10        http://www.apache.org/licenses/LICENSE-2.0
11
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 ==================================================================================
18 */
19
20 package control
21
22 import (
23         "fmt"
24         "io/ioutil"
25         "net/http"
26         "strconv"
27         "strings"
28         "testing"
29         "time"
30
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"
34 )
35
36 //-----------------------------------------------------------------------------
37 //
38 //-----------------------------------------------------------------------------
39 type testingSubmgrControl struct {
40         teststub.RmrControl
41         c *Control
42 }
43
44 type Counter struct {
45         Name  string
46         Value uint64
47 }
48
49 type CountersToBeAdded []Counter
50
51 var countersBeforeMap map[string]Counter
52 var toBeAddedCountersMap map[string]Counter
53
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)
66         mainCtrl.WaitCB()
67         mainCtrl.c.ReadyCB(nil)
68         return mainCtrl
69 }
70
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         mainCtrl.c.restDuplicateCtrl.Init()
77         // Read subIds and subscriptions from database
78         subIds, register, err := mainCtrl.c.ReadAllSubscriptionsFromSdl()
79         if err != nil {
80                 mc.TestError(t, "%v", err)
81         } else {
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)
89                 }
90         }
91         restSubscriptions, err := mainCtrl.c.ReadAllRESTSubscriptionsFromSdl()
92         if err != nil {
93                 mc.TestError(t, "%v", err)
94         } else {
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)
100                 }
101         }
102
103         go mainCtrl.c.HandleUncompletedSubscriptions(mainCtrl.c.registry.register)
104 }
105
106 func (mc *testingSubmgrControl) MakeTransactionNil(t *testing.T, subId uint32) {
107
108         mc.TestLog(t, "Makin transaction nil for SubId=%v", subId)
109         subs := mainCtrl.c.registry.GetSubscription(subId)
110         subs.TheTrans = nil
111 }
112
113 func (mc *testingSubmgrControl) SetResetTestFlag(t *testing.T, status bool) {
114         mc.TestLog(t, "ResetTestFlag set to %v", status)
115         mainCtrl.c.ResetTestFlag = status
116 }
117
118 func (mc *testingSubmgrControl) removeExistingSubscriptions(t *testing.T) {
119
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()
125 }
126
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)
132         }
133 }
134
135 func (mc *testingSubmgrControl) wait_registry_empty(t *testing.T, secs int) bool {
136         cnt := int(0)
137         i := 1
138         for ; i <= secs*10; i++ {
139                 cnt = len(mc.c.registry.register)
140                 if cnt == 0 {
141                         return true
142                 }
143                 time.Sleep(100 * time.Millisecond)
144         }
145         mc.TestError(t, "(submgr) no registry empty within %d secs: %d, register: %v", secs, cnt, mc.c.registry.register)
146         return false
147 }
148
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]
153 }
154
155 func (mc *testingSubmgrControl) wait_registry_next_subid_change(t *testing.T, origSubId uint32, secs int) (uint32, bool) {
156         i := 1
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
163                 }
164                 time.Sleep(100 * time.Millisecond)
165         }
166         mc.TestError(t, "(submgr) no subId change within %d secs", secs)
167         return 0, false
168 }
169
170 func (mc *testingSubmgrControl) wait_subs_clean(t *testing.T, e2SubsId uint32, secs int) bool {
171         var subs *Subscription
172         i := 1
173         for ; i <= secs*10; i++ {
174                 subs = mc.c.registry.GetSubscription(e2SubsId)
175                 if subs == nil {
176                         return true
177                 }
178                 time.Sleep(100 * time.Millisecond)
179         }
180         if subs != nil {
181                 mc.TestError(t, "(submgr) no clean within %d secs: %s", secs, subs.String())
182         } else {
183                 mc.TestError(t, "(submgr) no clean within %d secs: subs(N/A)", secs)
184         }
185         return false
186 }
187
188 func (mc *testingSubmgrControl) wait_multi_subs_clean(t *testing.T, e2SubsIds []uint32, secs int) bool {
189
190         purgedSubscriptions := 0
191
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])
196                         if subs == nil {
197                                 mc.TestLog(t, "(submgr) subscriber purged for esSubsId %v", e2SubsIds[k])
198                                 purgedSubscriptions += 1
199                                 if purgedSubscriptions == len(e2SubsIds) {
200                                         return true
201                                 }
202                         }
203                 }
204                 mc.TestLog(t, "(submgr) subscriptions pending purging %v/%v after %d msecs", purgedSubscriptions, len(e2SubsIds), i+500)
205                 time.Sleep(100 * time.Millisecond)
206         }
207
208         mc.TestError(t, "(submgr) no clean within %d secs: subs(N/A) - %v/%v subscriptions found still", secs, purgedSubscriptions, len(e2SubsIds))
209
210         return false
211 }
212
213 func (mc *testingSubmgrControl) wait_subs_trans_clean(t *testing.T, e2SubsId uint32, secs int) bool {
214         var trans TransactionIf
215         i := 1
216         for ; i <= secs*10; i++ {
217                 subs := mc.c.registry.GetSubscription(e2SubsId)
218                 if subs == nil {
219                         return true
220                 }
221                 trans = subs.GetTransaction()
222                 if trans == nil {
223                         return true
224                 }
225                 time.Sleep(100 * time.Millisecond)
226         }
227         if trans != nil {
228                 mc.TestError(t, "(submgr) no clean within %d secs: %s", secs, trans.String())
229         } else {
230                 mc.TestError(t, "(submgr) no clean within %d secs: trans(N/A)", secs)
231         }
232         return false
233 }
234
235 func (mc *testingSubmgrControl) get_subs_entrypoint_cnt(t *testing.T, origSubId uint32) int {
236         subs := mc.c.registry.GetSubscription(origSubId)
237         if subs == nil {
238                 mc.TestError(t, "(submgr) no subs %d exists during entrypoint cnt get", origSubId)
239                 return -1
240         }
241         return subs.EpList.Size()
242 }
243
244 func (mc *testingSubmgrControl) wait_subs_entrypoint_cnt_change(t *testing.T, origSubId uint32, orig int, secs int) (int, bool) {
245
246         subs := mc.c.registry.GetSubscription(origSubId)
247         if subs == nil {
248                 mc.TestError(t, "(submgr) no subs %d exists during entrypoint cnt wait", origSubId)
249                 return -1, true
250         }
251
252         i := 1
253         for ; i <= secs*10; i++ {
254                 curr := subs.EpList.Size()
255                 if curr != orig {
256                         return curr, true
257                 }
258                 time.Sleep(100 * time.Millisecond)
259         }
260         mc.TestError(t, "(submgr) no subs %d entrypoint cnt change within %d secs", origSubId, secs)
261         return 0, false
262 }
263
264 //
265 // Counter check for received message. Note might not be yet handled
266 //
267 func (mc *testingSubmgrControl) get_msgcounter(t *testing.T) uint64 {
268         return mc.c.CntRecvMsg
269 }
270
271 func (mc *testingSubmgrControl) wait_msgcounter_change(t *testing.T, orig uint64, secs int) (uint64, bool) {
272         i := 1
273         for ; i <= secs*10; i++ {
274                 curr := mc.c.CntRecvMsg
275                 if curr != orig {
276                         return curr, true
277                 }
278                 time.Sleep(100 * time.Millisecond)
279         }
280         mc.TestError(t, "(submgr) no msg counter change within %d secs", secs)
281         return 0, false
282 }
283
284 func (mc *testingSubmgrControl) GetMetrics(t *testing.T) (string, error) {
285         req, err := http.NewRequest("GET", "http://localhost:8080/ric/v1/metrics", nil)
286         if err != nil {
287                 return "", fmt.Errorf("Error reading request. %v", err)
288         }
289         client := &http.Client{Timeout: time.Second * 10}
290         resp, err := client.Do(req)
291         if err != nil {
292                 return "", fmt.Errorf("Error reading response. %v", err)
293         }
294         defer resp.Body.Close()
295
296         respBody, err := ioutil.ReadAll(resp.Body)
297         if err != nil {
298                 return "", fmt.Errorf("Error reading body. %v", err)
299         }
300         return string(respBody[:]), nil
301 }
302
303 func (mc *testingSubmgrControl) CounterValuesToBeVeriefied(t *testing.T, countersToBeAdded CountersToBeAdded) {
304
305         if len(toBeAddedCountersMap) == 0 {
306                 toBeAddedCountersMap = make(map[string]Counter)
307         }
308         for _, counter := range countersToBeAdded {
309                 toBeAddedCountersMap[counter.Name] = counter
310         }
311         mc.GetCounterValuesBefore(t)
312 }
313
314 func (mc *testingSubmgrControl) GetCounterValuesBefore(t *testing.T) {
315         countersBeforeMap = make(map[string]Counter)
316         countersBeforeMap = mc.GetCurrentCounterValues(t, toBeAddedCountersMap)
317 }
318
319 func (mc *testingSubmgrControl) VerifyCounterValues(t *testing.T) {
320
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)
329
330                                         //fmt.Printf("beforeCounter.Value=%v, toBeAddedCounter.Value=%v, \n",beforeCounter.Value, toBeAddedCounter.Value)
331                                 }
332                         } else {
333                                 mc.TestError(t, "Counter %v not in countersBeforeMap", toBeAddedCounter.Name)
334                         }
335                 } else {
336                         mc.TestError(t, "Counter %v not in currentCountersMap", toBeAddedCounter.Name)
337                 }
338         }
339
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)
347
348                                         //fmt.Printf("beforeCounter.Value=%v, toBeAddedCounter.Value=%v, \n",beforeCounter.Value, toBeAddedCounter.Value)
349                                 }
350                         } else {
351                                 mc.TestError(t, "Counter %v not in countersBeforeMap", beforeCounter.Name)
352                         }
353                 }
354         }
355
356         // Make map empty
357         //fmt.Printf("toBeAddedCountersMap=%v\n",toBeAddedCountersMap)
358         toBeAddedCountersMap = make(map[string]Counter)
359 }
360
361 func (mc *testingSubmgrControl) GetCurrentCounterValues(t *testing.T, chekedCountersMap map[string]Counter) map[string]Counter {
362         countersString, err := mc.GetMetrics(t)
363         if err != nil {
364                 mc.TestError(t, "Error GetMetrics() failed %v", err)
365                 return nil
366         }
367
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)
376                                         if err != nil {
377                                                 mc.TestError(t, "Error: strconv.ParseUint failed %v", err)
378                                         }
379                                         counter.Value = val
380                                         //fmt.Printf("counter=%v\n", counter)
381                                         retCounterMap[counter.Name] = counter
382                                 }
383                         }
384                 }
385         }
386
387         if len(retCounterMap) != len(chekedCountersMap) {
388                 mc.TestError(t, "Error: len(retCounterMap) != len(chekedCountersMap)")
389
390         }
391         return retCounterMap
392 }
393
394 func (mc *testingSubmgrControl) sendGetRequest(t *testing.T, addr string, path string) {
395
396         mc.TestLog(t, "GET http://"+addr+"%v", path)
397         req, err := http.NewRequest("GET", "http://"+addr+path, nil)
398         if err != nil {
399                 mc.TestError(t, "Error reading request. %v", err)
400                 return
401         }
402         req.Header.Set("Cache-Control", "no-cache")
403         client := &http.Client{Timeout: time.Second * 2}
404         resp, err := client.Do(req)
405         if err != nil {
406                 mc.TestError(t, "Error reading response. %v", err)
407                 return
408         }
409         defer resp.Body.Close()
410
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")
415                 return
416         }
417
418         respBody, err := ioutil.ReadAll(resp.Body)
419         if err != nil {
420                 mc.TestError(t, "Error reading body. %v", err)
421                 return
422         }
423         mc.TestLog(t, "%s", respBody)
424         return
425 }
426
427 func (mc *testingSubmgrControl) sendPostRequest(t *testing.T, addr string, path string) {
428
429         mc.TestLog(t, "POST http://"+addr+"%v", path)
430         req, err := http.NewRequest("POST", "http://"+addr+path, nil)
431         if err != nil {
432                 mc.TestError(t, "Error reading request. %v", err)
433                 return
434         }
435         client := &http.Client{Timeout: time.Second * 2}
436         resp, err := client.Do(req)
437         if err != nil {
438                 mc.TestError(t, "Error reading response. %v", err)
439                 return
440         }
441         defer resp.Body.Close()
442
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")
447                 return
448         }
449
450         respBody, err := ioutil.ReadAll(resp.Body)
451         if err != nil {
452                 mc.TestError(t, "Error reading body. %v", err)
453                 return
454         }
455         mc.TestLog(t, "%s", respBody)
456         return
457 }