-
Notifications
You must be signed in to change notification settings - Fork 446
/
Copy pathcompatibility-test.py
executable file
·432 lines (373 loc) · 15.1 KB
/
compatibility-test.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
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#!/usr/bin/env python
import logging
import unittest
import docker
import time
import os
import kuberay_utils.utils as utils
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
# Image version
ray_version = '1.9.0'
# Docker images
ray_image = 'rayproject/ray:1.9.0'
kuberay_operator_image = 'kuberay/operator:nightly'
kuberay_apiserver_image = 'kuberay/apiserver:nightly'
class BasicRayTestCase(unittest.TestCase):
cluster_template_file = 'tests/config/ray-cluster.mini.yaml.template'
@classmethod
def setUpClass(cls):
# Ray cluster is running inside a local Kind environment.
# We use port mapping to connect to the Kind environment
# from another local ray container. The local ray container
# outside Kind environment has the same ray version as the
# ray cluster running inside Kind environment.
utils.delete_cluster()
utils.create_cluster()
images = [ray_image, kuberay_operator_image, kuberay_apiserver_image]
utils.download_images(images)
utils.apply_kuberay_resources(images, kuberay_operator_image, kuberay_apiserver_image)
utils.create_kuberay_cluster(BasicRayTestCase.cluster_template_file,
ray_version, ray_image)
def test_simple_code(self):
# connect from a ray container client to ray cluster
# inside a local Kind environment and run a simple test
client = docker.from_env()
container = client.containers.run(ray_image,
remove=True,
detach=True,
tty=True,
network_mode='host')
rtn_code, output = container.exec_run(['python',
'-c', '''
import ray
ray.init(address='ray://127.0.0.1:10001')
def retry_with_timeout(func, count=90):
tmp = 0
err = None
while tmp < count:
try:
return func()
except Exception as e:
err = e
tmp += 1
assert err is not None
raise err
@ray.remote
def f(x):
return x * x
def get_result():
futures = [f.remote(i) for i in range(4)]
print(ray.get(futures))
return 0
rtn = retry_with_timeout(get_result)
assert rtn == 0
'''],
demux=True)
stdout_str, stderr_str = output
container.stop()
if stdout_str != b'[0, 1, 4, 9]\n':
logger.error('test_simple_code returns {}'.format(output))
raise Exception(('test_simple_code returns invalid result. ' +
'Expected: {} Actual: {} Stderr: {}').format(
b'[0, 1, 4, 9]', stdout_str, stderr_str))
if rtn_code != 0:
msg = 'invalid return code {}'.format(rtn_code)
logger.error(msg)
raise Exception(msg)
client.close()
def test_cluster_info(self):
# connect from a ray container client to ray cluster
# inside a local Kind environment and run a test that
# gets the amount of nodes in the ray cluster.
client = docker.from_env()
container = client.containers.run(ray_image,
remove=True,
detach=True,
tty=True,
network_mode='host')
rtn_code, output = container.exec_run(['python',
'-c', '''
import ray
ray.init(address='ray://127.0.0.1:10001')
print(len(ray.nodes()))
'''],
demux=True)
stdout_str, _ = output
container.stop()
if stdout_str != b'2\n':
logger.error('test_cluster_info returns {}'.format(output))
raise Exception(('test_cluster_info returns invalid result. ' +
'Expected: {} Actual: {}').format(b'2',
stdout_str))
if rtn_code != 0:
msg = 'invalid return code {}'.format(rtn_code)
logger.error(msg)
raise Exception(msg)
client.close()
class RayFTTestCase(unittest.TestCase):
cluster_template_file = 'tests/config/ray-cluster.ray-ft.yaml.template'
@classmethod
def setUpClass(cls):
if not utils.ray_ft_supported(ray_version):
raise unittest.SkipTest("ray ft is not supported")
utils.delete_cluster()
utils.create_cluster()
images = [ray_image, kuberay_operator_image, kuberay_apiserver_image]
utils.download_images(images)
utils.apply_kuberay_resources(images, kuberay_operator_image, kuberay_apiserver_image)
utils.create_kuberay_cluster(RayFTTestCase.cluster_template_file,
ray_version, ray_image)
def test_kill_head(self):
# This test will delete head node and wait for a new replacement to
# come up.
utils.shell_assert_success(
'kubectl delete pod $(kubectl get pods -A | grep -e "-head" | awk "{print \$2}")')
# wait for new head node to start
time.sleep(80)
utils.shell_assert_success('kubectl get pods -A')
# make sure the new head is ready
# shell_assert_success('kubectl wait --for=condition=Ready pod/$(kubectl get pods -A | grep -e "-head" | awk "{print \$2}") --timeout=900s')
# make sure both head and worker pods are ready
rtn = utils.shell_run(
'kubectl wait --for=condition=ready pod -l rayCluster=raycluster-compatibility-test --all --timeout=900s')
if rtn != 0:
utils.shell_run('kubectl get pods -A')
utils.shell_run(
'kubectl describe pod $(kubectl get pods | grep -e "-head" | awk "{print \$1}")')
utils.shell_run(
'kubectl logs $(kubectl get pods | grep -e "-head" | awk "{print \$1}")')
utils.shell_run(
'kubectl logs -n $(kubectl get pods -A | grep -e "-operator" | awk \'{print $1 " " $2}\')')
assert rtn == 0
def test_ray_serve(self):
client = docker.from_env()
container = client.containers.run(ray_image, remove=True, detach=True, stdin_open=True, tty=True,
network_mode='host', command=["/bin/sh", "-c", "python"])
s = container.attach_socket(
params={'stdin': 1, 'stream': 1, 'stdout': 1, 'stderr': 1})
s._sock.setblocking(0)
s._sock.sendall(b'''
import ray
import time
import ray.serve as serve
import os
import requests
from ray._private.test_utils import wait_for_condition
def retry_with_timeout(func, count=90):
tmp = 0
err = None
while tmp < count:
try:
return func()
except Exception as e:
err = e
tmp += 1
assert err is not None
raise err
ray.init(address='ray://127.0.0.1:10001')
@serve.deployment
def d(*args):
return f"{os.getpid()}"
d.deploy()
pid1 = ray.get(d.get_handle().remote())
print('ready')
''')
count = 0
while count < 90:
try:
buf = s._sock.recv(4096)
logger.info(buf.decode())
if buf.decode().find('ready') != -1:
break
except Exception as e:
pass
time.sleep(1)
count += 1
if count >= 90:
raise Exception('failed to run script')
# kill the gcs on head node. If fate sharing is enabled
# the whole head node pod will terminate.
utils.shell_assert_success(
'kubectl exec -it $(kubectl get pods -A| grep -e "-head" | awk "{print \\$2}") -- /bin/bash -c "ps aux | grep gcs_server | grep -v grep | awk \'{print \$2}\' | xargs kill"')
# wait for new head node getting created
time.sleep(10)
# make sure the new head is ready
utils.shell_assert_success(
'kubectl wait --for=condition=Ready pod/$(kubectl get pods -A | grep -e "-head" | awk "{print \$2}") --timeout=900s')
s._sock.sendall(b'''
def get_new_value():
return ray.get(d.get_handle().remote())
pid2 = retry_with_timeout(get_new_value)
if pid1 == pid2:
print('successful: {} {}'.format(pid1, pid2))
sys.exit(0)
else:
print('failed: {} {}'.format(pid1, pid2))
raise Exception('failed')
''')
count = 0
while count < 90:
try:
buf = s._sock.recv(4096)
logger.info(buf.decode())
if buf.decode().find('successful') != -1:
break
if buf.decode().find('failed') != -1:
raise Exception('test failed {}'.format(buf.decode()))
except Exception as e:
pass
time.sleep(1)
count += 1
if count >= 90:
raise Exception('failed to run script')
container.stop()
client.close()
def test_detached_actor(self):
# This test will run a ray client and start a detached actor at first.
# Then we will kill the head node and kuberay will start a new head node
# replacement. Finally, we will try to connect to the detached actor again.
client = docker.from_env()
container = client.containers.run(ray_image, remove=True, detach=True, stdin_open=True, tty=True,
network_mode='host', command=["/bin/sh", "-c", "python"])
s = container.attach_socket(
params={'stdin': 1, 'stream': 1, 'stdout': 1, 'stderr': 1})
s._sock.setblocking(0)
s._sock.sendall(b'''
import ray
import time
def retry_with_timeout(func, count=90):
tmp = 0
err = None
while tmp < count:
try:
return func()
except Exception as e:
err = e
tmp += 1
assert err is not None
raise err
ray.init(address='ray://127.0.0.1:10001')
@ray.remote
class A:
def ready(self):
import os
return os.getpid()
a = A.options(name="a", lifetime="detached", max_restarts=-1).remote()
res1 = ray.get(a.ready.remote())
print('ready')
''')
count = 0
while count < 90:
try:
buf = s._sock.recv(4096)
logger.info(buf.decode())
if buf.decode().find('ready') != -1:
break
except Exception as e:
pass
time.sleep(1)
count += 1
if count >= 90:
raise Exception('failed to run script')
# kill the gcs on head node. If fate sharing is enabled
# the whole head node pod will terminate.
utils.shell_assert_success(
'kubectl exec -it $(kubectl get pods -A| grep -e "-head" | awk "{print \\$2}") -- /bin/bash -c "ps aux | grep gcs_server | grep -v grep | awk \'{print \$2}\' | xargs kill"')
# wait for new head node getting created
time.sleep(10)
# make sure the new head is ready
utils.shell_assert_success(
'kubectl wait --for=condition=Ready pod/$(kubectl get pods -A | grep -e "-head" | awk "{print \$2}") --timeout=900s')
s._sock.sendall(b'''
def get_detached_actor():
return ray.get_actor("a")
a = retry_with_timeout(get_detached_actor)
def get_new_value():
return ray.get(a.ready.remote())
res2 = retry_with_timeout(get_new_value)
if res1 != res2:
print('successful: {} {}'.format(res1, res2))
sys.exit(0)
else:
print('failed: {} {}'.format(res1, res2))
raise Exception('failed')
''')
count = 0
while count < 90:
try:
buf = s._sock.recv(4096)
logger.info(buf.decode())
if buf.decode().find('successful') != -1:
break
if buf.decode().find('failed') != -1:
raise Exception('test failed {}'.format(buf.decode()))
except Exception as e:
pass
time.sleep(1)
count += 1
if count >= 90:
raise Exception('failed to run script')
container.stop()
client.close()
class RayServiceTestCase(unittest.TestCase):
service_template_file = 'tests/config/ray-service.yaml.template'
service_serve_update_template_file = 'tests/config/ray-service-serve-update.yaml.template'
service_cluster_update_template_file = 'tests/config/ray-service-cluster-update.yaml.template'
@classmethod
def setUpClass(cls):
if not utils.ray_service_supported(ray_version):
raise unittest.SkipTest("ray service is not supported")
# Ray Service is running inside a local Kind environment.
# We use the Ray nightly version now.
# We wait for the serve service ready.
# The test will check the successful response from serve service.
utils.delete_cluster()
utils.create_cluster()
images = [ray_image, kuberay_operator_image, kuberay_apiserver_image]
utils.download_images(images)
utils.apply_kuberay_resources(images, kuberay_operator_image, kuberay_apiserver_image)
utils.create_kuberay_service(
RayServiceTestCase.service_template_file, ray_version, ray_image)
def test_ray_serve_work(self):
time.sleep(5)
curl_cmd = 'curl -X POST -H \'Content-Type: application/json\' localhost:8000 -d \'["MANGO", 2]\''
utils.wait_for_condition(
lambda: utils.shell_run(curl_cmd) == 0,
timeout=15,
)
utils.create_kuberay_service(
RayServiceTestCase.service_serve_update_template_file,
ray_version, ray_image)
curl_cmd = 'curl -X POST -H \'Content-Type: application/json\' localhost:8000 -d \'["MANGO", 2]\''
time.sleep(5)
utils.wait_for_condition(
lambda: utils.shell_run(curl_cmd) == 0,
timeout=60,
)
utils.create_kuberay_service(
RayServiceTestCase.service_cluster_update_template_file,
ray_version, ray_image)
time.sleep(5)
curl_cmd = 'curl -X POST -H \'Content-Type: application/json\' localhost:8000 -d \'["MANGO", 2]\''
utils.wait_for_condition(
lambda: utils.shell_run(curl_cmd) == 0,
timeout=180,
)
def parse_environment():
global ray_version, ray_image, kuberay_operator_image, kuberay_apiserver_image
for k, v in os.environ.items():
if k == 'RAY_IMAGE':
ray_image = v
ray_version = ray_image.split(':')[-1]
elif k == 'OPERATOR_IMAGE':
kuberay_operator_image = v
elif k == 'APISERVER_IMAGE':
kuberay_apiserver_image = v
if __name__ == '__main__':
parse_environment()
logger.info('Setting Ray image to: {}'.format(ray_image))
logger.info('Setting Ray version to: {}'.format(ray_version))
logger.info('Setting KubeRay operator image to: {}'.format(kuberay_operator_image))
logger.info('Setting KubeRay apiserver image to: {}'.format(kuberay_apiserver_image))
unittest.main(verbosity=2)