Change qp's RMR listen port to 4560
[ric-app/qp.git] / tests / test_qp.py
1 # ==================================================================================
2 #       Copyright (c) 2020 AT&T 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 qp import main
20 from ricxappframe.xapp_frame import Xapp, RMRXapp
21
22 mock_qpd_xapp = None
23 mock_ts_xapp = None
24
25 """
26  these tests are not currently parallelizable (do not use this tox flag)
27  I would use setup_module, however that can't take monkeypatch fixtures
28  Currently looking for the best way to make this better:
29  https://stackoverflow.com/questions/60886013/python-monkeypatch-in-pytest-setup-module
30 """
31
32
33 def test_init_xapp(monkeypatch):
34
35     # monkeypatch post_init to set the data we want
36     def fake_post_init(self):
37         self.predict_requests = 0
38
39     # patch
40     monkeypatch.setattr("qp.main.post_init", fake_post_init)
41
42     # start qp
43     main.start(thread=True)
44
45
46 def test_rmr_flow(monkeypatch, qpd_to_qp, qp_prediction):
47     """
48     this flow mocks out the xapps on both sides of QP.
49     It first stands up a mock ts, then it starts up a mock qp-driver
50     which will immediately send requests to the running qp.
51     """
52
53     expected_result = {}
54
55     # define a mock traffic steering xapp that listens on 4563
56     def mock_ts_default_handler(self, summary, sbuf):
57         pass
58
59     def mock_ts_prediction_handler(self, summary, sbuf):
60         nonlocal expected_result  # closures ftw
61         pay = json.loads(summary["payload"])
62         expected_result = pay
63
64     global mock_ts_xapp
65     mock_ts_xapp = RMRXapp(mock_ts_default_handler, rmr_port=4563, use_fake_sdl=True)
66     mock_ts_xapp.register_callback(mock_ts_prediction_handler, 30002)
67     mock_ts_xapp.run(thread=True)
68
69     time.sleep(1)
70
71     # define a mock qp driver xapp that sends a message to QP under test
72     def mock_qpd_entry(self):
73
74         # good traffic steering request
75         val = json.dumps(qpd_to_qp).encode()
76         self.rmr_send(val, 30001)
77
78         # should trigger the default handler and do nothing
79         val = json.dumps({"test send 60001": 2}).encode()
80         self.rmr_send(val, 60001)
81
82     global mock_qpd_xapp
83     mock_qpd_xapp = Xapp(entrypoint=mock_qpd_entry, rmr_port=4666, use_fake_sdl=True)
84     mock_qpd_xapp.run()  # this will return since entry isn't a loop
85
86     time.sleep(1)
87
88     assert main.get_stats() == {"PredictRequests": 1}
89     assert expected_result == qp_prediction
90
91
92 def teardown_module():
93     """
94     this is like a "finally"; the name of this function is pytest magic
95     safer to put down here since certain failures above can lead to pytest never returning
96     for example if an exception gets raised before stop is called in any test function above,
97     pytest will hang forever
98     """
99     with suppress(Exception):
100         mock_ts_xapp.stop()
101     with suppress(Exception):
102         mock_qpd_xapp.stop()
103     with suppress(Exception):
104         main.stop()