Exemplo n.º 1
0
async def test_async_setup_raises_entry_not_ready(hass, canary):
    """Test that it throws ConfigEntryNotReady when exception occurs during setup."""
    canary.side_effect = ConnectTimeout()

    entry = await init_integration(hass)
    assert entry
    assert entry.state == ENTRY_STATE_SETUP_RETRY
Exemplo n.º 2
0
    def test_okta_connection_timeout(
            self,
            mock_print_tty,
            mock_makedirs,
            mock_open,
            mock_chmod
    ):
        responses.add(
            responses.POST,
            'https://organization.okta.com/api/v1/authn',
            body=ConnectTimeout()
        )

        with self.assertRaises(SystemExit):
            Okta(
                user_name="user_name",
                user_pass="******",
                organization="organization.okta.com"
            )

        print_tty_calls = [
            call("Error: Timed Out")
        ]

        mock_print_tty.assert_has_calls(print_tty_calls)
Exemplo n.º 3
0
def test_get_bib_record_connection_issues(mock_get):
    mock_get.side_effect = ConnectTimeout()
    assert_equal(get_bib_record("placeholder_mmsid"),
                 {"error": "Alma Connection Error - try again later."})
    mock_get.side_effect = ConnectionError()
    assert_equal(get_bib_record("placeholder_mmsid"),
                 {"error": "Alma Connection Error - try again later."})
Exemplo n.º 4
0
async def test_user_form_cannot_connect(hass, canary_config_flow):
    """Test we handle errors that should trigger the cannot connect error."""
    canary_config_flow.side_effect = HTTPError()

    result = await hass.config_entries.flow.async_init(
        DOMAIN, context={"source": SOURCE_USER}
    )

    result = await hass.config_entries.flow.async_configure(
        result["flow_id"],
        USER_INPUT,
    )

    assert result["type"] == RESULT_TYPE_FORM
    assert result["errors"] == {"base": "cannot_connect"}

    canary_config_flow.side_effect = ConnectTimeout()

    result = await hass.config_entries.flow.async_configure(
        result["flow_id"],
        USER_INPUT,
    )

    assert result["type"] == RESULT_TYPE_FORM
    assert result["errors"] == {"base": "cannot_connect"}
Exemplo n.º 5
0
def stub_timeout_then_response(resource_fixture):
    url_pattern = url_pattern_for(resource_fixture)
    json_body = json.dumps(resource_fixture['body'])
    with responses.RequestsMock(assert_all_requests_are_fired=True) as rsps:
      rsps.add(resource_fixture['method'], url_pattern, body=ConnectTimeout())
      rsps.add(resource_fixture['method'], url_pattern, body=json_body)
      yield rsps
Exemplo n.º 6
0
 def raise_multiple_exceptions():
     counter['i'] += 1
     if counter['i'] == 1:
         raise ConnectionError('one error')
     elif counter['i'] == 2:
         raise ConnectTimeout('another error')
     else:
         return 'success'
Exemplo n.º 7
0
def get_data(url):
    """ Получаем боксы с едой. """
    try:
        data = loads(get(url).content)
    except ConnectTimeout:
        raise ConnectTimeout()
    except JSONDecodeError:
        raise JSONDecodeError
    except TypeError:
        raise TypeError()

    return data
Exemplo n.º 8
0
def test_validate_bank_account_exception(session):
    """Test create_account."""
    input_bank_details = {
        'bankInstitutionNumber': 111,
        'bankTransitNumber': 222,
        'bankAccountNumber': 33333333
    }
    with patch('pay_api.services.oauth_service.requests.post', side_effect=ConnectTimeout('mocked error')):
        # Configure the mock to return a response with an OK status code.
        bank_details = cfs_service.validate_bank_account(input_bank_details)
        assert bank_details.get('status_code') == 503
        assert 'mocked error' in bank_details.get('message')
