コード例 #1
0
 def test_all_info(self, mock_curl):
     mock_curl.return_value = MockCurl(None, b"output", [
         (pycurl.DEBUG_TEXT, b"debug"),
         (pycurl.DEBUG_DATA_OUT, b"info\n"),
     ])
     request = lib.Request(
         lib.RequestTarget(
             "label",
             token="token_val",
             dest_list=_addr_list_to_dest(["host1", "host2"], port=123),
         ), lib.RequestData("action", [("data", "value")]))
     cookies = {
         "name1": "val1",
         "name2": "val2",
     }
     handle = lib._create_request_handle(request, cookies, 1)
     expected_opts = {
         pycurl.TIMEOUT: 1,
         pycurl.URL: request.url.encode("utf-8"),
         pycurl.COOKIE:
         "name1=val1;name2=val2;token=token_val".encode("utf-8"),
         pycurl.COPYPOSTFIELDS: "data=value".encode("utf-8"),
     }
     expected_opts.update(self._common_opts)
     self.assertLessEqual(set(expected_opts.items()),
                          set(handle.opts.items()))
     self.assertIs(request, handle.request_obj)
     self.assertEqual("", handle.output_buffer.getvalue().decode("utf-8"))
     self.assertEqual("", handle.debug_buffer.getvalue().decode("utf-8"))
     handle.perform()
     self.assertEqual("output",
                      handle.output_buffer.getvalue().decode("utf-8"))
     self.assertEqual("* debug\n>> info\n",
                      handle.debug_buffer.getvalue().decode("utf-8"))
コード例 #2
0
    def test_success(self, mock_create_handle, _, mock_con_successful,
                     mock_con_failure):
        com = self.get_multiaddress_communicator()
        counter = {"counter": 0}
        expected_response_list = []

        def _con_successful(handle):
            response = lib.Response(handle, True)
            expected_response_list.append(response)
            return response

        def _con_failure(handle, errno, err_msg):
            response = lib.Response(handle, False, errno, err_msg)
            expected_response_list.append(response)
            return response

        def _mock_create_request_handle(request, _, __):
            counter["counter"] += 1
            return (MockCurl(
                request=request) if counter["counter"] > 2 else MockCurl(
                    error=(pycurl.E_SEND_ERROR, "reason"),
                    request=request,
                ))

        mock_con_successful.side_effect = _con_successful
        mock_con_failure.side_effect = _con_failure
        mock_create_handle.side_effect = _mock_create_request_handle
        request = lib.Request(
            lib.RequestTarget(
                "label",
                dest_list=_addr_list_to_dest(
                    ["host{0}".format(i) for i in range(4)]),
            ),
            lib.RequestData("action"),
        )
        com.add_requests([request])
        response_list = list(com.start_loop())
        self.assertEqual(1, len(response_list))
        response = response_list[0]
        self.assertIs(response, expected_response_list[-1])
        self.assertTrue(response.was_connected)
        self.assertIs(request, response.request)
        self.assertEqual(Destination("host2", None), request.dest)
        self.assertEqual(3, mock_create_handle.call_count)
        self.assertEqual(3, len(expected_response_list))
        mock_create_handle.assert_has_calls([
            mock.call(request, {}, settings.default_request_timeout)
            for _ in range(3)
        ])
        logger_calls = (
            fixture_logger_request_retry_calls(expected_response_list[0],
                                               Destination("host0", None)) +
            fixture_logger_request_retry_calls(
                expected_response_list[1], Destination("host1", None)) + [
                    mock.call.log_request_start(request),
                    mock.call.log_response(response),
                ])
        self.assertEqual(logger_calls, self.mock_com_log.mock_calls)
        # pylint: disable=no-member, protected-access
        com._multi_handle.assert_no_handle_left()
