88d6f70c6d9ecdd0b447655682e35fd7c003651e
[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 to repatch alloc (already patched in patch_rmr) to fix the transactionid, alloc is called in send and recieve
80     def fake_alloc(_unused, _alsounused):
81         sbuf = rmr_mocks.Rmr_mbuf_t()
82         sbuf.contents.xaction = b"d49b53e478b711e9a1130242ac110002"
83         return sbuf
84
85     # we also need to repatch set, since in the send function, we alloc, then set a new transid
86     def fake_set_transactionid(sbuf):
87         sbuf.contents.xaction = b"d49b53e478b711e9a1130242ac110002"
88
89     # Note, we could have just patched summary, but this patches at a "lower level" so is a better test
90     monkeypatch.setattr("rmr.rmr.rmr_alloc_msg", fake_alloc)
91     monkeypatch.setattr("rmr.rmr.generate_and_set_transaction_id", fake_set_transactionid)
92
93
94 # Actual Tests
95
96
97 def test_xapp_put_good(client, monkeypatch, adm_type_good, adm_instance_good):
98     """ test policy put good"""
99
100     # no type there yet
101     res = client.get(ADM_CTRL_TYPE)
102     assert res.status_code == 404
103
104     # no types at all
105     res = client.get("/a1-p/policytypes")
106     assert res.status_code == 200
107     assert res.json == []
108
109     # instance 404 because type not there yet
110     monkeypatch.setattr("a1.a1rmr.dequeue_all_waiting_messages", _fake_dequeue_none)
111     res = client.get(ADM_CTRL_POLICIES)
112     assert res.status_code == 404
113
114     # put the type
115     res = client.put(ADM_CTRL_TYPE, json=adm_type_good)
116     assert res.status_code == 201
117
118     # type there now
119     res = client.get(ADM_CTRL_TYPE)
120     assert res.status_code == 200
121     assert res.json == adm_type_good
122     res = client.get("/a1-p/policytypes")
123     assert res.status_code == 200
124     assert res.json == [20000]
125
126     # instance 200 but empty list
127     res = client.get(ADM_CTRL_POLICIES)
128     assert res.status_code == 200
129     assert res.json == []
130
131     # no instance there yet
132     res = client.get(ADM_CTRL_INSTANCE)
133     assert res.status_code == 404
134     res = client.get(ADM_CTRL_INSTANCE_STATUS)
135     assert res.status_code == 404
136
137     # create a good instance
138     _test_put_patch(monkeypatch)
139     res = client.put(ADM_CTRL_INSTANCE, json=adm_instance_good)
140     assert res.status_code == 202
141
142     # instance 200 and in list
143     res = client.get(ADM_CTRL_POLICIES)
144     assert res.status_code == 200
145     assert res.json == [ADM_CTRL]
146
147     def get_instance_good(expected):
148         # get the instance
149         res = client.get(ADM_CTRL_INSTANCE)
150         assert res.status_code == 200
151         assert res.json == adm_instance_good
152
153         # get the instance status
154         res = client.get(ADM_CTRL_INSTANCE_STATUS)
155         assert res.status_code == 200
156         assert res.get_data(as_text=True) == expected
157
158     # try a status get but pretend we didn't get any ACKs yet to test NOT IN EFFECT
159     monkeypatch.setattr("a1.a1rmr.dequeue_all_waiting_messages", _fake_dequeue_none)
160     get_instance_good("NOT IN EFFECT")
161
162     # now pretend we did get a good ACK
163     monkeypatch.setattr("a1.a1rmr.dequeue_all_waiting_messages", _fake_dequeue)
164     get_instance_good("IN EFFECT")
165
166     # delete it
167     res = client.delete(ADM_CTRL_INSTANCE)
168     assert res.status_code == 202
169     res = client.delete(ADM_CTRL_INSTANCE)  # should be able to do multiple deletes
170     assert res.status_code == 202
171
172     # status after a delete, but there are no messages yet, should still return
173     monkeypatch.setattr("a1.a1rmr.dequeue_all_waiting_messages", _fake_dequeue)
174     get_instance_good("IN EFFECT")
175
176     # now pretend we deleted successfully
177     monkeypatch.setattr("a1.a1rmr.dequeue_all_waiting_messages", _fake_dequeue_deleted)
178     res = client.get(ADM_CTRL_INSTANCE_STATUS)  # cant get status
179     assert res.status_code == 404
180     res = client.get(ADM_CTRL_INSTANCE)  # cant get instance
181     assert res.status_code == 404
182     # list still 200 but no instance
183     res = client.get(ADM_CTRL_POLICIES)
184     assert res.status_code == 200
185     assert res.json == []
186
187
188 def test_xapp_put_good_bad_rmr(client, monkeypatch, adm_instance_good):
189     """
190     assert that rmr bad states don't cause problems
191     """
192     _test_put_patch(monkeypatch)
193     monkeypatch.setattr("rmr.rmr.rmr_send_msg", rmr_mocks.send_mock_generator(10))
194     res = client.put(ADM_CTRL_INSTANCE, json=adm_instance_good)
195     assert res.status_code == 202
196
197     monkeypatch.setattr("rmr.rmr.rmr_send_msg", rmr_mocks.send_mock_generator(5))
198     res = client.put(ADM_CTRL_INSTANCE, json=adm_instance_good)
199     assert res.status_code == 202
200
201
202 def test_bad_instances(client, monkeypatch, adm_type_good):
203     """
204     Test bad send failures
205     """
206     rmr_mocks.patch_rmr(monkeypatch)
207
208     # TODO: reenable this after delete!
209     # put the type
210     # res = client.put(ADM_CTRL_TYPE, json=adm_type_good)
211     # assert res.status_code == 201
212
213     # illegal type range
214     res = client.put("/a1-p/policytypes/19999", json=adm_type_good)
215     assert res.status_code == 400
216     res = client.put("/a1-p/policytypes/21024", json=adm_type_good)
217     assert res.status_code == 400
218
219     # bad body
220     res = client.put(ADM_CTRL_INSTANCE, json={"not": "expected"})
221     assert res.status_code == 400
222
223     # bad media type
224     res = client.put(ADM_CTRL_INSTANCE, data="notajson")
225     assert res.status_code == 415
226
227
228 def test_healthcheck(client):
229     """
230     test healthcheck
231     """
232     res = client.get("/a1-p/healthcheck")
233     assert res.status_code == 200