Tommy Carpenter | f9cd5cc | 2020-03-09 13:46:37 -0400 | [diff] [blame] | 1 | # ================================================================================== |
| 2 | # Copyright (c) 2020 Nokia |
| 3 | # Copyright (c) 2020 AT&T Intellectual Property. |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | # ================================================================================== |
| 17 | import json |
| 18 | import time |
| 19 | from contextlib import suppress |
yc999.jang | 750eb5b | 2021-06-10 19:04:18 +0900 | [diff] [blame^] | 20 | from ricxappframe.xapp_frame import _BaseXapp, Xapp, RMRXapp, RIC_HEALTH_CHECK_REQ, RIC_HEALTH_CHECK_RESP |
| 21 | from ricxappframe.constants import sdl_namespaces |
Tommy Carpenter | f9cd5cc | 2020-03-09 13:46:37 -0400 | [diff] [blame] | 22 | |
| 23 | rmr_xapp = None |
Tommy Carpenter | 09894e3 | 2020-04-02 19:45:19 -0400 | [diff] [blame] | 24 | rmr_xapp_health = None |
Tommy Carpenter | f9cd5cc | 2020-03-09 13:46:37 -0400 | [diff] [blame] | 25 | gen_xapp = None |
yc999.jang | 750eb5b | 2021-06-10 19:04:18 +0900 | [diff] [blame^] | 26 | rnib_xapp = None |
Tommy Carpenter | f9cd5cc | 2020-03-09 13:46:37 -0400 | [diff] [blame] | 27 | |
| 28 | |
Tommy Carpenter | 09894e3 | 2020-04-02 19:45:19 -0400 | [diff] [blame] | 29 | def test_rmr_init(): |
Tommy Carpenter | f9cd5cc | 2020-03-09 13:46:37 -0400 | [diff] [blame] | 30 | |
| 31 | # test variables |
Tommy Carpenter | 09894e3 | 2020-04-02 19:45:19 -0400 | [diff] [blame] | 32 | def_pay = None |
| 33 | sixty_pay = None |
Tommy Carpenter | f9cd5cc | 2020-03-09 13:46:37 -0400 | [diff] [blame] | 34 | |
| 35 | # create rmr app |
Tommy Carpenter | f9cd5cc | 2020-03-09 13:46:37 -0400 | [diff] [blame] | 36 | |
| 37 | def default_handler(self, summary, sbuf): |
Tommy Carpenter | 09894e3 | 2020-04-02 19:45:19 -0400 | [diff] [blame] | 38 | nonlocal def_pay |
| 39 | def_pay = json.loads(summary["payload"]) |
Tommy Carpenter | f9cd5cc | 2020-03-09 13:46:37 -0400 | [diff] [blame] | 40 | self.rmr_free(sbuf) |
| 41 | |
Tommy Carpenter | f9cd5cc | 2020-03-09 13:46:37 -0400 | [diff] [blame] | 42 | def sixtythou_handler(self, summary, sbuf): |
Tommy Carpenter | 09894e3 | 2020-04-02 19:45:19 -0400 | [diff] [blame] | 43 | nonlocal sixty_pay |
| 44 | sixty_pay = json.loads(summary["payload"]) |
Tommy Carpenter | f9cd5cc | 2020-03-09 13:46:37 -0400 | [diff] [blame] | 45 | self.rmr_free(sbuf) |
| 46 | |
Tommy Carpenter | 09894e3 | 2020-04-02 19:45:19 -0400 | [diff] [blame] | 47 | global rmr_xapp |
| 48 | rmr_xapp = RMRXapp(default_handler, rmr_port=4564, use_fake_sdl=True) |
Tommy Carpenter | f9cd5cc | 2020-03-09 13:46:37 -0400 | [diff] [blame] | 49 | rmr_xapp.register_callback(sixtythou_handler, 60000) |
Tommy Carpenter | 1c9ce6b | 2020-03-13 09:36:36 -0400 | [diff] [blame] | 50 | rmr_xapp.run(thread=True) # in unit tests we need to thread here or else execution is not returned! |
Tommy Carpenter | f9cd5cc | 2020-03-09 13:46:37 -0400 | [diff] [blame] | 51 | |
| 52 | time.sleep(1) |
| 53 | |
Tommy Carpenter | 09894e3 | 2020-04-02 19:45:19 -0400 | [diff] [blame] | 54 | # create a general xapp that will demonstrate some SDL functionality and launch some requests against the rmr xapp |
| 55 | |
Tommy Carpenter | f9cd5cc | 2020-03-09 13:46:37 -0400 | [diff] [blame] | 56 | def entry(self): |
| 57 | |
Tommy Carpenter | 09894e3 | 2020-04-02 19:45:19 -0400 | [diff] [blame] | 58 | time.sleep(1) |
| 59 | |
Tommy Carpenter | f9cd5cc | 2020-03-09 13:46:37 -0400 | [diff] [blame] | 60 | self.sdl_set("testns", "mykey", 6) |
| 61 | assert self.sdl_get("testns", "mykey") == 6 |
| 62 | assert self.sdl_find_and_get("testns", "myk") == {"mykey": 6} |
| 63 | assert self.healthcheck() |
| 64 | |
| 65 | val = json.dumps({"test send 60000": 1}).encode() |
| 66 | self.rmr_send(val, 60000) |
| 67 | |
| 68 | val = json.dumps({"test send 60001": 2}).encode() |
| 69 | self.rmr_send(val, 60001) |
| 70 | |
Lott, Christopher (cl778h) | e87ea19 | 2020-06-16 16:12:26 -0400 | [diff] [blame] | 71 | self.sdl_delete("testns", "bogus") |
| 72 | |
Tommy Carpenter | f9cd5cc | 2020-03-09 13:46:37 -0400 | [diff] [blame] | 73 | global gen_xapp |
Tommy Carpenter | 09894e3 | 2020-04-02 19:45:19 -0400 | [diff] [blame] | 74 | gen_xapp = Xapp(entrypoint=entry, use_fake_sdl=True) |
Tommy Carpenter | f9cd5cc | 2020-03-09 13:46:37 -0400 | [diff] [blame] | 75 | gen_xapp.run() |
| 76 | |
| 77 | time.sleep(1) |
| 78 | |
Tommy Carpenter | 09894e3 | 2020-04-02 19:45:19 -0400 | [diff] [blame] | 79 | assert def_pay == {"test send 60001": 2} |
| 80 | assert sixty_pay == {"test send 60000": 1} |
| 81 | |
| 82 | |
| 83 | def test_rmr_healthcheck(): |
| 84 | # thanos uses the rmr xapp to healthcheck the rmr xapp |
| 85 | |
| 86 | # test variables |
| 87 | health_pay = None |
| 88 | |
| 89 | def post_init(self): |
| 90 | self.rmr_send(b"", RIC_HEALTH_CHECK_REQ) |
| 91 | |
| 92 | def default_handler(self, summary, sbuf): |
| 93 | pass |
| 94 | |
| 95 | global rmr_xapp_health |
| 96 | rmr_xapp_health = RMRXapp(default_handler, post_init=post_init, rmr_port=4666, use_fake_sdl=True) |
| 97 | |
| 98 | def health_handler(self, summary, sbuf): |
| 99 | nonlocal health_pay |
| 100 | health_pay = summary["payload"] |
| 101 | self.rmr_free(sbuf) |
| 102 | |
| 103 | rmr_xapp_health.register_callback(health_handler, RIC_HEALTH_CHECK_RESP) |
| 104 | rmr_xapp_health.run(thread=True) # in unit tests we need to thread here or else execution is not returned! |
| 105 | |
| 106 | time.sleep(1) |
| 107 | |
| 108 | assert health_pay == b"OK\n" |
Tommy Carpenter | f9cd5cc | 2020-03-09 13:46:37 -0400 | [diff] [blame] | 109 | |
| 110 | |
yc999.jang | 750eb5b | 2021-06-10 19:04:18 +0900 | [diff] [blame^] | 111 | def test_get_list_nodeb(rnib_information): |
| 112 | global rnib_xapp |
| 113 | rnib_xapp = _BaseXapp(rmr_port=4777, rmr_wait_for_ready=False, use_fake_sdl=True) |
| 114 | |
| 115 | # Test there is no rnib information. |
| 116 | gnb_list = rnib_xapp.get_list_gnb_ids() |
| 117 | enb_list = rnib_xapp.get_list_enb_ids() |
| 118 | assert len(gnb_list) == 0 |
| 119 | assert len(enb_list) == 0 |
| 120 | |
| 121 | # Add rnib information directly. |
| 122 | for rnib in rnib_information: |
| 123 | rnib_xapp.sdl.add_member(sdl_namespaces.E2_MANAGER, "ENB", rnib, usemsgpack=False) |
| 124 | rnib_xapp.sdl.add_member(sdl_namespaces.E2_MANAGER, "GNB", rnib, usemsgpack=False) |
| 125 | |
| 126 | gnb_list = rnib_xapp.get_list_gnb_ids() |
| 127 | assert len(gnb_list) == len(rnib_information) |
| 128 | for gnb in gnb_list: |
| 129 | assert gnb.SerializeToString() in rnib_information |
| 130 | |
| 131 | enb_list = rnib_xapp.get_list_enb_ids() |
| 132 | assert len(enb_list) == len(rnib_information) |
| 133 | for enb in enb_list: |
| 134 | assert enb.SerializeToString() in rnib_information |
| 135 | |
| 136 | |
Tommy Carpenter | f9cd5cc | 2020-03-09 13:46:37 -0400 | [diff] [blame] | 137 | def teardown_module(): |
| 138 | """ |
| 139 | this is like a "finally"; the name of this function is pytest magic |
| 140 | safer to put down here since certain failures above can lead to pytest never returning |
| 141 | for example if an exception gets raised before stop is called in any test function above, pytest will hang forever |
| 142 | """ |
| 143 | with suppress(Exception): |
| 144 | gen_xapp.stop() |
| 145 | with suppress(Exception): |
| 146 | rmr_xapp.stop() |
Tommy Carpenter | 09894e3 | 2020-04-02 19:45:19 -0400 | [diff] [blame] | 147 | with suppress(Exception): |
| 148 | rmr_xapp_health.stop() |
yc999.jang | 750eb5b | 2021-06-10 19:04:18 +0900 | [diff] [blame^] | 149 | with suppress(Exception): |
| 150 | rnib_xapp.stop() |