コード例 #3
0
    def test_failure(self, mock_create_handle, _, mock_con_successful,
                     mock_con_failure):
        expected_response_list = []

        def _con_failure(handle, errno, err_msg):
            response = lib.Response(handle, False, errno, err_msg)
            expected_response_list.append(response)
            return response

        mock_con_failure.side_effect = _con_failure
        com = self.get_multiaddress_communicator()
        mock_create_handle.side_effect = lambda request, _, __: MockCurl(
            error=(pycurl.E_SEND_ERROR, "reason"),
            request=request,
        )
        request = lib.Request(
            lib.RequestTarget(
                "label",
                dest_list=_addr_list_to_dest(
                    ["host{0}".format(i) for i in range(4)]),
            ), lib.RequestData("action"))
        com.add_requests([request])
        response_list = list(com.start_loop())
        self.assertEqual(1, len(response_list))
        response = response_list[0]
        self.assertFalse(response.was_connected)
        self.assertIs(request, response.request)
        self.assertEqual(Destination("host3", None), request.dest)
        self.assertEqual(4, mock_create_handle.call_count)
        mock_con_successful.assert_not_called()
        self.assertEqual(4, len(expected_response_list))
        mock_create_handle.assert_has_calls([
            mock.call(request, {}, settings.default_request_timeout)
            for _ in range(3)
        ])
        logger_calls = (
            fixture_logger_request_retry_calls(expected_response_list[0],
                                               Destination("host0", None)) +
            fixture_logger_request_retry_calls(expected_response_list[1],
                                               Destination("host1", None)) +
            fixture_logger_request_retry_calls(
                expected_response_list[2], Destination("host2", None)) + [
                    mock.call.log_request_start(request),
                    mock.call.log_response(response),
                    mock.call.log_no_more_addresses(response)
                ])
        self.assertEqual(logger_calls, self.mock_com_log.mock_calls)
        com._multi_handle.assert_no_handle_left()
コード例 #4
0
 def test_connection_failure(self):
     request = lib.Request(lib.RequestTarget("host"),
                           lib.RequestData("request"))
     output = "output"
     debug = "debug"
     errno = 1
     error_msg = "error"
     handle = self.fixture_handle({}, request, output, debug)
     response = lib.Response.connection_failure(handle, errno, error_msg)
     self.assertEqual(request, response.request)
     self.assertFalse(response.was_connected)
     self.assertEqual(errno, response.errno)
     self.assertEqual(error_msg, response.error_msg)
     self.assertEqual(output, response.data)
     self.assertEqual(debug, response.debug)
     self.assertIsNone(response.response_code)
コード例 #5
0
 def test_connection_successful(self):
     request = lib.Request(lib.RequestTarget("host"),
                           lib.RequestData("request"))
     output = "output"
     debug = "debug"
     response_code = 200
     handle = self.fixture_handle({pycurl.RESPONSE_CODE: 200}, request,
                                  output, debug)
     response = lib.Response.connection_successful(handle)
     self.assertEqual(request, response.request)
     self.assertTrue(response.was_connected)
     self.assertIsNone(response.errno)
     self.assertIsNone(response.error_msg)
     self.assertEqual(output, response.data)
     self.assertEqual(debug, response.debug)
     self.assertEqual(response_code, response.response_code)
コード例 #6
0
 def test_basic(self, mock_curl):
     mock_curl.return_value = MockCurl(None)
     request = lib.Request(lib.RequestTarget("label"),
                           lib.RequestData("action"))
     handle = lib._create_request_handle(request, {}, 10)
     expected_opts = {
         pycurl.TIMEOUT: 10,
         pycurl.URL: request.url.encode("utf-8"),
     }
     expected_opts.update(self._common_opts)
     self.assertLessEqual(set(expected_opts.items()),
                          set(handle.opts.items()))
     self.assertFalse(pycurl.COOKIE in handle.opts)
     self.assertFalse(pycurl.COPYPOSTFIELDS in handle.opts)
     self.assertIs(request, handle.request_obj)
     self.assertEqual("", handle.output_buffer.getvalue().decode("utf-8"))
     self.assertEqual("", handle.debug_buffer.getvalue().decode("utf-8"))
     handle.perform()
     self.assertEqual("", handle.output_buffer.getvalue().decode("utf-8"))
     self.assertEqual("", handle.debug_buffer.getvalue().decode("utf-8"))
コード例 #7
0
def fixture_request(host_id=1, action="action"):
    return lib.Request(
        lib.RequestTarget("host{0}".format(host_id)),
        lib.RequestData(action),
    )
コード例 #8
0
 def _get_request(token=None):
     return lib.Request(lib.RequestTarget("host", token=token),
                        lib.RequestData("action"))
コード例 #9
0
 def _get_request(self, target):
     return lib.Request(target, lib.RequestData(self.action))