def setUp(self):
     loop = asyncio.new_event_loop()
     asyncio.set_event_loop(loop)
     self._loop = loop
     self._sync_rpc_client = SyncRPCClient(loop=loop, response_timeout=3)
     self._sync_rpc_client._conn_closed_table = {12345: False}
     ServiceRegistry.add_service('test', '0.0.0.0', 0)
     ServiceRegistry._PROXY_CONFIG = {
         'local_port': 2345,
         'cloud_address': 'test',
         'proxy_cloud_connections': True
     }
     self._req_body = GatewayRequest(gwId="test id",
                                     authority='mobility',
                                     path='/magma.MobilityService'
                                     '/ListAddedIPv4Blocks',
                                     headers={
                                         'te': 'trailers',
                                         'content-type': 'application/grpc',
                                         'user-agent': 'grpc-python/1.4.0',
                                         'grpc-accept-encoding': 'identity'
                                     },
                                     payload=bytes.fromhex('0000000000'))
     self._expected_resp = GatewayResponse(status="400",
                                           headers={"test_key": "test_val"},
                                           payload=b'\x00'
                                           b'\x00\x00\x00\n\n\x08')
     self._expected_err_msg = "test error"
Exemple #2
0
 def setUp(self):
     loop = asyncio.new_event_loop()
     asyncio.set_event_loop(loop)
     self._loop = loop
     self._proxy_client = ControlProxyHttpClient()
     ServiceRegistry._REGISTRY = {
         "services": {
             "mobilityd":
             {
                 "ip_address": "0.0.0.0",
                 "port": 3456,
             },
         },
     }
     ServiceRegistry.add_service('test', '0.0.0.0', 0)
     self._req_body = GatewayRequest(
         gwId="test id", authority='mobilityd',
         path='/magma.MobilityService'
              '/ListAddedIPv4Blocks',
         headers={
             'te': 'trailers',
             'content-type':
                 'application/grpc',
             'user-agent':
                 'grpc-python/1.4.0',
             'grpc-accept-encoding':
                 'identity',
         },
         payload=bytes.fromhex('0000000000'),
     )
    def test_http_client(self, mock_conn):
        req_body = GatewayRequest(gwId="test id", authority='mobilityd',
                                  path='/magma.MobilityService'
                                       '/ListAddedIPv4Blocks',
                                  headers={'te': 'trailers',
                                           'content-type': 'application/grpc',
                                           'user-agent': 'grpc-python/1.4.0',
                                           'grpc-accept-encoding': 'identity'},
                                  payload=bytes.fromhex('0000000000'))
        expected_payload = \
            b'\x00\x00\x00\x00\n\n\x08\x12\x04\xc0\xa8\x80\x00\x18\x18'
        expected_header = [(':status', '200'),
                           ('content-type', 'application/grpc')]
        expected_trailers = [('grpc-status', '0'), ('grpc-message', '')]

        mock_conn.side_effect = asyncio.coroutine(
            unittest.mock.MagicMock(
                return_value=MockClient(expected_payload, expected_header,
                                        expected_trailers, req_body)))
        future = asyncio.ensure_future(
            self._proxy_client.send(self._req_body))

        try:
            self._loop.run_until_complete(future)
            res = future.result()
            self.assertEqual(res.status,
                             '200')
            self.assertEqual(res.payload, expected_payload)
        except Exception as e:  # pylint: disable=broad-except
            self.fail(e)
        self._loop.close()
Exemple #4
0
    def test_http_client_unary(self, mock_conn):
        req_body = GatewayRequest(
            gwId="test id", authority='mobilityd',
            path='/magma.MobilityService'
                 '/ListAddedIPv4Blocks',
            headers={
                'te': 'trailers',
                'content-type': 'application/grpc',
                'user-agent': 'grpc-python/1.4.0',
                'grpc-accept-encoding': 'identity',
            },
            payload=bytes.fromhex('0000000000'),
        )
        expected_payload = \
            b'\x00\x00\x00\x00\n\n\x08\x12\x04\xc0\xa8\x80\x00\x18\x18'
        expected_header = [
            (':status', '200'),
            ('content-type', 'application/grpc'),
        ]
        expected_trailers = [('grpc-status', '0'), ('grpc-message', '')]

        mock_conn.side_effect = asyncio.coroutine(
            unittest.mock.MagicMock(
                return_value=MockUnaryClient(
                    expected_payload, expected_header,
                    expected_trailers, req_body,
                ),
            ),
        )

        request_queue = queue.Queue()
        conn_closed_table = {
            1234: False,
        }

        future = asyncio.ensure_future(
            self._proxy_client.send(
                self._req_body,
                1234,
                request_queue,
                conn_closed_table,
            ),
        )

        self._loop.run_until_complete(future)

        self.assertEqual(request_queue.qsize(), 1)
        res = request_queue.get()
        self.assertEqual(res.reqId, 1234)
        self.assertEqual(res.heartBeat, False)
        self.assertEqual(res.respBody.status, '200')
        self.assertEqual(res.respBody.payload, expected_payload)
        self.assertEqual(res.respBody.headers['grpc-status'], '0')
        self._loop.close()