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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
|
import json
import operator
import pytest
import responses
import pykube
from pykube import Deployment
from pykube import HTTPClient
from pykube import KubeConfig
from pykube import ObjectDoesNotExist
@pytest.fixture
def kubeconfig(tmpdir):
kubeconfig = tmpdir.join("kubeconfig")
kubeconfig.write(
"""
apiVersion: v1
clusters:
- cluster: {server: 'https://localhost:9443'}
name: test
contexts:
- context: {cluster: test, user: test}
name: test
current-context: test
kind: Config
preferences: {}
users:
- name: test
user: {token: testtoken}
"""
)
return kubeconfig
@pytest.fixture
def requests_mock():
return responses.RequestsMock(target="pykube.http.KubernetesHTTPAdapter._do_send")
@pytest.fixture
def api(kubeconfig):
config = KubeConfig.from_file(str(kubeconfig))
return HTTPClient(config)
def test_api_version(api, requests_mock):
with requests_mock as rsps:
rsps.add(
responses.GET,
"https://localhost:9443/version/",
json={"major": 1, "minor": 13},
)
assert api.version == (1, 13)
def test_get_ready_pods(api, requests_mock):
# example from README
with requests_mock as rsps:
rsps.add(
responses.GET,
"https://localhost:9443/api/v1/namespaces/gondor-system/pods",
json={
"items": [
{"metadata": {"name": "pod-1"}, "status": {}},
{
"metadata": {"name": "pod-2"},
"status": {"conditions": [{"type": "Ready", "status": "True"}]},
},
]
},
)
pods = pykube.Pod.objects(api).filter(namespace="gondor-system")
ready_pods = list(filter(operator.attrgetter("ready"), pods))
assert len(ready_pods) == 1
assert ready_pods[0].name == "pod-2"
def test_get_pod_by_name(api, requests_mock):
# example from README
with requests_mock as rsps:
rsps.add(
responses.GET,
"https://localhost:9443/api/v1/namespaces/gondor-system/pods/my-pod",
json={"spec": {"containers": [{"image": "hjacobs/kube-janitor"}]}},
)
rsps.add(
responses.GET,
"https://localhost:9443/api/v1/namespaces/gondor-system/pods/other-pod",
status=404,
)
pod = (
pykube.Pod.objects(api).filter(namespace="gondor-system").get(name="my-pod")
)
assert pod.obj["spec"]["containers"][0]["image"] == "hjacobs/kube-janitor"
pod = (
pykube.Pod.objects(api)
.filter(namespace="gondor-system")
.get_or_none(name="my-pod")
)
assert pod.obj["spec"]["containers"][0]["image"] == "hjacobs/kube-janitor"
pod = (
pykube.Pod.objects(api)
.filter(namespace="gondor-system")
.get_by_name("my-pod")
)
assert pod.obj["spec"]["containers"][0]["image"] == "hjacobs/kube-janitor"
pod = (
pykube.Pod.objects(api)
.filter(namespace="gondor-system")
.get_or_none(name="other-pod")
)
assert pod is None
def test_selector_query(api, requests_mock):
# example from README
with requests_mock as rsps:
rsps.add(
responses.GET,
"https://localhost:9443/api/v1/namespaces/gondor-system/pods?labelSelector=gondor.io%2Fname+in+%28api-web%2Capi-worker%29",
json={"items": [{"meta": {}}]},
)
pods = pykube.Pod.objects(api).filter(
namespace="gondor-system",
selector={"gondor.io/name__in": {"api-web", "api-worker"}},
)
assert len(list(pods)) == 1
rsps.add(
responses.GET,
"https://localhost:9443/api/v1/namespaces/default/pods?fieldSelector=status.phase%3DPending",
json={"items": [{"meta": {}}]},
)
pending_pods = pykube.objects.Pod.objects(api).filter(
field_selector={"status.phase": "Pending"}
)
assert len(list(pending_pods)) == 1
def test_create_delete_deployment(api, requests_mock):
# example from README
with requests_mock as rsps:
rsps.add(
responses.POST,
"https://localhost:9443/apis/apps/v1/namespaces/gondor-system/deployments",
json={},
)
obj = {
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": {"name": "my-deploy", "namespace": "gondor-system"},
"spec": {
"replicas": 3,
"selector": {"matchLabels": {"app": "nginx"}},
"template": {
"metadata": {"labels": {"app": "nginx"}},
"spec": {
"containers": [
{
"name": "nginx",
"image": "nginx",
"ports": [{"containerPort": 80}],
}
]
},
},
},
}
pykube.Deployment(api, obj).create()
rsps.add(
responses.DELETE,
"https://localhost:9443/apis/apps/v1/namespaces/gondor-system/deployments/my-deploy",
json={},
)
obj = {
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": {"name": "my-deploy", "namespace": "gondor-system"},
}
pykube.Deployment(api, obj).delete()
def test_list_deployments(api, requests_mock):
with requests_mock as rsps:
rsps.add(
responses.GET,
"https://localhost:9443/apis/apps/v1/namespaces/default/deployments",
json={"items": []},
)
assert list(Deployment.objects(api)) == []
assert len(rsps.calls) == 1
# ensure that we passed the token specified in kubeconfig..
assert rsps.calls[0].request.headers["Authorization"] == "Bearer testtoken"
def test_list_and_update_deployments(api, requests_mock):
with requests_mock as rsps:
rsps.add(
responses.GET,
"https://localhost:9443/apis/apps/v1/namespaces/default/deployments",
json={
"items": [{"metadata": {"name": "deploy-1"}, "spec": {"replicas": 3}}]
},
)
deployments = list(Deployment.objects(api))
assert len(deployments) == 1
deploy = deployments[0]
assert deploy.name == "deploy-1"
assert deploy.namespace == "default"
assert deploy.replicas == 3
deploy.replicas = 2
rsps.add(
responses.PATCH,
"https://localhost:9443/apis/apps/v1/namespaces/default/deployments/deploy-1",
json={"metadata": {"name": "deploy-1"}, "spec": {"replicas": 2}},
)
deploy.update()
assert len(rsps.calls) == 2
assert json.loads(rsps.calls[-1].request.body) == {
"metadata": {"name": "deploy-1"},
"spec": {"replicas": 2},
}
assert deploy.replicas == 2
def test_list_and_patch_deployments(api, requests_mock):
with requests_mock as rsps:
rsps.add(
responses.GET,
"https://localhost:9443/apis/apps/v1/namespaces/default/deployments",
json={
"items": [{"metadata": {"name": "deploy-1"}, "spec": {"replicas": 3}}]
},
)
deployments = list(Deployment.objects(api))
assert len(deployments) == 1
deploy = deployments[0]
assert deploy.name == "deploy-1"
assert deploy.namespace == "default"
assert deploy.replicas == 3
deploy.replicas = 2
rsps.add(
responses.PATCH,
"https://localhost:9443/apis/apps/v1/namespaces/default/deployments/deploy-1",
json={
"metadata": {"name": "deploy-1", "labels": {"a": "b"}},
"spec": {"replicas": 2},
},
)
deploy.patch({"metadata": {"labels": {"a": "b"}}})
assert len(rsps.calls) == 2
assert json.loads(rsps.calls[-1].request.body) == {
"metadata": {"labels": {"a": "b"}}
}
assert deploy.labels["a"] == "b"
def test_pod_exists(api, requests_mock):
with requests_mock as rsps:
obj = {
"apiVersion": "v1",
"kind": "Pod",
"metadata": {"name": "my-pod"},
"spec": {"containers": []},
}
pod = pykube.Pod(api, obj)
rsps.add(
responses.GET,
"https://localhost:9443/api/v1/namespaces/default/pods/my-pod",
status=404,
)
assert not pod.exists()
with pytest.raises(ObjectDoesNotExist):
pod.exists(ensure=True)
rsps.replace(
responses.GET,
"https://localhost:9443/api/v1/namespaces/default/pods/my-pod",
json={},
)
assert pod.exists()
def test_reload(api, requests_mock):
with requests_mock as rsps:
rsps.add(
responses.GET,
"https://localhost:9443/api/v1/namespaces/default/pods/my-pod",
json={"metadata": {"name": "my-pod", "labels": {"a": "foo"}}},
)
pod = pykube.Pod.objects(api).get_by_name("my-pod")
assert pod.labels["a"] == "foo"
rsps.replace(
responses.GET,
"https://localhost:9443/api/v1/namespaces/default/pods/my-pod",
json={"metadata": {"name": "my-pod", "labels": {"a": "bar"}}},
)
pod.reload()
assert pod.labels["a"] == "bar"
def test_resource_list(api, requests_mock):
with requests_mock as rsps:
data1 = {"resources": [{"kind": "Pod", "name": "pods"}]}
rsps.add(responses.GET, "https://localhost:9443/api/v1/", json=data1)
resource_list = api.resource_list("v1")
assert resource_list == data1
data2 = {"resources": [{"kind": "ExampleObject", "name": "exampleobjects"}]}
rsps.add(
responses.GET, "https://localhost:9443/apis/example.org/v1/", json=data2
)
resource_list = api.resource_list("example.org/v1")
assert resource_list == data2
def test_patch_subresource(api, requests_mock):
with requests_mock as rsps:
rsps.add(
responses.GET,
"https://localhost:9443/apis/apps/v1/namespaces/default/deployments",
json={"items": [{"metadata": {"name": "deploy-1"}}]},
)
deployments = list(Deployment.objects(api))
assert len(deployments) == 1
deploy = deployments[0]
assert deploy.name == "deploy-1"
assert deploy.namespace == "default"
rsps.add(
responses.PATCH,
"https://localhost:9443/apis/apps/v1/namespaces/default/deployments/deploy-1/status",
json={"metadata": {"name": "deploy-1"}, "status": {"field": "field"}},
)
deploy.patch({"status": {"field": "field"}}, subresource="status")
assert len(rsps.calls) == 2
assert json.loads(rsps.calls[-1].request.body) == {
"status": {"field": "field"},
}
|