Move to ricxappframe rmr, rmr3.6.3
[ric-plt/a1.git] / tests / test_controller.py
index 971ce3f..2aac5a9 100644 (file)
@@ -2,8 +2,8 @@
 tests for controller
 """
 # ==================================================================================
-#       Copyright (c) 2019 Nokia
-#       Copyright (c) 2018-2019 AT&T Intellectual Property.
+#       Copyright (c) 2019-2020 Nokia
+#       Copyright (c) 2018-2020 AT&T Intellectual Property.
 #
 #   Licensed under the Apache License, Version 2.0 (the "License");
 #   you may not use this file except in compliance with the License.
@@ -17,11 +17,12 @@ tests for controller
 #   See the License for the specific language governing permissions and
 #   limitations under the License.
 # ==================================================================================
-
 import time
 import json
-from rmr.rmr_mocks import rmr_mocks
-from a1 import a1rmr
+from ricxappframe.rmr.rmr_mocks import rmr_mocks
+from ricxappframe.xapp_sdl import SDLWrapper
+from ricsdl.exceptions import RejectedByBackend, NotConnected, BackendError
+from a1 import a1rmr, data
 
 RCV_ID = "test_receiver"
 ADM_CRTL_TID = 6660666
@@ -30,6 +31,7 @@ ADM_CTRL_POLICIES = "/a1-p/policytypes/{0}/policies".format(ADM_CRTL_TID)
 ADM_CTRL_INSTANCE = ADM_CTRL_POLICIES + "/" + ADM_CTRL_IID
 ADM_CTRL_INSTANCE_STATUS = ADM_CTRL_INSTANCE + "/status"
 ADM_CTRL_TYPE = "/a1-p/policytypes/{0}".format(ADM_CRTL_TID)
+ACK_MT = 20011
 
 
 def _fake_dequeue():
@@ -37,8 +39,8 @@ def _fake_dequeue():
     pay = json.dumps(
         {"policy_type_id": ADM_CRTL_TID, "policy_instance_id": ADM_CTRL_IID, "handler_id": RCV_ID, "status": "OK"}
     ).encode()
-    fake_msg = {"payload": pay}
-    return [fake_msg]
+    fake_msg = {"payload": pay, "message type": ACK_MT}
+    return [(fake_msg, None)]
 
 
 def _fake_dequeue_none():
@@ -49,31 +51,37 @@ def _fake_dequeue_none():
 def _fake_dequeue_deleted():
     """for monkeypatching  with a DELETED status"""
     new_msgs = []
+    good_pay = json.dumps(
+        {"policy_type_id": ADM_CRTL_TID, "policy_instance_id": ADM_CTRL_IID, "handler_id": RCV_ID, "status": "DELETED"}
+    ).encode()
 
-    # non existent type
+    # non existent type id
     pay = json.dumps(
         {"policy_type_id": 911, "policy_instance_id": ADM_CTRL_IID, "handler_id": RCV_ID, "status": "DELETED"}
     ).encode()
-    fake_msg = {"payload": pay}
-    new_msgs.append(fake_msg)
+    fake_msg = {"payload": pay, "message type": ACK_MT}
+    new_msgs.append((fake_msg, None))
 
+    # bad instance id
     pay = json.dumps(
         {"policy_type_id": ADM_CRTL_TID, "policy_instance_id": "darkness", "handler_id": RCV_ID, "status": "DELETED"}
     ).encode()
-    fake_msg = {"payload": pay}
-    new_msgs.append(fake_msg)
+    fake_msg = {"payload": pay, "message type": ACK_MT}
+    new_msgs.append((fake_msg, None))
+
+    # good body but bad message type
+    fake_msg = {"payload": good_pay, "message type": ACK_MT * 3}
+    new_msgs.append((fake_msg, None))
 
     # insert a bad one with a malformed body to make sure we keep going
-    new_msgs.append({"payload": "asdf"})
+    new_msgs.append(({"payload": "asdf", "message type": ACK_MT}, None))
 
     # not even a json
-    new_msgs.append("asdf")
+    new_msgs.append(("asdf", None))
 
-    pay = json.dumps(
-        {"policy_type_id": ADM_CRTL_TID, "policy_instance_id": ADM_CTRL_IID, "handler_id": RCV_ID, "status": "DELETED"}
-    ).encode()
-    fake_msg = {"payload": pay}
-    new_msgs.append(fake_msg)
+    # good
+    fake_msg = {"payload": good_pay, "message type": ACK_MT}
+    new_msgs.append((fake_msg, None))
 
     return new_msgs
 
@@ -81,28 +89,7 @@ def _fake_dequeue_deleted():
 def _test_put_patch(monkeypatch):
     rmr_mocks.patch_rmr(monkeypatch)
     # assert that rmr bad states don't cause problems
-    monkeypatch.setattr("rmr.rmr.rmr_send_msg", rmr_mocks.send_mock_generator(10))
-
-    # we need this because free expects a real sbuf
-    # TODO: move this into rmr_mocks
-    def noop(_sbuf):
-        pass
-
-    monkeypatch.setattr("rmr.rmr.rmr_free_msg", noop)
-
-    # we need to repatch alloc (already patched in patch_rmr) to fix the transactionid, alloc is called in send and recieve
-    def fake_alloc(_unused1, _unused2, _unused3, _unused4, _unused5):
-        sbuf = rmr_mocks.Rmr_mbuf_t()
-        sbuf.contents.xaction = b"d49b53e478b711e9a1130242ac110002"
-        return sbuf
-
-    # we also need to repatch set, since in the send function, we alloc, then set a new transid
-    def fake_set_transactionid(sbuf):
-        sbuf.contents.xaction = b"d49b53e478b711e9a1130242ac110002"
-
-    # Note, we could have just patched summary, but this patches at a "lower level" so is a better test
-    monkeypatch.setattr("rmr.rmr.rmr_alloc_msg", fake_alloc)
-    monkeypatch.setattr("rmr.rmr.generate_and_set_transaction_id", fake_set_transactionid)
+    monkeypatch.setattr("ricxappframe.rmr.rmr.rmr_send_msg", rmr_mocks.send_mock_generator(10))
 
 
 def _no_ac(client):
@@ -245,6 +232,9 @@ def _verify_instance_and_status(client, expected_instance, expected_status, expe
 def setup_module():
     """module level setup"""
 
+    # swap sdl for the fake backend
+    data.SDL = SDLWrapper(use_fake_sdl=True)
+
     def noop():
         pass
 
@@ -259,6 +249,8 @@ def test_workflow(client, monkeypatch, adm_type_good, adm_instance_good):
     """
     test a full A1 workflow
     """
+
+    # put type and instance
     _put_ac_type(client, adm_type_good)
     _put_ac_instance(client, monkeypatch, adm_instance_good)
 
@@ -358,6 +350,26 @@ def test_bad_instances(client, monkeypatch, adm_type_good):
     res = client.delete(ADM_CTRL_TYPE)
     assert res.status_code == 204
 
+    # test 503 handlers
+
+    def monkey_set(ns, key, value):
+        # set a key override function that throws sdl errors on certain keys
+        if key == "a1.policy_type.111":
+            raise RejectedByBackend()
+        if key == "a1.policy_type.112":
+            raise NotConnected()
+        if key == "a1.policy_type.113":
+            raise BackendError()
+
+    monkeypatch.setattr("a1.data.SDL.set", monkey_set)
+
+    res = client.put("/a1-p/policytypes/111", json=adm_type_good)
+    assert res.status_code == 503
+    res = client.put("/a1-p/policytypes/112", json=adm_type_good)
+    assert res.status_code == 503
+    res = client.put("/a1-p/policytypes/113", json=adm_type_good)
+    assert res.status_code == 503
+
 
 def test_illegal_types(client, adm_type_good):
     """