/
test_run.py
650 lines (567 loc) · 23 KB
/
test_run.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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
"""
Tests for RunCommand
"""
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
# TODO: include builtin libraries for the appropriate Python
# try:
# import __builtin__
# except ImportError:
# # Python 3
# import builtins as __builtin__
import os
import glob
import time
import tempfile
import platform
from argparse import ArgumentError
from io import open
try:
to_unicode = unicode
except NameError:
to_unicode = str
try:
def to_bytes(val):
return bytes(val)
to_bytes("test")
except TypeError:
def to_bytes(val):
return bytes(val, "utf-8")
to_bytes("test")
from datmo.config import Config
from datmo.cli.driver.helper import Helper
from datmo.cli.command.project import ProjectCommand
from datmo.cli.command.run import RunObject
from datmo.cli.command.run import RunCommand
from datmo.core.entity.task import Task as CoreTask
from datmo.core.util.exceptions import SessionDoesNotExist, DoesNotExist, \
MutuallyExclusiveArguments, RequiredArgumentMissing, PathDoesNotExist
from datmo.core.util.misc_functions import pytest_docker_environment_failed_instantiation
# provide mountable tmp directory for docker
tempfile.tempdir = "/tmp" if not platform.system() == "Windows" else None
test_datmo_dir = os.environ.get('TEST_DATMO_DIR', tempfile.gettempdir())
class TestRunCommand():
def setup_method(self):
self.temp_dir = tempfile.mkdtemp(dir=test_datmo_dir)
Config().set_home(self.temp_dir)
self.cli_helper = Helper()
self.snapshot_dict = {
"id": "test",
"model_id": "my_model",
"session_id": "my_session",
"message": "my message",
"code_id": "my_code_id",
"environment_id": "my_environment_id",
"file_collection_id": "my file collection",
"config": {
"test": 0.56
},
"stats": {
"test": 0.34
}
}
self.task_dict = {
"id": "test",
"model_id": "my_model",
"session_id": "my_session",
"command": "python test.py"
}
def teardown_method(self):
pass
def __set_variables(self):
self.project_command = ProjectCommand(self.cli_helper)
self.project_command.parse(
["init", "--name", "foobar", "--description", "test model"])
@self.project_command.cli_helper.input("\n")
def dummy(self):
return self.project_command.execute()
dummy(self)
self.run_command = RunCommand(self.cli_helper)
# Create environment_driver definition
self.env_def_path = os.path.join(self.temp_dir, "Dockerfile")
with open(self.env_def_path, "wb") as f:
f.write(to_bytes("FROM python:3.5-alpine"))
def test_run_object_instantiate(self):
task_obj = CoreTask(self.task_dict)
result = RunObject(task_obj)
assert result
assert isinstance(result, RunObject)
def test_run_should_fail1(self):
self.__set_variables()
# Test failure case
self.run_command.parse(["run"])
result = self.run_command.execute()
assert not result
@pytest_docker_environment_failed_instantiation(test_datmo_dir)
def test_run(self):
# TODO: Adding test with `--interactive` argument and terminate inside container
self.__set_variables()
# Test failure command execute
test_command = ["yo", "yo"]
self.run_command.parse(["run", test_command])
result = self.run_command.execute()
assert result
# Test success case
test_command = ["sh", "-c", "echo accuracy:0.45"]
test_ports = ["8888:8888", "9999:9999"]
test_dockerfile = os.path.join(self.temp_dir, "Dockerfile")
test_mem_limit = "4g"
# test for single set of ports
self.run_command.parse([
"run", "--ports", test_ports[0], "--environment-paths",
test_dockerfile, "--mem-limit", test_mem_limit, test_command
])
# test for desired side effects
assert self.run_command.args.cmd == test_command
assert self.run_command.args.ports == [test_ports[0]]
assert self.run_command.args.environment_paths == [test_dockerfile]
assert self.run_command.args.mem_limit == test_mem_limit
self.run_command.parse([
"run", "-p", test_ports[0], "-p", test_ports[1],
"--environment-paths", test_dockerfile, "--mem-limit",
test_mem_limit, test_command
])
# test for desired side effects
assert self.run_command.args.cmd == test_command
assert self.run_command.args.ports == test_ports
assert self.run_command.args.environment_paths == [test_dockerfile]
assert self.run_command.args.mem_limit == test_mem_limit
# test proper execution of run command
result = self.run_command.execute()
time.sleep(1)
assert result
assert isinstance(result, RunObject)
assert result.logs
assert "accuracy" in result.logs
assert result.results
assert result.results == {"accuracy": "0.45"}
assert result.status == "SUCCESS"
assert result.start_time
assert result.end_time
assert result.duration
assert result.core_snapshot_id
assert result.core_snapshot_id == result.after_snapshot_id
assert result.environment_id
# teardown
self.run_command.parse(["stop", "--all"])
# test when all is passed to stop all
self.run_command.execute()
@pytest_docker_environment_failed_instantiation(test_datmo_dir)
def test_run_data_dir(self):
# TODO: Adding test with `--interactive` argument and terminate inside container
self.__set_variables()
# Test success case
test_filename = "script.py"
test_command = ["python", test_filename]
test_dockerfile = os.path.join(self.temp_dir, "Dockerfile")
test_mem_limit = "4g"
# Test success for run with directory being passed
test_data_dir_1 = os.path.join(
tempfile.mkdtemp(dir=test_datmo_dir), "data1")
os.mkdir(test_data_dir_1)
test_data_dir_2 = os.path.join(
tempfile.mkdtemp(dir=test_datmo_dir), "data2")
os.mkdir(test_data_dir_2)
with open(os.path.join(test_data_dir_1, "file.txt"), "wb") as f:
f.write(to_bytes('my initial line in 1\n'))
with open(os.path.join(test_data_dir_2, "file.txt"), "wb") as f:
f.write(to_bytes('my initial line in 2\n'))
test_filename = "script.py"
test_filepath = os.path.join(self.temp_dir, test_filename)
with open(test_filepath, "wb") as f:
f.write(to_bytes("import os\n"))
f.write(to_bytes("print('hello')\n"))
f.write(to_bytes("import shutil\n"))
f.write(
to_bytes(
"with open(os.path.join('/data', 'data1', 'file.txt'), 'a') as f:\n"
))
f.write(to_bytes(" f.write('my test file in 1')\n"))
f.write(
to_bytes(
"with open(os.path.join('/data', 'data2', 'file.txt'), 'a') as f:\n"
))
f.write(to_bytes(" f.write('my test file in 2')\n"))
self.run_command.parse([
"run", "--environment-paths", test_dockerfile, "--data",
test_data_dir_1, "--data", test_data_dir_2, "--mem-limit",
test_mem_limit, test_command
])
# test proper execution of run command
result = self.run_command.execute()
time.sleep(1)
assert result
assert isinstance(result, RunObject)
assert result.logs
assert result.status == "SUCCESS"
assert result.start_time
assert result.end_time
assert result.duration
assert result.core_snapshot_id
assert result.core_snapshot_id == result.after_snapshot_id
assert result.environment_id
assert "my initial line in 1" in open(
os.path.join(test_data_dir_1, "file.txt"), "r").read()
assert "my test file in 1" in open(
os.path.join(test_data_dir_1, "file.txt"), "r").read()
assert "my initial line in 2" in open(
os.path.join(test_data_dir_2, "file.txt"), "r").read()
assert "my test file in 2" in open(
os.path.join(test_data_dir_2, "file.txt"), "r").read()
# teardown
self.run_command.parse(["stop", "--all"])
# test when all is passed to stop all
self.run_command.execute()
# test failure
test_data_dir_dne = os.path.join(
tempfile.mkdtemp(dir=test_datmo_dir), "data_dne")
self.run_command.parse([
"run", "--environment-paths", test_dockerfile, "--data",
test_data_dir_dne
])
result = self.run_command.execute()
assert not result
@pytest_docker_environment_failed_instantiation(test_datmo_dir)
def test_run_string_command(self):
# TODO: Adding test with `--interactive` argument and terminate inside container
self.__set_variables()
# Test success case
test_command = "sh -c 'echo accuracy:0.45'"
test_ports = ["8888:8888", "9999:9999"]
test_dockerfile = os.path.join(self.temp_dir, "Dockerfile")
test_mem_limit = "4g"
self.run_command.parse([
"run", "--ports", test_ports[0], "--ports", test_ports[1],
"--environment-paths", test_dockerfile, "--mem-limit",
test_mem_limit, test_command
])
# test for desired side effects
assert self.run_command.args.cmd == test_command
assert self.run_command.args.ports == test_ports
assert self.run_command.args.environment_paths == [test_dockerfile]
assert self.run_command.args.mem_limit == test_mem_limit
# test proper execution of run command
result = self.run_command.execute()
assert result
assert isinstance(result, RunObject)
assert result.logs
assert "accuracy" in result.logs
assert result.results
assert result.results == {"accuracy": "0.45"}
assert result.status == "SUCCESS"
assert result.start_time
assert result.end_time
assert result.duration
assert result.core_snapshot_id
assert result.core_snapshot_id == result.after_snapshot_id
assert result.environment_id
assert result
# teardown
self.run_command.parse(["stop", "--all"])
# test when all is passed to stop all
self.run_command.execute()
# def test_multiple_concurrent_run_command(self):
# test_dockerfile = os.path.join(self.temp_dir, "Dockerfile")
# test_command = ["sh", "-c", "echo accuracy:0.45"]
# manager = Manager()
# return_dict = manager.dict()
#
# def run_exec_func(procnum, return_dict):
# print("Creating Task object")
# run = RunCommand(self.temp_dir, self.cli_helper)
# print("Parsing command")
# run.parse(
# ["run", "--environment-paths", test_dockerfile, test_command])
# print("Executing command")
# result = run.execute()
# return_dict[procnum] = result
#
# self.__set_variables()
# test_dockerfile = os.path.join(self.temp_dir, "Dockerfile")
#
# # Run all three runs in parallel
# jobs = []
# number_runs = 3
# for i in range(number_runs):
# p = Process(target=run_exec_func, args=(i, return_dict))
# jobs.append(p)
# p.start()
#
# # Join
# for proc in jobs:
# proc.join()
#
# results = return_dict.values()
# assert len(results) == number_runs
# for result in results:
# assert result
# assert isinstance(result, CoreTask)
# assert result.logs
# assert "accuracy" in result.logs
# assert result.results
# assert result.results == {"accuracy": "0.45"}
# assert result.status == "SUCCESS"
@pytest_docker_environment_failed_instantiation(test_datmo_dir)
def test_run_notebook(self):
self.__set_variables()
# Update the default Dockerfile to test with
with open(self.env_def_path, "wb") as f:
f.write(to_bytes("FROM nbgallery/jupyter-alpine:latest"))
# Test success case
test_command = ["jupyter", "notebook", "list"]
test_ports = ["8888:8888", "9999:9999"]
test_mem_limit = "4g"
# test single ports option before command
self.run_command.parse([
"run", "--ports", test_ports[0], "--mem-limit", test_mem_limit,
test_command
])
# test for desired side effects
assert self.run_command.args.cmd == test_command
assert self.run_command.args.ports == [test_ports[0]]
assert self.run_command.args.mem_limit == test_mem_limit
# test multiple ports option before command
self.run_command.parse([
"run", "--ports", test_ports[0], "--ports", test_ports[1],
"--mem-limit", test_mem_limit, "--environment-paths",
self.env_def_path, test_command
])
# test for desired side effects
assert self.run_command.args.cmd == test_command
assert self.run_command.args.ports == test_ports
assert self.run_command.args.mem_limit == test_mem_limit
# test proper execution of run command
result = self.run_command.execute()
assert result
assert isinstance(result, RunObject)
assert result.logs
assert "Currently running servers" in result.logs
assert result.status == "SUCCESS"
# teardown
self.run_command.parse(["stop", "--all"])
# test when all is passed to stop all
self.run_command.execute()
def test_run_invalid_arg(self):
self.__set_variables()
exception_thrown = False
try:
self.run_command.parse(["run" "--foobar", "foobar"])
except Exception:
exception_thrown = True
assert exception_thrown
@pytest_docker_environment_failed_instantiation(test_datmo_dir)
def test_run_ls(self):
self.__set_variables()
# Running a task
test_command = ["sh", "-c", "echo accuracy:0.45"]
test_ports = ["8888:8888", "9999:9999"]
test_dockerfile = os.path.join(self.temp_dir, "Dockerfile")
test_mem_limit = "4g"
# test for single set of ports
self.run_command.parse([
"run", "--ports", test_ports[0], "--environment-paths",
test_dockerfile, "--mem-limit", test_mem_limit, test_command
])
# test proper execution of run command
self.run_command.execute()
# Test defaults
self.run_command.parse(["ls"])
run_objs = self.run_command.execute()
assert run_objs
assert run_objs[0].status == "SUCCESS"
# Test failure (format)
failed = False
try:
self.run_command.parse(["ls", "--format"])
except ArgumentError:
failed = True
assert failed
# Test success format csv
self.run_command.parse(["ls", "--format", "csv"])
run_objs = self.run_command.execute()
assert run_objs
assert run_objs[0].status == "SUCCESS"
# Test success format csv, download default
self.run_command.parse(["ls", "--format", "csv", "--download"])
run_objs = self.run_command.execute()
assert run_objs
assert run_objs[0].status == "SUCCESS"
test_wildcard = os.path.join(os.getcwd(), "run_ls_*")
paths = [n for n in glob.glob(test_wildcard) if os.path.isfile(n)]
assert paths
assert open(paths[0], "r").read()
os.remove(paths[0])
# Test success format csv, download exact path
test_path = os.path.join(self.temp_dir, "my_output")
self.run_command.parse([
"ls", "--format", "csv", "--download", "--download-path", test_path
])
run_objs = self.run_command.execute()
assert run_objs
assert run_objs[0].status == "SUCCESS"
assert os.path.isfile(test_path)
assert open(test_path, "r").read()
os.remove(test_path)
# Test success format table
self.run_command.parse(["ls"])
run_objs = self.run_command.execute()
assert run_objs
assert run_objs[0].status == "SUCCESS"
# Test success format table, download default
self.run_command.parse(["ls", "--download"])
run_objs = self.run_command.execute()
assert run_objs
assert run_objs[0].status == "SUCCESS"
test_wildcard = os.path.join(os.getcwd(), "run_ls_*")
paths = [n for n in glob.glob(test_wildcard) if os.path.isfile(n)]
assert paths
assert open(paths[0], "r").read()
os.remove(paths[0])
# Test success format table, download exact path
test_path = os.path.join(self.temp_dir, "my_output")
self.run_command.parse(
["ls", "--download", "--download-path", test_path])
run_objs = self.run_command.execute()
assert run_objs
assert run_objs[0].status == "SUCCESS"
assert os.path.isfile(test_path)
assert open(test_path, "r").read()
os.remove(test_path)
def test_run_ls_invalid_arg(self):
self.__set_variables()
exception_thrown = False
try:
self.run_command.parse(["ls" "--foobar", "foobar"])
except Exception:
exception_thrown = True
assert exception_thrown
@pytest_docker_environment_failed_instantiation(test_datmo_dir)
def test_run_stop_success(self):
# 1) Test stop with task_id
# 2) Test stop with all
self.__set_variables()
test_command = ["sh", "-c", "echo yo"]
test_ports = "8888:8888"
test_mem_limit = "4g"
test_dockerfile = os.path.join(self.temp_dir, "Dockerfile")
self.run_command.parse([
"run", "--ports", test_ports, "--environment-paths",
test_dockerfile, "--mem-limit", test_mem_limit, test_command
])
test_run_obj = self.run_command.execute()
# 1) Test option 1
self.run_command.parse(["stop", "--id", test_run_obj.id])
# test for desired side effects
assert self.run_command.args.id == test_run_obj.id
# test when task id is passed to stop it
run_stop_command = self.run_command.execute()
assert run_stop_command == True
# 2) Test option 2
self.run_command.parse(["stop", "--all"])
# test when all is passed to stop all
run_stop_command = self.run_command.execute()
assert run_stop_command == True
@pytest_docker_environment_failed_instantiation(test_datmo_dir)
def test_run_stop_failure_required_args(self):
self.__set_variables()
# Passing wrong task id
self.run_command.parse(["stop"])
failed = False
try:
_ = self.run_command.execute()
except RequiredArgumentMissing:
failed = True
assert failed
@pytest_docker_environment_failed_instantiation(test_datmo_dir)
def test_run_stop_failure_mutually_exclusive_vars(self):
self.__set_variables()
# Passing wrong task id
self.run_command.parse(["stop", "--id", "invalid-task-id", "--all"])
failed = False
try:
_ = self.run_command.execute()
except MutuallyExclusiveArguments:
failed = True
assert failed
def test_run_stop_failure_invalid_arg(self):
self.__set_variables()
exception_thrown = False
try:
self.run_command.parse(["stop" "--foobar", "foobar"])
except Exception:
exception_thrown = True
assert exception_thrown
def test_run_stop_invalid_task_id(self):
self.__set_variables()
# Passing wrong task id
self.run_command.parse(["stop", "--id", "invalid-task-id"])
# test when wrong task id is passed to stop it
result = self.run_command.execute()
assert not result
@pytest_docker_environment_failed_instantiation(test_datmo_dir)
def test_run_delete_success(self):
# 1) Test delete with run_id
self.__set_variables()
test_command = ["sh", "-c", "echo yo"]
test_ports = "8888:8888"
test_mem_limit = "4g"
test_dockerfile = os.path.join(self.temp_dir, "Dockerfile")
self.run_command.parse([
"run", "--ports", test_ports, "--environment-paths",
test_dockerfile, "--mem-limit", test_mem_limit, test_command
])
test_run_obj = self.run_command.execute()
# 1) Test option 1
self.run_command.parse(["delete", test_run_obj.id])
# test for desired side effects
assert self.run_command.args.id == test_run_obj.id
# test when task id is passed to delete it
run_delete_command = self.run_command.execute()
assert run_delete_command == True
def test_run_delete_invalid_task_id(self):
self.__set_variables()
# Passing wrong task id
self.run_command.parse(["delete", "invalid-task-id"])
# test when wrong task id is passed to stop it
result = self.run_command.execute()
assert not result
@pytest_docker_environment_failed_instantiation(test_datmo_dir)
def test_rerun(self):
self.__set_variables()
# Test success case
test_command = ["sh", "-c", "echo accuracy:0.45"]
test_ports = ["8888:8888", "9999:9999"]
test_dockerfile = os.path.join(self.temp_dir, "Dockerfile")
test_mem_limit = "4g"
# test for single set of ports
self.run_command.parse([
"run", "-p", test_ports[0], "--environment-paths", test_dockerfile,
"--mem-limit", test_mem_limit, test_command
])
# test proper execution of run command
run_obj = self.run_command.execute()
run_id = run_obj.id
# 1. Test success rerun
self.run_command.parse(["rerun", run_id])
result_run_obj = self.run_command.execute()
assert result_run_obj
assert isinstance(result_run_obj, RunObject)
assert result_run_obj.command == run_obj.command
assert result_run_obj.status == "SUCCESS"
assert result_run_obj.logs
assert result_run_obj.before_snapshot_id == run_obj.before_snapshot_id
# teardown
self.run_command.parse(["stop", "--all"])
# test when all is passed to stop all
self.run_command.execute()
@pytest_docker_environment_failed_instantiation(test_datmo_dir)
def test_rerun_invalid_arg(self):
self.__set_variables()
exception_thrown = False
try:
self.run_command.parse(["rerun", "foobar"])
self.run_command.execute()
except DoesNotExist:
exception_thrown = True
assert exception_thrown