Skip to content
This repository has been archived by the owner on Oct 3, 2020. It is now read-only.

Commit

Permalink
Add HTTP timeout (default: 10s) (#10)
Browse files Browse the repository at this point in the history
* #7 add HTTP timeout (default: 10s)

* test that default HTTP timeout was set
  • Loading branch information
hjacobs committed Mar 3, 2019
1 parent f838a61 commit c4962c9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
14 changes: 9 additions & 5 deletions pykube/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import datetime
import json
import posixpath
import re
import shlex
import subprocess

Expand All @@ -23,10 +22,11 @@

from .exceptions import HTTPError
from .utils import jsonpath_installed, jsonpath_parse
from .config import KubeConfig

from . import __version__

_ipv4_re = re.compile(r"^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$")
DEFAULT_HTTP_TIMEOUT = 10 # seconds


class KubernetesHTTPAdapter(requests.adapters.HTTPAdapter):
Expand All @@ -35,7 +35,7 @@ class KubernetesHTTPAdapter(requests.adapters.HTTPAdapter):
# it can be overwritten in unit tests to mock the actual HTTP calls
_do_send = requests.adapters.HTTPAdapter.send

def __init__(self, kube_config, **kwargs):
def __init__(self, kube_config: KubeConfig, **kwargs):
self.kube_config = kube_config

super().__init__(**kwargs)
Expand Down Expand Up @@ -150,14 +150,15 @@ class HTTPClient(object):
Client for interfacing with the Kubernetes API.
"""

def __init__(self, config):
def __init__(self, config: KubeConfig, timeout: float = DEFAULT_HTTP_TIMEOUT):
"""
Creates a new instance of the HTTPClient.
:Parameters:
- `config`: The configuration instance
"""
self.config = config
self.timeout = timeout
self.url = self.config.cluster["server"]

session = requests.Session()
Expand Down Expand Up @@ -193,7 +194,7 @@ def resource_list(self, api_version):
setattr(self, cached_attr, r.json())
return getattr(self, cached_attr)

def get_kwargs(self, **kwargs):
def get_kwargs(self, **kwargs) -> dict:
"""
Creates a full URL to request based on arguments.
Expand Down Expand Up @@ -228,6 +229,9 @@ def get_kwargs(self, **kwargs):
url = url[1:]
bits.append(url)
kwargs["url"] = self.url + posixpath.join(*bits)
if 'timeout' not in kwargs:
# apply default HTTP timeout
kwargs['timeout'] = self.timeout
return kwargs

def raise_for_status(self, resp):
Expand Down
8 changes: 5 additions & 3 deletions tests/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,25 @@
from unittest.mock import MagicMock

from pykube import __version__
from pykube.http import HTTPClient
from pykube.http import HTTPClient, DEFAULT_HTTP_TIMEOUT
from pykube.config import KubeConfig

GOOD_CONFIG_FILE_PATH = os.path.sep.join(["tests", "test_config_with_context.yaml"])


def test_http(monkeypatch):
cfg = KubeConfig.from_file(GOOD_CONFIG_FILE_PATH)
session = HTTPClient(cfg).session
api = HTTPClient(cfg)

mock_send = MagicMock()
mock_send.side_effect = Exception('MOCK HTTP')
monkeypatch.setattr('pykube.http.KubernetesHTTPAdapter._do_send', mock_send)

with pytest.raises(Exception):
session.get('http://localhost:9090/test')
api.get(url='test')

mock_send.assert_called_once()
assert mock_send.call_args[0][0].headers['Authorization'] == 'Basic YWRtOnNvbWVwYXNzd29yZA=='
assert mock_send.call_args[0][0].headers['User-Agent'] == f'pykube-ng/{__version__}'
# check that the default HTTP timeout was set
assert mock_send.call_args[1]['timeout'] == DEFAULT_HTTP_TIMEOUT

0 comments on commit c4962c9

Please sign in to comment.