Exemplo n.º 9
0
    def _get(self, url: str) -> List[Dict]:
        """
        Internal method to fetch data from target API and return json parsed. Returns error if get requests' status
        code is != 200

        :param url: send request to this url
        :return: list of raw data in python dicts
        """

        try:
            response = get(url, timeout=self._timeout)
            if response.status_code != 200:
                raise ValueError(
                    f'Error fetching data: {response.status_code} - {str(response.content or "")}'
                )
            return json.loads(response.content)
        except ConnectTimeout:
            raise ConnectTimeout('Error fetching data: Connection timeout')
        except ConnectionError as e:
            raise ConnectionError(
                f'Error fetching data: Unexpected connection error: {str(e)}')
Exemplo n.º 10
0
    def get_image(self):
        """
        helps in getting random htmls of memes pages and extracting random image from it
        :return:
        """
        try:
            # process = psutil.Process(os.getpid())
            # process.wait(timeout=timeout)
            response = requests.get('https://c.xkcd.com/random/comic/',
                                    timeout=timeout)
            tree = html.fromstring(response.content)
            image_src = tree.xpath('//*[@id="comic"]/img/@src')
            image_name = image_src[0].split('/')[-1]
            image_url = 'http:{}'.format(image_src[0])
            image_response = requests.get(image_url)

            if image_response.status_code in [200]:
                image_content = image_response.content
                Image().store_image(image_name, image_content)
            elif self.retry <= 3:
                self.retry += 1
                logging.info('retrying {} times'.format(self.retry))
                self.get_image()
        # except psutil.TimeoutExpired:
        #     logging.error('killing process due to timeout')
        #     process.kill()
        #     raise RuntimeError('Timeout')
        except ConnectTimeout:
            raise ConnectTimeout('Timeout exceeded')
        except ReadTimeout:
            raise ReadTimeout(' timeout error')
        except Exception as err:
            logging.error(
                'image downloading failed after {} retry with error: {}'.
                format(self.retry, str(err)))
            traceback.print_tb(err.__traceback__)
def test_retry_connection_timeout(azure):
    __test_retry_error(azure, 200, 2, body=ConnectTimeout())
