Releasing current master branch
[ric-plt/submgr.git] / pkg / control / ut_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         "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/teststub"
24         "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/teststubdummy"
25         "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/teststube2ap"
26         "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
27         "os"
28         "testing"
29         "time"
30 )
31
32 //-----------------------------------------------------------------------------
33 //
34 //-----------------------------------------------------------------------------
35 func CaseBegin(desc string) *teststub.TestWrapper {
36         tent := teststub.NewTestWrapper(desc)
37         tent.Info(desc)
38         return tent
39 }
40
41 //-----------------------------------------------------------------------------
42 //
43 //-----------------------------------------------------------------------------
44 const teststubPortSeed int = 55555
45
46 //-----------------------------------------------------------------------------
47 //
48 //-----------------------------------------------------------------------------
49
50 var xappConn1 *teststube2ap.E2Stub
51 var xappConn2 *teststube2ap.E2Stub
52 var e2termConn1 *teststube2ap.E2Stub
53 var e2termConn2 *teststube2ap.E2Stub
54 var rtmgrHttp *testingHttpRtmgrStub
55 var mainCtrl *testingSubmgrControl
56
57 var dummystub *teststubdummy.RmrDummyStub
58
59 func ut_test_init() func() {
60         tent := CaseBegin("ut_test_init")
61
62         //---------------------------------
63         //
64         //---------------------------------
65         rtmgrHttp = createNewHttpRtmgrStub("RTMGRSTUB", "8989")
66         go rtmgrHttp.run()
67
68         //---------------------------------
69         //
70         //---------------------------------
71
72         //
73         //Cfg creation won't work like this as xapp-frame reads it during init.
74         //
75         /*
76             cfgstr:=`{
77               "local": {
78                   "host": ":8080"
79               },
80               "logger": {
81                   "level": 4
82               },
83               "rmr": {
84                  "protPort": "tcp:14560",
85                  "maxSize": 4096,
86                  "numWorkers": 1,
87                  "txMessages": ["RIC_SUB_REQ", "RIC_SUB_DEL_REQ"],
88                  "rxMessages": ["RIC_SUB_RESP", "RIC_SUB_FAILURE", "RIC_SUB_DEL_RESP", "RIC_SUB_DEL_FAILURE", "RIC_INDICATION"]
89               },
90               "db": {
91                   "host": "localhost",
92                   "port": 6379,
93                   "namespaces": ["sdl", "rnib"]
94               },
95                  "rtmgr" : {
96                    "HostAddr" : "localhost",
97                    "port" : "8989",
98                    "baseUrl" : "/"
99                  }
100            `
101
102            cfgfilename,_ := testCreateTmpFile(cfgstr)
103            defer os.Remove(cfgfilename)
104            os.Setenv("CFG_FILE", cfgfilename)
105         */
106         tent.Info("Using cfg file %s", os.Getenv("CFG_FILE"))
107
108         //---------------------------------
109         // Static routetable for rmr
110         //
111         // NOTE: Routing table is configured so, that responses
112         //       are duplicated to xapp1 and xapp2 instances.
113         //       If XID is not matching xapp stub will just
114         //       drop message. (Messages 12011, 12012, 12021, 12022)
115         //
116         // NOTE2: teststubPortSeed message type is for stub rmr connectivity probing
117         //
118         // NOTE3: Ports per entity:
119         //
120         // Entity
121         // -------------------
122
123         mainsrc := teststub.RmrSrcId{xapp.RmrEndpoint{"localhost", 14560}}
124         xapp1src := teststub.RmrSrcId{xapp.RmrEndpoint{"localhost", 13560}}
125         xapp2src := teststub.RmrSrcId{xapp.RmrEndpoint{"localhost", 13660}}
126         e2term1src := teststub.RmrSrcId{xapp.RmrEndpoint{"localhost", 15560}}
127         e2term2src := teststub.RmrSrcId{xapp.RmrEndpoint{"localhost", 15660}}
128         dummysrc := teststub.RmrSrcId{xapp.RmrEndpoint{"localhost", 16560}}
129
130         //---------------------------------
131         rt := &teststub.RmrRouteTable{}
132         rt.AddRoute(12010, "", -1, mainsrc.String())
133         rt.AddRoute(12010, mainsrc.String(), -1, "%meid")
134         rt.AddRoute(12011, e2term1src.String(), -1, mainsrc.String())
135         rt.AddRoute(12012, e2term1src.String(), -1, mainsrc.String())
136         rt.AddRoute(12011, e2term2src.String(), -1, mainsrc.String())
137         rt.AddRoute(12012, e2term2src.String(), -1, mainsrc.String())
138         rt.AddRoute(12011, mainsrc.String(), -1, xapp2src.String()+";"+xapp1src.String())
139         rt.AddRoute(12012, mainsrc.String(), -1, xapp2src.String()+";"+xapp1src.String())
140         rt.AddRoute(12020, "", -1, mainsrc.String())
141         rt.AddRoute(12020, mainsrc.String(), -1, "%meid")
142         rt.AddRoute(12021, e2term1src.String(), -1, mainsrc.String())
143         rt.AddRoute(12022, e2term1src.String(), -1, mainsrc.String())
144         rt.AddRoute(12021, e2term2src.String(), -1, mainsrc.String())
145         rt.AddRoute(12022, e2term2src.String(), -1, mainsrc.String())
146         rt.AddRoute(12021, mainsrc.String(), -1, xapp2src.String()+";"+xapp1src.String())
147         rt.AddRoute(12022, mainsrc.String(), -1, xapp2src.String()+";"+xapp1src.String())
148         rt.AddRoute(teststubPortSeed, "", -1, xapp2src.String()+";"+xapp1src.String()+";"+e2term1src.String()+";"+e2term2src.String()+";"+dummysrc.String())
149
150         rt.AddMeid(e2term1src.String(), []string{"RAN_NAME_1", "RAN_NAME_2"})
151         rt.AddMeid(e2term2src.String(), []string{"RAN_NAME_11", "RAN_NAME_12"})
152
153         rt.Enable()
154         tent.Info("rttable[%s]", rt.Table())
155
156         //---------------------------------
157         //
158         //---------------------------------
159         tent.Info("### submgr ctrl run ###")
160         mainCtrl = createSubmgrControl(mainsrc, teststub.RmrRtgSvc{})
161
162         //
163         // Tweak to get around with fact that alarm package alternates RMR_SEED_RT environment variable
164         //
165         //xapp-frame inits alarms when readycb is coming from xapps rmr
166         //alarm will make new rmr instance and overrides RMR_SEED_RT and RMR_RTG_SVC
167         //env variables. Re-create rt info.
168         for i := 0; i < int(10)*2; i++ {
169                 if os.Getenv("RMR_SEED_RT") == rt.FileName() {
170                         tent.Info("Waiting that alarm alternates RMR_SEED_RT=%s", os.Getenv("RMR_SEED_RT"))
171                         time.Sleep(500 * time.Millisecond)
172                 } else {
173                         tent.Info("Alarm has alternated RMR_SEED_RT=%s, so waiting 0.5 secs before restoring it", os.Getenv("RMR_SEED_RT"))
174                         time.Sleep(500 * time.Millisecond)
175                         rt.Enable()
176                         tent.Info("rttable[%s]", rt.Table())
177                         break
178                 }
179         }
180
181         if os.Getenv("RMR_SEED_RT") != rt.FileName() {
182                 tent.Error("Unittest timing issue with alarm RMR_SEED_RT=%s", os.Getenv("RMR_SEED_RT"))
183                 os.Exit(1)
184         }
185
186         //---------------------------------
187         //
188         //---------------------------------
189         tent.Info("### xapp1 stub run ###")
190         xappConn1 = teststube2ap.CreateNewE2Stub("xappstub1", xapp1src, teststub.RmrRtgSvc{}, "RMRXAPP1STUB", teststubPortSeed)
191
192         //---------------------------------
193         //
194         //---------------------------------
195         tent.Info("### xapp2 stub run ###")
196         xappConn2 = teststube2ap.CreateNewE2Stub("xappstub2", xapp2src, teststub.RmrRtgSvc{}, "RMRXAPP2STUB", teststubPortSeed)
197
198         //---------------------------------
199         //
200         //---------------------------------
201         tent.Info("### e2term1 stub run ###")
202         e2termConn1 = teststube2ap.CreateNewE2termStub("e2termstub1", e2term1src, teststub.RmrRtgSvc{}, "RMRE2TERMSTUB1", teststubPortSeed)
203
204         //---------------------------------
205         //
206         //---------------------------------
207         tent.Info("### e2term2 stub run ###")
208         e2termConn2 = teststube2ap.CreateNewE2termStub("e2termstub2", e2term2src, teststub.RmrRtgSvc{}, "RMRE2TERMSTUB2", teststubPortSeed)
209
210         //---------------------------------
211         // Just to test dummy stub
212         //---------------------------------
213         tent.Info("### dummy stub run ###")
214         dummystub = teststubdummy.CreateNewRmrDummyStub("dummystub", dummysrc, teststub.RmrRtgSvc{}, "DUMMYSTUB", teststubPortSeed)
215
216         //---------------------------------
217         // Testing message sending
218         //---------------------------------
219         if teststub.RmrStubControlWaitAlive(10, teststubPortSeed, mainCtrl.c.RMRClient, tent) == false {
220                 os.Exit(1)
221         }
222
223         if os.Getenv("RMR_SEED_RT") != rt.FileName() {
224                 tent.Error("Unittest timing issue with alarm RMR_SEED_RT=%s", os.Getenv("RMR_SEED_RT"))
225                 os.Exit(1)
226         }
227
228         return func() { rt.Disable() }
229 }
230
231 //-----------------------------------------------------------------------------
232 //
233 //-----------------------------------------------------------------------------
234 func TestMain(m *testing.M) {
235         CaseBegin("TestMain start")
236         cleanfn := ut_test_init()
237         code := m.Run()
238         cleanfn()
239         os.Exit(code)
240 }