Final A1 V1.0.0 Release (but 1.1.0 to come)
[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_workflow(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     # assert that rmr bad states don't cause problems
147     monkeypatch.setattr("rmr.rmr.rmr_send_msg", rmr_mocks.send_mock_generator(10))
148     res = client.put(ADM_CTRL_INSTANCE, json=adm_instance_good)
149     assert res.status_code == 202
150
151     # instance 200 and in list
152     res = client.get(ADM_CTRL_POLICIES)
153     assert res.status_code == 200
154     assert res.json == [ADM_CTRL]
155
156     def get_instance_good(expected):
157         # get the instance
158         res = client.get(ADM_CTRL_INSTANCE)
159         assert res.status_code == 200
160         assert res.json == adm_instance_good
161
162         # get the instance status
163         res = client.get(ADM_CTRL_INSTANCE_STATUS)
164         assert res.status_code == 200
165         assert res.get_data(as_text=True) == expected
166
167     # try a status get but pretend we didn't get any ACKs yet to test NOT IN EFFECT
168     monkeypatch.setattr("a1.a1rmr.dequeue_all_waiting_messages", _fake_dequeue_none)
169     get_instance_good("NOT IN EFFECT")
170
171     # now pretend we did get a good ACK
172     monkeypatch.setattr("a1.a1rmr.dequeue_all_waiting_messages", _fake_dequeue)
173     get_instance_good("IN EFFECT")
174
175     # cant delete type until there are no instances
176     res = client.delete(ADM_CTRL_TYPE)
177     assert res.status_code == 400
178
179     # delete it
180     res = client.delete(ADM_CTRL_INSTANCE)
181     assert res.status_code == 202
182     res = client.delete(ADM_CTRL_INSTANCE)  # should be able to do multiple deletes
183     assert res.status_code == 202
184
185     # status after a delete, but there are no messages yet, should still return
186     monkeypatch.setattr("a1.a1rmr.dequeue_all_waiting_messages", _fake_dequeue)
187     get_instance_good("IN EFFECT")
188
189     # now pretend we deleted successfully
190     monkeypatch.setattr("a1.a1rmr.dequeue_all_waiting_messages", _fake_dequeue_deleted)
191     res = client.get(ADM_CTRL_INSTANCE_STATUS)  # cant get status
192     assert res.status_code == 404
193     res = client.get(ADM_CTRL_INSTANCE)  # cant get instance
194     assert res.status_code == 404
195
196     # list still 200 but no instance
197     res = client.get(ADM_CTRL_POLICIES)
198     assert res.status_code == 200
199     assert res.json == []
200
201     # delete the type
202     res = client.delete(ADM_CTRL_TYPE)
203     assert res.status_code == 204
204
205     # cant touch this
206     res = client.get(ADM_CTRL_TYPE)
207     assert res.status_code == 404
208     res = client.delete(ADM_CTRL_TYPE)
209     assert res.status_code == 404
210
211
212 def test_bad_instances(client, monkeypatch, adm_type_good):
213     """
214     test various failure modes
215     """
216     # put the type (needed for some of the tests below)
217     rmr_mocks.patch_rmr(monkeypatch)
218     res = client.put(ADM_CTRL_TYPE, json=adm_type_good)
219     assert res.status_code == 201
220
221     # bad body
222     res = client.put(ADM_CTRL_INSTANCE, json={"not": "expected"})
223     assert res.status_code == 400
224
225     # bad media type
226     res = client.put(ADM_CTRL_INSTANCE, data="notajson")
227     assert res.status_code == 415
228
229     # delete a non existent instance
230     res = client.delete(ADM_CTRL_INSTANCE + "DARKNESS")
231     assert res.status_code == 404
232
233     # get a non existent instance
234     monkeypatch.setattr("a1.a1rmr.dequeue_all_waiting_messages", _fake_dequeue)
235     res = client.get(ADM_CTRL_INSTANCE + "DARKNESS")
236     assert res.status_code == 404
237
238     # delete the type (as cleanup)
239     res = client.delete(ADM_CTRL_TYPE)
240     assert res.status_code == 204
241
242
243 def test_illegal_types(client, monkeypatch, adm_type_good):
244     """
245     Test illegal types
246     """
247     res = client.put("/a1-p/policytypes/19999", json=adm_type_good)
248     assert res.status_code == 400
249     res = client.put("/a1-p/policytypes/21024", json=adm_type_good)
250     assert res.status_code == 400
251
252
253 def test_healthcheck(client):
254     """
255     test healthcheck
256     """
257     res = client.get("/a1-p/healthcheck")
258     assert res.status_code == 200