9ec0e8411fb4474b8c71832a3529784a395280a7
[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         xapp.Logger.Debug("Replacing real db with test db")
59         mainCtrl.c.db = CreateMock() // This overrides real database for testing
60         xapp.SetReadyCB(mainCtrl.ReadyCB, nil)
61         go xapp.RunWithParams(mainCtrl.c, false)
62         mainCtrl.WaitCB()
63         mainCtrl.c.ReadyCB(nil)
64         return mainCtrl
65 }
66
67 func (mc *testingSubmgrControl) SimulateRestart(t *testing.T) {
68         mc.TestLog(t, "Simulating submgr restart")
69         mainCtrl.c.registry.subIds = nil
70         // Initialize subIds slice and subscription map
71         mainCtrl.c.registry.Initialize()
72         // Read subIds and subscriptions from database
73         subIds, register, err := mainCtrl.c.ReadAllSubscriptionsFromSdl()
74         if err != nil {
75                 mc.TestError(t, "%v", err)
76         } else {
77                 mainCtrl.c.registry.register = nil
78                 mainCtrl.c.registry.subIds = subIds
79                 mainCtrl.c.registry.register = register
80
81                 mc.TestLog(t, "register:")
82                 for subId, subs := range register {
83                         mc.TestLog(t, "  subId=%v", subId)
84                         mc.TestLog(t, "  subs.SubRespRcvd=%v", subs.SubRespRcvd)
85                         mc.TestLog(t, "  subs=%v\n", subs)
86                 }
87
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)
93                 }
94         }
95         go mainCtrl.c.HandleUncompletedSubscriptions(mainCtrl.c.registry.register)
96 }
97
98 func (mc *testingSubmgrControl) MakeTransactionNil(t *testing.T, subId uint32) {
99
100         mc.TestLog(t, "Makin transaction nil for SubId=%v", subId)
101         subs := mainCtrl.c.registry.GetSubscription(subId)
102         subs.TheTrans = nil
103 }
104
105 func (mc *testingSubmgrControl) SetResetTestFlag(t *testing.T, status bool) {
106         mc.TestLog(t, "ResetTestFlag set to %v", status)
107         mainCtrl.c.ResetTestFlag = status
108 }
109
110 func (mc *testingSubmgrControl) removeExistingSubscriptions(t *testing.T) {
111
112         mc.TestLog(t, "Removing existing subscriptions")
113         mainCtrl.c.RemoveAllSubscriptionsFromSdl()
114         mainCtrl.c.registry.subIds = nil
115         // Initialize subIds slice and subscription map
116         mainCtrl.c.registry.Initialize()
117 }
118
119 func PringSubscriptionQueryResult(resp models.SubscriptionList) {
120         for _, item := range resp {
121                 fmt.Printf("item.SubscriptionID=%v\n", item.SubscriptionID)
122                 fmt.Printf("item.Meid=%v\n", item.Meid)
123                 fmt.Printf("item.ClientEndpoint=%v\n", item.ClientEndpoint)
124         }
125 }
126
127 func (mc *testingSubmgrControl) wait_registry_empty(t *testing.T, secs int) bool {
128         cnt := int(0)
129         i := 1
130         for ; i <= secs*10; i++ {
131                 cnt = len(mc.c.registry.register)
132                 if cnt == 0 {
133                         return true
134                 }
135                 time.Sleep(100 * time.Millisecond)
136         }
137         mc.TestError(t, "(submgr) no registry empty within %d secs: %d", secs, cnt)
138         return false
139 }
140
141 func (mc *testingSubmgrControl) get_registry_next_subid(t *testing.T) uint32 {
142         mc.c.registry.mutex.Lock()
143         defer mc.c.registry.mutex.Unlock()
144         return mc.c.registry.subIds[0]
145 }
146
147 func (mc *testingSubmgrControl) wait_registry_next_subid_change(t *testing.T, origSubId uint32, secs int) (uint32, bool) {
148         i := 1
149         for ; i <= secs*10; i++ {
150                 mc.c.registry.mutex.Lock()
151                 currSubId := mc.c.registry.subIds[0]
152                 mc.c.registry.mutex.Unlock()
153                 if currSubId != origSubId {
154                         return currSubId, true
155                 }
156                 time.Sleep(100 * time.Millisecond)
157         }
158         mc.TestError(t, "(submgr) no subId change within %d secs", secs)
159         return 0, false
160 }
161
162 func (mc *testingSubmgrControl) wait_subs_clean(t *testing.T, e2SubsId uint32, secs int) bool {
163         var subs *Subscription
164         i := 1
165         for ; i <= secs*10; i++ {
166                 subs = mc.c.registry.GetSubscription(e2SubsId)
167                 if subs == nil {
168                         return true
169                 }
170                 time.Sleep(100 * time.Millisecond)
171         }
172         if subs != nil {
173                 mc.TestError(t, "(submgr) no clean within %d secs: %s", secs, subs.String())
174         } else {
175                 mc.TestError(t, "(submgr) no clean within %d secs: subs(N/A)", secs)
176         }
177         return false
178 }
179
180 func (mc *testingSubmgrControl) wait_multi_subs_clean(t *testing.T, e2SubsIds []uint32, secs int) bool {
181
182         purgedSubscriptions := 0
183
184         for i := 1; i <= secs*10; i++ {
185                 purgedSubscriptions = 0
186                 for k := 0; k <= len(e2SubsIds); i++ {
187                         subs := mc.c.registry.GetSubscription(e2SubsIds[k])
188                         if subs == nil {
189                                 mc.TestLog(t, "(submgr) subscriber purged for esSubsId %v", e2SubsIds[k])
190                                 purgedSubscriptions += 1
191                                 if purgedSubscriptions == len(e2SubsIds) {
192                                         return true
193                                 }
194                         }
195                 }
196                 mc.TestLog(t, "(submgr) subscriptions pending purging %v/%v after %d msecs", purgedSubscriptions, len(e2SubsIds), i+500)
197                 time.Sleep(100 * time.Millisecond)
198         }
199
200         mc.TestError(t, "(submgr) no clean within %d secs: subs(N/A) - %v/%v subscriptions found still", secs, purgedSubscriptions, len(e2SubsIds))
201
202         return false
203 }
204
205 func (mc *testingSubmgrControl) wait_subs_trans_clean(t *testing.T, e2SubsId uint32, secs int) bool {
206         var trans TransactionIf
207         i := 1
208         for ; i <= secs*10; i++ {
209                 subs := mc.c.registry.GetSubscription(e2SubsId)
210                 if subs == nil {
211                         return true
212                 }
213                 trans = subs.GetTransaction()
214                 if trans == nil {
215                         return true
216                 }
217                 time.Sleep(100 * time.Millisecond)
218         }
219         if trans != nil {
220                 mc.TestError(t, "(submgr) no clean within %d secs: %s", secs, trans.String())
221         } else {
222                 mc.TestError(t, "(submgr) no clean within %d secs: trans(N/A)", secs)
223         }
224         return false
225 }
226
227 func (mc *testingSubmgrControl) get_subs_entrypoint_cnt(t *testing.T, origSubId uint32) int {
228         subs := mc.c.registry.GetSubscription(origSubId)
229         if subs == nil {
230                 mc.TestError(t, "(submgr) no subs %d exists during entrypoint cnt get", origSubId)
231                 return -1
232         }
233         return subs.EpList.Size()
234 }
235
236 func (mc *testingSubmgrControl) wait_subs_entrypoint_cnt_change(t *testing.T, origSubId uint32, orig int, secs int) (int, bool) {
237
238         subs := mc.c.registry.GetSubscription(origSubId)
239         if subs == nil {
240                 mc.TestError(t, "(submgr) no subs %d exists during entrypoint cnt wait", origSubId)
241                 return -1, true
242         }
243
244         i := 1
245         for ; i <= secs*10; i++ {
246                 curr := subs.EpList.Size()
247                 if curr != orig {
248                         return curr, true
249                 }
250                 time.Sleep(100 * time.Millisecond)
251         }
252         mc.TestError(t, "(submgr) no subs %d entrypoint cnt change within %d secs", origSubId, secs)
253         return 0, false
254 }
255
256 //
257 // Counter check for received message. Note might not be yet handled
258 //
259 func (mc *testingSubmgrControl) get_msgcounter(t *testing.T) uint64 {
260         return mc.c.CntRecvMsg
261 }
262
263 func (mc *testingSubmgrControl) wait_msgcounter_change(t *testing.T, orig uint64, secs int) (uint64, bool) {
264         i := 1
265         for ; i <= secs*10; i++ {
266                 curr := mc.c.CntRecvMsg
267                 if curr != orig {
268                         return curr, true
269                 }
270                 time.Sleep(100 * time.Millisecond)
271         }
272         mc.TestError(t, "(submgr) no msg counter change within %d secs", secs)
273         return 0, false
274 }
275
276 func (mc *testingSubmgrControl) GetMetrics(t *testing.T) (string, error) {
277         req, err := http.NewRequest("GET", "http://localhost:8080/ric/v1/metrics", nil)
278         if err != nil {
279                 return "", fmt.Errorf("Error reading request. %v", err)
280         }
281         client := &http.Client{Timeout: time.Second * 10}
282         resp, err := client.Do(req)
283         if err != nil {
284                 return "", fmt.Errorf("Error reading response. %v", err)
285         }
286         defer resp.Body.Close()
287
288         respBody, err := ioutil.ReadAll(resp.Body)
289         if err != nil {
290                 return "", fmt.Errorf("Error reading body. %v", err)
291         }
292         return string(respBody[:]), nil
293 }
294
295 func (mc *testingSubmgrControl) CounterValuesToBeVeriefied(t *testing.T, countersToBeAdded CountersToBeAdded) {
296
297         if len(toBeAddedCountersMap) == 0 {
298                 toBeAddedCountersMap = make(map[string]Counter)
299         }
300         for _, counter := range countersToBeAdded {
301                 toBeAddedCountersMap[counter.Name] = counter
302         }
303         mc.GetCounterValuesBefore(t)
304 }
305
306 func (mc *testingSubmgrControl) GetCounterValuesBefore(t *testing.T) {
307         countersBeforeMap = make(map[string]Counter)
308         countersBeforeMap = mc.GetCurrentCounterValues(t, toBeAddedCountersMap)
309 }
310
311 func (mc *testingSubmgrControl) VerifyCounterValues(t *testing.T) {
312         currentCountersMap := mc.GetCurrentCounterValues(t, toBeAddedCountersMap)
313         for _, toBeAddedCounter := range toBeAddedCountersMap {
314                 if currentCounter, ok := currentCountersMap[toBeAddedCounter.Name]; ok == true {
315                         if beforeCounter, ok := countersBeforeMap[toBeAddedCounter.Name]; ok == true {
316                                 if currentCounter.Value != beforeCounter.Value+toBeAddedCounter.Value {
317                                         mc.TestError(t, "Error in expected counter value: counterName %v, current value %v, expected value %v",
318                                                 currentCounter.Name, currentCounter.Value, beforeCounter.Value+toBeAddedCounter.Value)
319
320                                         //fmt.Printf("beforeCounter.Value=%v, toBeAddedCounter.Value=%v, \n",beforeCounter.Value, toBeAddedCounter.Value)
321                                 }
322                         } else {
323                                 mc.TestError(t, "Counter %v not in countersBeforeMap", toBeAddedCounter.Name)
324                         }
325                 } else {
326                         mc.TestError(t, "Counter %v not in currentCountersMap", toBeAddedCounter.Name)
327                 }
328         }
329
330         // Make map empty
331         //fmt.Printf("toBeAddedCountersMap=%v\n",toBeAddedCountersMap)
332         toBeAddedCountersMap = make(map[string]Counter)
333 }
334
335 func (mc *testingSubmgrControl) GetCurrentCounterValues(t *testing.T, chekedCountersMap map[string]Counter) map[string]Counter {
336         countersString, err := mc.GetMetrics(t)
337         if err != nil {
338                 mc.TestError(t, "Error GetMetrics() failed %v", err)
339                 return nil
340         }
341
342         retCounterMap := make(map[string]Counter)
343         stringsTable := strings.Split(countersString, "\n")
344         for _, counter := range chekedCountersMap {
345                 for _, counterString := range stringsTable {
346                         if !strings.Contains(counterString, "#") && strings.Contains(counterString, counter.Name) {
347                                 counterString := strings.Split(counterString, " ")
348                                 if strings.Contains(counterString[0], counter.Name) {
349                                         val, err := strconv.ParseUint(counterString[1], 10, 64)
350                                         if err != nil {
351                                                 mc.TestError(t, "Error: strconv.ParseUint failed %v", err)
352                                         }
353                                         counter.Value = val
354                                         //fmt.Printf("counter=%v\n", counter)
355                                         retCounterMap[counter.Name] = counter
356                                 }
357                         }
358                 }
359         }
360
361         if len(retCounterMap) != len(chekedCountersMap) {
362                 mc.TestError(t, "Error: len(retCounterMap) != len(chekedCountersMap)")
363
364         }
365         return retCounterMap
366 }
367
368 func (mc *testingSubmgrControl) sendGetRequest(t *testing.T, addr string, path string) {
369
370         mc.TestLog(t, "GET http://"+addr+"%v", path)
371         req, err := http.NewRequest("GET", "http://"+addr+path, nil)
372         if err != nil {
373                 mc.TestError(t, "Error reading request. %v", err)
374                 return
375         }
376         req.Header.Set("Cache-Control", "no-cache")
377         client := &http.Client{Timeout: time.Second * 2}
378         resp, err := client.Do(req)
379         if err != nil {
380                 mc.TestError(t, "Error reading response. %v", err)
381                 return
382         }
383         defer resp.Body.Close()
384
385         mc.TestLog(t, "Response status: %v", resp.Status)
386         mc.TestLog(t, "Response Headers: %v", resp.Header)
387         if !strings.Contains(resp.Status, "200 OK") {
388                 mc.TestError(t, "Wrong response status")
389                 return
390         }
391
392         respBody, err := ioutil.ReadAll(resp.Body)
393         if err != nil {
394                 mc.TestError(t, "Error reading body. %v", err)
395                 return
396         }
397         mc.TestLog(t, "%s", respBody)
398         return
399 }
400
401 func (mc *testingSubmgrControl) sendPostRequest(t *testing.T, addr string, path string) {
402
403         mc.TestLog(t, "POST http://"+addr+"%v", path)
404         req, err := http.NewRequest("POST", "http://"+addr+path, nil)
405         if err != nil {
406                 mc.TestError(t, "Error reading request. %v", err)
407                 return
408         }
409         client := &http.Client{Timeout: time.Second * 2}
410         resp, err := client.Do(req)
411         if err != nil {
412                 mc.TestError(t, "Error reading response. %v", err)
413                 return
414         }
415         defer resp.Body.Close()
416
417         mc.TestLog(t, "Response status: %v", resp.Status)
418         mc.TestLog(t, "Response Headers: %v", resp.Header)
419         if !strings.Contains(resp.Status, "200 OK") {
420                 mc.TestError(t, "Wrong response status")
421                 return
422         }
423
424         respBody, err := ioutil.ReadAll(resp.Body)
425         if err != nil {
426                 mc.TestError(t, "Error reading body. %v", err)
427                 return
428         }
429         mc.TestLog(t, "%s", respBody)
430         return
431 }