def test_retry_exception_str(self):
        self.assertEqual(
            str(MaxRetryError(HTTPConnectionPool(host='localhost'), "Test.", None)),
            "HTTPConnectionPool(host='localhost', port=None): Max retries exceeded with url: Test.")

        err = SocketError("Test")
        self.assertEqual(
            str(MaxRetryError(HTTPConnectionPool(host='localhost'), "Test.", err)),
            "HTTPConnectionPool(host='localhost', port=None): Max retries exceeded with url: Test. (caused by <class 'socket.error'>: Test)")
Exemple #2
0
    def test_write(self):
        with patch("etcd.Client", autospec=True) as mock:
            client = etcd_util.EtcdClient({})
            etcd_client = mock.return_value

            etcd_client.write.return_value = MagicMock(value="salt")
            self.assertEqual(client.write("/some-key", "salt"), "salt")
            etcd_client.write.assert_called_with(
                "/some-key", "salt", ttl=None, dir=False
            )

            self.assertEqual(client.write("/some-key", "salt", ttl=5), "salt")
            etcd_client.write.assert_called_with("/some-key", "salt", ttl=5, dir=False)

            etcd_client.write.return_value = MagicMock(dir=True)
            self.assertEqual(
                client.write("/some-dir", "salt", ttl=0, directory=True), True
            )
            etcd_client.write.assert_called_with("/some-dir", None, ttl=0, dir=True)

            # Check when a file is attempted to be written to a read-only root
            etcd_client.write.side_effect = etcd.EtcdRootReadOnly()
            self.assertEqual(client.write("/", "some-val", directory=False), None)

            # Check when a directory is attempted to be written to a read-only root
            etcd_client.write.side_effect = etcd.EtcdRootReadOnly()
            self.assertEqual(client.write("/", None, directory=True), None)

            # Check when a file is attempted to be written when unable to connect to the service
            etcd_client.write.side_effect = MaxRetryError(None, None)
            self.assertEqual(
                client.write("/some-key", "some-val", directory=False), None
            )

            # Check when a directory is attempted to be written when unable to connect to the service
            etcd_client.write.side_effect = MaxRetryError(None, None)
            self.assertEqual(client.write("/some-dir", None, directory=True), None)

            # Check when a file is attempted to be written to a directory that already exists (name-collision)
            etcd_client.write.side_effect = etcd.EtcdNotFile()
            self.assertEqual(
                client.write("/some-dir", "some-val", directory=False), None
            )

            # Check when a directory is attempted to be written to a file that already exists (name-collision)
            etcd_client.write.side_effect = etcd.EtcdNotDir()
            self.assertEqual(client.write("/some-key", None, directory=True), None)

            # Check when a directory is attempted to be written to a directory that already exists (update-ttl)
            etcd_client.write.side_effect = etcd.EtcdNotFile()
            self.assertEqual(client.write("/some-dir", None, directory=True), True)

            etcd_client.write.side_effect = ValueError
            self.assertEqual(client.write("/some-key", "some-val"), None)

            etcd_client.write.side_effect = Exception
            self.assertRaises(Exception, client.set, "some-key", "some-val")
    def increment(self,
                  method=None,
                  url=None,
                  response=None,
                  error=None,
                  _pool=None,
                  _stacktrace=None):
        """Return a new Retry object with incremented retry counters."""
        if self.retry_timeout < datetime.now():
            raise MaxRetryError(
                _pool, url, error or ResponseError("max_retry_time exceeded"))

        new_retry = super().increment(method, url, response, error, _pool,
                                      _stacktrace)

        if response is not None:
            parsed_error = InfluxDBError(response=response)
        elif error is not None:
            parsed_error = error
        else:
            parsed_error = f"Failed request to: {url}"

        message = f"The retriable error occurred during request. Reason: '{parsed_error}'."
        if isinstance(parsed_error, InfluxDBError):
            message += f" Retry in {parsed_error.retry_after}s."

        if self.retry_callback:
            self.retry_callback(parsed_error)

        logger.warning(message)

        return new_retry
