1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
"""
pykube.http unittests
"""
import os
from pathlib import Path
from unittest import mock
import pytest
from pykube import __version__
from pykube.config import KubeConfig
from pykube.http import DEFAULT_HTTP_TIMEOUT
from pykube.http import HTTPClient
BASEDIR = Path("tests")
GOOD_CONFIG_FILE_PATH = BASEDIR / "test_config_with_context.yaml"
CONFIG_WITH_INSECURE_SKIP_TLS_VERIFY = (
BASEDIR / "test_config_with_insecure_skip_tls_verify.yaml"
)
CONFIG_WITH_OVERRIDES = BASEDIR / "test_config_overrides.yaml"
CONFIG_WITH_OIDC_AUTH = BASEDIR / "test_config_with_oidc_auth.yaml"
def test_http(monkeypatch):
cfg = KubeConfig.from_file(GOOD_CONFIG_FILE_PATH)
api = HTTPClient(cfg)
mock_send = mock.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()
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
def test_http_with_dry_run(monkeypatch):
cfg = KubeConfig.from_file(GOOD_CONFIG_FILE_PATH)
api = HTTPClient(cfg, dry_run=True)
mock_send = mock.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)
mock_send = mock.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 SSL is not verified
assert not mock_send.call_args[1]["verify"]
def test_http_override_certificate_authority(monkeypatch):
cfg = KubeConfig.from_file(CONFIG_WITH_OVERRIDES)
api = HTTPClient(cfg, dry_run=True)
k = mock.patch.dict(
os.environ, {"PYKUBE_SSL_CERTIFICATE_AUTHORITY": "/var/foo/bar/ca"}
)
k.start()
mock_send = mock.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 path to overwritten CA is used
assert mock_send.call_args[1]["verify"] == "/var/foo/bar/ca"
k.stop()
def test_http_do_not_overwrite_auth(monkeypatch):
cfg = KubeConfig.from_file(GOOD_CONFIG_FILE_PATH)
api = HTTPClient(cfg)
mock_send = mock.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", headers={"Authorization": "Bearer testtoken"})
mock_send.assert_called_once()
assert mock_send.call_args[0][0].headers["Authorization"] == "Bearer testtoken"
def test_http_with_oidc_auth_no_refresh(monkeypatch):
cfg = KubeConfig.from_file(CONFIG_WITH_OIDC_AUTH)
api = HTTPClient(cfg)
mock_send = mock.MagicMock()
mock_send.side_effect = Exception("MOCK HTTP")
monkeypatch.setattr("pykube.http.KubernetesHTTPAdapter._do_send", mock_send)
with mock.patch(
"pykube.http.KubernetesHTTPAdapter._is_valid_jwt", return_value=True
) as mock_jwt:
with pytest.raises(Exception):
api.get(url="test")
mock_jwt.assert_called_once_with("some-id-token")
mock_send.assert_called_once()
assert mock_send.call_args[0][0].headers["Authorization"] == "Bearer some-id-token"
def test_get_kwargs():
cfg = KubeConfig.from_file(GOOD_CONFIG_FILE_PATH)
api = HTTPClient(cfg)
assert api.get_kwargs(version="v1") == {
"timeout": 10,
"url": "http://localhost/api/v1/",
}
assert api.get_kwargs(version="/apis") == {
"timeout": 10,
"url": "http://localhost/apis/",
}
assert api.get_kwargs(version="storage.k8s.io/v1") == {
"timeout": 10,
"url": "http://localhost/apis/storage.k8s.io/v1/",
}
|