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_xapp_put_good(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 res = client.put(ADM_CTRL_INSTANCE, json=adm_instance_good)
147 assert res.status_code == 202
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]
154 def get_instance_good(expected):
156 res = client.get(ADM_CTRL_INSTANCE)
157 assert res.status_code == 200
158 assert res.json == adm_instance_good
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
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")
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")
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
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")
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 == []
195 def test_xapp_put_good_bad_rmr(client, monkeypatch, adm_instance_good):
197 assert that rmr bad states don't cause problems
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
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
209 def test_bad_instances(client, monkeypatch, adm_type_good):
211 Test bad send failures
213 rmr_mocks.patch_rmr(monkeypatch)
215 # TODO: reenable this after delete!
217 # res = client.put(ADM_CTRL_TYPE, json=adm_type_good)
218 # assert res.status_code == 201
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
227 res = client.put(ADM_CTRL_INSTANCE, json={"not": "expected"})
228 assert res.status_code == 400
231 res = client.put(ADM_CTRL_INSTANCE, data="notajson")
232 assert res.status_code == 415
235 def test_healthcheck(client):
239 res = client.get("/a1-p/healthcheck")
240 assert res.status_code == 200