Juha Hyttinen | ff8dccd | 2019-12-10 14:34:07 +0200 | [diff] [blame^] | 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 | "errors" |
| 24 | "fmt" |
| 25 | "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp" |
| 26 | "io/ioutil" |
| 27 | "os" |
| 28 | "testing" |
| 29 | "time" |
| 30 | ) |
| 31 | |
| 32 | //----------------------------------------------------------------------------- |
| 33 | // |
| 34 | //----------------------------------------------------------------------------- |
| 35 | type testingControl struct { |
| 36 | desc string |
| 37 | syncChan chan struct{} |
| 38 | } |
| 39 | |
| 40 | func (tc *testingControl) ReadyCB(data interface{}) { |
| 41 | xapp.Logger.Info("testingControl(%s) ReadyCB", tc.desc) |
| 42 | tc.syncChan <- struct{}{} |
| 43 | return |
| 44 | } |
| 45 | |
| 46 | //----------------------------------------------------------------------------- |
| 47 | // |
| 48 | //----------------------------------------------------------------------------- |
| 49 | type testingRmrControl struct { |
| 50 | testingControl |
| 51 | rmrClientTest *xapp.RMRClient |
| 52 | rmrConChan chan *xapp.RMRParams |
| 53 | } |
| 54 | |
| 55 | func (tc *testingRmrControl) Consume(msg *xapp.RMRParams) (err error) { |
| 56 | xapp.Logger.Info("testingRmrControl(%s) Consume", tc.desc) |
| 57 | tc.rmrConChan <- msg |
| 58 | return |
| 59 | } |
| 60 | |
| 61 | func (tc *testingRmrControl) RmrSend(params *xapp.RMRParams) (err error) { |
| 62 | // |
| 63 | //NOTE: Do this way until xapp-frame sending is improved |
| 64 | // |
| 65 | status := false |
| 66 | i := 1 |
| 67 | for ; i <= 10 && status == false; i++ { |
| 68 | status = tc.rmrClientTest.SendMsg(params) |
| 69 | if status == false { |
| 70 | xapp.Logger.Info("rmr.Send() failed. Retry count %v, Mtype: %v, SubId: %v, Xid %s", i, params.Mtype, params.SubId, params.Xid) |
| 71 | time.Sleep(500 * time.Millisecond) |
| 72 | } |
| 73 | } |
| 74 | if status == false { |
| 75 | err = errors.New("rmr.Send() failed") |
| 76 | tc.rmrClientTest.Free(params.Mbuf) |
| 77 | } |
| 78 | return |
| 79 | } |
| 80 | |
| 81 | func createNewRmrControl(desc string, rtfile string, port string, stat string) *testingRmrControl { |
| 82 | os.Setenv("RMR_SEED_RT", rtfile) |
| 83 | os.Setenv("RMR_SRC_ID", "localhost:"+port) |
| 84 | xapp.Logger.Info("Using rt file %s", os.Getenv("RMR_SEED_RT")) |
| 85 | xapp.Logger.Info("Using src id %s", os.Getenv("RMR_SRC_ID")) |
| 86 | newConn := &testingRmrControl{} |
| 87 | newConn.desc = desc |
| 88 | newConn.syncChan = make(chan struct{}) |
| 89 | newConn.rmrClientTest = xapp.NewRMRClientWithParams("tcp:"+port, 4096, 1, stat) |
| 90 | newConn.rmrConChan = make(chan *xapp.RMRParams) |
| 91 | newConn.rmrClientTest.SetReadyCB(newConn.ReadyCB, nil) |
| 92 | go newConn.rmrClientTest.Start(newConn) |
| 93 | <-newConn.syncChan |
| 94 | return newConn |
| 95 | } |
| 96 | |
| 97 | //----------------------------------------------------------------------------- |
| 98 | // |
| 99 | //----------------------------------------------------------------------------- |
| 100 | |
| 101 | func testError(t *testing.T, pattern string, args ...interface{}) { |
| 102 | xapp.Logger.Error(fmt.Sprintf(pattern, args...)) |
| 103 | t.Errorf(fmt.Sprintf(pattern, args...)) |
| 104 | } |
| 105 | |
| 106 | func testCreateTmpFile(str string) (string, error) { |
| 107 | file, err := ioutil.TempFile("/tmp", "*.rt") |
| 108 | if err != nil { |
| 109 | return "", err |
| 110 | } |
| 111 | _, err = file.WriteString(str) |
| 112 | if err != nil { |
| 113 | file.Close() |
| 114 | return "", err |
| 115 | } |
| 116 | return file.Name(), nil |
| 117 | } |
| 118 | |
| 119 | //----------------------------------------------------------------------------- |
| 120 | // |
| 121 | //----------------------------------------------------------------------------- |
| 122 | |
| 123 | var xappConn *testingRmrControl |
| 124 | var e2termConn *testingRmrControl |
| 125 | |
| 126 | func TestMain(m *testing.M) { |
| 127 | xapp.Logger.Info("TestMain start") |
| 128 | |
| 129 | // |
| 130 | //Cfg creation won't work like this as xapp-frame reads it during init. |
| 131 | // |
| 132 | /* |
| 133 | cfgstr:=`{ |
| 134 | "local": { |
| 135 | "host": ":8080" |
| 136 | }, |
| 137 | "logger": { |
| 138 | "level": 4 |
| 139 | }, |
| 140 | "rmr": { |
| 141 | "protPort": "tcp:14560", |
| 142 | "maxSize": 4096, |
| 143 | "numWorkers": 1, |
| 144 | "txMessages": ["RIC_SUB_REQ", "RIC_SUB_DEL_REQ"], |
| 145 | "rxMessages": ["RIC_SUB_RESP", "RIC_SUB_FAILURE", "RIC_SUB_DEL_RESP", "RIC_SUB_DEL_FAILURE", "RIC_INDICATION"] |
| 146 | }, |
| 147 | "db": { |
| 148 | "host": "localhost", |
| 149 | "port": 6379, |
| 150 | "namespaces": ["sdl", "rnib"] |
| 151 | } |
| 152 | }` |
| 153 | |
| 154 | cfgfilename,_ := testCreateTmpFile(cfgstr) |
| 155 | defer os.Remove(cfgfilename) |
| 156 | os.Setenv("CFG_FILE", cfgfilename) |
| 157 | */ |
| 158 | xapp.Logger.Info("Using cfg file %s", os.Getenv("CFG_FILE")) |
| 159 | |
| 160 | //--------------------------------- |
| 161 | // |
| 162 | //--------------------------------- |
| 163 | xapp.Logger.Info("### submgr main run ###") |
| 164 | |
| 165 | subsrt := `newrt|start |
| 166 | mse|12010|-1|localhost:14560 |
| 167 | mse|12010,localhost:14560|-1|localhost:15560 |
| 168 | mse|12011,localhost:15560|-1|localhost:14560 |
| 169 | mse|12011|-1|localhost:13560 |
| 170 | mse|12012,localhost:15560|-1|localhost:14560 |
| 171 | mse|12012|-1|localhost:13560 |
| 172 | mse|12020|-1|localhost:14560 |
| 173 | mse|12020,localhost:14560|-1|localhost:15560 |
| 174 | mse|12021,localhost:15560|-1|localhost:14560 |
| 175 | mse|12021|-1|localhost:13560 |
| 176 | mse|12022,localhost:15560|-1|localhost:14560 |
| 177 | mse|12022|-1|localhost:13560 |
| 178 | newrt|end |
| 179 | ` |
| 180 | |
| 181 | subrtfilename, _ := testCreateTmpFile(subsrt) |
| 182 | defer os.Remove(subrtfilename) |
| 183 | os.Setenv("RMR_SEED_RT", subrtfilename) |
| 184 | xapp.Logger.Info("Using rt file %s", os.Getenv("RMR_SEED_RT")) |
| 185 | |
| 186 | mainCtrl := &testingControl{} |
| 187 | mainCtrl.desc = "main" |
| 188 | mainCtrl.syncChan = make(chan struct{}) |
| 189 | |
| 190 | os.Setenv("RMR_SRC_ID", "localhost:14560") |
| 191 | c := NewControl() |
| 192 | c.skipRouteUpdate = true |
| 193 | xapp.SetReadyCB(mainCtrl.ReadyCB, nil) |
| 194 | go xapp.RunWithParams(c, false) |
| 195 | <-mainCtrl.syncChan |
| 196 | |
| 197 | //--------------------------------- |
| 198 | // |
| 199 | //--------------------------------- |
| 200 | xapp.Logger.Info("### xapp rmr run ###") |
| 201 | |
| 202 | xapprt := `newrt|start |
| 203 | mse|12010|-1|localhost:14560 |
| 204 | mse|12011|-1|localhost:13560 |
| 205 | mse|12012|-1|localhost:13560 |
| 206 | mse|12020|-1|localhost:14560 |
| 207 | mse|12021|-1|localhost:13560 |
| 208 | mse|12022|-1|localhost:13560 |
| 209 | newrt|end |
| 210 | ` |
| 211 | |
| 212 | xapprtfilename, _ := testCreateTmpFile(xapprt) |
| 213 | defer os.Remove(xapprtfilename) |
| 214 | xappConn = createNewRmrControl("xappConn", xapprtfilename, "13560", "RMRXAPPSTUB") |
| 215 | |
| 216 | //--------------------------------- |
| 217 | // |
| 218 | //--------------------------------- |
| 219 | xapp.Logger.Info("### e2term rmr run ###") |
| 220 | |
| 221 | e2termrt := `newrt|start |
| 222 | mse|12010|-1|localhost:15560 |
| 223 | mse|12011|-1|localhost:14560 |
| 224 | mse|12012|-1|localhost:14560 |
| 225 | mse|12020|-1|localhost:15560 |
| 226 | mse|12021|-1|localhost:14560 |
| 227 | mse|12022|-1|localhost:14560 |
| 228 | newrt|end |
| 229 | ` |
| 230 | |
| 231 | e2termrtfilename, _ := testCreateTmpFile(e2termrt) |
| 232 | defer os.Remove(e2termrtfilename) |
| 233 | e2termConn = createNewRmrControl("e2termConn", e2termrtfilename, "15560", "RMRE2TERMSTUB") |
| 234 | |
| 235 | code := m.Run() |
| 236 | os.Exit(code) |
| 237 | } |