Exemplo n.º 12
0
def get_response(URL,
                 method="get",
                 verify=True,
                 cert=None,
                 params=None,
                 data=None,
                 json=None,
                 headers=None,
                 delay=1,
                 max_attempts=8):
    """
    Make a RESTful API request with exponential backoff, and return
    the response.  This is a wrapper for the :mod:`requests` package.

    :param URL:  the URL of the service to be called
    :param method:  the HTML method for making the request
    :param verify:  a flag indicating whether or not to verify the server's
        certificate.  This flag is only meaningful for SSL
        connections, and is necessary in development environments with
        servers using self-signed certificates.
    :param cert:  the path(s) to a client-side certificate and/or key.  If
        separate certificate and key are to be passed, this should be a
        tuple of the two paths.
    :param params:  the parameters of the request
    :param data:  the data payload of the request.  A payload submitted
        using this object will be transferred to the API server as a
        series of values joined by "&".
    :param json:  a data payload in JSON form, or a PyThon object to be
        JSON-encoded
    :param headers:  the headers of the request
    :param delay:  the delay for the first retry.  Later retries
        increase the delay by factors of 2.
    :param max_attempts:  the maximum number of times to attempt
        the request

    :returns:  whatever response is returned for the request, as a
        :class:`requests.Response` object

    """

    # Set the verify flag for the sessions.
    session.verify = verify

    # If necessary, add a client-side certificate and/or key to the
    # session.
    session.cert = cert

    # Create a dict of keyword arguments (this allows optional arguments
    # to be omitted entirely).  We encode any "data" block in UTF-8
    # because requests can choke when attempting to send Unicode.
    req_kwargs = {}
    status = None
    for kwarg_key in ("params", "data", "json", "headers"):
        kwarg_value = locals()[kwarg_key]
        if kwarg_value is not None:
            if kwarg_key == "data":
                req_kwargs[kwarg_key] = kwarg_value.encode("utf-8")
            else:
                req_kwargs[kwarg_key] = kwarg_value

    # Create request objects.  Doing it this way, rather than with
    # "requests.request" (or ".get" or ".post"), lets us pass the request
    # object as an argument when raising a ConnectTimeout.  Using a
    # persistent session object (which is required for this approach)
    # should also have peformance advantages.
    request = Request(method=method, url=URL, **req_kwargs)
    prepped_req = request.prepare()

    #    # Diagnostic code for printing the raw request.
    #    stdout.write('\n{}\n{}\n\n{}\n'.format(
    #                 prepped_req.method + ' ' + prepped_req.url,
    #                 '\n'.join('{}:  {}'.format(k, v)
    #                 for k, v in prepped_req.headers.items()),
    #                 'Request body:  ' + str(prepped_req.body)))
    #    stdout.flush()

    # Call the API, using exponential backoff as necessary.

    for attempt in xrange(max_attempts):

        try:

            response = session.send(prepped_req)
            status = response.status_code

            if 200 <= status < 300:
                return response

            # If this is a transient error, we want to retry.
            elif status in [502, 503, 504]:
                try:
                    response.raise_for_status()
                except Exception as err:
                    logger.warn('Server at ' + URL + ' returned status code ' +
                                '"' + str(status) + '" with the following ' +
                                'error message:\n' + str(err) +
                                '\nRetrying....')
                    stderr.flush()

            # In some cases, retrying won't do us any good.
            else:
                logger.warn("Received the following error message from " +
                            "server at " + URL + ":\n" + response.text)
                stderr.flush()
                response.raise_for_status()

        except (ConnectionError, Timeout) as err:
            logger.warn("Attempt to connect to " + URL + " failed with the " +
                        "following error message:\n" + str(err) +
                        "\nRetrying....")
            stderr.flush()

        fuzz = randint(1, 1000) / 1000
        sleep(delay + fuzz)
        delay = 2 * delay

    # If no valid response is received, raise an error, with the status
    # code (if any) of the last response as the error message.
    if status:
        status_code = "status code " + str(status)
    else:
        status_code = "no response from server"
    err_msg = "Unable to complete request to " + URL + ":  " + status_code + "."
    raise ConnectTimeout(err_msg, request=request)
Exemplo n.º 13
0
def test_validate_target_timeout(get_mock):
    get_mock.side_effect = ConnectTimeout()
    with pytest.raises(ConnectionTimeout):
        useless_func_validate_target(url='http://test')
Exemplo n.º 14
0
          status=200), Rule('http://test.com', 1),
     CheckResult(datetime(2020, 11, 19), 'http://test.com', 10, 200, None)),
    (dict(method=responses.GET, url='https://test.com',
          status=200), Rule('https://test.com', 1),
     CheckResult(datetime(2020, 11, 19), 'http://test.com', 10, 200, None)),
    (dict(method=responses.HEAD, url='http://test.com',
          status=200), Rule('http://test.com', 1, 'HEAD'),
     CheckResult(datetime(2020, 11, 19), 'http://test.com', 10, 200, None)),
    (dict(method=responses.GET, url='http://test.com',
          status=404), Rule('http://test.com', 1),
     CheckResult(datetime(2020, 11, 19), 'http://test.com', 10, 404, None)),
    (dict(method=responses.GET, url='http://test.com',
          status=404), Rule('http://test.com', 1),
     CheckResult(datetime(2020, 11, 19), 'http://test.com', 10, 404, None)),
    (dict(method=responses.GET, url='http://test.com',
          body=ConnectTimeout()), Rule('http://test.com', 1),
     CheckResult(datetime(2020, 11, 19), 'http://test.com', failed=True)),
]


@responses.activate
@pytest.mark.parametrize("responses_add, rule, expected_result",
                         test_check_data)
def test_check(responses_add, rule, expected_result):
    responses.add(**responses_add)

    with mock.patch('monitoring.checker.utcnow',
                    return_value=datetime(2020, 11, 19)):
        result = check(rule, 20)
    assert result.failed == expected_result.failed
    assert result.status_code == expected_result.status_code