Exemple #4
0
    def test_access_token_not_in_exception_traceback(self):
        """Check that access token is replaced within chained request exceptions."""
        backend_name = 'ibmq_qasm_simulator'
        backend = self.provider.get_backend(backend_name)
        circuit = transpile(self.qc1, backend, seed_transpiler=self.seed)
        qobj = assemble(circuit, backend, shots=1)
        client = backend._api_client

        exception_message = 'The access token in this exception ' \
                            'message should be replaced: {}'.format(self.access_token)
        exception_traceback_str = ''
        try:
            with mock.patch.object(HTTPConnectionPool,
                                   'urlopen',
                                   side_effect=MaxRetryError(
                                       HTTPConnectionPool('host'),
                                       'url',
                                       reason=exception_message)):
                _ = client.job_submit(backend.name(), qobj.to_dict())
        except RequestsApiError:
            exception_traceback_str = traceback.format_exc()

        self.assertTrue(exception_traceback_str)
        if self.access_token in exception_traceback_str:
            self.fail(
                'Access token not replaced in request exception traceback.')
Exemple #5
0
def wait_server_close(url: str, waiting_message: str, final_message: str):
    """
    Wait for a server to close

    :param url: the url to close the server
    :type url: str

    :param waiting_message: this message will pop up every minute
    :type waiting_message: str

    :param final_message: this message will pop up when the connection is down
    :type final_message: str

    .. info :: if you add `%d` in your messages you can include time count in it

    .. warning :: Raise a MaxRetryError if the server has not closed after 10s
    """
    count = 0
    server_started = False
    while not server_started:
        if count == 10:
            raise MaxRetryError(url, None)
        try:
            get(url)
            sleep(1)
            logger.info(waiting_message, count)
            count += 1
        except (RequestsConnectionError, MaxRetryError) as _:
            server_started = True
    logger.info(final_message, count)
Exemple #6
0
    def test_write(self):
        with patch('etcd.Client', autospec=True) as mock:
            client = etcd_util.EtcdClient({})
            etcd_client = mock.return_value

            etcd_client.write.return_value = MagicMock(value='salt')
            self.assertEqual(client.write('/some-key', 'salt'), 'salt')
            etcd_client.write.assert_called_with('/some-key', 'salt', ttl=None, dir=False)

            self.assertEqual(client.write('/some-key', 'salt', ttl=5), 'salt')
            etcd_client.write.assert_called_with('/some-key', 'salt', ttl=5, dir=False)

            etcd_client.write.return_value = MagicMock(dir=True)
            self.assertEqual(client.write('/some-dir', 'salt', ttl=0, directory=True), True)
            etcd_client.write.assert_called_with('/some-dir', None, ttl=0, dir=True)

            etcd_client.write.side_effect = etcd.EtcdRootReadOnly()
            self.assertEqual(client.write('/', 'some-val'), None)

            etcd_client.write.side_effect = etcd.EtcdNotFile()
            self.assertEqual(client.write('/some-key', 'some-val'), None)

            etcd_client.write.side_effect = etcd.EtcdNotDir()
            self.assertEqual(client.write('/some-dir', 'some-val'), None)

            etcd_client.write.side_effect = MaxRetryError(None, None)
            self.assertEqual(client.write('/some-key', 'some-val'), None)

            etcd_client.write.side_effect = ValueError
            self.assertEqual(client.write('/some-key', 'some-val'), None)

            etcd_client.write.side_effect = Exception
            self.assertRaises(Exception, client.set, 'some-key', 'some-val')
Exemple #7
0
    def test_read(self):
        '''
        Test to make sure we interact with etcd correctly
        '''
        with patch('etcd.Client', autospec=True) as mock:
            etcd_client = mock.return_value
            etcd_return = MagicMock(value='salt')
            etcd_client.read.return_value = etcd_return
            client = etcd_util.EtcdClient({})

            self.assertEqual(client.read('/salt'), etcd_return)
            etcd_client.read.assert_called_with('/salt', recursive=False, wait=False, timeout=None)

            client.read('salt', True, True, 10, 5)
            etcd_client.read.assert_called_with('salt', recursive=True, wait=True, timeout=10, waitIndex=5)

            etcd_client.read.side_effect = etcd.EtcdKeyNotFound
            self.assertRaises(etcd.EtcdKeyNotFound, client.read, 'salt')

            etcd_client.read.side_effect = etcd.EtcdConnectionFailed
            self.assertRaises(etcd.EtcdConnectionFailed, client.read, 'salt')

            etcd_client.read.side_effect = etcd.EtcdValueError
            self.assertRaises(etcd.EtcdValueError, client.read, 'salt')

            etcd_client.read.side_effect = ValueError
            self.assertRaises(ValueError, client.read, 'salt')

            etcd_client.read.side_effect = ReadTimeoutError(None, None, None)
            self.assertRaises(etcd.EtcdConnectionFailed, client.read, 'salt')

            etcd_client.read.side_effect = MaxRetryError(None, None)
            self.assertRaises(etcd.EtcdConnectionFailed, client.read, 'salt')
