def test_constructor_hasattr_False(self): self.target2 = Request("FlowId2", Request.Method.GET, "flows/FlowId2", self.value) self.assertEqual(self.target2.object_id, "FlowId2") self.assertEqual(self.target2.method, Request.Method.GET) self.assertEqual(self.target2.path, "flows/FlowId2") self.assertEqual(self.target2.body, self.value)
def send_request_message(self, request): # for deep copy of Request object obj = msgpack.unpackb(msgpack.packb(request.packed_object())) rcv_request = Request.create_from_packed(obj) response = self.dispatcher.dispatch_request(rcv_request) # for deep copy of Response object obj = msgpack.unpackb(msgpack.packb(response.packed_object())) return Response.create_from_packed(obj)
def test_dispatch_request_object_id_not_in_local_objects(self): request = Request("NoneID", "method", "path") remote_object = RemoteObject("remote_object", self.target) self.target._MessageDispatcher__local_objects =\ {"remote_object": remote_object} self.result = self.target.dispatch_request(request) self.assertEqual(self.result.status_code, 404) self.assertEqual(self.result.body, None)
def test_request_sync(self): with patch("org.o3project.odenos.remoteobject.transport." + "remote_message_transport.RemoteMessageTransport." + "send_request_message") as mock_send_request_message: request = Request("object_id", "method", "path") mock_send_request_message.return_value = "send_request_message" self.result = self.target.request_sync(request) self.assertEqual(self.result, "send_request_message")
def test_send_request_message(self): request = Request("object_id", "method", "path") response = Response(200, "body") self.target.dispatcher.dispatch_request =\ MagicMock(return_value=response) result = self.target.send_request_message(request) self.assertEqual(result.status_code, 200) self.assertEqual(result.body, "body")
def __send_request(self, object_id, method, path, body=None): resp = Response(Response.StatusCode.INTERNAL_SERVER_ERROR, None) req = Request(object_id, method, path, body=body) try: resp = self.__dispatcher.request_sync(req) except: logging.error("Exception: Request to " + object_id + " Method:" + method + " Path:" + path) logging.error(traceback.format_exc()) return resp
def subscribe_event(self, event_subscription): self.__subscription_map.remove_subscription( event_subscription.subscriber_id) self.__subscription_map.set_subscription(event_subscription) self.__update_subscriber() request = Request( self.event_manager_id, Request.Method.PUT, "settings/event_subscriptions/%s" % event_subscription.subscriber_id, event_subscription) return self.request_sync(request)
def test_dispatch_request_object_id_in_local_objects(self): request = Request("remote_object", "method", "path") remote_object = RemoteObject("remote_object", self.target) self.target._MessageDispatcher__local_objects =\ {"remote_object": remote_object} with patch("org.o3project.odenos.remoteobject." + "remote_object.RemoteObject." + "dispatch_request") as mock_dispatch_request: mock_dispatch_request.return_value = "mock_dispatch_request" self.result = self.target.dispatch_request(request) self.assertEqual(self.result, "mock_dispatch_request")
def test_action(self): self.target.add_rule([{ "pattern": "(param1)", "method": "GET", "func": self.dummyFunc0, "params": 0 }, { "pattern": "(param2)", "method": "PUT", "func": self.dummyFunc1, "params": 1 }]) request = Request("id1", "GET", "path/to/param1", None) response = self.target.action(request) self.assertEqual(self.call_func0, 1) self.assertEqual(response.status_code, 101)
def __redisSubscriberRunnable(self): for mesg in self.__redisSubscriber.listen(): if mesg['type'] != 'message': continue try: bio = BytesIO() bio.write(mesg['data']) bio.seek(0) upk = msgpack.Unpacker(bio) tp = upk.unpack() sno = upk.unpack() srcid = upk.unpack() if tp == self.TYPE_REQUEST: request = Request.create_from_packed(upk.unpack()) def request_runnable(request, sno, srcid): response = self.dispatch_request(request) pk = msgpack.Packer() resb = bytearray() resb.extend(pk.pack(self.TYPE_RESPONSE)) resb.extend(pk.pack(sno)) resb.extend(pk.pack(request.object_id)) resb.extend(pk.pack(response.packed_object())) self.__pubsqueue.put( MessageDispatcher.PublishData(None, self.TYPE_RESPONSE, sno, srcid, resb)) self.thread_pool.submit(request_runnable, request, sno, srcid) elif tp == self.TYPE_RESPONSE: trans = self.__clients[srcid] response = Response.create_from_packed(upk.unpack()) trans.signalResponse(sno, response) elif tp == self.TYPE_EVENT: event = Event.create_from_packed(upk.unpack()) def event_runnable(event): self.dispatch_event(event) self.thread_pool.submit(event_runnable, event) except: logging.error(traceback.format_exc()) pass
def test_action_with_param_attribute_params_is_1(self): self.target.add_rule([{ "pattern": "(param1)", "method": "GET", "func": self.dummyFunc0, "params": 0 }, { "pattern": "(param2)", "method": "PUT", "func": self.dummyFunc1, "params": 1 }]) request = Request("id1", "PUT", "path/to/param2", "body") response = self.target.action(request) self.assertEqual(self.call_func1, 1) self.assertEqual(self.func1_body, "body") self.assertEqual(response.status_code, 102)
def test_action_with_pattern_not_matched(self): self.target.add_rule([{ "pattern": "(param1)", "method": "GET", "func": self.dummyFunc0, "params": 0 }, { "pattern": "(param2)", "method": "PUT", "func": self.dummyFunc1, "params": 1 }]) request = Request("id1", "GET", "path/to/param3", "body") response = self.target.action(request) self.assertEqual(self.call_func0, 0) self.assertEqual(self.call_func1, 0) self.assertEqual(self.func1_body, None) self.assertEqual(response.status_code, 404)
def __redisSubscriberRunnable(self): for mesg in self.__redisSubscriber.listen(): if mesg['type'] != 'message': continue try: bio = BytesIO() bio.write(mesg['data']) bio.seek(0) upk = msgpack.Unpacker(bio) tp = upk.unpack() sno = upk.unpack() srcid = upk.unpack() if tp == self.TYPE_REQUEST: request = Request.create_from_packed(upk.unpack()) def request_runnable(request, sno, srcid): response = self.dispatch_request(request) pk = msgpack.Packer() resb = bytearray() resb.extend(pk.pack(self.TYPE_RESPONSE)) resb.extend(pk.pack(sno)) resb.extend(pk.pack(request.object_id)) resb.extend(pk.pack(response.packed_object())) self.__pubsqueue.put( MessageDispatcher.PublishData( None, self.TYPE_RESPONSE, sno, srcid, resb)) self.thread_pool.submit(request_runnable, request, sno, srcid) elif tp == self.TYPE_RESPONSE: trans = self.__clients[srcid] response = Response.create_from_packed(upk.unpack()) trans.signalResponse(sno, response) elif tp == self.TYPE_EVENT: event = Event.create_from_packed(upk.unpack()) def event_runnable(event): self.dispatch_event(event) self.thread_pool.submit(event_runnable, event) except: logging.error(traceback.format_exc()) pass
def test_on_request(self): request = Request("ObjectId", Request.Method.GET, "component_managers", None) self.result = self.target._on_request(request) self.assertEqual(self.result.status_code, 404) self.assertEqual(self.result.body, None)
class RequestTest(unittest.TestCase): Type = "BasicFlow" Version = "v01" Flow_id = "Id01" Owner = "Owner" Enabled = True Priority = "65535" Status = "none" Attributes = {"bandwidth": "10Mbps", "req_bandwidth": "11Mbps", "latency": "10msec", "req_latency": "11msec"} Publisher_id = 'Id1' Event_type = 'flow' value = {"type": Type, "version": Version, "flow_id": Flow_id, "owner": Owner, "enabled": Enabled, "priority": Priority, "status": Status, "attributes": Attributes} Packed = ["FlowId2", Request.Method.GET, "flows/FlowId2", value] flow_target = Flow(Type, Version, Flow_id, Owner, Enabled, Priority, Status, Attributes) flow_target_packed_object = flow_target.packed_object() def setUp(self): self.target = Request("FlowId1", Request.Method.GET, "flows/FlowId1", self.flow_target) def tearDown(self): self.target = None def test_constructor_hasattr_True(self): self.assertEqual(self.target.object_id, "FlowId1") self.assertEqual(self.target.method, Request.Method.GET) self.assertEqual(self.target.path, "flows/FlowId1") self.assertEqual(self.target.body, self.value) def test_constructor_hasattr_False(self): self.target2 = Request("FlowId2", Request.Method.GET, "flows/FlowId2", self.value) self.assertEqual(self.target2.object_id, "FlowId2") self.assertEqual(self.target2.method, Request.Method.GET) self.assertEqual(self.target2.path, "flows/FlowId2") self.assertEqual(self.target2.body, self.value) def test_create_from_packed(self): self.result = self.target.create_from_packed(self.Packed) self.assertEqual(self.result.object_id, "FlowId2") self.assertEqual(self.result.method, Request.Method.GET) self.assertEqual(self.result.path, "flows/FlowId2") self.assertEqual(self.result.body, self.value) def test_packed_object(self): self.result = self.target.packed_object() self.assertEqual(self.result, ("FlowId1", Request.Method.GET, "flows/FlowId1", self.value)) self.assertEqual(len(self.result), 4)
def setUp(self): self.target = Request("FlowId1", Request.Method.GET, "flows/FlowId1", self.flow_target)
def _request_sync(self, object_id, method, path, body=None): return self.dispatcher.request_sync( Request(object_id, method, path, body))
def test_redisSubscriberRunnable_TYPE_REQUEST(self): request = Request("object_id", "method", "path", None) request_packed = request.packed_object() response = Response(200, "body") response_packed = response.packed_object() pk = msgpack.Packer() resb = bytearray() resb.extend(pk.pack(0)) resb.extend(pk.pack(0)) resb.extend(pk.pack("object_id")) resb.extend(pk.pack(request_packed)) self.value = {"type": "message", "data": resb} self.value02 = {"type": "ERROR", "data": resb} resb_check = bytearray() resb_check.extend(pk.pack(1)) resb_check.extend(pk.pack(0)) resb_check.extend(pk.pack("object_id")) resb_check.extend(pk.pack(response_packed)) self.target.thread_pool = futures.ThreadPoolExecutor(max_workers=8) self.target._MessageDispatcher__redisSubscriber = redis.StrictRedis( host=self.target._MessageDispatcher__redisServer, port=self.target._MessageDispatcher__redisPort) self.target._MessageDispatcher__redisSubscriber =\ self.target._MessageDispatcher__redisSubscriber.pubsub() def dummy_request_runnable(arg, request, sno, srcid): arg(request, sno, srcid) with nested( patch("redis.client.PubSub.subscribe"), patch("redis.client.PubSub.unsubscribe"), patch("redis.client.PubSub.listen"), patch("concurrent.futures.ThreadPoolExecutor.submit"), patch(self.REQUEST_PATH + ".create_from_packed"), patch(self.DISPATCHER_PATH + ".dispatch_request")) as ( mock_subscribe, mock_unsubscribe, mock_listen, mock_submit, mock_request, mock_dispatch_request): mock_subscribe.return_value = None mock_unsubscribe.return_value = None mock_listen.return_value = [self.value, self.value02] mock_request.return_value = request mock_dispatch_request.return_value = response mock_submit.side_effect = dummy_request_runnable self.target._MessageDispatcher__redisSubscriberRunnable() self.result = self.target._MessageDispatcher__pubsqueue.get() mock_request.assert_called_once_with( ["object_id", "method", "path", None]) self.assertEqual(mock_submit.call_count, 1) self.assertEqual( self.result.trans, None) self.assertEqual( self.result.type, 1) self.assertEqual( self.result.sno, 0) self.assertEqual( self.result.channel, "object_id") self.assertEqual( self.result.data, resb_check)
def test_redisSubscriberRunnable_TYPE_REQUEST(self): request = Request("object_id", "method", "path", None) request_packed = request.packed_object() response = Response(200, "body") response_packed = response.packed_object() pk = msgpack.Packer() resb = bytearray() resb.extend(pk.pack(0)) resb.extend(pk.pack(0)) resb.extend(pk.pack("object_id")) resb.extend(pk.pack(request_packed)) self.value = {"type": "message", "data": resb} self.value02 = {"type": "ERROR", "data": resb} resb_check = bytearray() resb_check.extend(pk.pack(1)) resb_check.extend(pk.pack(0)) resb_check.extend(pk.pack("object_id")) resb_check.extend(pk.pack(response_packed)) self.target.thread_pool = futures.ThreadPoolExecutor(max_workers=8) self.target._MessageDispatcher__redisSubscriber = redis.StrictRedis( host=self.target._MessageDispatcher__redisServer, port=self.target._MessageDispatcher__redisPort) self.target._MessageDispatcher__redisSubscriber =\ self.target._MessageDispatcher__redisSubscriber.pubsub() def dummy_request_runnable(arg, request, sno, srcid): arg(request, sno, srcid) with nested(patch("redis.client.PubSub.subscribe"), patch("redis.client.PubSub.unsubscribe"), patch("redis.client.PubSub.listen"), patch("concurrent.futures.ThreadPoolExecutor.submit"), patch(self.REQUEST_PATH + ".create_from_packed"), patch(self.DISPATCHER_PATH + ".dispatch_request")) as (mock_subscribe, mock_unsubscribe, mock_listen, mock_submit, mock_request, mock_dispatch_request): mock_subscribe.return_value = None mock_unsubscribe.return_value = None mock_listen.return_value = [self.value, self.value02] mock_request.return_value = request mock_dispatch_request.return_value = response mock_submit.side_effect = dummy_request_runnable self.target._MessageDispatcher__redisSubscriberRunnable() self.result = self.target._MessageDispatcher__pubsqueue.get() mock_request.assert_called_once_with( ["object_id", "method", "path", None]) self.assertEqual(mock_submit.call_count, 1) self.assertEqual(self.result.trans, None) self.assertEqual(self.result.type, 1) self.assertEqual(self.result.sno, 0) self.assertEqual(self.result.channel, "object_id") self.assertEqual(self.result.data, resb_check)
class RequestTest(unittest.TestCase): Type = "BasicFlow" Version = "v01" Flow_id = "Id01" Owner = "Owner" Enabled = True Priority = "65535" Status = "none" Attributes = { "bandwidth": "10Mbps", "req_bandwidth": "11Mbps", "latency": "10msec", "req_latency": "11msec" } Publisher_id = 'Id1' Event_type = 'flow' value = { "type": Type, "version": Version, "flow_id": Flow_id, "owner": Owner, "enabled": Enabled, "priority": Priority, "status": Status, "attributes": Attributes } Packed = ["FlowId2", Request.Method.GET, "flows/FlowId2", value] flow_target = Flow(Type, Version, Flow_id, Owner, Enabled, Priority, Status, Attributes) flow_target_packed_object = flow_target.packed_object() def setUp(self): self.target = Request("FlowId1", Request.Method.GET, "flows/FlowId1", self.flow_target) def tearDown(self): self.target = None def test_constructor_hasattr_True(self): self.assertEqual(self.target.object_id, "FlowId1") self.assertEqual(self.target.method, Request.Method.GET) self.assertEqual(self.target.path, "flows/FlowId1") self.assertEqual(self.target.body, self.value) def test_constructor_hasattr_False(self): self.target2 = Request("FlowId2", Request.Method.GET, "flows/FlowId2", self.value) self.assertEqual(self.target2.object_id, "FlowId2") self.assertEqual(self.target2.method, Request.Method.GET) self.assertEqual(self.target2.path, "flows/FlowId2") self.assertEqual(self.target2.body, self.value) def test_create_from_packed(self): self.result = self.target.create_from_packed(self.Packed) self.assertEqual(self.result.object_id, "FlowId2") self.assertEqual(self.result.method, Request.Method.GET) self.assertEqual(self.result.path, "flows/FlowId2") self.assertEqual(self.result.body, self.value) def test_packed_object(self): self.result = self.target.packed_object() self.assertEqual( self.result, ("FlowId1", Request.Method.GET, "flows/FlowId1", self.value)) self.assertEqual(len(self.result), 4)