Towards a1 1.0.0: rmr improvements
[ric-plt/a1.git] / tests / test_controller.py
1 # ==================================================================================
2 #       Copyright (c) 2019 Nokia
3 #       Copyright (c) 2018-2019 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 tempfile
18 import os
19
20 from rmr.rmr_mocks import rmr_mocks
21 from a1 import app
22 import pytest
23
24
25 ADM_CTRL = "admission_control_policy"
26 ADM_CTRL_POLICIES = "/a1-p/policytypes/20000/policies"
27 ADM_CTRL_INSTANCE = ADM_CTRL_POLICIES + "/" + ADM_CTRL
28 ADM_CTRL_INSTANCE_STATUS = ADM_CTRL_INSTANCE + "/status"
29 ADM_CTRL_TYPE = "/a1-p/policytypes/20000"
30 TEST_TYPE = "/a1-p/policytypes/20001"
31
32
33 # http://flask.pocoo.org/docs/1.0/testing/
34 @pytest.fixture
35 def client():
36     db_fd, app.app.config["DATABASE"] = tempfile.mkstemp()
37     app.app.config["TESTING"] = True
38     cl = app.app.test_client()
39
40     yield cl
41
42     os.close(db_fd)
43     os.unlink(app.app.config["DATABASE"])
44
45
46 def _fake_dequeue(_filter_type):
47     """
48     for monkeypatching a1rmnr.dequeue_all_messages with a good status
49     """
50     fake_msg = {}
51     pay = b'{"policy_type_id": 20000, "policy_instance_id": "admission_control_policy", "handler_id": "test_receiver", "status": "OK"}'
52     fake_msg["payload"] = pay
53     new_messages = [fake_msg]
54     return new_messages
55
56
57 def _fake_dequeue_none(_filter_type):
58     """
59     for monkeypatching a1rmnr.dequeue_all_messages with no waiting messages
60     """
61     return []
62
63
64 def _fake_dequeue_deleted(_filter_type):
65     """
66     for monkeypatching a1rmnr.dequeue_all_messages with a DELETED status
67     """
68     fake_msg = {}
69     pay = b'{"policy_type_id": 20000, "policy_instance_id": "admission_control_policy", "handler_id": "test_receiver", "status": "DELETED"}'
70     fake_msg["payload"] = pay
71     new_messages = [fake_msg]
72     return new_messages
73
74
75 def _test_put_patch(monkeypatch):
76     rmr_mocks.patch_rmr(monkeypatch)
77     monkeypatch.setattr("rmr.rmr.rmr_send_msg", rmr_mocks.send_mock_generator(0))  # good sends for this whole batch
78
79     # we need this because free expects a real sbuf
80     # TODO: move this into rmr_mocks
81     def noop(_sbuf):
82         pass
83
84     monkeypatch.setattr("rmr.rmr.rmr_free_msg", noop)
85
86     # we need to repatch alloc (already patched in patch_rmr) to fix the transactionid, alloc is called in send and recieve
87     def fake_alloc(_unused, _alsounused):
88         sbuf = rmr_mocks.Rmr_mbuf_t()
89         sbuf.contents.xaction = b"d49b53e478b711e9a1130242ac110002"
90         return sbuf
91
92     # we also need to repatch set, since in the send function, we alloc, then set a new transid
93     def fake_set_transactionid(sbuf):
94         sbuf.contents.xaction = b"d49b53e478b711e9a1130242ac110002"
95
96     # Note, we could have just patched summary, but this patches at a "lower level" so is a better test
97     monkeypatch.setattr("rmr.rmr.rmr_alloc_msg", fake_alloc)
98     monkeypatch.setattr("rmr.rmr.generate_and_set_transaction_id", fake_set_transactionid)
99
100
101 # Actual Tests
102
103
104 def test_xapp_put_good(client, monkeypatch, adm_type_good, adm_instance_good):
105     """ test policy put good"""
106
107     # no type there yet
108     res = client.get(ADM_CTRL_TYPE)
109     assert res.status_code == 404
110
111     # no types at all
112     res = client.get("/a1-p/policytypes")
113     assert res.status_code == 200
114     assert res.json == []
115
116     # instance 404 because type not there yet
117     monkeypatch.setattr("a1.a1rmr.dequeue_all_waiting_messages", _fake_dequeue_none)
118     res = client.get(ADM_CTRL_POLICIES)
119     assert res.status_code == 404
120
121     # put the type
122     res = client.put(ADM_CTRL_TYPE, json=adm_type_good)
123     assert res.status_code == 201
124
125     # type there now
126     res = client.get(ADM_CTRL_TYPE)
127     assert res.status_code == 200
128     assert res.json == adm_type_good
129     res = client.get("/a1-p/policytypes")
130     assert res.status_code == 200
131     assert res.json == [20000]
132
133     # instance 200 but empty list
134     res = client.get(ADM_CTRL_POLICIES)
135     assert res.status_code == 200
136     assert res.json == []
137
138     # no instance there yet
139     res = client.get(ADM_CTRL_INSTANCE)
140     assert res.status_code == 404
141     res = client.get(ADM_CTRL_INSTANCE_STATUS)
142     assert res.status_code == 404
143
144     # create a good instance
145     _test_put_patch(monkeypatch)
146     res = client.put(ADM_CTRL_INSTANCE, json=adm_instance_good)
147     assert res.status_code == 202
148
149     # instance 200 and in list
150     res = client.get(ADM_CTRL_POLICIES)
151     assert res.status_code == 200
152     assert res.json == [ADM_CTRL]
153
154     def get_instance_good(expected):
155         # get the instance
156         res = client.get(ADM_CTRL_INSTANCE)
157         assert res.status_code == 200
158         assert res.json == adm_instance_good
159
160         # get the instance status
161         res = client.get(ADM_CTRL_INSTANCE_STATUS)
162         assert res.status_code == 200
163         assert res.get_data(as_text=True) == expected
164
165     # try a status get but pretend we didn't get any ACKs yet to test NOT IN EFFECT
166     monkeypatch.setattr("a1.a1rmr.dequeue_all_waiting_messages", _fake_dequeue_none)
167     get_instance_good("NOT IN EFFECT")
168
169     # now pretend we did get a good ACK
170     monkeypatch.setattr("a1.a1rmr.dequeue_all_waiting_messages", _fake_dequeue)
171     get_instance_good("IN EFFECT")
172
173     # delete it
174     res = client.delete(ADM_CTRL_INSTANCE)
175     assert res.status_code == 202
176     res = client.delete(ADM_CTRL_INSTANCE)  # should be able to do multiple deletes
177     assert res.status_code == 202
178
179     # status after a delete, but there are no messages yet, should still return
180     monkeypatch.setattr("a1.a1rmr.dequeue_all_waiting_messages", _fake_dequeue)
181     get_instance_good("IN EFFECT")
182
183     # now pretend we deleted successfully
184     monkeypatch.setattr("a1.a1rmr.dequeue_all_waiting_messages", _fake_dequeue_deleted)
185     res = client.get(ADM_CTRL_INSTANCE_STATUS)  # cant get status
186     assert res.status_code == 404
187     res = client.get(ADM_CTRL_INSTANCE)  # cant get instance
188     assert res.status_code == 404
189     # list still 200 but no instance
190     res = client.get(ADM_CTRL_POLICIES)
191     assert res.status_code == 200
192     assert res.json == []
193
194
195 def test_xapp_put_good_bad_rmr(client, monkeypatch, adm_instance_good):
196     """
197     assert that rmr bad states don't cause problems
198     """
199     _test_put_patch(monkeypatch)
200     monkeypatch.setattr("rmr.rmr.rmr_send_msg", rmr_mocks.send_mock_generator(10))
201     res = client.put(ADM_CTRL_INSTANCE, json=adm_instance_good)
202     assert res.status_code == 202
203
204     monkeypatch.setattr("rmr.rmr.rmr_send_msg", rmr_mocks.send_mock_generator(5))
205     res = client.put(ADM_CTRL_INSTANCE, json=adm_instance_good)
206     assert res.status_code == 202
207
208
209 def test_bad_instances(client, monkeypatch, adm_type_good):
210     """
211     Test bad send failures
212     """
213     rmr_mocks.patch_rmr(monkeypatch)
214
215     # TODO: reenable this after delete!
216     # put the type
217     # res = client.put(ADM_CTRL_TYPE, json=adm_type_good)
218     # assert res.status_code == 201
219
220     # illegal type range
221     res = client.put("/a1-p/policytypes/19999", json=adm_type_good)
222     assert res.status_code == 400
223     res = client.put("/a1-p/policytypes/21024", json=adm_type_good)
224     assert res.status_code == 400
225
226     # bad body
227     res = client.put(ADM_CTRL_INSTANCE, json={"not": "expected"})
228     assert res.status_code == 400
229
230     # bad media type
231     res = client.put(ADM_CTRL_INSTANCE, data="notajson")
232     assert res.status_code == 415
233
234
235 def test_healthcheck(client):
236     """
237     test healthcheck
238     """
239     res = client.get("/a1-p/healthcheck")
240     assert res.status_code == 200