Exemple #8
0
 def test_dropbox_folder_list_handles_max_retry_error(self, mock_metadata):
     mock_response = mock.Mock()
     url = self.project.api_url_for('dropbox_folder_list', folder_id='/')
     mock_metadata.side_effect = MaxRetryError(mock_response, url)
     with mock.patch.object(type(self.node_settings), 'has_auth', True):
         res = self.app.get(url, auth=self.user.auth, expect_errors=True)
     assert_equal(res.status_code, http.REQUEST_TIMEOUT)
Exemple #9
0
def test_timeout():
    transport = Transport(urlparse.urlparse('http://localhost'))
    responses.add('POST', '/', status=202,
                  body=MaxRetryError(None, None, reason=TimeoutError()))
    with pytest.raises(TransportException) as exc_info:
        transport.send('x', {}, timeout=5)
    assert 'timeout' in str(exc_info.value)
Exemple #10
0
 def test_dropbox_hgrid_data_contents_handles_max_retry_error(
         self, mock_metadata):
     mock_response = mock.Mock()
     url = self.project.api_url_for('dropbox_hgrid_data_contents')
     mock_metadata.side_effect = MaxRetryError(mock_response, url)
     res = self.app.get(url, auth=self.user.auth, expect_errors=True)
     assert_equal(res.status_code, httplib.REQUEST_TIMEOUT)
    def test_retry_exception_str(self):
        assert (str(
            MaxRetryError(
                HTTPConnectionPool(host="localhost"), "Test.",
                None)) == "HTTPConnectionPool(host='localhost', port=None): "
                "Max retries exceeded with url: Test. (Caused by None)")

        err = SocketError("Test")

        # using err.__class__ here, as socket.error is an alias for OSError
        # since Py3.3 and gets printed as this
        assert (str(
            MaxRetryError(
                HTTPConnectionPool(host="localhost"), "Test.",
                err)) == "HTTPConnectionPool(host='localhost', port=None): "
                "Max retries exceeded with url: Test. "
                "(Caused by %r)" % err)
Exemple #12
0
    def test_retry_exception_str(self):
        self.assertEqual(
            str(MaxRetryError(
                HTTPConnectionPool(host='localhost'), "Test.", None)),
            "HTTPConnectionPool(host='localhost', port=None): "
            "Max retries exceeded with url: Test. (Caused by redirect)")

        err = SocketError("Test")

        # using err.__class__ here, as socket.error is an alias for OSError
        # since Py3.3 and gets printed as this
        self.assertEqual(
            str(MaxRetryError(
                HTTPConnectionPool(host='localhost'), "Test.", err)),
            "HTTPConnectionPool(host='localhost', port=None): "
            "Max retries exceeded with url: Test. "
            "(Caused by {0}: Test)".format(str(err.__class__)))
Exemple #13
0
class TestPickle(object):
    @pytest.mark.parametrize('exception', [
        HTTPError(None),
        MaxRetryError(None, None, None),
        LocationParseError(None),
        ConnectTimeoutError(None),
        HTTPError('foo'),
        HTTPError('foo', IOError('foo')),
        MaxRetryError(HTTPConnectionPool('localhost'), '/', None),
        LocationParseError('fake location'),
        ClosedPoolError(HTTPConnectionPool('localhost'), None),
        EmptyPoolError(HTTPConnectionPool('localhost'), None),
        ReadTimeoutError(HTTPConnectionPool('localhost'), '/', None),
    ])
    def test_exceptions(self, exception):
        result = pickle.loads(pickle.dumps(exception))
        assert isinstance(result, type(exception))
def test_timeout():
    transport = Transport("http://localhost", timeout=5)
    try:
        responses.add("POST", "/", status=202, body=MaxRetryError(None, None, reason=TimeoutError()))
        with pytest.raises(TransportException) as exc_info:
            transport.send("x")
        assert "timeout" in str(exc_info.value)
    finally:
        transport.close()
