pceicicd | 74b8333 | 2021-04-20 19:02:13 +0000 | [diff] [blame] | 1 | # ================================================================================== |
| 2 | # Copyright (c) 2020 China Mobile Technology (USA) Inc. Intellectual Property. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | # ================================================================================== |
| 16 | import json |
| 17 | import time |
| 18 | from contextlib import suppress |
| 19 | from lp import main, sdl |
pceicicd | 63df192 | 2021-07-03 23:01:13 +0000 | [diff] [blame^] | 20 | from lp.exceptions import UENotFound, CellNotFound |
| 21 | from ricxappframe.xapp_frame import Xapp, RMRXapp |
| 22 | import pytest |
| 23 | import pdb |
| 24 | |
| 25 | @pytest.fixture |
| 26 | def ue_metrics(): |
| 27 | return { |
| 28 | "UEID": "9876543", |
| 29 | "ServingCellID": "460-7-797-57315", |
| 30 | "MeasTimestampUEPDCPBytes": "2021-06-20 13:58:29.220", |
| 31 | "MeasPeriodUEPDCPBytes": 20, |
| 32 | "UEPDCPBytesDL": 250000, |
| 33 | "UEPDCPBytesUL": 100000, |
| 34 | "MeasTimestampUEPRBUsage": "2021-06-20 13:58:29.220", |
| 35 | "MeasPeriodUEPRBUsage": 20, |
| 36 | "UEPRBUsageDL": 10, |
| 37 | "UEPRBUsageUL": 30, |
| 38 | "MeasTimestampRF": "2021-06-20 13:58:29.210", |
| 39 | "MeasPeriodRF": 40, |
| 40 | "ServingCellRF": {"RSRP": -115, "RSRQ": -16, "RSSINR": -5}, |
| 41 | "NeighborCellRF": [ |
| 42 | {"CID": "460-7-797-57314", "CellRF": {"RSRP": -90, "RSRQ": -13, "RSSINR": -2.5}}, |
| 43 | {"CID": "460-7-797-57316", "CellRF": {"RSRP": -140, "RSRQ": -17, "RSSINR": -6}}, |
| 44 | ], |
| 45 | "FAKE_BAD_DATA_TEST": "THIS SHOULD GET DELETED", |
| 46 | } |
| 47 | |
| 48 | |
| 49 | @pytest.fixture |
| 50 | def ue_metrics_with_bad_cell(): |
| 51 | return { |
| 52 | "UEID": "3456789", |
| 53 | "ServingCellID": "460-7-797-57315", |
| 54 | "MeasTimestampUEPDCPBytes": "2021-06-20 13:58:29.220", |
| 55 | "MeasPeriodUEPDCPBytes": 20, |
| 56 | "UEPDCPBytesDL": 250000, |
| 57 | "UEPDCPBytesUL": 100000, |
| 58 | "MeasTimestampUEPRBUsage": "2021-06-20 13:58:29.220", |
| 59 | "MeasPeriodUEPRBUsage": 20, |
| 60 | "UEPRBUsageDL": 10, |
| 61 | "UEPRBUsageUL": 30, |
| 62 | "MeasTimestampRF": "2021-06-20 13:58:29.210", |
| 63 | "MeasPeriodRF": 40, |
| 64 | "ServingCellRF": {"RSRP": -115, "RSRQ": -16, "RSSINR": -5}, |
| 65 | "NeighborCellRF": [ |
| 66 | {"CID": "460-7-797-57314", "CellRF": {"RSRP": -90, "RSRQ": -13, "RSSINR": -2.5}}, |
| 67 | {"CID": "CANTTOUCHTHIS", "CellRF": {"RSRP": -140, "RSRQ": -17, "RSSINR": -6}}, |
| 68 | ], |
| 69 | } |
| 70 | |
| 71 | |
| 72 | @pytest.fixture |
| 73 | def cell_metrics_1(): |
| 74 | return { |
| 75 | "CellID": "460-7-797-57314", |
| 76 | "MeasTimestampPDCPBytes": "2021-06-20 13:58:29.220", |
| 77 | "MeasPeriodPDCPBytes": 20, |
| 78 | "PDCPBytesDL": 2000000, |
| 79 | "PDCPBytesUL": 1200000, |
| 80 | "MeasTimestampAvailPRB": "2021-06-20 13:58:29.220", |
| 81 | "MeasPeriodAvailPRB": 20, |
| 82 | "AvailPRBDL": 30, |
| 83 | "AvailPRBUL": 50, |
| 84 | } |
| 85 | |
| 86 | |
| 87 | @pytest.fixture |
| 88 | def cell_metrics_2(): |
| 89 | return { |
| 90 | "CellID": "460-7-797-57315", |
| 91 | "MeasTimestampPDCPBytes": "2021-06-20 13:58:29.220", |
| 92 | "MeasPeriodPDCPBytes": 20, |
| 93 | "PDCPBytesDL": 800000, |
| 94 | "PDCPBytesUL": 400000, |
| 95 | "MeasTimestampAvailPRB": "2021-06-20 13:58:29.220", |
| 96 | "MeasPeriodAvailPRB": 20, |
| 97 | "AvailPRBDL": 30, |
| 98 | "AvailPRBUL": 45, |
| 99 | "FAKE_BAD_DATA_TEST": "THIS SHOULD GET DELETED", |
| 100 | } |
| 101 | |
| 102 | |
| 103 | @pytest.fixture |
| 104 | def cell_metrics_3(): |
| 105 | return { |
| 106 | "CellID": "460-7-797-57316", |
| 107 | "MeasTimestampPDCPBytes": "2021-06-20 13:58:29.220", |
| 108 | "MeasPeriodPDCPBytes": 20, |
| 109 | "PDCPBytesDL": 1900000, |
| 110 | "PDCPBytesUL": 1000000, |
| 111 | "MeasTimestampAvailPRB": "2021-06-20 13:58:29.220", |
| 112 | "MeasPeriodAvailPRB": 20, |
| 113 | "AvailPRBDL": 60, |
| 114 | "AvailPRBUL": 80, |
| 115 | } |
| 116 | |
| 117 | |
| 118 | @pytest.fixture |
| 119 | def good_cell(): |
| 120 | return { |
| 121 | "PredictionUE": "9876543", |
| 122 | "UEMeasurements": { |
| 123 | "ServingCellID": "460-7-797-57315", |
| 124 | "MeasTimestampUEPDCPBytes": "2021-06-20 13:58:29.220", |
| 125 | "MeasPeriodUEPDCPBytes": 20, |
| 126 | "UEPDCPBytesDL": 250000, |
| 127 | "UEPDCPBytesUL": 100000, |
| 128 | "MeasTimestampUEPRBUsage": "2021-06-20 13:58:29.220", |
| 129 | "MeasPeriodUEPRBUsage": 20, |
| 130 | "UEPRBUsageDL": 10, |
| 131 | "UEPRBUsageUL": 30, |
| 132 | }, |
| 133 | "CellMeasurements": [ |
| 134 | { |
| 135 | "CellID": "460-7-797-57314", |
| 136 | "MeasTimestampPDCPBytes": "2021-06-20 13:58:29.220", |
| 137 | "MeasPeriodPDCPBytes": 20, |
| 138 | "PDCPBytesDL": 2000000, |
| 139 | "PDCPBytesUL": 1200000, |
| 140 | "MeasTimestampAvailPRB": "2021-06-20 13:58:29.220", |
| 141 | "MeasPeriodAvailPRB": 20, |
| 142 | "AvailPRBDL": 30, |
| 143 | "AvailPRBUL": 50, |
| 144 | "MeasTimestampRF": "2021-06-20 13:58:29.210", |
| 145 | "MeasPeriodRF": 40, |
| 146 | "RFMeasurements": {"RSRP": -90, "RSRQ": -13, "RSSINR": -2.5}, |
| 147 | }, |
| 148 | { |
| 149 | "CellID": "460-7-797-57316", |
| 150 | "MeasTimestampPDCPBytes": "2021-06-20 13:58:29.220", |
| 151 | "MeasPeriodPDCPBytes": 20, |
| 152 | "PDCPBytesDL": 1900000, |
| 153 | "PDCPBytesUL": 1000000, |
| 154 | "MeasTimestampAvailPRB": "2021-06-20 13:58:29.220", |
| 155 | "MeasPeriodAvailPRB": 20, |
| 156 | "AvailPRBDL": 60, |
| 157 | "AvailPRBUL": 80, |
| 158 | "MeasTimestampRF": "2021-06-20 13:58:29.210", |
| 159 | "MeasPeriodRF": 40, |
| 160 | "RFMeasurements": {"RSRP": -140, "RSRQ": -17, "RSSINR": -6}, |
| 161 | }, |
| 162 | { |
| 163 | "CellID": "460-7-797-57315", |
| 164 | "MeasTimestampPDCPBytes": "2021-06-20 13:58:29.220", |
| 165 | "MeasPeriodPDCPBytes": 20, |
| 166 | "PDCPBytesDL": 800000, |
| 167 | "PDCPBytesUL": 400000, |
| 168 | "MeasTimestampAvailPRB": "2021-06-20 13:58:29.220", |
| 169 | "MeasPeriodAvailPRB": 20, |
| 170 | "AvailPRBDL": 30, |
| 171 | "AvailPRBUL": 45, |
| 172 | "MeasTimestampRF": "2021-06-20 13:58:29.210", |
| 173 | "MeasPeriodRF": 40, |
| 174 | "RFMeasurements": {"RSRP": -115, "RSRQ": -16, "RSSINR": -5}, |
| 175 | }, |
| 176 | ], |
| 177 | } |
| 178 | |
| 179 | |
| 180 | @pytest.fixture |
| 181 | def bad_cell(): |
| 182 | return { |
| 183 | "PredictionUE": "3456789", |
| 184 | "UEMeasurements": { |
| 185 | "ServingCellID": "460-7-797-57315", |
| 186 | "MeasTimestampUEPDCPBytes": "2021-06-20 13:58:29.220", |
| 187 | "MeasPeriodUEPDCPBytes": 20, |
| 188 | "UEPDCPBytesDL": 250000, |
| 189 | "UEPDCPBytesUL": 100000, |
| 190 | "MeasTimestampUEPRBUsage": "2021-06-20 13:58:29.220", |
| 191 | "MeasPeriodUEPRBUsage": 20, |
| 192 | "UEPRBUsageDL": 10, |
| 193 | "UEPRBUsageUL": 30, |
| 194 | }, |
| 195 | "CellMeasurements": [ |
| 196 | { |
| 197 | "CellID": "460-7-797-57314", |
| 198 | "MeasTimestampPDCPBytes": "2021-06-20 13:58:29.220", |
| 199 | "MeasPeriodPDCPBytes": 20, |
| 200 | "PDCPBytesDL": 2000000, |
| 201 | "PDCPBytesUL": 1200000, |
| 202 | "MeasTimestampAvailPRB": "2021-06-20 13:58:29.220", |
| 203 | "MeasPeriodAvailPRB": 20, |
| 204 | "AvailPRBDL": 30, |
| 205 | "AvailPRBUL": 50, |
| 206 | "MeasTimestampRF": "2021-06-20 13:58:29.210", |
| 207 | "MeasPeriodRF": 40, |
| 208 | "RFMeasurements": {"RSRP": -90, "RSRQ": -13, "RSSINR": -2.5}, |
| 209 | }, |
| 210 | { |
| 211 | "CellID": "460-7-797-57315", |
| 212 | "MeasTimestampPDCPBytes": "2021-06-20 13:58:29.220", |
| 213 | "MeasPeriodPDCPBytes": 20, |
| 214 | "PDCPBytesDL": 800000, |
| 215 | "PDCPBytesUL": 400000, |
| 216 | "MeasTimestampAvailPRB": "2021-06-20 13:58:29.220", |
| 217 | "MeasPeriodAvailPRB": 20, |
| 218 | "AvailPRBDL": 30, |
| 219 | "AvailPRBUL": 45, |
| 220 | "MeasTimestampRF": "2021-06-20 13:58:29.210", |
| 221 | "MeasPeriodRF": 40, |
| 222 | "RFMeasurements": {"RSRP": -115, "RSRQ": -16, "RSSINR": -5}, |
| 223 | }, |
| 224 | ], |
| 225 | } |
pceicicd | 74b8333 | 2021-04-20 19:02:13 +0000 | [diff] [blame] | 226 | |
| 227 | mock_lp_xapp = None |
| 228 | # tox.ini sets env var to this value |
| 229 | config_file_path = "/tmp/config.json" |
| 230 | |
| 231 | def init_config_file(): |
| 232 | with open(config_file_path, "w") as file: |
| 233 | file.write('{ "version_int" : 1 }') |
| 234 | |
| 235 | |
| 236 | def write_config_file(): |
| 237 | # generate an inotify/config event |
| 238 | with open(config_file_path, "w") as file: |
| 239 | file.write('{ "version_int" : 2 }') |
| 240 | |
| 241 | |
pceicicd | 63df192 | 2021-07-03 23:01:13 +0000 | [diff] [blame^] | 242 | def test_init_xapp(monkeypatch, ue_metrics, cell_metrics_1, cell_metrics_2, cell_metrics_3, ue_metrics_with_bad_cell, good_cell): |
| 243 | |
| 244 | _original_post_init = main.post_init |
| 245 | |
| 246 | def fake_post_init(self): |
| 247 | _original_post_init(self) |
| 248 | self.sdl_set(sdl.UE_NS, "9876543", json.dumps(ue_metrics).encode(), usemsgpack=False) |
| 249 | self.sdl_set(sdl.UE_NS, "3456789", json.dumps(ue_metrics_with_bad_cell).encode(), usemsgpack=False) |
| 250 | self.sdl_set(sdl.CELL_NS, "460-7-797-57314", json.dumps(cell_metrics_1).encode(), usemsgpack=False) |
| 251 | self.sdl_set(sdl.CELL_NS, "460-7-797-57315", json.dumps(cell_metrics_2).encode(), usemsgpack=False) |
| 252 | self.sdl_set(sdl.CELL_NS, "460-7-797-57316", json.dumps(cell_metrics_3).encode(), usemsgpack=False) |
| 253 | |
| 254 | expected=sdl.get_uedata(self, "9876543") |
| 255 | assert expected==good_cell |
| 256 | expected=sdl.get_uedata(self, "3456789") |
| 257 | try: |
| 258 | sdl.get_uedata(self, "1234567") |
| 259 | except UENotFound: |
| 260 | self.logger.warning("UE doesn't exist!") |
| 261 | |
| 262 | |
| 263 | # patch |
| 264 | monkeypatch.setattr("lp.main.post_init", fake_post_init) |
| 265 | |
pceicicd | 74b8333 | 2021-04-20 19:02:13 +0000 | [diff] [blame] | 266 | # establish config |
| 267 | init_config_file() |
| 268 | |
pceicicd | 63df192 | 2021-07-03 23:01:13 +0000 | [diff] [blame^] | 269 | # start lp |
| 270 | main.start(thread=True) |
| 271 | |
pceicicd | 74b8333 | 2021-04-20 19:02:13 +0000 | [diff] [blame] | 272 | # wait a bit then update config |
| 273 | time.sleep(1) |
| 274 | write_config_file() |
| 275 | |
pceicicd | 63df192 | 2021-07-03 23:01:13 +0000 | [diff] [blame^] | 276 | def test_mock_xapp(): |
| 277 | # define a mock traffic steering xapp |
| 278 | def mock_ts_entry(self): |
| 279 | |
| 280 | # make sure a bad steering request doesn't blow up in lp |
| 281 | val = "".encode() # send empty string encoded message |
| 282 | self.rmr_send(val, 30000) |
| 283 | val = "just a string".encode() # not json |
| 284 | self.rmr_send(val, 30000) |
| 285 | val = json.dumps({"key": "value"}).encode() # json but missing UEPredictionSet |
| 286 | self.rmr_send(val, 30000) |
| 287 | |
| 288 | # valid request body but missing cell id |
| 289 | val = json.dumps({"UEPredictionSet": ["NOTVALIDUEs"]}).encode() |
| 290 | self.rmr_send(val, 30000) |
| 291 | |
| 292 | # good traffic steering request |
| 293 | val = json.dumps({"UEPredictionSet": ["9876543", "3456789"]}).encode() |
| 294 | self.rmr_send(val, 30000) |
| 295 | |
| 296 | # should trigger the default handler and do nothing |
| 297 | val = json.dumps({"send other message types": 1}).encode() |
| 298 | self.rmr_send(val, 60001) |
| 299 | |
| 300 | global mock_ts_xapp |
| 301 | mock_ts_xapp = Xapp(entrypoint=mock_ts_entry, rmr_port=4564, use_fake_sdl=True) |
| 302 | mock_ts_xapp.run() # this will return since entry isn't a loop |
| 303 | |
| 304 | def teardown_module(): |
| 305 | """ |
| 306 | this is like a "finally"; the name of this function is pytest magic |
| 307 | safer to put down here since certain failures above can lead to pytest never returning |
| 308 | for example if an exception gets raised before stop is called in any test function above, |
| 309 | pytest will hang forever |
| 310 | """ |
| 311 | with suppress(Exception): |
| 312 | mock_ts_xapp.stop() |
| 313 | with suppress(Exception): |
| 314 | mock_lp_xapp.stop() |
| 315 | with suppress(Exception): |
| 316 | main.stop() |