-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
conftest.py
241 lines (202 loc) · 7.69 KB
/
conftest.py
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
import contextlib
import os
import subprocess
import tempfile
import time
from pathlib import Path
from typing import Dict, List
import docker
import pytest
from pytest import FixtureRequest
from pytest_kind import KindCluster
from jina.logging.logger import JinaLogger
client = docker.from_env()
cur_dir = os.path.dirname(__file__)
class KindClusterWrapper:
def __init__(self, kind_cluster: KindCluster, logger: JinaLogger) -> None:
self._cluster = kind_cluster
self._cluster.ensure_kubectl()
self._kube_config_path = os.path.join(
os.getcwd(), '.pytest-kind/pytest-kind/kubeconfig'
)
self._log = logger
self._set_kube_config()
self._install_linkderd(kind_cluster)
self._loaded_images = set()
def _linkerd_install_cmd(
self, kind_cluster: KindCluster, cmd, tool_name: str
) -> None:
self._log.info(f'Installing {tool_name} to Cluster...')
kube_out = subprocess.check_output(
(str(kind_cluster.kubectl_path), 'version'),
env=os.environ,
)
self._log.info(f'kuberbetes versions: {kube_out}')
# since we need to pipe to commands and the linkerd output can bee too long
# there is a risk of deadlock and hanging tests: https://docs.python.org/3/library/subprocess.html#popen-objects
# to avoid this, the right mechanism is implemented in subprocess.run and subprocess.check_output, but output
# must be piped to a file-like object, not to stdout
proc_stdout = tempfile.TemporaryFile()
proc = subprocess.run(
cmd,
stdout=proc_stdout,
env={"KUBECONFIG": str(kind_cluster.kubeconfig_path)},
)
proc_stdout.seek(0)
kube_out = subprocess.check_output(
(
str(kind_cluster.kubectl_path),
'apply',
'-f',
'-',
),
stdin=proc_stdout,
env=os.environ,
)
returncode = proc.returncode
self._log.info(
f'Installing {tool_name} to Cluster returned code {returncode}, kubectl output was {kube_out}'
)
if returncode is not None and returncode != 0:
raise Exception(f'Installing {tool_name} failed with {returncode}')
def _install_linkderd(self, kind_cluster: KindCluster) -> None:
# linkerd < 2.12: only linkerd install is needed
# in later versions, linkerd install --crds will be needed
self._linkerd_install_cmd(
kind_cluster, [f'{Path.home()}/.linkerd2/bin/linkerd', 'install'], 'Linkerd'
)
self._log.info('check linkerd status')
out = subprocess.check_output(
[f'{Path.home()}/.linkerd2/bin/linkerd', 'check'],
env=os.environ,
)
print(f'linkerd check yields {out.decode() if out else "nothing"}')
def install_linkderd_smi(self) -> None:
self._log.info('Installing Linkerd SMI to Cluster...')
proc = subprocess.Popen(
[f'{Path.home()}/.linkerd2/bin/linkerd-smi', 'install'],
stdout=subprocess.PIPE,
env={"KUBECONFIG": str(self._cluster.kubeconfig_path)},
)
kube_out = subprocess.check_output(
(
str(self._cluster.kubectl_path),
'apply',
'-f',
'-',
),
stdin=proc.stdout,
env=os.environ,
)
self._log.info('Poll status of linkerd smi install')
returncode = proc.poll()
self._log.info(
f'Installing Linkerd to Cluster returned code {returncode}, kubectl output was {kube_out}'
)
if returncode is not None and returncode != 0:
raise Exception(f"Installing linkerd failed with {returncode}")
self._log.info('check linkerd status')
out = subprocess.check_output(
[f'{Path.home()}/.linkerd2/bin/linkerd-smi', 'check'],
env=os.environ,
)
print(f'linkerd check yields {out.decode() if out else "nothing"}')
def _set_kube_config(self):
self._log.info(f'Setting KUBECONFIG to {self._kube_config_path}')
os.environ['KUBECONFIG'] = self._kube_config_path
def load_docker_images(
self, images: List[str], image_tag_map: Dict[str, str]
) -> None:
for image in images:
full_image_name = image + ':' + image_tag_map[image]
if full_image_name not in self._loaded_images:
if image != 'alpine' and image != 'jinaai/jina':
build_docker_image(image, image_tag_map)
self._cluster.load_docker_image(full_image_name)
self._loaded_images.add(full_image_name)
@pytest.fixture()
def test_dir() -> str:
return cur_dir
@pytest.fixture
def logger() -> JinaLogger:
return JinaLogger('kubernetes-testing')
@pytest.fixture(scope='session')
def k8s_cluster(kind_cluster: KindCluster) -> KindClusterWrapper:
return KindClusterWrapper(kind_cluster, JinaLogger('kubernetes-cluster-logger'))
@pytest.fixture
def image_name_tag_map() -> Dict[str, str]:
return {
'reload-executor': '0.13.1',
'test-executor': '0.13.1',
'slow-process-executor': '0.14.1',
'executor-merger': '0.1.1',
'set-text-executor': '0.1.1',
'failing-executor': '0.1.1',
'jinaai/jina': 'test-pip',
'custom-gateway': '0.1.1',
'test-stateful-executor': '0.13.1',
'multiprotocol-gateway': '0.1.1',
'slow-load-executor': '0.1.1',
}
def build_docker_image(image_name: str, image_name_tag_map: Dict[str, str]) -> str:
logger = JinaLogger('kubernetes-testing')
image_tag = image_name + ':' + image_name_tag_map[image_name]
image, build_logs = client.images.build(
path=os.path.join(cur_dir, image_name), tag=image_tag
)
for chunk in build_logs:
if 'stream' in chunk:
for line in chunk['stream'].splitlines():
logger.debug(line)
return image.tags[-1]
@pytest.fixture(autouse=True)
def set_test_pip_version() -> None:
os.environ['JINA_GATEWAY_IMAGE'] = 'jinaai/jina:test-pip'
yield
del os.environ['JINA_GATEWAY_IMAGE']
@pytest.fixture(autouse=True)
def load_cluster_config(k8s_cluster: KindClusterWrapper) -> None:
import kubernetes
try:
# try loading kube config from disk first
kubernetes.config.load_kube_config()
except kubernetes.config.config_exception.ConfigException:
# if the config could not be read from disk, try loading in cluster config
# this works if we are running inside k8s
kubernetes.config.load_incluster_config()
@pytest.fixture
def docker_images(
request: FixtureRequest,
image_name_tag_map: Dict[str, str],
k8s_cluster: KindClusterWrapper,
) -> List[str]:
image_names: List[str] = request.param
k8s_cluster.load_docker_images(image_names, image_name_tag_map)
images = [
image_name + ':' + image_name_tag_map[image_name] for image_name in image_names
]
return images
@contextlib.contextmanager
def shell_portforward(
kubectl_path, pod_or_service, port1, port2, namespace, waiting: float = 1
):
try:
proc = subprocess.Popen(
[
kubectl_path,
'port-forward',
pod_or_service,
f'{port1}:{port2}',
'--namespace',
namespace,
]
)
# Go and the port-forwarding needs some ms to be ready
time.sleep(waiting)
yield None
time.sleep(waiting)
except Exception as err:
# Suppress extension exception
raise OSError(err) from None
finally:
proc.kill()