Exemple #15
0
def test_timeout(mock_urlopen):
    transport = Transport("http://localhost", timeout=5)
    mock_urlopen.side_effect = MaxRetryError(None, None, reason=TimeoutError())
    try:
        with pytest.raises(TransportException) as exc_info:
            transport.send("x")
        assert "timeout" in str(exc_info.value)
    finally:
        transport.close()
Exemple #16
0
 def test_do_request_exception_args_1(self):
     self.mock_requests(
     ).post.side_effect = requests.exceptions.ConnectionError(
         MaxRetryError('Abc', 'http://xxx.yyy', 'too many redirects'))
     with self.assertRaises(RequestException) as context:
         self.rest_client.do_request('POST', '/test')
         self.assertEqual(
             'UnitTest REST API cannot be reached. Please '
             'check your configuration and that the API '
             'endpoint is accessible', context.exception.message)
Exemple #17
0
 def test_retry_session(self, github_retry):
     urlpart1 = "https://uploads.github.com:443/repos/brave/brave-browser/"
     urlpart2 = "releases/17325413/assets?name=brave-browser-0.61.51-1.x86_64.rpm"
     github_retry.side_effect = MaxRetryError("retry_session_pool",
                                              urlpart1 + urlpart2,
                                              MaxRetryError)
     try:
         github_retry()
     except MaxRetryError as mre:
         print("Caught MaxRetryError: {}".format(mre))
Exemple #18
0
class TestPickle(object):
    @pytest.mark.parametrize(
        "exception",
        [
            HTTPError(None),
            MaxRetryError(None, None, None),
            LocationParseError(None),
            ConnectTimeoutError(None),
            HTTPError("foo"),
            HTTPError("foo", IOError("foo")),
            MaxRetryError(HTTPConnectionPool("localhost"), "/", None),
            LocationParseError("fake location"),
            ClosedPoolError(HTTPConnectionPool("localhost"), None),
            EmptyPoolError(HTTPConnectionPool("localhost"), None),
            ReadTimeoutError(HTTPConnectionPool("localhost"), "/", None),
        ],
    )
    def test_exceptions(self, exception):
        result = pickle.loads(pickle.dumps(exception))
        assert isinstance(result, type(exception))
Exemple #19
0
 def _issue_command(self, commands: str):
     fields = {'commands': commands.split('\n')}
     r = self._get_connection_pool().request(
         'POST',
         urljoin(self.url, 'api/printer/command'),
         headers=self._get_connection_headers(),
         body=json.dumps(fields).encode('utf-8'))
     if r.status == 204:
         return True
     else:
         raise MaxRetryError("Error sending command to instance")
