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

Commit ab9c27e

Browse files
lukemassahjacobs
authored andcommitted
Add flag to http client to allow for the client to be dryrun (#43)
1 parent 6ece171 commit ab9c27e

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

pykube/http.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ class HTTPClient:
160160
Client for interfacing with the Kubernetes API.
161161
"""
162162

163-
def __init__(self, config: KubeConfig, timeout: float = DEFAULT_HTTP_TIMEOUT):
163+
def __init__(self, config: KubeConfig, timeout: float = DEFAULT_HTTP_TIMEOUT, dry_run: bool = False):
164164
"""
165165
Creates a new instance of the HTTPClient.
166166
@@ -170,6 +170,7 @@ def __init__(self, config: KubeConfig, timeout: float = DEFAULT_HTTP_TIMEOUT):
170170
self.config = config
171171
self.timeout = timeout
172172
self.url = self.config.cluster["server"]
173+
self.dry_run = dry_run
173174

174175
session = requests.Session()
175176
session.headers['User-Agent'] = f'pykube-ng/{__version__}'
@@ -242,6 +243,11 @@ def get_kwargs(self, **kwargs) -> dict:
242243
if 'timeout' not in kwargs:
243244
# apply default HTTP timeout
244245
kwargs['timeout'] = self.timeout
246+
if self.dry_run:
247+
# Add http query param for dryRun
248+
params = kwargs.get('params', {})
249+
params['dryRun'] = 'All'
250+
kwargs['params'] = params
245251
return kwargs
246252

247253
def raise_for_status(self, resp):

tests/test_http.py

+16
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,22 @@ def test_http(monkeypatch):
3434
assert mock_send.call_args[1]['timeout'] == DEFAULT_HTTP_TIMEOUT
3535

3636

37+
def test_http_with_dry_run(monkeypatch):
38+
cfg = KubeConfig.from_file(GOOD_CONFIG_FILE_PATH)
39+
api = HTTPClient(cfg, dry_run=True)
40+
41+
mock_send = MagicMock()
42+
mock_send.side_effect = Exception('MOCK HTTP')
43+
monkeypatch.setattr('pykube.http.KubernetesHTTPAdapter._do_send', mock_send)
44+
45+
with pytest.raises(Exception):
46+
api.get(url='test')
47+
48+
mock_send.assert_called_once()
49+
# check that dry run http parameters were set
50+
assert mock_send.call_args[0][0].url == "http://localhost/api/v1/test?dryRun=All"
51+
52+
3753
def test_http_insecure_skip_tls_verify(monkeypatch):
3854
cfg = KubeConfig.from_file(CONFIG_WITH_INSECURE_SKIP_TLS_VERIFY)
3955
api = HTTPClient(cfg)

0 commit comments

Comments
 (0)