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