Exemple #20
0
    def test_server(self, requests):
        """Ensure it's possible to fetch cookies from a server and errors are handled properly"""
        requests.return_value = requests
        requests.get.return_value = requests

        requests.cookies = {'session': self.encoded}
        stdout, stderr = self.call('--decode', '--server',
                                   'http://*****:*****@localhost:8080')

        self.assertEqual(stdout.read().strip(), str(self.decoded))

        for call in requests.mock_calls:
            if call[INDEX_FN_NAME] == 'get':
                if 'proxies' in call[INDEX_KWARGS]:
                    break

        else:
            raise AssertionError(
                'Didn\'t find "proxies" argument in call args.')

        error_reason = ProxyError()
        error_reason.args = ('Cannot connect to proxy',
                             OSError('Tunnel connection failed'))
        error = ProxyError(
            MaxRetryError(reason=error_reason,
                          pool=MagicMock(),
                          url='http://*****:*****@localhost:8080')

        self.assertIn('Tunnel connection failed', stderr.read().strip())
Exemple #21
0
 def test_do_request_exception_nested_args(self):
     self.mock_requests(
     ).delete.side_effect = requests.exceptions.ConnectionError(
         MaxRetryError('Xyz', 'https://foo.bar',
                       Exception('Foo: [Errno -42] bla bla bla')))
     with self.assertRaises(RequestException) as context:
         self.rest_client.do_request('DELETE', '/test')
         self.assertEqual(
             'UnitTest REST API cannot be reached: bla '
             'bla bla [errno -42]. Please check your '
             'configuration and that the API endpoint '
             'is accessible', context.exception.message)
Exemple #22
0
 def test_exceptions_with_objects(self):
     assert self.cycle(HTTPError('foo'))
     assert self.cycle(
         MaxRetryError(HTTPConnectionPool('localhost'), '/', None))
     assert self.cycle(LocationParseError('fake location'))
     assert self.cycle(
         ClosedPoolError(HTTPConnectionPool('localhost'), None))
     assert self.cycle(EmptyPoolError(HTTPConnectionPool('localhost'),
                                      None))
     assert self.cycle(
         HostChangedError(HTTPConnectionPool('localhost'), '/', None))
     assert self.cycle(
         ReadTimeoutError(HTTPConnectionPool('localhost'), '/', None))
Exemple #23
0
    def test_wait_server_start(self, mock_get):
        """
        Tests wait_server_start
        """
        mock_get.side_effect = MaxRetryError(None, "http://false")
        with self.assertRaises(MaxRetryError):
            with self.assertLogs() as logs:
                wait_server_start("http://false", "Wait for: %ds", "")
        self.assertEqual(
            logs.output,
            [
                "INFO:root:Wait for: 0s",
                "INFO:root:Wait for: 1s",
                "INFO:root:Wait for: 2s",
                "INFO:root:Wait for: 3s",
                "INFO:root:Wait for: 4s",
                "INFO:root:Wait for: 5s",
                "INFO:root:Wait for: 6s",
                "INFO:root:Wait for: 7s",
                "INFO:root:Wait for: 8s",
                "INFO:root:Wait for: 9s",
            ],
        )

        mock_response = mock.Mock()
        mock_response.status_code.return_value = 200
        mock_get.side_effect = [
            MaxRetryError(None, "http://false"), mock_response
        ]
        with self.assertLogs() as logs:
            wait_server_start("http://false", "Wait for: %ds", "Awaited: %ds")
        self.assertEqual(logs.output,
                         ["INFO:root:Wait for: 0s", "INFO:root:Awaited: 1s"])

        mock_get.side_effect = mock_response
        with self.assertLogs() as logs:
            wait_server_start("http://false", "Wait for: %ds", "Awaited: %ds")
        self.assertEqual(logs.output, ["INFO:root:Awaited: 0s"])
def test_timeout(mock_urlopen, elasticapm_client):
    elasticapm_client.server_version = (8, 0
                                        )  # avoid making server_info request
    transport = Transport("http://localhost",
                          timeout=5,
                          client=elasticapm_client)
    transport.start_thread()
    mock_urlopen.side_effect = MaxRetryError(None, None, reason=TimeoutError())
    try:
        with pytest.raises(TransportException) as exc_info:
            transport.send("x")
        assert "timeout" in str(exc_info.value)
    finally:
        transport.close()
Exemple #25
0
 def increment(self,
               method=None,
               url=None,
               response=None,
               error=None,
               _pool=None,
               _stacktrace=None):
     if response:
         mashery_error = response.getheader("X-Mashery-Error-Code")
         if mashery_error == 'ERR_403_DEVELOPER_OVER_QPD':
             raise MaxRetryError(_pool, url, error)
     return super(WhispirRetry, self).increment(method=method,
                                                url=url,
                                                response=response,
                                                error=error,
                                                _pool=_pool,
                                                _stacktrace=_stacktrace)
def bad_minio_endpoint(mocker):
    'Simulate the Minio end point not being there at all'
    response1 = mocker.MagicMock()
    response1.data = '<xml></xml>'

    minio_client = mocker.MagicMock(spec=minio.Minio)
    pool = mocker.MagicMock()
    pool.host = 'http://dude'
    pool.port = 'fork-me'
    minio_client.list_objects.side_effect = MaxRetryError(pool, 'http://dude')

    mocker.patch('servicex.minio_adaptor.Minio', return_value=minio_client)

    p_rename = mocker.patch('servicex.minio_adaptor.Path.rename',
                            mocker.MagicMock())
    mocker.patch('servicex.minio_adaptor.Path.mkdir', mocker.MagicMock())

    return p_rename, minio_client
Exemple #27
0
def test_read():
    """
    Test to make sure we interact with etcd correctly
    """
    with patch("etcd.Client", autospec=True) as mock:
        etcd_client = mock.return_value
        etcd_return = MagicMock(value="salt")
        etcd_client.read.return_value = etcd_return
        client = etcd_util.EtcdClient({})

        assert client.read("/salt") == etcd_return
        etcd_client.read.assert_called_with(
            "/salt", recursive=False, wait=False, timeout=None
        )

        client.read("salt", True, True, 10, 5)
        etcd_client.read.assert_called_with(
            "salt", recursive=True, wait=True, timeout=10, waitIndex=5
        )

        etcd_client.read.side_effect = etcd.EtcdKeyNotFound
        with pytest.raises(etcd.EtcdKeyNotFound):
            client.read("salt")

        etcd_client.read.side_effect = etcd.EtcdConnectionFailed
        with pytest.raises(etcd.EtcdConnectionFailed):
            client.read("salt")

        etcd_client.read.side_effect = etcd.EtcdValueError
        with pytest.raises(etcd.EtcdValueError):
            client.read("salt")

        etcd_client.read.side_effect = ValueError
        with pytest.raises(ValueError):
            client.read("salt")

        etcd_client.read.side_effect = ReadTimeoutError(None, None, None)
        with pytest.raises(etcd.EtcdConnectionFailed):
            client.read("salt")

        etcd_client.read.side_effect = MaxRetryError(None, None)
        with pytest.raises(etcd.EtcdConnectionFailed):
            client.read("salt")
Exemple #28
0
    def _print_file(self, file: File):
        # Accepts Django File or ContentFile class
        file_name = ''.join(
            random.choices(string.ascii_uppercase + string.digits, k=10)
        ) + '.gcode' if file.name is None else file.name
        with file.open('r') as f:
            # We add a M400 command at the end of the file, so, we avoid problems due marlin gcode cache
            file_content = f.read() + '\nM400 \nM115'
            r = json.loads(self._get_connection_pool().request(
                'POST',
                urljoin(self.url, 'api/files/local'),
                headers=self._get_connection_headers(json_content=False),
                fields={
                    'print': True,
                    'file': (file_name, file_content)
                }).data.decode('utf-8'))

        if r.get('done'):
            return file_name
        else:
            raise MaxRetryError("Error sending command to instance")
Exemple #29
0
    def test_watch(self):
        with patch('etcd.Client', autospec=True) as client_mock:
            client = etcd_util.EtcdClient({})

            with patch.object(client, 'read', autospec=True) as mock:
                mock.return_value = MagicMock(value='stack', key='/some-key', modifiedIndex=1, dir=False)
                self.assertDictEqual(client.watch('/some-key'),
                                     {'value': 'stack', 'key': '/some-key', 'mIndex': 1, 'changed': True, 'dir': False})
                mock.assert_called_with('/some-key', wait=True, recursive=False, timeout=0, waitIndex=None)

                mock.side_effect = iter([etcd_util.EtcdUtilWatchTimeout, mock.return_value])
                self.assertDictEqual(client.watch('/some-key'),
                                     {'value': 'stack', 'changed': False, 'mIndex': 1, 'key': '/some-key', 'dir': False})

                mock.side_effect = iter([etcd_util.EtcdUtilWatchTimeout, etcd.EtcdKeyNotFound])
                self.assertEqual(client.watch('/some-key'),
                                 {'value': None, 'changed': False, 'mIndex': 0, 'key': '/some-key', 'dir': False})

                mock.side_effect = iter([etcd_util.EtcdUtilWatchTimeout, ValueError])
                self.assertEqual(client.watch('/some-key'), {})

                mock.side_effect = None
                mock.return_value = MagicMock(value='stack', key='/some-key', modifiedIndex=1, dir=True)
                self.assertDictEqual(client.watch('/some-dir', recurse=True, timeout=5, index=10),
                                     {'value': 'stack', 'key': '/some-key', 'mIndex': 1, 'changed': True, 'dir': True})
                mock.assert_called_with('/some-dir', wait=True, recursive=True, timeout=5, waitIndex=10)

                # iter(list(Exception)) works correctly with both mock<1.1 and mock>=1.1
                mock.side_effect = iter([MaxRetryError(None, None)])
                self.assertEqual(client.watch('/some-key'), {})

                mock.side_effect = iter([etcd.EtcdConnectionFailed()])
                self.assertEqual(client.watch('/some-key'), {})

                mock.side_effect = None
                mock.return_value = None
                self.assertEqual(client.watch('/some-key'), {})
Exemple #30
0
    def urlopen(self, method, url, redirect=True, **kw):
        """
        Same as :meth:`urllib3.connectionpool.HTTPConnectionPool.urlopen`
        with custom cross-host redirect logic and only sends the request-uri
        portion of the ``url``.

        The given ``url`` parameter must be absolute, such that an appropriate
        :class:`urllib3.connectionpool.ConnectionPool` can be chosen for it.
        """
        #===============================================================================================================
        # add by mz
        error_type = kw.get('error_type')

        if error_type:

            from urllib3.exceptions import LocationValueError, HostChangedError, LocationParseError, ConnectTimeoutError
            from urllib3.exceptions import ProxyError, TimeoutError, ReadTimeoutError, ProtocolError, DecodeError
            from urllib3.exceptions import ResponseError, ResponseNotChunked, SSLError, HTTPError, HTTPWarning, PoolError
            from urllib3.exceptions import RequestError, MaxRetryError, TimeoutStateError, NewConnectionError
            from urllib3.exceptions import EmptyPoolError, ClosedPoolError, SecurityWarning, SubjectAltNameWarning
            from urllib3.exceptions import InsecureRequestWarning, SystemTimeWarning, InsecurePlatformWarning
            from urllib3.exceptions import SNIMissingWarning, DependencyWarning, ProxySchemeUnknown, HeaderParsingError
            get_error = {
                "LocationValueError":
                LocationValueError(),
                "HostChangedError":
                HostChangedError(pool=1, url=2),
                "LocationParseError":
                LocationParseError(url),
                "ConnectTimeoutError":
                ConnectTimeoutError(),
                "ProxyError":
                ProxyError(),
                "TimeoutError":
                TimeoutError(),
                "ReadTimeoutError":
                ReadTimeoutError(pool=1, url=2, message="ReadTimeoutError"),
                "ProtocolError":
                ProtocolError(),
                "DecodeError":
                DecodeError(),
                "ResponseError":
                ResponseError(),
                "ResponseNotChunked":
                ResponseNotChunked(),
                "SSLError":
                SSLError(),
                "HTTPError":
                HTTPError(),
                "HTTPWarning":
                HTTPWarning(),
                "PoolError":
                PoolError(pool=1, message=2),
                "RequestError":
                RequestError(pool=1, url=2, message="RequestError"),
                "MaxRetryError":
                MaxRetryError(pool=1, url=2, reason=None),
                "TimeoutStateError":
                TimeoutStateError(),
                "NewConnectionError":
                NewConnectionError(pool=1, message="NewConnectionError"),
                "EmptyPoolError":
                EmptyPoolError(pool=1, message="EmptyPoolError"),
                "ClosedPoolError":
                ClosedPoolError(pool=1, message="ClosedPoolError"),
                "SecurityWarning":
                SecurityWarning(),
                "SubjectAltNameWarning":
                SubjectAltNameWarning(),
                "InsecureRequestWarning":
                InsecureRequestWarning(),
                "SystemTimeWarning":
                SystemTimeWarning(),
                "InsecurePlatformWarning":
                InsecurePlatformWarning(),
                "SNIMissingWarning":
                SNIMissingWarning(),
                "DependencyWarning":
                DependencyWarning(),
                "ProxySchemeUnknown":
                ProxySchemeUnknown(scheme=1),
                "HeaderParsingError":
                HeaderParsingError(defects=1, unparsed_data=2)
            }
            error_ = get_error[error_type]
            raise error_
        #===============================================================================================================

        u = parse_url(url)
        conn = self.connection_from_host(u.host, port=u.port, scheme=u.scheme)

        kw['assert_same_host'] = False
        kw['redirect'] = False
        if 'headers' not in kw:
            kw['headers'] = self.headers

        if self.proxy is not None and u.scheme == "http":
            response = conn.urlopen(method, url, **kw)
        else:
            response = conn.urlopen(method, u.request_uri, **kw)

        redirect_location = redirect and response.get_redirect_location()
        if not redirect_location:
            return response

        # Support relative URLs for redirecting.
        redirect_location = urljoin(url, redirect_location)

        # RFC 2616, Section 10.3.4
        if response.status == 303:
            method = 'GET'

        log.info("Redirecting %s -> %s" % (url, redirect_location))
        kw['retries'] = kw.get('retries', 3) - 1  # Persist retries countdown
        kw['redirect'] = redirect
        return self.urlopen(method, redirect_location, **kw)