Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [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 xapp |
| 21 | |
| 22 | import ( |
| 23 | "github.com/gorilla/mux" |
Juha Hyttinen | 3cfee96 | 2020-02-13 13:51:00 +0200 | [diff] [blame] | 24 | "github.com/spf13/viper" |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 25 | "net/http" |
| 26 | "net/http/httptest" |
| 27 | "os" |
| 28 | "strings" |
| 29 | "testing" |
| 30 | "time" |
| 31 | ) |
| 32 | |
Juha Hyttinen | 3cfee96 | 2020-02-13 13:51:00 +0200 | [diff] [blame] | 33 | //var _ = func() bool { |
| 34 | // testing.Init() |
| 35 | // return true |
| 36 | //}() |
Mohamed Abukar | 5120ec1 | 2020-02-04 11:01:24 +0200 | [diff] [blame] | 37 | |
Juha Hyttinen | 3cfee96 | 2020-02-13 13:51:00 +0200 | [diff] [blame] | 38 | type Consumer struct{} |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 39 | |
Mohamed Abukar | f11ab7a | 2019-08-14 16:55:01 +0300 | [diff] [blame] | 40 | func (m Consumer) Consume(params *RMRParams) (err error) { |
Mohamed Abukar | f11ab7a | 2019-08-14 16:55:01 +0300 | [diff] [blame] | 41 | Sdl.Store("myKey", params.Payload) |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 42 | return nil |
| 43 | } |
| 44 | |
| 45 | // Test cases |
| 46 | func TestMain(m *testing.M) { |
Mohamed Abukar | 791a77f | 2020-02-12 19:08:42 +0200 | [diff] [blame] | 47 | go RunWithParams(Consumer{}, viper.GetBool("db.waitForSdl")) |
Mohamed Abukar | 5120ec1 | 2020-02-04 11:01:24 +0200 | [diff] [blame] | 48 | time.Sleep(time.Duration(5) * time.Second) |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 49 | code := m.Run() |
| 50 | os.Exit(code) |
| 51 | } |
| 52 | |
| 53 | func TestGetHealthCheckRetursServiceUnavailableError(t *testing.T) { |
| 54 | req, _ := http.NewRequest("GET", "/ric/v1/health/ready", nil) |
Mohamed Abukar | 5120ec1 | 2020-02-04 11:01:24 +0200 | [diff] [blame] | 55 | /*response :=*/ executeRequest(req) |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 56 | |
Mohamed Abukar | 5120ec1 | 2020-02-04 11:01:24 +0200 | [diff] [blame] | 57 | //checkResponseCode(t, http.StatusServiceUnavailable, response.Code) |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | func TestGetHealthCheckReturnsSuccess(t *testing.T) { |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 61 | for Rmr.IsReady() == false { |
| 62 | time.Sleep(time.Duration(2) * time.Second) |
| 63 | } |
| 64 | |
| 65 | req, _ := http.NewRequest("GET", "/ric/v1/health/ready", nil) |
| 66 | response := executeRequest(req) |
| 67 | |
| 68 | checkResponseCode(t, http.StatusOK, response.Code) |
| 69 | } |
| 70 | |
| 71 | func TestInjectQuerySinglePath(t *testing.T) { |
| 72 | var handler = func(w http.ResponseWriter, r *http.Request) { |
| 73 | } |
| 74 | |
| 75 | Resource.InjectQueryRoute("/ric/v1/user", handler, "GET", "foo", "bar") |
| 76 | |
| 77 | req, _ := http.NewRequest("GET", "/ric/v1/user?foo=bar", nil) |
| 78 | response := executeRequest(req) |
| 79 | checkResponseCode(t, http.StatusOK, response.Code) |
| 80 | } |
| 81 | |
| 82 | func TestInjectQueryMultiplePaths(t *testing.T) { |
| 83 | var handler = func(w http.ResponseWriter, r *http.Request) { |
| 84 | } |
| 85 | |
| 86 | Resource.InjectQueryRoute("/ric/v1/user", handler, "GET", "foo", "bar", "id", "mykey") |
| 87 | |
| 88 | req, _ := http.NewRequest("GET", "/ric/v1/user?foo=bar&id=mykey", nil) |
| 89 | response := executeRequest(req) |
| 90 | checkResponseCode(t, http.StatusOK, response.Code) |
| 91 | } |
| 92 | |
| 93 | func TestInjectQueryFailures(t *testing.T) { |
| 94 | var handler = func(w http.ResponseWriter, r *http.Request) { |
| 95 | } |
| 96 | |
| 97 | Resource.InjectQueryRoute("/ric/v1/user", handler, "GET", "foo", "bar", "id", "mykey") |
| 98 | |
| 99 | req, _ := http.NewRequest("GET", "/ric/v1/user?invalid=bar&no=mykey", nil) |
| 100 | response := executeRequest(req) |
| 101 | checkResponseCode(t, http.StatusNotFound, response.Code) |
| 102 | } |
| 103 | |
| 104 | func TestMessagesReceivedSuccessfully(t *testing.T) { |
Mohamed Abukar | 9568a2d | 2020-02-18 16:50:32 +0200 | [diff] [blame] | 105 | time.Sleep(time.Duration(5) * time.Second) |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 106 | for i := 0; i < 100; i++ { |
Mohamed Abukar | f11ab7a | 2019-08-14 16:55:01 +0300 | [diff] [blame] | 107 | params := &RMRParams{} |
| 108 | params.Mtype = 10004 |
| 109 | params.SubId = -1 |
| 110 | params.Payload = []byte{1, 2, 3, 4, 5, 6} |
Mohamed Abukar | 19461e1 | 2019-08-23 08:46:11 +0300 | [diff] [blame] | 111 | params.Meid = &RMRMeid{PlmnID: "1234", EnbID: "7788", RanName: "RanName-1234"} |
Mohamed Abukar | f11ab7a | 2019-08-14 16:55:01 +0300 | [diff] [blame] | 112 | params.Xid = "TestXID" |
| 113 | Rmr.SendMsg(params) |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | // Allow time to process the messages |
| 117 | time.Sleep(time.Duration(2) * time.Second) |
| 118 | |
Mohamed Abukar | 791a77f | 2020-02-12 19:08:42 +0200 | [diff] [blame] | 119 | waitForSdl := viper.GetBool("db.waitForSdl") |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 120 | stats := getMetrics(t) |
| 121 | if !strings.Contains(stats, "ricxapp_RMR_Transmitted 100") { |
Mohamed Abukar | 9568a2d | 2020-02-18 16:50:32 +0200 | [diff] [blame] | 122 | t.Errorf("Error: ricxapp_RMR_Transmitted value incorrect: %v", stats) |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | if !strings.Contains(stats, "ricxapp_RMR_Received 100") { |
Mohamed Abukar | 9568a2d | 2020-02-18 16:50:32 +0200 | [diff] [blame] | 126 | t.Errorf("Error: ricxapp_RMR_Received value incorrect: %v", stats) |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | if !strings.Contains(stats, "ricxapp_RMR_TransmitError 0") { |
| 130 | t.Errorf("Error: ricxapp_RMR_TransmitError value incorrect") |
| 131 | } |
| 132 | |
| 133 | if !strings.Contains(stats, "ricxapp_RMR_ReceiveError 0") { |
| 134 | t.Errorf("Error: ricxapp_RMR_ReceiveError value incorrect") |
| 135 | } |
| 136 | |
Mohamed Abukar | 791a77f | 2020-02-12 19:08:42 +0200 | [diff] [blame] | 137 | if waitForSdl && !strings.Contains(stats, "ricxapp_SDL_Stored 100") { |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 138 | t.Errorf("Error: ricxapp_SDL_Stored value incorrect") |
| 139 | } |
| 140 | |
Mohamed Abukar | 791a77f | 2020-02-12 19:08:42 +0200 | [diff] [blame] | 141 | if waitForSdl && !strings.Contains(stats, "ricxapp_SDL_StoreError 0") { |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 142 | t.Errorf("Error: ricxapp_SDL_StoreError value incorrect") |
| 143 | } |
| 144 | } |
| 145 | |
wahidw | ad0a271 | 2020-03-04 09:54:15 +0000 | [diff] [blame] | 146 | func TestMessagesReceivedSuccessfullyUsingWh(t *testing.T) { |
| 147 | time.Sleep(time.Duration(5) * time.Second) |
| 148 | whid := Rmr.Openwh("localhost:4560") |
| 149 | time.Sleep(time.Duration(1) * time.Second) |
| 150 | for i := 0; i < 100; i++ { |
| 151 | params := &RMRParams{} |
| 152 | params.Mtype = 10004 |
| 153 | params.SubId = -1 |
| 154 | params.Payload = []byte{1, 2, 3, 4, 5, 6} |
| 155 | params.Meid = &RMRMeid{PlmnID: "1234", EnbID: "7788", RanName: "RanName-1234"} |
| 156 | params.Xid = "TestXID" |
| 157 | params.Whid = int(whid) |
| 158 | Rmr.SendMsg(params) |
| 159 | } |
| 160 | |
| 161 | // Allow time to process the messages |
| 162 | time.Sleep(time.Duration(2) * time.Second) |
| 163 | |
| 164 | waitForSdl := viper.GetBool("db.waitForSdl") |
| 165 | stats := getMetrics(t) |
| 166 | if !strings.Contains(stats, "ricxapp_RMR_Transmitted 200") { |
| 167 | t.Errorf("Error: ricxapp_RMR_Transmitted value incorrect: %v", stats) |
| 168 | } |
| 169 | |
| 170 | if !strings.Contains(stats, "ricxapp_RMR_Received 200") { |
| 171 | t.Errorf("Error: ricxapp_RMR_Received value incorrect: %v", stats) |
| 172 | } |
| 173 | |
| 174 | if !strings.Contains(stats, "ricxapp_RMR_TransmitError 0") { |
| 175 | t.Errorf("Error: ricxapp_RMR_TransmitError value incorrect") |
| 176 | } |
| 177 | |
| 178 | if !strings.Contains(stats, "ricxapp_RMR_ReceiveError 0") { |
| 179 | t.Errorf("Error: ricxapp_RMR_ReceiveError value incorrect") |
| 180 | } |
| 181 | |
| 182 | if waitForSdl && !strings.Contains(stats, "ricxapp_SDL_Stored 200") { |
| 183 | t.Errorf("Error: ricxapp_SDL_Stored value incorrect") |
| 184 | } |
| 185 | |
| 186 | if waitForSdl && !strings.Contains(stats, "ricxapp_SDL_StoreError 0") { |
| 187 | t.Errorf("Error: ricxapp_SDL_StoreError value incorrect") |
| 188 | } |
| 189 | Rmr.Closewh(int(whid)) |
| 190 | } |
| 191 | |
wahidw | e0948fe | 2020-03-19 14:49:41 +0000 | [diff] [blame^] | 192 | func TestMessagesReceivedSuccessfullyUsingWhCall(t *testing.T) { |
| 193 | time.Sleep(time.Duration(5) * time.Second) |
| 194 | whid := Rmr.Openwh("localhost:4560") |
| 195 | params := &RMRParams{} |
| 196 | params.Payload = []byte("newrt|start\nnewrt|end\n") |
| 197 | params.Whid = int(whid) |
| 198 | params.Callid = 4 |
| 199 | params.Timeout = 1000 |
| 200 | Rmr.SendCallMsg(params) |
| 201 | |
| 202 | // Allow time to process the messages |
| 203 | time.Sleep(time.Duration(2) * time.Second) |
| 204 | |
| 205 | waitForSdl := viper.GetBool("db.waitForSdl") |
| 206 | stats := getMetrics(t) |
| 207 | if !strings.Contains(stats, "ricxapp_RMR_Transmitted 200") { |
| 208 | t.Errorf("Error: ricxapp_RMR_Transmitted value incorrect: %v", stats) |
| 209 | } |
| 210 | |
| 211 | if !strings.Contains(stats, "ricxapp_RMR_Received 201") { |
| 212 | t.Errorf("Error: ricxapp_RMR_Received value incorrect: %v", stats) |
| 213 | } |
| 214 | |
| 215 | if !strings.Contains(stats, "ricxapp_RMR_TransmitError 1") { |
| 216 | t.Errorf("Error: ricxapp_RMR_TransmitError value incorrect") |
| 217 | } |
| 218 | |
| 219 | if !strings.Contains(stats, "ricxapp_RMR_ReceiveError 0") { |
| 220 | t.Errorf("Error: ricxapp_RMR_ReceiveError value incorrect") |
| 221 | } |
| 222 | |
| 223 | if waitForSdl && !strings.Contains(stats, "ricxapp_SDL_Stored 201") { |
| 224 | t.Errorf("Error: ricxapp_SDL_Stored value incorrect") |
| 225 | } |
| 226 | |
| 227 | if waitForSdl && !strings.Contains(stats, "ricxapp_SDL_StoreError 0") { |
| 228 | t.Errorf("Error: ricxapp_SDL_StoreError value incorrect") |
| 229 | } |
| 230 | Rmr.Closewh(int(whid)) |
| 231 | } |
| 232 | |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 233 | func TestSubscribeChannels(t *testing.T) { |
Mohamed Abukar | 791a77f | 2020-02-12 19:08:42 +0200 | [diff] [blame] | 234 | if !viper.GetBool("db.waitForSdl") { |
| 235 | return |
| 236 | } |
| 237 | |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 238 | var NotificationCb = func(ch string, events ...string) { |
| 239 | if ch != "channel1" { |
| 240 | t.Errorf("Error: Callback function called with incorrect params") |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | if err := Sdl.Subscribe(NotificationCb, "channel1"); err != nil { |
| 245 | t.Errorf("Error: Subscribe failed: %v", err) |
| 246 | } |
| 247 | time.Sleep(time.Duration(2) * time.Second) |
| 248 | |
| 249 | if err := Sdl.StoreAndPublish("channel1", "event", "key1", "data1"); err != nil { |
| 250 | t.Errorf("Error: Publish failed: %v", err) |
| 251 | } |
| 252 | } |
| 253 | |
Mohamed Abukar | 775722c | 2019-06-10 16:41:57 +0300 | [diff] [blame] | 254 | func TestGetRicMessageSuccess(t *testing.T) { |
| 255 | id, ok := Rmr.GetRicMessageId("RIC_SUB_REQ") |
| 256 | if !ok || id != 12010 { |
| 257 | t.Errorf("Error: GetRicMessageId failed: id=%d", id) |
| 258 | } |
| 259 | |
| 260 | name := Rmr.GetRicMessageName(12010) |
Mohamed Abukar | 3e611c6 | 2019-07-05 13:40:11 +0300 | [diff] [blame] | 261 | if name != "RIC_SUB_REQ" { |
Mohamed Abukar | 775722c | 2019-06-10 16:41:57 +0300 | [diff] [blame] | 262 | t.Errorf("Error: GetRicMessageName failed: name=%s", name) |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | func TestGetRicMessageFails(t *testing.T) { |
Mohamed Abukar | 791a77f | 2020-02-12 19:08:42 +0200 | [diff] [blame] | 267 | ok := Rmr.IsRetryError(&RMRParams{status: 0}) |
Mohamed Abukar | 775722c | 2019-06-10 16:41:57 +0300 | [diff] [blame] | 268 | if ok { |
Mohamed Abukar | 791a77f | 2020-02-12 19:08:42 +0200 | [diff] [blame] | 269 | t.Errorf("Error: IsRetryError returned wrong value") |
Mohamed Abukar | 775722c | 2019-06-10 16:41:57 +0300 | [diff] [blame] | 270 | } |
| 271 | |
Mohamed Abukar | 791a77f | 2020-02-12 19:08:42 +0200 | [diff] [blame] | 272 | ok = Rmr.IsRetryError(&RMRParams{status: 10}) |
| 273 | if !ok { |
| 274 | t.Errorf("Error: IsRetryError returned wrong value") |
| 275 | } |
| 276 | |
| 277 | ok = Rmr.IsNoEndPointError(&RMRParams{status: 5}) |
| 278 | if ok { |
| 279 | t.Errorf("Error: IsNoEndPointError returned wrong value") |
| 280 | } |
| 281 | |
| 282 | ok = Rmr.IsNoEndPointError(&RMRParams{status: 2}) |
| 283 | if !ok { |
| 284 | t.Errorf("Error: IsNoEndPointError returned wrong value") |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | func TestIsErrorFunctions(t *testing.T) { |
| 289 | id, ok := Rmr.GetRicMessageId("RIC_SUB_REQ") |
| 290 | if !ok || id != 12010 { |
| 291 | t.Errorf("Error: GetRicMessageId failed: id=%d", id) |
| 292 | } |
| 293 | |
| 294 | name := Rmr.GetRicMessageName(12010) |
| 295 | if name != "RIC_SUB_REQ" { |
| 296 | t.Errorf("Error: GetRicMessageName failed: name=%s", name) |
Mohamed Abukar | 775722c | 2019-06-10 16:41:57 +0300 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 300 | func TestTeardown(t *testing.T) { |
| 301 | Sdl.Clear() |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | // Helper functions |
| 305 | func executeRequest(req *http.Request) *httptest.ResponseRecorder { |
| 306 | rr := httptest.NewRecorder() |
| 307 | vars := map[string]string{"id": "1"} |
| 308 | req = mux.SetURLVars(req, vars) |
| 309 | Resource.router.ServeHTTP(rr, req) |
| 310 | |
| 311 | return rr |
| 312 | } |
| 313 | |
| 314 | func checkResponseCode(t *testing.T, expected, actual int) { |
| 315 | if expected != actual { |
| 316 | t.Errorf("Expected response code %d. Got %d\n", expected, actual) |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | func getMetrics(t *testing.T) string { |
| 321 | req, _ := http.NewRequest("GET", "/ric/v1/metrics", nil) |
| 322 | response := executeRequest(req) |
| 323 | |
| 324 | return response.Body.String() |
Mohamed Abukar | 3e611c6 | 2019-07-05 13:40:11 +0300 | [diff] [blame] | 325 | } |