Threading pt 2 (of 3, likely)
* Move database cleanup (e.g., deleting instances based on statuses) into the polling loop
* Rework how unit testing works with the polling loop; prior, exceptions were being thrown silently from the thread but not printed. The polling thread has now been paramaterized with override functions for the purposes of testing
* Make type cleanup more efficient since we know exactly what instances were touched, and it's inefficient to iterate over all instances if they were not
* Bump rmr-python version, and bump rmr version
* Still an item left to do in this work; refactor the thread slightly to tie in a healthcheck with a1s healthcheck. We need k8s to restart a1 if that thread dies too.
Change-Id: Ia7c4f29c9fd4de4287f17ec0d88c6129a06a5a87
Signed-off-by: Tommy Carpenter <tc677g@att.com>
diff --git a/tests/test_controller.py b/tests/test_controller.py
index e5c3ac7..63831b0 100644
--- a/tests/test_controller.py
+++ b/tests/test_controller.py
@@ -17,7 +17,7 @@
import time
from rmr.rmr_mocks import rmr_mocks
-from a1 import run
+from a1 import a1rmr
ADM_CTRL = "admission_control_policy"
@@ -28,21 +28,19 @@
TEST_TYPE = "/a1-p/policytypes/20001"
-def _fake_dequeue(_mrc, _filter_type):
+def _fake_dequeue():
"""for monkeypatching with a good status"""
- fake_msg = {}
pay = b'{"policy_type_id": 20000, "policy_instance_id": "admission_control_policy", "handler_id": "test_receiver", "status": "OK"}'
- fake_msg["payload"] = pay
- new_messages = [fake_msg]
- return new_messages
+ fake_msg = {"payload": pay}
+ return [fake_msg]
-def _fake_dequeue_none(_mrc, _filter_type):
+def _fake_dequeue_none():
"""for monkeypatching with no waiting messages"""
return []
-def _fake_dequeue_deleted(_mrc, _filter_type):
+def _fake_dequeue_deleted():
"""for monkeypatching with a DELETED status"""
new_msgs = []
@@ -55,6 +53,10 @@
fake_msg = {"payload": pay}
new_msgs.append(fake_msg)
+ # insert a bad one with a malformed body to make sure we keep going
+ fake_msg = {"payload": "asdf"}
+ new_msgs.append(fake_msg)
+
pay = b'{"policy_type_id": 20000, "policy_instance_id": "admission_control_policy", "handler_id": "test_receiver", "status": "DELETED"}'
fake_msg = {"payload": pay}
new_msgs.append(fake_msg)
@@ -98,15 +100,19 @@
def setup_module():
"""module level setup"""
global RMR_THREAD
- RMR_THREAD = run.start_rmr_thread(real_init=False)
+
+ def noop():
+ pass
+
+ # launch the thread with a fake init func and a patched rcv func; we will "repatch" later
+ RMR_THREAD = a1rmr.start_rmr_thread(init_func_override=noop, rcv_func_override=_fake_dequeue_none)
# Actual Tests
-def test_workflow_nothing_there_yet(client, monkeypatch, adm_type_good, adm_instance_good):
+def test_workflow_nothing_there_yet(client):
""" test policy put good"""
- monkeypatch.setattr("rmr.helpers.rmr_rcvall_msgs", _fake_dequeue_none)
# no type there yet
res = client.get(ADM_CTRL_TYPE)
assert res.status_code == 404
@@ -125,7 +131,6 @@
"""
test a full A1 workflow
"""
- monkeypatch.setattr("rmr.helpers.rmr_rcvall_msgs", _fake_dequeue_none)
# put the type
res = client.put(ADM_CTRL_TYPE, json=adm_type_good)
assert res.status_code == 201
@@ -183,7 +188,7 @@
get_instance_good("NOT IN EFFECT")
# now pretend we did get a good ACK
- monkeypatch.setattr("rmr.helpers.rmr_rcvall_msgs", _fake_dequeue)
+ RMR_THREAD._rcv_func = _fake_dequeue
time.sleep(1) # wait for the rmr thread
get_instance_good("IN EFFECT")
@@ -198,22 +203,20 @@
assert res.status_code == 202
# status after a delete, but there are no messages yet, should still return
- monkeypatch.setattr("rmr.helpers.rmr_rcvall_msgs", _fake_dequeue)
time.sleep(1) # wait for the rmr thread
get_instance_good("IN EFFECT")
# now pretend we deleted successfully
- monkeypatch.setattr("rmr.helpers.rmr_rcvall_msgs", _fake_dequeue_deleted)
+ RMR_THREAD._rcv_func = _fake_dequeue_deleted
time.sleep(1) # wait for the rmr thread
- res = client.get(ADM_CTRL_INSTANCE_STATUS) # cant get status
- assert res.status_code == 404
- res = client.get(ADM_CTRL_INSTANCE) # cant get instance
- assert res.status_code == 404
-
# list still 200 but no instance
res = client.get(ADM_CTRL_POLICIES)
assert res.status_code == 200
assert res.json == []
+ res = client.get(ADM_CTRL_INSTANCE_STATUS) # cant get status
+ assert res.status_code == 404
+ res = client.get(ADM_CTRL_INSTANCE) # cant get instance
+ assert res.status_code == 404
# delete the type
res = client.delete(ADM_CTRL_TYPE)
@@ -248,7 +251,7 @@
assert res.status_code == 404
# get a non existent instance
- monkeypatch.setattr("rmr.helpers.rmr_rcvall_msgs", _fake_dequeue)
+ RMR_THREAD._rcv_func = _fake_dequeue
time.sleep(1)
res = client.get(ADM_CTRL_INSTANCE + "DARKNESS")
assert res.status_code == 404