Exemplo n.º 1
0
    def _fetch_operator_csv_modifications(self):
        """Fetch operator CSV modifications"""

        if not self.operator_csv_modifications_url:
            return None

        session = get_retrying_requests_session()

        self.log.info(
            "Fetching operator CSV modifications data from %s",
            self.operator_csv_modifications_url
        )
        resp = session.get(self.operator_csv_modifications_url)
        try:
            resp.raise_for_status()
        except Exception as exc:
            raise RuntimeError(
                f"Failed to fetch the operator CSV modification JSON "
                f"from {self.operator_csv_modifications_url}: {exc}"
            ) from exc

        try:
            csv_modifications = resp.json()
        except Exception as exc:
            # catching raw Exception because requests uses various json decoders
            # in different versions
            raise RuntimeError(
                f"Failed to parse operator CSV modification JSON "
                f"from {self.operator_csv_modifications_url}: {exc}"
            ) from exc

        self.log.info("Operator CSV modifications: %s", csv_modifications)

        self._validate_operator_csv_modifications(csv_modifications)
        return csv_modifications
Exemplo n.º 2
0
    def __init__(self,
                 username=None,
                 password=None,
                 verify=True,
                 access=None,
                 auth_b64=None):
        """Initialize HTTPBearerAuth object.

        :param username: str, username to be used for authentication
        :param password: str, password to be used for authentication
        :param verify: bool, whether or not to verify server identity when
            fetching Bearer token from realm
        :param access: iter<str>, iterable (list, tuple, etc) of access to be
            requested; possible values to be included are 'pull' and/or 'push';
            defaults to ('pull',)
        :param auth_b64: str, base64 credendials as described in RFC 7617
        """
        self.username = username
        self.password = password
        self.auth_b64 = auth_b64
        self.verify = verify
        self.access = access or ('pull', )

        self._token_cache = {}
        self._retry_session = get_retrying_requests_session(
        )  # Used when querying for token
Exemplo n.º 3
0
def test_get_retrying_requests_session(times):
    """
    Test that retries are set properly for http(s):// adapters

    Most arguments are simply passed to Retry.__init__, test only basic functionality
    """
    if times is not None:
        session = retries.get_retrying_requests_session(times=times)
    else:
        session = retries.get_retrying_requests_session()

    http = session.adapters['http://']
    https = session.adapters['https://']

    assert isinstance(http.max_retries, Retry)
    assert isinstance(https.max_retries, Retry)

    expected_total = times if times is not None else HTTP_MAX_RETRIES
    assert http.max_retries.total == expected_total
    assert https.max_retries.total == expected_total
Exemplo n.º 4
0
def test_log_error_response(http_code, caplog):
    api_url = 'https://localhost/api/v1/foo'
    json_data = {'message': 'value error'}
    responses.add(responses.GET, api_url, json=json_data, status=http_code)

    session = retries.get_retrying_requests_session()
    session.get(api_url)

    content = json.dumps(json_data).encode()
    expected = f"Error response from {api_url}: {content}"
    if 400 <= http_code <= 599:
        assert expected in caplog.text
    else:
        assert expected not in caplog.text