-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
test_cli.py
411 lines (352 loc) · 13.6 KB
/
test_cli.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
from contextlib import contextmanager
import json
import logging
from pathlib import Path
import tempfile
import os
from unittest import mock
import yaml
from typing import Optional
from click.testing import CliRunner
import pytest
from ray.dashboard.modules.job.cli import job_cli_group
logger = logging.getLogger(__name__)
@pytest.fixture
def mock_sdk_client():
class AsyncIterator:
def __init__(self, seq):
self._seq = seq
self.iter = iter(self._seq)
def __aiter__(self):
return self
async def __anext__(self):
try:
return next(self.iter)
except StopIteration:
self.iter = iter(self._seq)
raise StopAsyncIteration
if "RAY_ADDRESS" in os.environ:
del os.environ["RAY_ADDRESS"]
with mock.patch("ray.dashboard.modules.job.cli.JobSubmissionClient") as mock_client:
# In python 3.6 it will fail with error
# 'async for' requires an object with __aiter__ method, got MagicMock"
mock_client().tail_job_logs.return_value = AsyncIterator(range(10))
yield mock_client
@pytest.fixture
def runtime_env_formats():
with tempfile.TemporaryDirectory() as tmp_dir:
path = Path(tmp_dir)
test_env = {
"working_dir": "s3://bogus.zip",
"conda": "conda_env",
"pip": ["pip-install-test"],
"env_vars": {"hi": "hi2"},
}
yaml_file = path / "env.yaml"
with yaml_file.open(mode="w") as f:
yaml.dump(test_env, f)
yield test_env, json.dumps(test_env), yaml_file
@contextmanager
def set_env_var(key: str, val: Optional[str] = None):
old_val = os.environ.get(key, None)
if val is not None:
os.environ[key] = val
elif key in os.environ:
del os.environ[key]
yield
if key in os.environ:
del os.environ[key]
if old_val is not None:
os.environ[key] = old_val
def check_exit_code(result, exit_code):
assert result.exit_code == exit_code, result.output
def _job_cli_group_test_address(mock_sdk_client, cmd, *args):
runner = CliRunner()
create_cluster_if_needed = True if cmd == "submit" else False
# Test passing address via command line.
result = runner.invoke(job_cli_group, [cmd, "--address=arg_addr", *args])
mock_sdk_client.assert_called_with("arg_addr", create_cluster_if_needed)
with pytest.raises(AssertionError):
mock_sdk_client.assert_called_with("some_other_addr", True)
check_exit_code(result, 0)
# Test passing address via env var.
with set_env_var("RAY_ADDRESS", "env_addr"):
result = runner.invoke(job_cli_group, [cmd, *args])
check_exit_code(result, 0)
# RAY_ADDRESS is read inside the SDK client.
mock_sdk_client.assert_called_with(None, create_cluster_if_needed)
# Test passing no address.
result = runner.invoke(job_cli_group, [cmd, *args])
check_exit_code(result, 0)
mock_sdk_client.assert_called_with(None, create_cluster_if_needed)
class TestList:
def test_address(self, mock_sdk_client):
_job_cli_group_test_address(mock_sdk_client, "list")
def test_list(self, mock_sdk_client):
runner = CliRunner()
with set_env_var("RAY_ADDRESS", "env_addr"):
result = runner.invoke(
job_cli_group,
["list"],
)
check_exit_code(result, 0)
result = runner.invoke(job_cli_group, ["submit", "--", "echo hello"])
check_exit_code(result, 0)
result = runner.invoke(
job_cli_group,
["list"],
)
check_exit_code(result, 0)
class TestSubmit:
def test_address(self, mock_sdk_client):
_job_cli_group_test_address(mock_sdk_client, "submit", "--", "echo", "hello")
def test_working_dir(self, mock_sdk_client):
runner = CliRunner()
mock_client_instance = mock_sdk_client.return_value
with set_env_var("RAY_ADDRESS", "env_addr"):
result = runner.invoke(job_cli_group, ["submit", "--", "echo hello"])
check_exit_code(result, 0)
mock_client_instance.submit_job.assert_called_with(
entrypoint='"echo hello"',
submission_id=None,
runtime_env={},
entrypoint_num_cpus=None,
entrypoint_num_gpus=None,
entrypoint_resources=None,
)
result = runner.invoke(
job_cli_group,
["submit", "--working-dir", "blah", "--", "echo hello"],
)
check_exit_code(result, 0)
mock_client_instance.submit_job.assert_called_with(
entrypoint='"echo hello"',
submission_id=None,
runtime_env={"working_dir": "blah"},
entrypoint_num_cpus=None,
entrypoint_num_gpus=None,
entrypoint_resources=None,
)
result = runner.invoke(
job_cli_group, ["submit", "--working-dir='.'", "--", "echo hello"]
)
check_exit_code(result, 0)
mock_client_instance.submit_job.assert_called_with(
entrypoint='"echo hello"',
submission_id=None,
runtime_env={"working_dir": "'.'"},
entrypoint_num_cpus=None,
entrypoint_num_gpus=None,
entrypoint_resources=None,
)
def test_runtime_env(self, mock_sdk_client, runtime_env_formats):
runner = CliRunner()
mock_client_instance = mock_sdk_client.return_value
env_dict, env_json, env_yaml = runtime_env_formats
with set_env_var("RAY_ADDRESS", "env_addr"):
# Test passing via file.
result = runner.invoke(
job_cli_group, ["submit", "--runtime-env", env_yaml, "--", "echo hello"]
)
check_exit_code(result, 0)
mock_client_instance.submit_job.assert_called_with(
entrypoint='"echo hello"',
submission_id=None,
runtime_env=env_dict,
entrypoint_num_cpus=None,
entrypoint_num_gpus=None,
entrypoint_resources=None,
)
# Test passing via json.
result = runner.invoke(
job_cli_group,
["submit", "--runtime-env-json", env_json, "--", "echo hello"],
)
check_exit_code(result, 0)
mock_client_instance.submit_job.assert_called_with(
entrypoint='"echo hello"',
submission_id=None,
runtime_env=env_dict,
entrypoint_num_cpus=None,
entrypoint_num_gpus=None,
entrypoint_resources=None,
)
# Test passing both throws an error.
result = runner.invoke(
job_cli_group,
[
"submit",
"--runtime-env",
env_yaml,
"--runtime-env-json",
env_json,
"--",
"echo hello",
],
)
check_exit_code(result, 1)
assert "Only one of" in str(result.exception)
# Test overriding working_dir.
env_dict.update(working_dir=".")
result = runner.invoke(
job_cli_group,
[
"submit",
"--runtime-env",
env_yaml,
"--working-dir",
".",
"--",
"echo hello",
],
)
check_exit_code(result, 0)
mock_client_instance.submit_job.assert_called_with(
entrypoint='"echo hello"',
submission_id=None,
runtime_env=env_dict,
entrypoint_num_cpus=None,
entrypoint_num_gpus=None,
entrypoint_resources=None,
)
result = runner.invoke(
job_cli_group,
[
"submit",
"--runtime-env-json",
env_json,
"--working-dir",
".",
"--",
"echo hello",
],
)
check_exit_code(result, 0)
mock_client_instance.submit_job.assert_called_with(
entrypoint='"echo hello"',
submission_id=None,
runtime_env=env_dict,
entrypoint_num_cpus=None,
entrypoint_num_gpus=None,
entrypoint_resources=None,
)
def test_job_id(self, mock_sdk_client):
runner = CliRunner()
mock_client_instance = mock_sdk_client.return_value
with set_env_var("RAY_ADDRESS", "env_addr"):
result = runner.invoke(job_cli_group, ["submit", "--", "echo hello"])
check_exit_code(result, 0)
mock_client_instance.submit_job.assert_called_with(
entrypoint='"echo hello"',
submission_id=None,
runtime_env={},
entrypoint_num_cpus=None,
entrypoint_num_gpus=None,
entrypoint_resources=None,
)
result = runner.invoke(
job_cli_group,
["submit", "--submission-id=my_job_id", "--", "echo hello"],
)
check_exit_code(result, 0)
mock_client_instance.submit_job.assert_called_with(
entrypoint='"echo hello"',
submission_id="my_job_id",
runtime_env={},
entrypoint_num_cpus=None,
entrypoint_num_gpus=None,
entrypoint_resources=None,
)
def test_entrypoint_num_cpus(self, mock_sdk_client):
runner = CliRunner()
mock_client_instance = mock_sdk_client.return_value
with set_env_var("RAY_ADDRESS", "env_addr"):
result = runner.invoke(
job_cli_group,
["submit", "--entrypoint-num-cpus=2", "--", "echo hello"],
)
assert result.exit_code == 0
mock_client_instance.submit_job.assert_called_with(
entrypoint='"echo hello"',
submission_id=None,
runtime_env={},
entrypoint_num_cpus=2,
entrypoint_num_gpus=None,
entrypoint_resources=None,
)
def test_entrypoint_num_gpus(self, mock_sdk_client):
runner = CliRunner()
mock_client_instance = mock_sdk_client.return_value
with set_env_var("RAY_ADDRESS", "env_addr"):
result = runner.invoke(
job_cli_group,
["submit", "--entrypoint-num-gpus=2", "--", "echo hello"],
)
assert result.exit_code == 0
mock_client_instance.submit_job.assert_called_with(
entrypoint='"echo hello"',
submission_id=None,
runtime_env={},
entrypoint_num_cpus=None,
entrypoint_num_gpus=2,
entrypoint_resources=None,
)
@pytest.mark.parametrize(
"resources",
[
("--entrypoint-num-cpus=2", {"entrypoint_num_cpus": 2}),
("--entrypoint-num-gpus=2", {"entrypoint_num_gpus": 2}),
(
"""--entrypoint-resources={"Custom":3}""",
{"entrypoint_resources": {"Custom": 3}},
),
],
)
def test_entrypoint_resources(self, mock_sdk_client, resources):
runner = CliRunner()
mock_client_instance = mock_sdk_client.return_value
with set_env_var("RAY_ADDRESS", "env_addr"):
result = runner.invoke(
job_cli_group,
["submit", resources[0], "--", "echo hello"],
)
print(result.output)
assert result.exit_code == 0
expected_kwargs = {
"entrypoint": '"echo hello"',
"submission_id": None,
"runtime_env": {},
"entrypoint_num_cpus": None,
"entrypoint_num_gpus": None,
"entrypoint_resources": None,
}
expected_kwargs.update(resources[1])
mock_client_instance.submit_job.assert_called_with(**expected_kwargs)
def test_entrypoint_resources_invalid_json(self, mock_sdk_client):
runner = CliRunner()
with set_env_var("RAY_ADDRESS", "env_addr"):
result = runner.invoke(
job_cli_group,
[
"submit",
"""--entrypoint-resources={"Custom":3""",
"--",
"echo hello",
],
)
print(result.output)
assert result.exit_code == 1
assert "not a valid JSON string" in result.output
class TestDelete:
def test_address(self, mock_sdk_client):
_job_cli_group_test_address(mock_sdk_client, "delete", "fake_job_id")
def test_delete(self, mock_sdk_client):
runner = CliRunner()
mock_client_instance = mock_sdk_client.return_value
with set_env_var("RAY_ADDRESS", "env_addr"):
result = runner.invoke(job_cli_group, ["delete", "job_id"])
check_exit_code(result, 0)
mock_client_instance.delete_job.assert_called_with("job_id")
if __name__ == "__main__":
import sys
sys.exit(pytest.main(["-v", __file__]))