1 # ==================================================================================
2 # Copyright (c) 2019 Nokia
3 # Copyright (c) 2018-2019 AT&T Intellectual Property.
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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 # ==================================================================================
20 from rmr.rmr_mocks import rmr_mocks
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"
33 # http://flask.pocoo.org/docs/1.0/testing/
36 db_fd, app.app.config["DATABASE"] = tempfile.mkstemp()
37 app.app.config["TESTING"] = True
38 cl = app.app.test_client()
43 os.unlink(app.app.config["DATABASE"])
46 def _fake_dequeue(_filter_type):
48 for monkeypatching a1rmnr.dequeue_all_messages with a good status
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]
57 def _fake_dequeue_none(_filter_type):
59 for monkeypatching a1rmnr.dequeue_all_messages with no waiting messages
64 def _fake_dequeue_deleted(_filter_type):
66 for monkeypatching a1rmnr.dequeue_all_messages with a DELETED status
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]
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
79 # we need this because free expects a real sbuf
80 # TODO: move this into rmr_mocks
84 monkeypatch.setattr("rmr.rmr.rmr_free_msg", noop)
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"
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"
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)
104 def test_workflow(client, monkeypatch, adm_type_good, adm_instance_good):
105 """ test policy put good"""
108 res = client.get(ADM_CTRL_TYPE)
109 assert res.status_code == 404
112 res = client.get("/a1-p/policytypes")
113 assert res.status_code == 200
114 assert res.json == []
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
122 res = client.put(ADM_CTRL_TYPE, json=adm_type_good)
123 assert res.status_code == 201
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]
133 # instance 200 but empty list
134 res = client.get(ADM_CTRL_POLICIES)
135 assert res.status_code == 200
136 assert res.json == []
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
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
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]
156 def get_instance_good(expected):
158 res = client.get(ADM_CTRL_INSTANCE)
159 assert res.status_code == 200
160 assert res.json == adm_instance_good
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
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")
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")
175 # cant delete type until there are no instances
176 res = client.delete(ADM_CTRL_TYPE)
177 assert res.status_code == 400
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
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")
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
196 # list still 200 but no instance
197 res = client.get(ADM_CTRL_POLICIES)
198 assert res.status_code == 200
199 assert res.json == []
202 res = client.delete(ADM_CTRL_TYPE)
203 assert res.status_code == 204
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
212 def test_bad_instances(client, monkeypatch, adm_type_good):
214 test various failure modes
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
222 res = client.put(ADM_CTRL_INSTANCE, json={"not": "expected"})
223 assert res.status_code == 400
226 res = client.put(ADM_CTRL_INSTANCE, data="notajson")
227 assert res.status_code == 415
229 # delete a non existent instance
230 res = client.delete(ADM_CTRL_INSTANCE + "DARKNESS")
231 assert res.status_code == 404
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
238 # delete the type (as cleanup)
239 res = client.delete(ADM_CTRL_TYPE)
240 assert res.status_code == 204
243 def test_illegal_types(client, monkeypatch, adm_type_good):
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
253 def test_healthcheck(client):
257 res = client.get("/a1-p/healthcheck")
258 assert res.status_code == 200