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

Commit

Permalink
Add flag to http client to allow for the client to be dryrun
Browse files Browse the repository at this point in the history
  • Loading branch information
lukemassa committed Sep 6, 2019
1 parent 58f407f commit 5bdaf49
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
8 changes: 7 additions & 1 deletion pykube/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ class HTTPClient:
Client for interfacing with the Kubernetes API.
"""

def __init__(self, config: KubeConfig, timeout: float = DEFAULT_HTTP_TIMEOUT):
def __init__(self, config: KubeConfig, timeout: float = DEFAULT_HTTP_TIMEOUT, dry_run: bool = False):
"""
Creates a new instance of the HTTPClient.
Expand All @@ -170,6 +170,7 @@ def __init__(self, config: KubeConfig, timeout: float = DEFAULT_HTTP_TIMEOUT):
self.config = config
self.timeout = timeout
self.url = self.config.cluster["server"]
self.dry_run = dry_run

session = requests.Session()
session.headers['User-Agent'] = f'pykube-ng/{__version__}'
Expand Down Expand Up @@ -242,6 +243,11 @@ def get_kwargs(self, **kwargs) -> dict:
if 'timeout' not in kwargs:
# apply default HTTP timeout
kwargs['timeout'] = self.timeout
if self.dry_run:
# Add http query param for dryRun
params = kwargs.get('params', {})
params['dryRun'] = 'All'
kwargs['params'] = params
return kwargs

def raise_for_status(self, resp):
Expand Down
16 changes: 16 additions & 0 deletions tests/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,22 @@ def test_http(monkeypatch):
assert mock_send.call_args[1]['timeout'] == DEFAULT_HTTP_TIMEOUT


def test_http_with_dryrun(monkeypatch):
cfg = KubeConfig.from_file(GOOD_CONFIG_FILE_PATH)
api = HTTPClient(cfg, dry_run=True)

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

with pytest.raises(Exception):
api.get(url='test')

mock_send.assert_called_once()
# check that dry run http parameters were set
assert mock_send.call_args[0][0].url == "http://localhost/api/v1/test?dryRun=All"


def test_http_insecure_skip_tls_verify(monkeypatch):
cfg = KubeConfig.from_file(CONFIG_WITH_INSECURE_SKIP_TLS_VERIFY)
api = HTTPClient(cfg)
Expand Down

0 comments on commit 5bdaf49

Please sign in to comment.